Add documentation in eventhandler.nim

This commit is contained in:
SeanOMik 2020-06-18 21:23:56 -05:00
parent 9e4da6a718
commit deb19c8842
No known key found for this signature in database
GPG Key ID: FA4D55AC05268A88
1 changed files with 23 additions and 0 deletions

View File

@ -2,25 +2,47 @@ import tables, hashes, json, message, user, guild, clientobjects
type type
EventType* = enum EventType* = enum
## The event type.
evtReady = "READY" evtReady = "READY"
evtMessageCreate = "MESSAGE_CREATE" evtMessageCreate = "MESSAGE_CREATE"
evtGuildCreate = "GUILD_CREATE" evtGuildCreate = "GUILD_CREATE"
BaseEvent* = object of RootObj BaseEvent* = object of RootObj
## Base event that all events inherit from.
## It stores a reference to the DiscordClient and name of the event.
client*: DiscordClient client*: DiscordClient
name*: string name*: string
ReadyEvent* = object of BaseEvent ReadyEvent* = object of BaseEvent
## The ready event is triggered everytime the bot starts up.
## Stores the readyPayload (JSON Payload) that gets received and the bot's user.
readyPayload*: JsonNode readyPayload*: JsonNode
clientUser*: User clientUser*: User
MessageCreateEvent* = object of BaseEvent MessageCreateEvent* = object of BaseEvent
## The Message Create event is triggered when someone sends a message.
message*: Message message*: Message
GuildCreateEvent* = object of BaseEvent GuildCreateEvent* = object of BaseEvent
## The Guild Create event is triggered when the bot starts, or when
## it gets added to a new guild.
guild*: Guild 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)]]()
proc registerEventListener*(event: EventType, listener: proc(event: BaseEvent)) = proc registerEventListener*(event: EventType, listener: proc(event: BaseEvent)) =
## Register an event listener.
##
## Examples:
##
## .. code-block:: nim
## registerEventListener(EventType.evtReady, proc(bEvt: BaseEvent) =
## let event = ReadyEvent(bEvt)
## bot.clientUser = event.clientUser
##
## echo "Ready! (v", nimcordMajor, ".", nimcordMinor, ".", nimcordMicro, ")"
## echo "Logged in as: ", bot.clientUser.username, "#", bot.clientUser.discriminator
## echo "ID: ", bot.clientUser.id
## echo "--------------------"
## )
if (eventListeners.hasKey($event)): if (eventListeners.hasKey($event)):
eventListeners[$event].add(cast[proc(event: BaseEvent)](listener)) eventListeners[$event].add(cast[proc(event: BaseEvent)](listener))
@ -32,6 +54,7 @@ proc registerEventListener*(event: EventType, listener: proc(event: BaseEvent))
echo "Added new event listener: ", $event echo "Added new event listener: ", $event
proc dispatchEvent*[T: BaseEvent](event: T) = proc dispatchEvent*[T: BaseEvent](event: T) =
## Dispatches an event so something can listen to it.
if (eventListeners.hasKey(event.name)): if (eventListeners.hasKey(event.name)):
let listeners = eventListeners[event.name] let listeners = eventListeners[event.name]
echo "Dispatching event: ", event.name echo "Dispatching event: ", event.name