久久久久久久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 服務器時的歡迎消息)
主站蜘蛛池模板: 999久久久 | 成人动漫视频网站 | 欧美a免费 | 久久久久九九九女人毛片 | 中文字幕一区二区三区乱码在线 | 亚洲在线| 成人做爰www免费看视频网站 | 精品免费在线 | av资源网站| 日本在线免费视频 | 国产日产精品一区二区三区四区 | 国产精品久久久久aaaa | 韩日av在线 | 久久草在线视频 | av激情影院| 欧美福利专区 | 精品视频一区二区三区在线观看 | 先锋资源亚洲 | 欧美色性| 在线一区| 亚洲欧洲在线观看视频 | 国产精品视频一区二区三区 | 性欧美精品一区二区三区在线播放 | 日韩中文欧美 | 久久久久久久一级 | 97福利在线 | 亚洲成人av | 欧美精品第三页 | 爱高潮www亚洲精品 中文字幕免费视频 | 国产精品一码二码三码在线 | 久久久久国产 | 国产中文字幕亚洲 | 在线观看深夜视频 | 国产丝袜一区二区三区免费视频 | 国产精品18毛片一区二区 | 国产亚洲成av人在线观看导航 | 久久精品欧美一区二区三区麻豆 | 国产精品一区二区免费 | 日日骚网 | 亚洲综合大片69999 | 中文字幕亚洲在线 |