From deb19c8842193bb6ca8d3d0abc262816aac5a844 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Thu, 18 Jun 2020 21:23:56 -0500 Subject: [PATCH] Add documentation in eventhandler.nim --- src/eventhandler.nim | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/eventhandler.nim b/src/eventhandler.nim index 9267a93..7e288e6 100644 --- a/src/eventhandler.nim +++ b/src/eventhandler.nim @@ -2,25 +2,47 @@ import tables, hashes, json, message, user, guild, clientobjects type EventType* = enum + ## The event type. evtReady = "READY" evtMessageCreate = "MESSAGE_CREATE" evtGuildCreate = "GUILD_CREATE" 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 name*: string 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 clientUser*: User MessageCreateEvent* = object of BaseEvent + ## The Message Create event is triggered when someone sends a message. message*: Message 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 # Table storing all the event listeners let eventListeners = newTable[string, seq[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)): 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 proc dispatchEvent*[T: BaseEvent](event: T) = + ## Dispatches an event so something can listen to it. if (eventListeners.hasKey(event.name)): let listeners = eventListeners[event.name] echo "Dispatching event: ", event.name