Skip to content

Commit

Permalink
Make id and name optional (bug when deleted emoji is removed from a m…
Browse files Browse the repository at this point in the history
…essage reaction) and fix unused import and add used pragmas
  • Loading branch information
krisppurg committed Sep 10, 2020
1 parent 2c5fa47 commit 2591f72
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
2 changes: 1 addition & 1 deletion dimscord/dispatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ proc guildEmojisUpdate(s: Shard, data: JsonNode) {.async.} =
for emoji in data["emojis"]:
let emji = newEmoji(emoji)
emojis.add(emji)
guild.emojis[emji.id] = emji
guild.emojis[get emji.id] = emji

await s.client.events.guild_emojis_update(s, guild, emojis)

Expand Down
18 changes: 8 additions & 10 deletions dimscord/gateway.nim
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import httpclient, ws, asyncnet, asyncdispatch
import strformat, options, sequtils, strutils, restapi, dispatch
import strformat, options, strutils, restapi, dispatch
import tables, random, times, constants, objects, json, math
import nativesockets

when defined(discordCompress):
import zip/zlib

randomize()
{.hint[XDeclaredButNotUsed]: off.}
{.warning[UnusedImport]: off.}

const
opDispatch = 0
Expand All @@ -27,7 +25,7 @@ var
backoff = false
reconnectable = true

proc reset(s: Shard) =
proc reset(s: Shard) {.used.} =
s.authenticating = false
s.resuming = false

Expand Down Expand Up @@ -81,7 +79,7 @@ proc extractCloseData(data: string): tuple[code: int, reason: string] = # Code f
0
result.reason = if data.len > 2: data[2..^1] else: ""

proc handleDisconnect(s: Shard, msg: string): bool =
proc handleDisconnect(s: Shard, msg: string): bool {.used.} =
let closeData = extractCloseData(msg)

s.logShard("Socket suspended", (
Expand All @@ -97,7 +95,7 @@ proc handleDisconnect(s: Shard, msg: string): bool =
result = false
log("Fatal error: " & closeData.reason)

proc sockClosed(s: Shard): bool =
proc sockClosed(s: Shard): bool {.used.} =
return s.connection == nil or s.connection.tcpSocket.isClosed or s.stop

proc updateStatus*(s: Shard, game = none GameStatus;
Expand All @@ -121,7 +119,7 @@ proc updateStatus*(s: Shard, game = none GameStatus;

asyncCheck s.sendSock(opStatusUpdate, payload)

proc identify(s: Shard) {.async.} =
proc identify(s: Shard) {.async, used.} =
if s.authenticating or s.sockClosed: return

if backoff:
Expand Down Expand Up @@ -157,7 +155,7 @@ proc identify(s: Shard) {.async.} =
await s.sendSock(opIdentify, payload, ignore = true)
if s.client.max_shards > 1: await sleepAsync 5000

proc resume(s: Shard) {.async.} =
proc resume*(s: Shard) {.async.} =
if s.authenticating or s.sockClosed: return

s.authenticating = true
Expand Down Expand Up @@ -214,7 +212,7 @@ proc voiceStateUpdate*(s: Shard,
"channel_id": channel_id
})

proc handleDispatch(s: Shard, event: string, data: JsonNode) {.async.} =
proc handleDispatch(s: Shard, event: string, data: JsonNode) {.async, used.} =
s.logShard("Received event: " & event) # please do not enable dimscordDebug while you are on a large guild.

case event:
Expand Down Expand Up @@ -469,7 +467,7 @@ proc endSession*(cl: DiscordClient) {.async.} =
shard.cache.clear()

proc setupShard(cl: DiscordClient, i: int;
cache_prefs: CacheTablePrefs): Shard =
cache_prefs: CacheTablePrefs): Shard {.used.} =
result = newShard(i, cl)
cl.shards.add(i, result)

Expand Down
20 changes: 7 additions & 13 deletions dimscord/objects.nim
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ type
emoji*: Emoji
reacted*: bool
Emoji* = object
id*, name*: string
id*, name*: Option[string]
require_colons*, managed*, animated*: Option[bool]
user*: User
roles*: seq[string]
Expand Down Expand Up @@ -178,12 +178,10 @@ type
instance*: bool
Presence* = object
user*: User
roles*: seq[string]
game*: Option[GameActivity]
guild_id*, status*: string
activities*: seq[GameActivity]
client_status*: tuple[web, desktop, mobile: string]
premium_since*, nick*: Option[string]
Guild* = ref object
id*, name*, owner_id*: string
region*, preferred_locale*: string
Expand Down Expand Up @@ -429,7 +427,10 @@ proc clear*(c: CacheTable) =
c.dmChannels.clear()

proc `$`*(e: Emoji): string =
result = if e.id != "": e.name & ":" & e.id else: e.name
result = if e.id.isSome:
e.name.get("?") & ":" & e.id.get
else:
e.name.get("?")

macro keyCheckOptInt(obj: typed, obj2: typed,
lits: varargs[untyped]): untyped =
Expand Down Expand Up @@ -777,17 +778,14 @@ proc newVoiceState*(data: JsonNode): VoiceState =
data.keyCheckOptStr(result, guild_id, channel_id)

proc newEmoji*(data: JsonNode): Emoji =
result = Emoji(name: data["name"].str)
result = Emoji(user: newUser(data["user"]))

for r in data{"roles"}.getElems:
result.roles.add(r.str)

data.keyCheckStr(result, id)
data.keyCheckOptStr(result, id, name)
data.keyCheckOptBool(result, require_colons, managed, animated)

if "user" in data:
result.user = newUser(data["user"])

proc newGameActivity*(data: JsonNode): GameActivity =
result = GameActivity(
name: data["name"].str,
Expand Down Expand Up @@ -840,10 +838,6 @@ proc newPresence*(data: JsonNode): Presence =
mobile: "offline"
)
)
data.keyCheckOptStr(result, nick, premium_since)

for role in data{"roles"}.getElems:
result.roles.add(role.str)

for activity in data["activities"].elems:
result.activities.add(newGameActivity(activity))
Expand Down

0 comments on commit 2591f72

Please sign in to comment.