From 9382e2e921729935835c7f8f4b9e915276c1dca8 Mon Sep 17 00:00:00 2001 From: QuantumlyTangled Date: Mon, 17 May 2021 14:43:33 +0200 Subject: [PATCH 1/4] fix: event handlers reference --- src/bot.ts | 18 +++++------------- src/plugins/proxyEvents.ts | 4 ++-- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index df6398718..a6e5b5648 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -11,17 +11,12 @@ export let secretKey = ""; export let botId = 0n; export let applicationId = 0n; -/** - * Used internally to track the source of truth for event functions - * @private - */ -export let _eventHandlers: EventHandlerFunctions = {}; -export let eventHandlers: EventHandlerFunctions = _eventHandlers; +export let eventHandlers: EventHandlerFunctions = {}; export let proxyWSURL = `wss://gateway.discord.gg`; export async function startBot(config: BotConfig) { - if (config.eventHandlers) _eventHandlers = config.eventHandlers; + if (config.eventHandlers) eventHandlers = config.eventHandlers; ws.identifyPayload.token = `Bot ${config.token}`; rest.token = `Bot ${config.token}`; ws.identifyPayload.intents = config.intents.reduce( @@ -49,16 +44,13 @@ export async function startBot(config: BotConfig) { ws.spawnShards(); } -export function overloadEventHandlers(__eventHandlers: EventHandlerFunctions) { - eventHandlers = __eventHandlers; +export function replaceEventHandlers(newEventHandlers: EventHandlerFunctions) { + eventHandlers = newEventHandlers; } /** Allows you to dynamically update the event handlers by passing in new eventHandlers */ export function updateEventHandlers(newEventHandlers: EventHandlerFunctions) { - _eventHandlers = { - ..._eventHandlers, - ...newEventHandlers, - }; + Object.assign(eventHandlers, newEventHandlers); } /** INTERNAL LIB function used to set the bot Id once the READY event is sent by Discord. */ diff --git a/src/plugins/proxyEvents.ts b/src/plugins/proxyEvents.ts index 97d2cbcd0..80ee5ace8 100644 --- a/src/plugins/proxyEvents.ts +++ b/src/plugins/proxyEvents.ts @@ -1,9 +1,9 @@ -import { overloadEventHandlers, _eventHandlers } from "../bot.ts"; +import { eventHandlers, replaceEventHandlers } from "../bot.ts"; import type { EventHandlerFunctions } from "../types/discordeno/eventHandlers.ts"; import type { EventEmitter } from "https://deno.land/std@0.96.0/node/events.ts"; export function proxyEvent(emitter: EventEmitter) { - overloadEventHandlers(new Proxy(_eventHandlers, { + replaceEventHandlers(new Proxy(eventHandlers, { get(target, prop: keyof EventHandlerFunctions) { return target[prop] !== undefined ? target[prop] : ((...args: unknown[]) => emitter.emit(prop, ...args)); }, From a505ed48cbb30e49ec16a4cda89f0204cc7ec1ea Mon Sep 17 00:00:00 2001 From: QuantumlyTangled Date: Mon, 17 May 2021 14:44:44 +0200 Subject: [PATCH 2/4] fix: revert name change --- src/bot.ts | 10 +++++----- src/plugins/proxyEvents.ts | 4 ++-- src/types/discordeno/eventHandlers.ts | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index a6e5b5648..4333d0135 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -1,6 +1,6 @@ import { getGatewayBot } from "./helpers/misc/get_gateway_bot.ts"; import { rest } from "./rest/rest.ts"; -import type { EventHandlerFunctions } from "./types/discordeno/eventHandlers.ts"; +import type { EventHandlers } from "./types/discordeno/eventHandlers.ts"; import { DiscordGatewayIntents } from "./types/gateway/gateway_intents.ts"; import { snowflakeToBigint } from "./util/bigint.ts"; import { GATEWAY_VERSION } from "./util/constants.ts"; @@ -11,7 +11,7 @@ export let secretKey = ""; export let botId = 0n; export let applicationId = 0n; -export let eventHandlers: EventHandlerFunctions = {}; +export let eventHandlers: EventHandlers = {}; export let proxyWSURL = `wss://gateway.discord.gg`; @@ -44,12 +44,12 @@ export async function startBot(config: BotConfig) { ws.spawnShards(); } -export function replaceEventHandlers(newEventHandlers: EventHandlerFunctions) { +export function replaceEventHandlers(newEventHandlers: EventHandlers) { eventHandlers = newEventHandlers; } /** Allows you to dynamically update the event handlers by passing in new eventHandlers */ -export function updateEventHandlers(newEventHandlers: EventHandlerFunctions) { +export function updateEventHandlers(newEventHandlers: EventHandlers) { Object.assign(eventHandlers, newEventHandlers); } @@ -67,5 +67,5 @@ export interface BotConfig { token: string; compress?: boolean; intents: (DiscordGatewayIntents | keyof typeof DiscordGatewayIntents)[]; - eventHandlers?: EventHandlerFunctions; + eventHandlers?: EventHandlers; } diff --git a/src/plugins/proxyEvents.ts b/src/plugins/proxyEvents.ts index 80ee5ace8..5af8bb67e 100644 --- a/src/plugins/proxyEvents.ts +++ b/src/plugins/proxyEvents.ts @@ -1,10 +1,10 @@ import { eventHandlers, replaceEventHandlers } from "../bot.ts"; -import type { EventHandlerFunctions } from "../types/discordeno/eventHandlers.ts"; +import type { EventHandlers } from "../types/discordeno/eventHandlers.ts"; import type { EventEmitter } from "https://deno.land/std@0.96.0/node/events.ts"; export function proxyEvent(emitter: EventEmitter) { replaceEventHandlers(new Proxy(eventHandlers, { - get(target, prop: keyof EventHandlerFunctions) { + get(target, prop: keyof EventHandlers) { return target[prop] !== undefined ? target[prop] : ((...args: unknown[]) => emitter.emit(prop, ...args)); }, })); diff --git a/src/types/discordeno/eventHandlers.ts b/src/types/discordeno/eventHandlers.ts index 887d5f504..a460928db 100644 --- a/src/types/discordeno/eventHandlers.ts +++ b/src/types/discordeno/eventHandlers.ts @@ -28,7 +28,7 @@ import { VoiceServerUpdate } from "../voice/voice_server_update.ts"; import { DebugArg } from "./debug_arg.ts"; import { GuildUpdateChange } from "./guild_update_change.ts"; -export type EventHandlers = { +export type EventHandlersDefinitions = { /** Sent when a new Slash Command is created, relevant to the current user. */ applicationCommandCreate: [data: ApplicationCommandCreateUpdateDelete]; /** Sent when a Slash Command relevant to the current user is updated. */ @@ -160,6 +160,6 @@ export type EventHandlers = { inviteDelete: [data: InviteDelete]; } -export type EventHandlerFunctions = { - [E in keyof EventHandlers]?: (...args: EventHandlers[E]) => unknown; +export type EventHandlers = { + [E in keyof EventHandlersDefinitions]?: (...args: EventHandlersDefinitions[E]) => unknown; }; From 91d3b36097427f8cc3f5f0a0d597ed0b89e9d5d3 Mon Sep 17 00:00:00 2001 From: QuantumlyTangled Date: Mon, 17 May 2021 14:48:21 +0200 Subject: [PATCH 3/4] feat: add tests for event --- tests/ws/start_bot.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/ws/start_bot.ts b/tests/ws/start_bot.ts index 266da17e9..43f59b136 100644 --- a/tests/ws/start_bot.ts +++ b/tests/ws/start_bot.ts @@ -3,7 +3,7 @@ import { cache } from "../../src/cache.ts"; import { deleteGuild } from "../../src/helpers/guilds/delete_guild.ts"; import { delay } from "../../src/util/utils.ts"; import { ws } from "../../src/ws/ws.ts"; -import { assertExists } from "../deps.ts"; +import { assertExists, assertEquals } from "../deps.ts"; // Set necessary settings // Disables the logger which logs everything @@ -33,8 +33,13 @@ Deno.test({ const token = Deno.env.get("DISCORD_TOKEN"); if (!token) throw new Error("Token is not provided"); + let didReady = false; + await startBot({ token, + eventHandlers: { + ready: () => didReady = true + }, intents: [ "GuildMessages", "Guilds", @@ -53,6 +58,7 @@ Deno.test({ // Assertions assertExists(botId); + assertEquals(true, didReady); }, ...defaultTestOptions, }); From 324df8f0f2e05430116ad96ba54a76f7ccbc8333 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Mon, 17 May 2021 09:40:00 -0400 Subject: [PATCH 4/4] Update src/bot.ts --- src/bot.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/bot.ts b/src/bot.ts index 4333d0135..431abed1a 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -50,6 +50,7 @@ export function replaceEventHandlers(newEventHandlers: EventHandlers) { /** Allows you to dynamically update the event handlers by passing in new eventHandlers */ export function updateEventHandlers(newEventHandlers: EventHandlers) { + // Object.assign instead of ... operator because of the Proxy used Object.assign(eventHandlers, newEventHandlers); }