From f5bab02a83e4aea55e4519206b6a4a05a35c0681 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Wed, 17 Jun 2020 22:19:13 -0500 Subject: [PATCH] Add GuildCreateEvent --- src/eventdispatcher.nim | 48 +++++++++++++++++++++++++++++++---------- src/eventhandler.nim | 7 +++++- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/eventdispatcher.nim b/src/eventdispatcher.nim index a7392a3..8d6be6e 100644 --- a/src/eventdispatcher.nim +++ b/src/eventdispatcher.nim @@ -1,22 +1,48 @@ -import eventhandler, json, tables, hashes, message +import eventhandler, json, tables, message, user, nimcordutils, httpClient, strformat, cache, guild, sequtils, asyncdispatch, clientobjects -proc readyEvent(json: JsonNode) = - let readyEvent = ReadyEvent(readyPayload: json, name: $EventType.evtReady) +proc readyEvent(discordClient: DiscordClient, json: JsonNode) = + var readyEvent = ReadyEvent(client: discordClient, readyPayload: json, name: $EventType.evtReady) + + # Get client user + var client = newHttpClient() + # Add headers + client.headers = newHttpHeaders({"Authorization": fmt("Bot {discordClient.token}"), + "User-Agent": "NimCord (https://github.com/SeanOMik/nimcord, v0.0.0)", + "X-RateLimit-Precision": "millisecond"}) + echo "Sending GET request, URL: body: {}" + + waitForRateLimits(0, RateLimitBucketType.global) + var json = handleResponse(client.request(endpoint("/users/@me"), HttpGet, ""), 0, RateLimitBucketType.global) + + let clientUser: User = newUser(json) + readyEvent.clientUser = clientUser + dispatchEvent(readyEvent) -proc messageCreateEvent(json: JsonNode) = +proc messageCreateEvent(discordClient: DiscordClient, json: JsonNode) = let msg = newMessage(json) - let messageCreateEvnt = MessageCreateEvent(message: msg, name: $EventType.evtMessageCreate) + let messageCreateEvnt = MessageCreateEvent(client: discordClient, message: msg, name: $EventType.evtMessageCreate) dispatchEvent(messageCreateEvnt) -let internalEventTable: Table[string, proc(json: JsonNode) {.nimcall.}] = { +proc guildCreateEvent(discordClient: DiscordClient, json: JsonNode) = + let g = newGuild(json) + let guildCreateEvnt = GuildCreateEvent(client: discordClient, guild: g, name: $EventType.evtGuildCreate) + + discordClient.cache.guilds.insert(g) + discordClient.cache.channels = concat(discordClient.cache.channels, g.channels) + discordClient.cache.members = concat(discordClient.cache.members, g.members) + dispatchEvent(guildCreateEvnt) + +let internalEventTable: Table[string, proc(discordClient: DiscordClient, json: JsonNode) {.nimcall.}] = { "READY": readyEvent, - "MESSAGE_CREATE": messageCreateEvent + "MESSAGE_CREATE": messageCreateEvent, + "GUILD_CREATE": guildCreateEvent }.toTable -proc handleDiscordEvent*(json: JsonNode, eventName: string) = +proc handleDiscordEvent*(discordClient: DiscordClient, json: JsonNode, eventName: string) {.async.} = if (internalEventTable.hasKey(eventName)): - let eventProc:proc(json: JsonNode) = internalEventTable[eventName] - eventProc(json) + let eventProc: proc(discordClient: DiscordClient, json: JsonNode) = internalEventTable[eventName] + eventProc(discordClient, json) else: - echo "Failed to find event: ", eventName \ No newline at end of file + echo "Failed to find event: ", eventName + \ No newline at end of file diff --git a/src/eventhandler.nim b/src/eventhandler.nim index f4ed93b..9267a93 100644 --- a/src/eventhandler.nim +++ b/src/eventhandler.nim @@ -1,16 +1,21 @@ -import tables, hashes, json, message +import tables, hashes, json, message, user, guild, clientobjects type EventType* = enum evtReady = "READY" evtMessageCreate = "MESSAGE_CREATE" + evtGuildCreate = "GUILD_CREATE" BaseEvent* = object of RootObj + client*: DiscordClient name*: string ReadyEvent* = object of BaseEvent readyPayload*: JsonNode + clientUser*: User MessageCreateEvent* = object of BaseEvent message*: Message + GuildCreateEvent* = object of BaseEvent + guild*: Guild # Table storing all the event listeners let eventListeners = newTable[string, seq[proc(event: BaseEvent)]]()