mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-03 09:20:08 +00:00
refactor: make event handler types a type (#948)
* refactor: make event handler types a type * refactor: use proper naming * refactor: correct implementation naming * refactor: add back comments * fix: cleanup import statement
This commit is contained in:
12
src/bot.ts
12
src/bot.ts
@@ -1,6 +1,6 @@
|
||||
import { getGatewayBot } from "./helpers/misc/get_gateway_bot.ts";
|
||||
import { rest } from "./rest/rest.ts";
|
||||
import type { EventHandlers } from "./types/discordeno/eventHandlers.ts";
|
||||
import type { EventHandlerFunctions } 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";
|
||||
@@ -15,8 +15,8 @@ export let applicationId = 0n;
|
||||
* Used internally to track the source of truth for event functions
|
||||
* @private
|
||||
*/
|
||||
export let _eventHandlers: EventHandlers = {};
|
||||
export let eventHandlers: EventHandlers = _eventHandlers;
|
||||
export let _eventHandlers: EventHandlerFunctions = {};
|
||||
export let eventHandlers: EventHandlerFunctions = _eventHandlers;
|
||||
|
||||
export let proxyWSURL = `wss://gateway.discord.gg`;
|
||||
|
||||
@@ -49,12 +49,12 @@ export async function startBot(config: BotConfig) {
|
||||
ws.spawnShards();
|
||||
}
|
||||
|
||||
export function overloadEventHandlers(__eventHandlers: EventHandlers) {
|
||||
export function overloadEventHandlers(__eventHandlers: EventHandlerFunctions) {
|
||||
eventHandlers = __eventHandlers;
|
||||
}
|
||||
|
||||
/** Allows you to dynamically update the event handlers by passing in new eventHandlers */
|
||||
export function updateEventHandlers(newEventHandlers: EventHandlers) {
|
||||
export function updateEventHandlers(newEventHandlers: EventHandlerFunctions) {
|
||||
_eventHandlers = {
|
||||
..._eventHandlers,
|
||||
...newEventHandlers,
|
||||
@@ -75,5 +75,5 @@ export interface BotConfig {
|
||||
token: string;
|
||||
compress?: boolean;
|
||||
intents: (DiscordGatewayIntents | keyof typeof DiscordGatewayIntents)[];
|
||||
eventHandlers?: EventHandlers;
|
||||
eventHandlers?: EventHandlerFunctions;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { EventHandlers, overloadEventHandlers, _eventHandlers } from "../../mod.ts";
|
||||
import { overloadEventHandlers, _eventHandlers } 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, {
|
||||
get(target, prop: keyof EventHandlers) {
|
||||
return target[prop] !== undefined ? target[prop] : ((...args: unknown[]) => emitter.emit(prop, ...args));
|
||||
},
|
||||
}));
|
||||
overloadEventHandlers(new Proxy(_eventHandlers, {
|
||||
get(target, prop: keyof EventHandlerFunctions) {
|
||||
return target[prop] !== undefined ? target[prop] : ((...args: unknown[]) => emitter.emit(prop, ...args));
|
||||
},
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -22,60 +22,36 @@ import {
|
||||
PresenceUpdate,
|
||||
TypingStart,
|
||||
User,
|
||||
VoiceState,
|
||||
VoiceState
|
||||
} from "../mod.ts";
|
||||
import { VoiceServerUpdate } from "../voice/voice_server_update.ts";
|
||||
import { DebugArg } from "./debug_arg.ts";
|
||||
import { GuildUpdateChange } from "./guild_update_change.ts";
|
||||
|
||||
export interface EventHandlers {
|
||||
export type EventHandlers = {
|
||||
/** Sent when a new Slash Command is created, relevant to the current user. */
|
||||
applicationCommandCreate?: (
|
||||
data: ApplicationCommandCreateUpdateDelete,
|
||||
) => unknown;
|
||||
applicationCommandCreate: [data: ApplicationCommandCreateUpdateDelete];
|
||||
/** Sent when a Slash Command relevant to the current user is updated. */
|
||||
applicationCommandUpdate?: (
|
||||
data: ApplicationCommandCreateUpdateDelete,
|
||||
) => unknown;
|
||||
applicationCommandUpdate: [data: ApplicationCommandCreateUpdateDelete];
|
||||
/** Sent when a Slash Command relevant to the current user is deleted. */
|
||||
applicationCommandDelete?: (
|
||||
data: ApplicationCommandCreateUpdateDelete,
|
||||
) => unknown;
|
||||
applicationCommandDelete: [data: ApplicationCommandCreateUpdateDelete];
|
||||
/** Sent when properties about the user change. */
|
||||
botUpdate?: (user: User) => unknown;
|
||||
botUpdate: [user: User];
|
||||
/** Sent when a new guild channel is created, relevant to the current user. */
|
||||
channelCreate?: (channel: DiscordenoChannel) => unknown;
|
||||
channelCreate: [channel: DiscordenoChannel];
|
||||
/** Sent when a channel is updated. This is not sent when the field `last_message_id` is altered. To keep track of the `last_message_id` changes, you must listen for `MESSAGE_CREATE` events. */
|
||||
channelUpdate?: (
|
||||
channel: DiscordenoChannel,
|
||||
oldChannel: DiscordenoChannel,
|
||||
) => unknown;
|
||||
channelUpdate: [channel: DiscordenoChannel, oldChannel: DiscordenoChannel];
|
||||
/** Sent when a channel relevant to the current user is deleted. */
|
||||
channelDelete?: (channel: DiscordenoChannel) => unknown;
|
||||
channelDelete: [channel: DiscordenoChannel];
|
||||
/** Sent when a message pin is updated */
|
||||
channelPinsUpdate?: (
|
||||
channel: DiscordenoChannel,
|
||||
guild?: DiscordenoGuild,
|
||||
lastPinTimestamp?: string | null,
|
||||
) => unknown;
|
||||
debug?: (args: string | DebugArg, data?: string) => unknown;
|
||||
channelPinsUpdate: [channel: DiscordenoChannel, guild?: DiscordenoGuild, lastPinTimestamp?: string | null];
|
||||
debug: [args: string | DebugArg, data?: string];
|
||||
/** Sent before every event. Discordeno awaits the execution of this event before main event gets sent. */
|
||||
dispatchRequirements?: (
|
||||
data: DiscordGatewayPayload,
|
||||
shardId: number,
|
||||
) => unknown;
|
||||
dispatchRequirements: [data: DiscordGatewayPayload, shardId: number];
|
||||
/** Sent when a user is banned from a guild. */
|
||||
guildBanAdd?: (
|
||||
guild: DiscordenoGuild,
|
||||
user: User,
|
||||
member?: DiscordenoMember,
|
||||
) => unknown;
|
||||
guildBanAdd: [guild: DiscordenoGuild, user: User, member?: DiscordenoMember];
|
||||
/** Sent when a user is unbanned from a guild. */
|
||||
guildBanRemove?: (
|
||||
guild: DiscordenoGuild,
|
||||
user: User,
|
||||
member?: DiscordenoMember,
|
||||
) => unknown;
|
||||
guildBanRemove: [guild: DiscordenoGuild, user: User, member?: DiscordenoMember];
|
||||
/**
|
||||
* This event can be sent in three different scenarios:
|
||||
* 1. When a user is initially connecting, to lazily load and backfill information for all unavailable guilds sent in the `READY` event. Guilds that are unavailable due to an outage will send a `GUILD_DELETE` event.
|
||||
@@ -84,191 +60,106 @@ export interface EventHandlers {
|
||||
*
|
||||
* This event does not get sent on startup
|
||||
*/
|
||||
guildCreate?: (guild: DiscordenoGuild) => unknown;
|
||||
guildCreate: [guild: DiscordenoGuild];
|
||||
/** This event does get sent on start when shards are loading the guilds */
|
||||
guildLoaded?: (guild: DiscordenoGuild) => unknown;
|
||||
guildLoaded: [guild: DiscordenoGuild];
|
||||
/** When a guild goes available this event will be ran. */
|
||||
guildAvailable?: (guild: DiscordenoGuild) => unknown;
|
||||
guildAvailable: [guild: DiscordenoGuild];
|
||||
/** When a guild goes unavailable this event will be ran. */
|
||||
guildUnavailable?: (guild: DiscordenoGuild) => unknown;
|
||||
guildUnavailable: [guild: DiscordenoGuild];
|
||||
/** Sent when a guilds integration gets updated */
|
||||
guildIntegrationsUpdate?: (guild: DiscordenoGuild) => unknown;
|
||||
guildIntegrationsUpdate: [guild: DiscordenoGuild];
|
||||
/** Sent when a guild is updated. */
|
||||
guildUpdate?: (
|
||||
guild: DiscordenoGuild,
|
||||
changes: GuildUpdateChange[],
|
||||
) => unknown;
|
||||
guildUpdate: [guild: DiscordenoGuild, changes: GuildUpdateChange[]];
|
||||
/** Sent when a guild becomes or was already unavailable due to an outage, or when the user leaves or is removed from a guild. If the `unavailable` field is not set, the user was removed from the guild. */
|
||||
guildDelete?: (guild: DiscordenoGuild) => unknown;
|
||||
guildDelete: [guild: DiscordenoGuild];
|
||||
/** Sent when a guild's emojis have been updated. */
|
||||
guildEmojisUpdate?: (
|
||||
guild: DiscordenoGuild,
|
||||
emojis: Collection<bigint, Emoji>,
|
||||
oldEmojis: Collection<bigint, Emoji>,
|
||||
) => unknown;
|
||||
guildEmojisUpdate: [guild: DiscordenoGuild, emojis: Collection<bigint, Emoji>, oldEmojis: Collection<bigint, Emoji>];
|
||||
/** Sent when a new user joins a guild. */
|
||||
guildMemberAdd?: (
|
||||
guild: DiscordenoGuild,
|
||||
member: DiscordenoMember,
|
||||
) => unknown;
|
||||
guildMemberAdd: [guild: DiscordenoGuild, member: DiscordenoMember];
|
||||
/** Sent when a user is removed from a guild (leave/kick/ban). */
|
||||
guildMemberRemove?: (
|
||||
guild: DiscordenoGuild,
|
||||
user: User,
|
||||
member?: DiscordenoMember,
|
||||
) => unknown;
|
||||
guildMemberRemove: [guild: DiscordenoGuild, user: User, member?: DiscordenoMember];
|
||||
/** Sent when a guild member is updated. This will also fire when the user object of a guild member changes. */
|
||||
guildMemberUpdate?: (
|
||||
guild: DiscordenoGuild,
|
||||
member: DiscordenoMember,
|
||||
oldMember?: DiscordenoMember,
|
||||
) => unknown;
|
||||
guildMemberUpdate: [guild: DiscordenoGuild, member: DiscordenoMember, oldMember?: DiscordenoMember];
|
||||
/** Sent when a user uses a Slash Command. */
|
||||
interactionCreate?: (
|
||||
data: Omit<Interaction, "member">,
|
||||
member?: DiscordenoMember,
|
||||
) => unknown;
|
||||
interactionCreate: [data: Omit<Interaction, "member">, member?: DiscordenoMember];
|
||||
/** Sent when a user uses a Slash Command in a guild. */
|
||||
interactionGuildCreate?: (
|
||||
data: Omit<Interaction, "member">,
|
||||
member: DiscordenoMember,
|
||||
) => unknown;
|
||||
interactionGuildCreate: [data: Omit<Interaction, "member">, member: DiscordenoMember];
|
||||
/** Sent when a user uses a Slash Command in a dm. */
|
||||
interactionDMCreate?: (
|
||||
data: Omit<Interaction, "member">,
|
||||
) => unknown;
|
||||
interactionDMCreate: [data: Omit<Interaction, "member">];
|
||||
/** Sent when a message is created. */
|
||||
messageCreate?: (message: DiscordenoMessage) => unknown;
|
||||
messageCreate: [message: DiscordenoMessage];
|
||||
/** Sent when a message is deleted. */
|
||||
messageDelete?: (
|
||||
partial: { id: string; channel: DiscordenoChannel },
|
||||
message?: DiscordenoMessage,
|
||||
) => unknown;
|
||||
messageDelete: [partial: { id: string; channel: DiscordenoChannel }, message?: DiscordenoMessage];
|
||||
/** Sent when a message is updated. */
|
||||
messageUpdate?: (
|
||||
message: DiscordenoMessage,
|
||||
oldMessage: DiscordenoMessage,
|
||||
) => unknown;
|
||||
messageUpdate: [message: DiscordenoMessage, oldMessage: DiscordenoMessage];
|
||||
/** Sent when a user updates its nickname */
|
||||
nicknameUpdate?: (
|
||||
guild: DiscordenoGuild,
|
||||
member: DiscordenoMember,
|
||||
nickname: string,
|
||||
oldNickname?: string,
|
||||
) => unknown;
|
||||
nicknameUpdate: [guild: DiscordenoGuild, member: DiscordenoMember, nickname: string, oldNickname?: string];
|
||||
/** A user's presence is their current state on a guild. This event is sent when a user's presence or info, such as name or avatar, is updated. */
|
||||
presenceUpdate?: (
|
||||
presence: PresenceUpdate,
|
||||
oldPresence?: PresenceUpdate,
|
||||
) => unknown;
|
||||
presenceUpdate: [presence: PresenceUpdate, oldPresence?: PresenceUpdate];
|
||||
/** Sent before every event execution. Discordeno will not await its execution. */
|
||||
raw?: (data: GatewayPayload) => unknown;
|
||||
raw: [data: GatewayPayload];
|
||||
/** Sent when all shards went ready. */
|
||||
ready?: () => unknown;
|
||||
ready: [];
|
||||
/** Sent when a user adds a reaction to a message. */
|
||||
reactionAdd?: (
|
||||
data: MessageReactionAdd,
|
||||
message?: DiscordenoMessage,
|
||||
) => unknown;
|
||||
reactionAdd: [data: MessageReactionAdd, message?: DiscordenoMessage];
|
||||
/** Sent when a user removes a reaction from a message. */
|
||||
reactionRemove?: (
|
||||
data: MessageReactionRemove,
|
||||
message?: DiscordenoMessage,
|
||||
) => unknown;
|
||||
reactionRemove: [data: MessageReactionRemove, message?: DiscordenoMessage];
|
||||
/** Sent when a user explicitly removes all reactions from a message. */
|
||||
reactionRemoveAll?: (
|
||||
payload: MessageReactionRemoveAll,
|
||||
message?: DiscordenoMessage,
|
||||
) => unknown;
|
||||
reactionRemoveAll: [payload: MessageReactionRemoveAll, message?: DiscordenoMessage];
|
||||
/** Sent when a bot removes all instances of a given emoji from the reactions of a message. */
|
||||
reactionRemoveEmoji?: (
|
||||
emoji: Partial<Emoji>,
|
||||
messageId: bigint,
|
||||
channelId: bigint,
|
||||
guildId?: bigint,
|
||||
) => unknown;
|
||||
reactionRemoveEmoji: [emoji: Partial<Emoji>, messageId: bigint, channelId: bigint, guildId?: bigint];
|
||||
/** Sent when a guild role is created. */
|
||||
roleCreate?: (guild: DiscordenoGuild, role: DiscordenoRole) => unknown;
|
||||
roleCreate: [guild: DiscordenoGuild, role: DiscordenoRole];
|
||||
/** Sent when a guild role is deleted. */
|
||||
roleDelete?: (guild: DiscordenoGuild, role: DiscordenoRole) => unknown;
|
||||
roleDelete: [guild: DiscordenoGuild, role: DiscordenoRole];
|
||||
/** Sent when a guild role is updated. */
|
||||
roleUpdate?: (
|
||||
guild: DiscordenoGuild,
|
||||
role: DiscordenoRole,
|
||||
old: DiscordenoRole,
|
||||
) => unknown;
|
||||
roleGained?: (
|
||||
guild: DiscordenoGuild,
|
||||
member: DiscordenoMember,
|
||||
roleId: bigint,
|
||||
) => unknown;
|
||||
roleLost?: (
|
||||
guild: DiscordenoGuild,
|
||||
member: DiscordenoMember,
|
||||
roleId: bigint,
|
||||
) => unknown;
|
||||
shardReady?: (shardId: number) => unknown;
|
||||
roleUpdate: [guild: DiscordenoGuild, role: DiscordenoRole, old: DiscordenoRole];
|
||||
roleGained: [guild: DiscordenoGuild, member: DiscordenoMember, roleId: bigint];
|
||||
roleLost: [guild: DiscordenoGuild, member: DiscordenoMember, roleId: bigint];
|
||||
shardReady: [shardId: number];
|
||||
/** Sent when a shard failed to load. */
|
||||
shardFailedToLoad?: (
|
||||
shardId: number,
|
||||
unavailableGuildIds: Set<bigint>,
|
||||
) => unknown;
|
||||
shardFailedToLoad: [shardId: number, unavailableGuildIds: Set<bigint>];
|
||||
/** Sent when a thread is created */
|
||||
threadCreate?: (channel: DiscordenoChannel) => unknown;
|
||||
threadCreate: [channel: DiscordenoChannel];
|
||||
/** Sent when a thread is updated */
|
||||
threadUpdate?: (
|
||||
cahnnel: DiscordenoChannel,
|
||||
oldChannel: DiscordenoChannel,
|
||||
) => unknown;
|
||||
threadUpdate: [cahnnel: DiscordenoChannel, oldChannel: DiscordenoChannel];
|
||||
/** Sent when the bot gains access to threads */
|
||||
threadListSync?: (
|
||||
channels: Collection<bigint, DiscordenoChannel>,
|
||||
members: ThreadMember[],
|
||||
guildId: bigint,
|
||||
) => unknown;
|
||||
threadListSync: [channels: Collection<bigint, DiscordenoChannel>, members: ThreadMember[], guildId: bigint];
|
||||
/** Sent when the current users thread member is updated */
|
||||
threadMemberUpdate?: (threadMember: ThreadMember) => unknown;
|
||||
threadMemberUpdate: [threadMember: ThreadMember];
|
||||
/** Sent when anyone is added to or removed from a thread */
|
||||
threadMembersUpdate?: (update: ThreadMembersUpdate) => unknown;
|
||||
threadMembersUpdate: [update: ThreadMembersUpdate];
|
||||
/** Sent when a thread is deleted */
|
||||
threadDelete?: (channel: DiscordenoChannel) => unknown;
|
||||
threadDelete: [channel: DiscordenoChannel];
|
||||
/** Sent when a user starts typing in a channel. */
|
||||
typingStart?: (data: TypingStart) => unknown;
|
||||
typingStart: [data: TypingStart];
|
||||
/** Sent when a user joins a voice channel */
|
||||
voiceChannelJoin?: (member: DiscordenoMember, channelId: bigint) => unknown;
|
||||
voiceChannelJoin: [member: DiscordenoMember, channelId: bigint];
|
||||
/** Sent when a user leaves a voice channel. Does not get sent when user switches the voice channel */
|
||||
voiceChannelLeave?: (member: DiscordenoMember, channelId: bigint) => unknown;
|
||||
voiceChannelLeave: [member: DiscordenoMember, channelId: bigint];
|
||||
/** Sent when a user switches the voice channel */
|
||||
voiceChannelSwitch?: (
|
||||
member: DiscordenoMember,
|
||||
channelId: bigint,
|
||||
oldChannelId: bigint,
|
||||
) => unknown;
|
||||
voiceChannelSwitch: [member: DiscordenoMember, channelId: bigint, oldChannelId: bigint];
|
||||
/** Sent when a voice server is updated with information for making the bot connect to a voice channel. */
|
||||
voiceServerUpdate?: (
|
||||
payload: VoiceServerUpdate,
|
||||
guild: DiscordenoGuild,
|
||||
) => unknown;
|
||||
voiceServerUpdate: [payload: VoiceServerUpdate, guild: DiscordenoGuild];
|
||||
/** Sent when someone joins/leaves/moves voice channels. */
|
||||
voiceStateUpdate?: (
|
||||
member: DiscordenoMember,
|
||||
voiceState: VoiceState,
|
||||
) => unknown;
|
||||
voiceStateUpdate: [member: DiscordenoMember, voiceState: VoiceState];
|
||||
/** Sent when a guild channel's webhook is created, updated, or deleted. */
|
||||
webhooksUpdate?: (channelId: bigint, guildId: bigint) => unknown;
|
||||
webhooksUpdate: [channelId: bigint, guildId: bigint];
|
||||
/** Sent when a member has passed the guild's Membership Screening requirements */
|
||||
membershipScreeningPassed?: (
|
||||
guild: DiscordenoGuild,
|
||||
member: DiscordenoMember,
|
||||
) => unknown;
|
||||
membershipScreeningPassed: [guild: DiscordenoGuild, member: DiscordenoMember];
|
||||
/** Sent when an integration is created on a server such as twitch, youtube etc.. */
|
||||
integrationCreate?: (data: IntegrationCreateUpdate) => unknown;
|
||||
integrationCreate: [data: IntegrationCreateUpdate];
|
||||
/** Sent when an integration is updated. */
|
||||
integrationUpdate?: (data: IntegrationCreateUpdate) => unknown;
|
||||
integrationUpdate: [data: IntegrationCreateUpdate];
|
||||
/** Sent when an integration is deleted. */
|
||||
integrationDelete?: (data: IntegrationDelete) => undefined;
|
||||
integrationDelete: [data: IntegrationDelete];
|
||||
/** Sent when a new invite to a channel is created. */
|
||||
inviteCreate?: (data: InviteCreate) => unknown;
|
||||
inviteCreate: [data: InviteCreate];
|
||||
/** Sent when an invite is deleted. */
|
||||
inviteDelete?: (data: InviteDelete) => unknown;
|
||||
inviteDelete: [data: InviteDelete];
|
||||
}
|
||||
|
||||
export type EventHandlerFunctions = {
|
||||
[E in keyof EventHandlers]?: (...args: EventHandlers[E]) => unknown;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user