久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

堅持將變量添加到 Discord Client 對象 Typescript

Stuck with adding variable to Discord Client object Typescript(堅持將變量添加到 Discord Client 對象 Typescript)
本文介紹了堅持將變量添加到 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 日韩精品一区二区三区免费观看 | 亚洲国产精品久久久 | 国产精品久久久久久久久久久久冷 | 草草草网站 | 日韩在线精品强乱中文字幕 | 自拍偷拍第一页 | 日韩激情一区 | 韩日视频在线观看 | 国产.com| 久久99久久99精品免视看婷婷 | 特黄视频 | 美女黄网 | cao在线 | 久久久久久久久久久一区二区 | 亚洲一区二区三区乱码aⅴ 四虎在线视频 | 天堂视频中文在线 | 欧美精品一区二区三区在线 | 美女天堂在线 | 亚洲精品一区中文字幕乱码 | 欧美一区二区三区高清视频 | 欧美国产视频 | 欧产日产国产精品99 | 精品中文在线 | 欧美日韩不卡在线 | 美女福利网站 | 国产91在线 | 中日 | 国产人成精品一区二区三 | 中文字幕精| 国产在线精品一区二区三区 | 最新中文字幕在线 | 国产成人综合亚洲欧美94在线 | 色婷婷综合久久久久中文一区二区 | 国产激情视频在线 | 国产一区二区在线视频 | 美女啪啪国产 | 日韩精品免费视频 | 日韩成人免费 | 五月精品视频 | 日韩精品在线看 | 精品一区二区久久久久久久网站 | 免费影视在线观看 |