This repository has been archived on 2023-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
nimcord/src/eventhandler.nim

64 lines
2.5 KiB
Nim
Raw Normal View History

2020-06-18 03:19:13 +00:00
import tables, hashes, json, message, user, guild, clientobjects
2020-05-30 04:59:23 +00:00
type
EventType* = enum
2020-06-19 02:23:56 +00:00
## The event type.
2020-05-30 04:59:23 +00:00
evtReady = "READY"
2020-05-31 05:09:30 +00:00
evtMessageCreate = "MESSAGE_CREATE"
2020-06-18 03:19:13 +00:00
evtGuildCreate = "GUILD_CREATE"
2020-05-30 04:59:23 +00:00
BaseEvent* = object of RootObj
2020-06-19 02:23:56 +00:00
## Base event that all events inherit from.
## It stores a reference to the DiscordClient and name of the event.
2020-06-18 03:19:13 +00:00
client*: DiscordClient
2020-05-30 04:59:23 +00:00
name*: string
ReadyEvent* = object of BaseEvent
2020-06-19 02:23:56 +00:00
## The ready event is triggered everytime the bot starts up.
## Stores the readyPayload (JSON Payload) that gets received and the bot's user.
2020-05-30 04:59:23 +00:00
readyPayload*: JsonNode
2020-06-18 03:19:13 +00:00
clientUser*: User
2020-05-31 05:09:30 +00:00
MessageCreateEvent* = object of BaseEvent
2020-06-19 02:23:56 +00:00
## The Message Create event is triggered when someone sends a message.
2020-05-31 05:09:30 +00:00
message*: Message
2020-06-18 03:19:13 +00:00
GuildCreateEvent* = object of BaseEvent
2020-06-19 02:23:56 +00:00
## The Guild Create event is triggered when the bot starts, or when
## it gets added to a new guild.
2020-06-18 03:19:13 +00:00
guild*: Guild
2020-05-30 04:59:23 +00:00
# Table storing all the event listeners
let eventListeners = newTable[string, seq[proc(event: BaseEvent)]]()
proc registerEventListener*(event: EventType, listener: proc(event: BaseEvent)) =
2020-06-19 02:23:56 +00:00
## 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 "--------------------"
## )
2020-05-30 04:59:23 +00:00
if (eventListeners.hasKey($event)):
eventListeners[$event].add(cast[proc(event: BaseEvent)](listener))
2020-05-30 04:59:23 +00:00
echo "Added other event listener: ", $event
else:
let tmp = @[listener]
eventListeners.add($event, tmp)
echo "Added new event listener: ", $event
proc dispatchEvent*[T: BaseEvent](event: T) =
2020-06-19 02:23:56 +00:00
## Dispatches an event so something can listen to it.
2020-05-30 04:59:23 +00:00
if (eventListeners.hasKey(event.name)):
let listeners = eventListeners[event.name]
echo "Dispatching event: ", event.name
2020-05-30 04:59:23 +00:00
for index, eventListener in listeners.pairs:
eventListener(event)
else:
echo "No event listeners for event: ", event.name