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

View file

@ -1,4 +1,5 @@
import { Client, REST, Routes, SlashCommandBuilder } from "discord.js";
import { EventType } from "../constants/EventType";
import IEventItem from "../contracts/IEventItem";
import { CoreClient } from "./client";
@ -49,19 +50,46 @@ export class Util {
// Load the events
loadEvents(client: Client, events: IEventItem[]) {
events.forEach((e) => {
client.on('channelCreate', e.Event.channelCreate);
client.on('channelDelete', e.Event.channelDelete);
client.on('channelUpdate', e.Event.channelUpdate);
client.on('guildBanAdd', e.Event.guildBanAdd);
client.on('guildBanRemove', e.Event.guildBanRemove);
client.on('guildCreate', e.Event.guildCreate);
client.on('guildMemberAdd', e.Event.guildMemberAdd);
client.on('guildMemberRemove', e.Event.guildMemberRemove);
client.on('guildMemberUpdate', e.Event.guildMemberUpdate);
client.on('messageCreate', e.Event.messageCreate);
client.on('messageDelete', e.Event.messageDelete);
client.on('messageUpdate', e.Event.messageUpdate);
client.on('ready', e.Event.ready);
switch(e.EventType) {
case EventType.ChannelCreate:
client.on('channelCreate', (channel) => e.ExecutionFunction(channel));
break;
case EventType.ChannelDelete:
client.on('channelDelete', (channel) => e.ExecutionFunction(channel));
break;
case EventType.ChannelUpdate:
client.on('channelUpdate', (channel) => e.ExecutionFunction(channel));
break;
case EventType.GuildBanAdd:
client.on('guildBanAdd', (ban) => e.ExecutionFunction(ban));
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 {
Event: Event,
EventType: EventType,
ExecutionFunction: Function,
}

View file

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

View file

@ -28,6 +28,7 @@ import RemoveLobby from "./commands/501231711271780357/Lobby/remove";
import MemberEvents from "./events/MemberEvents";
import MessageEvents from "./events/MessageEvents";
import Bunny from "./commands/bunny";
import { EventType } from "./constants/EventType";
export default class Registry {
public static RegisterCommands() {
@ -64,7 +65,15 @@ export default class Registry {
}
public static RegisterEvents() {
CoreClient.RegisterEvent(new MemberEvents());
CoreClient.RegisterEvent(new MessageEvents());
const memberEvents = new MemberEvents();
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);
}
}