【发布时间】:2021-10-02 10:31:07
【问题描述】:
我和我的团队目前正在使用 discordjs 和 typescript 开发一个不和谐机器人项目。我们制作了一个像这样工作的事件处理程序,
主文件滚动通过文件夹events\ 并找出具有.js 或.ts 扩展名的文件。然后导入文件。
例如,ErrorEvent 文件如下所示,
// 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)
}
}
在上面的代码中,Event 类是,
// 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;
}
// a run method that has 1 parameter, error which is a string containing the error message
}
事件类构造函数有两个参数,客户端和事件名称。每个事件的事件名称都不同。
但我要添加的是一个名为 run 的方法。当事件发生时将调用此运行方法。 run 方法应该在每个子类中定义,而不是在主类中。但是,不同类型的运行方法参数会有所不同。例如,
MessageEvent -> 参数 = message:Message (Discord.Message) ErrorEvent -> Params = error:string(表示错误的字符串) 等等
我不知道如何制作一个允许不同函数使用不同参数的方法。我也想把一个接口和这个类联系起来。
我制作的接口文件,但链接时出错,
// file: src\interface\EventInt.ts
import CustomClient from "../libs/customClient";
export default interface Events {
client: CustomClient,
name: string,
run:(args:unknown) => void
}```
【问题讨论】:
标签: javascript typescript class discord bots