pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

堅(jiān)持將變量添加到 Discord Client 對(duì)象 Typescript

Stuck with adding variable to Discord Client object Typescript(堅(jiān)持將變量添加到 Discord Client 對(duì)象 Typescript)
本文介紹了堅(jiān)持將變量添加到 Discord Client 對(duì)象 Typescript的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(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)!

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

相關(guān)文檔推薦

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(如何讓我的機(jī)器人提及發(fā)出該機(jī)器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復(fù)必須使用導(dǎo)入來(lái)加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來(lái)自特定服務(wù)器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復(fù)“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務(wù)器時(shí)的歡迎消息)
主站蜘蛛池模板: 大型低温冷却液循环泵-低温水槽冷阱「厂家品牌」京华仪器_京华仪器 | 数控车床-立式加工中心-多功能机床-小型车床-山东临沂金星机床有限公司 | 采暖炉_取暖炉_生物质颗粒锅炉_颗粒壁炉_厂家加盟批发_烟台蓝澳采暖设备有限公司 | 学叉车培训|叉车证报名|叉车查询|叉车证怎么考-工程机械培训网 | 广东恩亿梯电源有限公司【官网】_UPS不间断电源|EPS应急电源|模块化机房|电动汽车充电桩_UPS电源厂家(恩亿梯UPS电源,UPS不间断电源,不间断电源UPS) | 北京开业庆典策划-年会活动策划公司-舞龙舞狮团大鼓表演-北京盛乾龙狮鼓乐礼仪庆典策划公司 | 熔体泵|换网器|熔体齿轮泵|熔体计量泵厂家-郑州巴特熔体泵有限公司 | Dataforth隔离信号调理模块-信号放大模块-加速度振动传感器-北京康泰电子有限公司 | 云南标线|昆明划线|道路标线|交通标线-就选云南云路施工公司-云南云路科技有限公司 | 电梯装饰-北京万达中意电梯装饰有限公司 | 线粒体膜电位荧光探针-细胞膜-标记二抗-上海复申生物科技有限公司 | 电伴热系统施工_仪表电伴热保温箱厂家_沃安电伴热管缆工业技术(济南)有限公司 | 济南律师,济南法律咨询,山东法律顾问-山东沃德律师事务所 | 安徽净化工程设计_无尘净化车间工程_合肥净化实验室_安徽创世环境科技有限公司 | 陕西安玻璃自动感应门-自动重叠门-磁悬浮平开门厂家【捷申达门业】 | 多米诺-多米诺世界纪录团队-多米诺世界-多米诺团队培训-多米诺公关活动-多米诺创意广告-多米诺大型表演-多米诺专业赛事 | 垃圾压缩设备_垃圾处理设备_智能移动式垃圾压缩设备--山东明莱环保设备有限公司 | 护腰带生产厂家_磁石_医用_热压护腰_登山护膝_背姿矫正带_保健护具_医疗护具-衡水港盛 | 天一线缆邯郸有限公司_煤矿用电缆厂家_矿用光缆厂家_矿用控制电缆_矿用通信电缆-天一线缆邯郸有限公司 | 圣才学习网-考研考证学习平台,提供万种考研考证电子书、题库、视频课程等考试资料 | TPM咨询,精益生产管理,5S,6S现场管理培训_华谋咨询公司 | 板框压滤机-隔膜压滤机-厢式压滤机生产厂家-禹州市君工机械设备有限公司 | 电磁辐射仪-电磁辐射检测仪-pm2.5检测仪-多功能射线检测仪-上海何亦仪器仪表有限公司 | CCE素质教育博览会 | CCE素博会 | 教育展 | 美育展 | 科教展 | 素质教育展 | 交联度测试仪-湿漏电流测试仪-双85恒温恒湿试验箱-常州市科迈实验仪器有限公司 | 杭州营业执照代办-公司变更价格-许可证办理流程_杭州福道财务管理咨询有限公司 | Safety light curtain|Belt Sway Switches|Pull Rope Switch|ultrasonic flaw detector-Shandong Zhuoxin Machinery Co., Ltd | 泰安办公家具-泰安派格办公用品有限公司 | 超声波清洗机_细胞破碎仪_实验室超声仪器_恒温水浴-广东洁盟深那仪器 | 武汉印刷厂-不干胶标签印刷厂-武汉不干胶印刷-武汉标签印刷厂-武汉标签制作 - 善进特种标签印刷厂 | 智成电子深圳tdk一级代理-提供TDK电容电感贴片蜂鸣器磁芯lambda电源代理经销,TDK代理商有哪些TDK一级代理商排名查询。-深圳tdk一级代理 | 西安微信朋友圈广告投放_微信朋友圈推广_西安度娘网络科技有限公司 | 小港信息港-鹤壁信息港 鹤壁老百姓便民生活信息网站 | 杭州营业执照代办-公司变更价格-许可证办理流程_杭州福道财务管理咨询有限公司 | 热回收盐水机组-反应釜冷水机组-高低温冷水机组-北京蓝海神骏科技有限公司 | 工控机-工业平板电脑-研华工控机-研越无风扇嵌入式box工控机 | 400电话_400电话申请_866元/年_【400电话官方业务办理】-俏号网 3dmax渲染-效果图渲染-影视动画渲染-北京快渲科技有限公司 | 谷歌关键词优化-外贸网站优化-Google SEO小语种推广-思亿欧外贸快车 | 微波消解仪器_智能微波消解仪报价_高压微波消解仪厂家_那艾 | 驾驶人在线_专业学车门户网站 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 |