Add GuildCreateEvent
This commit is contained in:
parent
875050c6b6
commit
f5bab02a83
|
@ -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) =
|
proc readyEvent(discordClient: DiscordClient, json: JsonNode) =
|
||||||
let readyEvent = ReadyEvent(readyPayload: json, name: $EventType.evtReady)
|
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)
|
dispatchEvent(readyEvent)
|
||||||
|
|
||||||
proc messageCreateEvent(json: JsonNode) =
|
proc messageCreateEvent(discordClient: DiscordClient, json: JsonNode) =
|
||||||
let msg = newMessage(json)
|
let msg = newMessage(json)
|
||||||
let messageCreateEvnt = MessageCreateEvent(message: msg, name: $EventType.evtMessageCreate)
|
let messageCreateEvnt = MessageCreateEvent(client: discordClient, message: msg, name: $EventType.evtMessageCreate)
|
||||||
dispatchEvent(messageCreateEvnt)
|
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,
|
"READY": readyEvent,
|
||||||
"MESSAGE_CREATE": messageCreateEvent
|
"MESSAGE_CREATE": messageCreateEvent,
|
||||||
|
"GUILD_CREATE": guildCreateEvent
|
||||||
}.toTable
|
}.toTable
|
||||||
|
|
||||||
proc handleDiscordEvent*(json: JsonNode, eventName: string) =
|
proc handleDiscordEvent*(discordClient: DiscordClient, json: JsonNode, eventName: string) {.async.} =
|
||||||
if (internalEventTable.hasKey(eventName)):
|
if (internalEventTable.hasKey(eventName)):
|
||||||
let eventProc:proc(json: JsonNode) = internalEventTable[eventName]
|
let eventProc: proc(discordClient: DiscordClient, json: JsonNode) = internalEventTable[eventName]
|
||||||
eventProc(json)
|
eventProc(discordClient, json)
|
||||||
else:
|
else:
|
||||||
echo "Failed to find event: ", eventName
|
echo "Failed to find event: ", eventName
|
||||||
|
|
|
@ -1,16 +1,21 @@
|
||||||
import tables, hashes, json, message
|
import tables, hashes, json, message, user, guild, clientobjects
|
||||||
|
|
||||||
type
|
type
|
||||||
EventType* = enum
|
EventType* = enum
|
||||||
evtReady = "READY"
|
evtReady = "READY"
|
||||||
evtMessageCreate = "MESSAGE_CREATE"
|
evtMessageCreate = "MESSAGE_CREATE"
|
||||||
|
evtGuildCreate = "GUILD_CREATE"
|
||||||
|
|
||||||
BaseEvent* = object of RootObj
|
BaseEvent* = object of RootObj
|
||||||
|
client*: DiscordClient
|
||||||
name*: string
|
name*: string
|
||||||
ReadyEvent* = object of BaseEvent
|
ReadyEvent* = object of BaseEvent
|
||||||
readyPayload*: JsonNode
|
readyPayload*: JsonNode
|
||||||
|
clientUser*: User
|
||||||
MessageCreateEvent* = object of BaseEvent
|
MessageCreateEvent* = object of BaseEvent
|
||||||
message*: Message
|
message*: Message
|
||||||
|
GuildCreateEvent* = object of BaseEvent
|
||||||
|
guild*: Guild
|
||||||
|
|
||||||
# Table storing all the event listeners
|
# Table storing all the event listeners
|
||||||
let eventListeners = newTable[string, seq[proc(event: BaseEvent)]]()
|
let eventListeners = newTable[string, seq[proc(event: BaseEvent)]]()
|
||||||
|
|
Reference in New Issue