1 回答

TA贡献1883条经验 获得超3个赞
我在使用打字稿并遵循https://discordjs.guide的指南时遇到了同样的问题
默认情况下,commands它不是对象的现有属性类型,但您可以通过创建文件Discord.Client轻松地使用您自己的类型扩展 Discord.js 类型。.d.ts
discord.d.ts我的项目目录中有文件,它包含:
declare module "discord.js" {
export interface Client {
commands: Collection<unknown, any>
}
}
这解决了我的问题。
如果您使用discord.js 指南中的单文件样式命令,甚至更好:
import { Message } from "discord.js";
declare module "discord.js" {
export interface Client {
commands: Collection<unknown, Command>
}
export interface Command {
name: string,
description: string,
execute: (message: Message, args: string[]) => SomeType // Can be `Promise<SomeType>` if using async
}
}
这样,您还可以在从 访问命令对象时获得代码补全,如果需要this.client.commands.get("commandName"),您还可以从.Commandimport { Command } from "discord.js"
当我想从命令文件中严格键入导出的命令时,我发现这很有用,例如:
import { Command } from "discord.js";
// Now `command` is strictly typed to `Command` interface
const command: Command = {
name: "someCommand",
description: "Some Command",
execute(message, args): SomeType /* Can be Promise<SomeType> if using async */ {
// do something
}
};
export = command;
添加回答
举报