Improve event handler to only run events that have been registered

This commit is contained in:
Ethan Lane 2022-10-03 18:02:40 +01:00
parent ed8f5927c8
commit 287aafaca2
Signed by: Vylpes
GPG key ID: EED233CC06D12504
6 changed files with 75 additions and 20 deletions

View file

@ -1,6 +1,7 @@
import { Client } from "discord.js"; import { Client } from "discord.js";
import * as dotenv from "dotenv"; import * as dotenv from "dotenv";
import { createConnection } from "typeorm"; import { createConnection } from "typeorm";
import { EventType } from "../constants/EventType";
import ICommandItem from "../contracts/ICommandItem"; import ICommandItem from "../contracts/ICommandItem";
import IEventItem from "../contracts/IEventItem"; import IEventItem from "../contracts/IEventItem";
import { Command } from "../type/command"; import { Command } from "../type/command";
@ -65,9 +66,10 @@ export class CoreClient extends Client {
CoreClient._commandItems.push(item); CoreClient._commandItems.push(item);
} }
public static RegisterEvent(event: Event) { public static RegisterEvent(eventType: EventType, func: Function) {
const item: IEventItem = { const item: IEventItem = {
Event: event, EventType: eventType,
ExecutionFunction: func,
}; };
CoreClient._eventItems.push(item); CoreClient._eventItems.push(item);

View file

@ -1,4 +1,5 @@
import { Client, REST, Routes, SlashCommandBuilder } from "discord.js"; import { Client, REST, Routes, SlashCommandBuilder } from "discord.js";
import { EventType } from "../constants/EventType";
import IEventItem from "../contracts/IEventItem"; import IEventItem from "../contracts/IEventItem";
import { CoreClient } from "./client"; import { CoreClient } from "./client";
@ -49,19 +50,46 @@ export class Util {
// Load the events // Load the events
loadEvents(client: Client, events: IEventItem[]) { loadEvents(client: Client, events: IEventItem[]) {
events.forEach((e) => { events.forEach((e) => {
client.on('channelCreate', e.Event.channelCreate); switch(e.EventType) {
client.on('channelDelete', e.Event.channelDelete); case EventType.ChannelCreate:
client.on('channelUpdate', e.Event.channelUpdate); client.on('channelCreate', (channel) => e.ExecutionFunction(channel));
client.on('guildBanAdd', e.Event.guildBanAdd); break;
client.on('guildBanRemove', e.Event.guildBanRemove); case EventType.ChannelDelete:
client.on('guildCreate', e.Event.guildCreate); client.on('channelDelete', (channel) => e.ExecutionFunction(channel));
client.on('guildMemberAdd', e.Event.guildMemberAdd); break;
client.on('guildMemberRemove', e.Event.guildMemberRemove); case EventType.ChannelUpdate:
client.on('guildMemberUpdate', e.Event.guildMemberUpdate); client.on('channelUpdate', (channel) => e.ExecutionFunction(channel));
client.on('messageCreate', e.Event.messageCreate); break;
client.on('messageDelete', e.Event.messageDelete); case EventType.GuildBanAdd:
client.on('messageUpdate', e.Event.messageUpdate); client.on('guildBanAdd', (ban) => e.ExecutionFunction(ban));
client.on('ready', e.Event.ready); break;
case EventType.GuildBanRemove:
client.on('guildBanRemove', (ban) => e.ExecutionFunction(ban));
break;
case EventType.GuildCreate:
client.on('guildCreate', (guild) => e.ExecutionFunction(guild));
break;
case EventType.GuildMemberAdd:
client.on('guildMemberAdd', (member) => e.ExecutionFunction(member));
break;
case EventType.GuildMemberRemove:
client.on('guildMemberRemove', (member) => e.ExecutionFunction(member));
break;
case EventType.GuildMemberUpdate:
client.on('guildMemberUpdate', (oldMember, newMember) => e.ExecutionFunction(oldMember, newMember));
break;
case EventType.MessageCreate:
client.on('messageCreate', (message) => e.ExecutionFunction(message));
break;
case EventType.MessageDelete:
client.on('messageDelete', (message) => e.ExecutionFunction(message));
break;
case EventType.MessageUpdate:
client.on('messageUpdate', (oldMessage, newMessage) => e.ExecutionFunction(oldMessage, newMessage));
break;
default:
console.error('Event not implemented.');
}
}); });
} }
} }

View file

@ -0,0 +1,15 @@
export enum EventType {
ChannelCreate,
ChannelDelete,
ChannelUpdate,
GuildBanAdd,
GuildBanRemove,
GuildCreate,
GuildMemberAdd,
GuildMemberRemove,
GuildMemberUpdate,
MessageCreate,
MessageDelete,
MessageUpdate,
Ready,
}

View file

@ -1,6 +1,7 @@
import { Event } from "../type/event"; import { EventType } from "../constants/EventType";
export default interface IEventItem { export default interface IEventItem {
Event: Event, EventType: EventType,
ExecutionFunction: Function,
} }

View file

@ -71,7 +71,7 @@ export default class MessageEvents extends Event {
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setColor(EmbedColours.Ok) .setColor(EmbedColours.Ok)
.setTitle("Message Deleted") .setTitle("Message Edited")
.setDescription(`${newMessage.author} \`${newMessage.author.tag}\``) .setDescription(`${newMessage.author} \`${newMessage.author.tag}\``)
.addFields([ .addFields([
{ {

View file

@ -28,6 +28,7 @@ import RemoveLobby from "./commands/501231711271780357/Lobby/remove";
import MemberEvents from "./events/MemberEvents"; import MemberEvents from "./events/MemberEvents";
import MessageEvents from "./events/MessageEvents"; import MessageEvents from "./events/MessageEvents";
import Bunny from "./commands/bunny"; import Bunny from "./commands/bunny";
import { EventType } from "./constants/EventType";
export default class Registry { export default class Registry {
public static RegisterCommands() { public static RegisterCommands() {
@ -64,7 +65,15 @@ export default class Registry {
} }
public static RegisterEvents() { public static RegisterEvents() {
CoreClient.RegisterEvent(new MemberEvents()); const memberEvents = new MemberEvents();
CoreClient.RegisterEvent(new MessageEvents()); const messageEvents = new MessageEvents();
CoreClient.RegisterEvent(EventType.GuildMemberAdd, memberEvents.guildMemberAdd);
CoreClient.RegisterEvent(EventType.GuildMemberRemove, memberEvents.guildMemberRemove);
CoreClient.RegisterEvent(EventType.GuildMemberUpdate, memberEvents.guildMemberUpdate);
CoreClient.RegisterEvent(EventType.MessageDelete, messageEvents.messageDelete);
CoreClient.RegisterEvent(EventType.MessageUpdate, messageEvents.messageUpdate);
CoreClient.RegisterEvent(EventType.MessageCreate, messageEvents.messageCreate);
} }
} }