diff --git a/src/nimcord/client.nim b/src/nimcord/client.nim index 212255f..df365b1 100644 --- a/src/nimcord/client.nim +++ b/src/nimcord/client.nim @@ -227,19 +227,17 @@ proc startConnection*(client: DiscordClient, shardAmount: int = 1) {.async.} = while true: if not shard.reconnecting: 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: raise newException(IOError, "Failed to get gateway url, token may of been incorrect!") 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 = %* { "op": ord(opPresenceUpdate), "d": presence.presenceToJson() @@ -247,6 +245,14 @@ proc updateClientPresence*(shard: Shard, presence: Presence) {.async.} = 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 = ## Create a DiscordClient using a token. ## @@ -256,4 +262,6 @@ proc newDiscordClient*(tkn: string, commandPrefix: string): DiscordClient = var cac: Cache new(cac) - result = DiscordClient(token: tkn, cache: cac, commandPrefix: commandPrefix) \ No newline at end of file + result = DiscordClient(token: tkn, cache: cac, commandPrefix: commandPrefix, instanceID: nextInstanceId) + clientInstances.add(nextInstanceId, result) + nextInstanceId++ \ No newline at end of file diff --git a/src/nimcord/clientobjects.nim b/src/nimcord/clientobjects.nim index cb0f634..168f495 100644 --- a/src/nimcord/clientobjects.nim +++ b/src/nimcord/clientobjects.nim @@ -10,6 +10,7 @@ type shardCount*: int endpoint*: string commandPrefix*: string + instanceID*: uint8 Shard* = ref object id*: int diff --git a/src/nimcord/discordobject.nim b/src/nimcord/discordobject.nim index d18fade..aa01505 100644 --- a/src/nimcord/discordobject.nim +++ b/src/nimcord/discordobject.nim @@ -1,7 +1,8 @@ type Snowflake* = uint64 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 = return obj1.id == obj2.id