Start removing global variables

This commit is contained in:
SeanOMik 2020-08-20 10:01:20 -05:00
parent dfa7017342
commit 95279cadb8
No known key found for this signature in database
GPG Key ID: 4FD4F747563397F7
3 changed files with 21 additions and 11 deletions

View File

@ -227,19 +227,17 @@ proc startConnection*(client: DiscordClient, shardAmount: int = 1) {.async.} =
while true: while true:
if not shard.reconnecting: if not shard.reconnecting:
poll() poll()
#[ client.ws = await newAsyncWebsocketClient(url[6..url.high], Port 443,
path = "/v=6&encoding=json", true)
asyncCheck client.handleWebsocketPacket()
# Now just wait. Dont poll for new events while we're reconnecting
while true:
if not client.reconnecting:
poll() ]#
else: else:
raise newException(IOError, "Failed to get gateway url, token may of been incorrect!") raise newException(IOError, "Failed to get gateway url, token may of been incorrect!")
proc updateClientPresence*(shard: Shard, presence: Presence) {.async.} = proc updateClientPresence*(shard: Shard, presence: Presence) {.async.} =
## Start a bot's presence.
##
## Examples:
##
## .. code-block:: nim
## let presence = newPresence("with Nimcord", activityTypeGame, clientStatusIdle, false)
## asyncCheck event.shard.updateClientPresence(presence) ## Will read "Playing with Nimcord"
let jsonPayload = %* { let jsonPayload = %* {
"op": ord(opPresenceUpdate), "op": ord(opPresenceUpdate),
"d": presence.presenceToJson() "d": presence.presenceToJson()
@ -247,6 +245,14 @@ proc updateClientPresence*(shard: Shard, presence: Presence) {.async.} =
await shard.sendGatewayRequest(jsonPayload) await shard.sendGatewayRequest(jsonPayload)
# DiscordClient stored instances:
let clientInstances = newTable[uint8, DiscordClient]()
var nextInstanceId = 0
proc getClientInstance*(instanceID: uint8): DiscordClient =
## Get a client instance with instance id. Mainly used internally.
return clientInstances[instanceID]
proc newDiscordClient*(tkn: string, commandPrefix: string): DiscordClient = proc newDiscordClient*(tkn: string, commandPrefix: string): DiscordClient =
## Create a DiscordClient using a token. ## Create a DiscordClient using a token.
## ##
@ -256,4 +262,6 @@ proc newDiscordClient*(tkn: string, commandPrefix: string): DiscordClient =
var cac: Cache var cac: Cache
new(cac) new(cac)
result = DiscordClient(token: tkn, cache: cac, commandPrefix: commandPrefix) result = DiscordClient(token: tkn, cache: cac, commandPrefix: commandPrefix, instanceID: nextInstanceId)
clientInstances.add(nextInstanceId, result)
nextInstanceId++

View File

@ -10,6 +10,7 @@ type
shardCount*: int shardCount*: int
endpoint*: string endpoint*: string
commandPrefix*: string commandPrefix*: string
instanceID*: uint8
Shard* = ref object Shard* = ref object
id*: int id*: int

View File

@ -1,7 +1,8 @@
type type
Snowflake* = uint64 Snowflake* = uint64
DiscordObject* = object of RootObj DiscordObject* = object of RootObj
id*: Snowflake id*: Snowflake ## ID of this discord object.
clientInstanceID*: uint8 ## Client instance id for this object. Mainly used internally.
proc `==`*(obj1: DiscordObject, obj2: DiscordObject): bool = proc `==`*(obj1: DiscordObject, obj2: DiscordObject): bool =
return obj1.id == obj2.id return obj1.id == obj2.id