Merge pull request #949 from QuantumlyTangled/fix/eventhandlers/reference

fix: event handlers reference
This commit is contained in:
Skillz4Killz
2021-05-17 09:40:47 -04:00
committed by GitHub
4 changed files with 23 additions and 24 deletions

View File

@@ -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,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: EventHandlers = {};
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,14 @@ export async function startBot(config: BotConfig) {
ws.spawnShards();
}
export function overloadEventHandlers(__eventHandlers: EventHandlerFunctions) {
eventHandlers = __eventHandlers;
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) {
_eventHandlers = {
..._eventHandlers,
...newEventHandlers,
};
export function updateEventHandlers(newEventHandlers: EventHandlers) {
// Object.assign instead of ... operator because of the Proxy used
Object.assign(eventHandlers, newEventHandlers);
}
/** INTERNAL LIB function used to set the bot Id once the READY event is sent by Discord. */
@@ -75,5 +68,5 @@ export interface BotConfig {
token: string;
compress?: boolean;
intents: (DiscordGatewayIntents | keyof typeof DiscordGatewayIntents)[];
eventHandlers?: EventHandlerFunctions;
eventHandlers?: EventHandlers;
}

View File

@@ -1,10 +1,10 @@
import { overloadEventHandlers, _eventHandlers } from "../bot.ts";
import type { EventHandlerFunctions } from "../types/discordeno/eventHandlers.ts";
import { eventHandlers, replaceEventHandlers } from "../bot.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) {
overloadEventHandlers(new Proxy(_eventHandlers, {
get(target, prop: keyof EventHandlerFunctions) {
replaceEventHandlers(new Proxy(eventHandlers, {
get(target, prop: keyof EventHandlers) {
return target[prop] !== undefined ? target[prop] : ((...args: unknown[]) => emitter.emit(prop, ...args));
},
}));

View File

@@ -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;
};

View File

@@ -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,
});