【发布时间】:2021-07-26 21:24:46
【问题描述】:
我是 TypeScript 的新手,我想在创建一个非常简单的 Discord Bot 的同时学习该语言,使用一个名为 Discord.JS 的模块。 我的问题是我想扩展他们的Client 类并添加我自己的方法和对象,以便在名为 myClient
的类中将内容存储在内存中我的目录树(不包括/dist)
├── package.json
├── package-lock.json
├── src
│ ├── bot.ts
│ ├── commands
│ │ └── test.ts
│ └── @types
│ └── index.d.ts
└── tsconfig.json
@types/index.d.ts
import { Client, Message, PermissionString, TextChannel, VoiceConnection, Collection, ClientOptions } from "discord.js";
export type ApplicationCommandOptionChoice = {
readonly name: String;
readonly value: String | number;
}
export type ApplicationCommandOption = {
readonly type: number;
readonly name: String;
readonly description: String;
readonly required?: Boolean;
readonly choices?: Array<ApplicationCommandOptionChoice>;
readonly options?: Array<ApplicationCommandOption>;
}
export interface ApplicationCommandModule<object> {
readonly name: String;
readonly description: String;
readonly options?: Array<ApplicationCommandOption>;
readonly default_permission?: Boolean;
readonly permissions: PermissionString = 'SEND_MESSAGES';
readonly usage: String;
readonly devOnly?: Boolean;
readonly execute: (args?: Array<T>) => any;
}
export class myClient extends Client {
constructor(options: ClientOptions) {
super(options);
this.commands = new Collection();
}
public commands: Collection<String, object>;
lookupCommand(name: String) {
const command = this.commands.get(name);
if (command) return command as ApplicationCommandModule<object>;
return null;
}
executeCommand(name: String, ...args: any[]) {
const command = this.commands.get(name);
if (command) return (command as ApplicationCommandModule<object>).execute(args);
return null;
}
}
bot.ts
import Discord from 'discord.js';
import { myClient } from './@types';
const client = new myClient({ intents: Discord.Intents.NON_PRIVILEGED });
tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"outDir": "./dist",
"strict": true,
"types": ["./src/@types"],
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
当我运行 npm run dev 这是ts-node-dev --respawn ./src/bot.ts 时,我收到以下错误:
但是,如果我将 export class myClient extends Client { ... } 代码 sn-p 从 index.d.ts 移动到 bot.ts,它就可以工作。这让我很困惑,因为我不想在那里上课。
那么我该如何正确导入myClient 类呢?另外,为什么它是从@types 自动导入的,这不应该是“已知的”吗?
更新:我将我的 myClient 类移动到一个名为 client.ts 的 .ts 文件中并导入它,它可以工作。为什么会有这种行为?类只能在 .ts 文件中吗?当我检查 node_modules 他们的类扩展在 index.d.ts 但不是他们的方法。那么,如果我将类结构放在 index.d.ts 中,我应该把它的方法放在哪里?
【问题讨论】:
-
只是为了填写,我尝试在我的自定义对象中进行转换,如 (client as any).myObject 以避免上述麻烦,但这并没有给我任何类型和自动完成,所以我想不要这样做。
-
您确定 discord.js 与 typescript 兼容吗?
-
.d.ts文件不应该包含逻辑,只有类型。在.ts文件中扩展类,并将基类型放在.d.ts文件中。 typescriptlang.org/docs/handbook/declaration-files/… -
@AnžePintar 是的。
-
@Lioness100 哦,这就解释了,谢谢。是的,将其移至 .ts 文件是可行的,我将按照链接确保我做对了。
标签: typescript discord.js