Currently, my team and I are working on a Discord bot project using discordjs and TypeScript. We have implemented an event handler in the main file that scans through a folder called `events\` to find files with `.js` or `.ts` extensions and imports them.
Here is an example of an ErrorEvent file:
// file: src\events\ErrorEvent.ts
import CustomClient from "../libs/customClient";
import Event from "../libs/structure/event/Event";
export default class ErrorEvent extends Event {
constructor(client:CustomClient ){
super(client, "error");
}
run = async(error:string):Promise<void>=> {
console.log("Discord client error!", error)
}
}
In the code above, the `Event` class is defined as follows:
// file: src\libs\structure\event\Event.ts
import CustomClient from "../../customClient";
export default class Event {
client:CustomClient;
name:string;
constructor(client:CustomClient, name:string) {
this.client = client;
this.name = name;
}
// 'run' method with one parameter, 'error', which is a string containing the error message
}
The constructor for the event class takes two parameters - `client` and `name` of the event, where the name varies for each event.
However, I aim to introduce a method called `run` that will be executed when an event occurs. This method should be defined in every subclass but not in the main class, with different parameters for each subclass. For instance,
MessageEvent -> Params = message: Message (Discord.Message) ErrorEvent -> Params = error: string (String representing error) etc.
I am unsure how to create a method that accommodates different parameters for various functions. Additionally, I wish to connect an interface with this class.
Although I created an interface file, it generates errors when linked:
// file: src\interface\EventInt.ts
import CustomClient from "../libs/customClient";
export default interface Events {
client: CustomClient,
name: string,
run: (args: unknown) => void
}