問(wèn)題描述
我是 Typescript 的新手,正在使用 Typescript 編寫(xiě)一個(gè) Discord 機(jī)器人.我想添加一個(gè)變量commands"到客戶端對(duì)象.例如在 Javascript 中,你使用這個(gè):
Im new to Typescript and writing a Discord bot using Typescript. I want to add a variable "commands" to the Client object. For example in Javascript, you using this:
Javascript
const { Client } = require('discord.js');
const client = new Client();
client.commands = 'commands';
console.log(client.commands);
// 'commands'
但現(xiàn)在我想添加類似于 Typescript 的內(nèi)容.但是當(dāng)我在 Typescript 中使用它時(shí),出現(xiàn)以下錯(cuò)誤:
but now I want to add something similar to Typescript. But when Im using this in Typescript, I got the following error:
Property 'commands' does not exist on type 'Client'.ts(2339)
我該如何解決這個(gè)問(wèn)題?
How can I solve this?
我現(xiàn)在的代碼:
export class HalloClient {
private client: Client;
constructor() {
this.client = new Client();
this.client.commands = new Collection();
}
public start(): void {
console.log(`- Client | Starting process...`);
new RegisterEvents('../events/', this.client).load();
new MongoConnection(process.env.mongouri).createConnection();
console.log(this.client);
this.client.login(process.env.token);
}
}
推薦答案
我在使用 typescript 并遵循 https://discordjs.guide
I was having the same issue when using typescript and following the guide from https://discordjs.guide
默認(rèn)情況下,commands
不是 Discord.Client
對(duì)象上的現(xiàn)有屬性類型,但您可以通過(guò)創(chuàng)建一個(gè) <代碼>.d.ts 文件.
By default, commands
is not an existing attribute type on Discord.Client
object, but you can easily extend Discord.js typings with your own type by creating a .d.ts
file.
我的項(xiàng)目目錄中有 discord.d.ts
文件,它包含:
I have discord.d.ts
file on my project directory, and it contains:
declare module "discord.js" {
export interface Client {
commands: Collection<unknown, any>
}
}
這解決了我的問(wèn)題.
如果您使用 discord.js 指南中的單文件樣式命令,效果會(huì)更好:
Or even better if you are using the single-file style command from discord.js guide:
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")
中訪問(wèn)命令對(duì)象時(shí)也可以得到代碼補(bǔ)全,也可以導(dǎo)入Command
如果需要,請(qǐng)從 import { Command } from "discord.js"
輸入.
This way, you also get code completion when accessing a command object from this.client.commands.get("commandName")
, and you also can import Command
type if you need it from import { Command } from "discord.js"
.
當(dāng)我想從命令文件中嚴(yán)格鍵入導(dǎo)出的命令時(shí),我發(fā)現(xiàn)這很有用,例如:
I find this useful when I want to strictly type my exported command from my command file, for example:
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;
這篇關(guān)于堅(jiān)持將變量添加到 Discord Client 對(duì)象 Typescript的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!