坚持将变量添加到 Discord Client 对象 Typescript

时间:2023-02-11
本文介绍了坚持将变量添加到 Discord Client 对象 Typescript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Typescript 的新手,正在使用 Typescript 编写一个 Discord 机器人.我想添加一个变量commands"到客户端对象.例如在 Javascript 中,你使用这个:

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'

但现在我想添加类似于 Typescript 的内容.但是当我在 Typescript 中使用它时,出现以下错误:

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)

我该如何解决这个问题?

How can I solve this?

我现在的代码:

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

默认情况下,commands 不是 Discord.Client 对象上的现有属性类型,但您可以通过创建一个 <代码>.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.

我的项目目录中有 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>
    }
}

这解决了我的问题.

如果您使用 discord.js 指南中的单文件样式命令,效果会更好:

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")中访问命令对象时也可以得到代码补全,也可以导入Command 如果需要,请从 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".

当我想从命令文件中严格键入导出的命令时,我发现这很有用,例如:

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;

这篇关于坚持将变量添加到 Discord Client 对象 Typescript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:addRole 不是函数 下一篇:是否有向群组的所有成员发送私人消息的命令?

相关文章

最新文章