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

如何為我的 Discord.js 機器人編寫事件/命令處理程

How do I code event/command handlers for my Discord.js bot?(如何為我的 Discord.js 機器人編寫事件/命令處理程序?)
本文介紹了如何為我的 Discord.js 機器人編寫事件/命令處理程序?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我已經開始使用 Discord.js 庫在 Node.js 中創建 Discord 機器人.但是,所有代碼都包含在一個索引文件中.

I've started creating a Discord bot in Node.js using the Discord.js library. However, all the code is contained within a single index file.

如何將命令和事件分別組織到單獨的文件中,并在需要時運行它們?

How do I organize the commands and events each into separate files, and run them when needed?

推薦答案

為您的機器人組織代碼的一種出色、簡潔的方法是使用事件和命令處理程序.

An excellent, clean way to organize the code for your bot is to employ event and command handlers.

您從一個小的索引文件開始初始化客戶端和其余代碼.事件處理程序保存每個事件的文件,并在事件發出時調用它們.然后,在客戶端的 message 事件中,您可以通過運行來自預期命令文件的代碼.

You start out with a small index file to initialize the client and the rest of the code. An event handler keeps the files for each event, and calls on them when the event is emitted. Then, in your client's message event, you can avoid long if chains and switch/case altogether by running the code from the intended command's file.

您需要了解的基本 Node.js 結構是 module.

The basic Node.js structure you'll need to understand is a module.

[A module is a] 您希望在應用程序中包含的一組函數.

[A module is a] set of functions you want to include in your application.

引自 w3schools.com.

因此,可以將模塊視為一個包含代碼片段的整齊粘貼的盒子.你可以把包裹帶到某個地方,打開它,然后拆開包裝.在 JavaScript 術語中,您可以在程序中的其他地方要求該模塊,并利用其中包含的代碼.模塊可以包含您需要在代碼的不同位置使用的變量、類、函數等.

So, think of a module as a neatly taped up box containing pieces of code. You can take the package somewhere, open it up, and unpack the pieces. In JavaScript terms, you can require the module somewhere else in your program, and utilize the code contained within it. Modules can contain variables, classes, functions, etc. that you need to use throughout different locations across your code.

既然您知道什么是模塊,那么您必須了解如何使用它們.

Now that you know what a module is, you have to understand how to work with them.

出于處理程序的目的,您將只使用 module 對象的 exports 屬性.通過對模塊使用 require()module.exports 返回.請考慮以下設置.

For the purpose of the handlers, you're only going to be using the exports property of the module object. By using require() for a module, module.exports is returned. Consider the following setups.

Question.js

class Question {
  constructor(author, details) {
    this.author = author;
    this.details = details;
    this.answers = [];
  }
}

module.exports = Question;

newQuestion.js

const Question = require('./Question.js');

const myQuestion = new Question('me', 'How to code event/command handlers?');

Question.js 中,創建了一個新類 Question,并將其分配給 module.exports.然后,當 newQuestion.js 中需要 Question.js 時,Question 被聲明為導出的類.它可以像往常一樣使用.

In Question.js, a new class, Question, is created and assigned to module.exports. Then, when Question.js is required in newQuestion.js, Question is declared as the exported class. It can be used just as usual.

現在,例如,如果您需要導出多個類...

Now, for example, if you needed to export multiple classes...

Posts.js

class Question {...}
class Answer {...}

module.exports = { Question, Answer };

// Alternatively...
// module.exports.Question = Question;
// module.exports.Answer = Answer;

newQuestion.js

const { Question } = require('./Posts.js');

const myQuestion = new Question(...);

這樣,module.exports 被定義為一個對象,包含所創建的類.這意味著 require() 將返回一個對象,因此您可以 從對象中解構所需的類.

In this way, module.exports is defined as an object, containing the created classes. This means that require() will return an object instead, so you can destructure the needed class from the object.

您應該首先為您的活動創建一個文件夾,然后為每個活動創建一個文件.根據事件的名稱命名文件.例如,對于您客戶的 message event,該文件應命名為 message.js.

You should start by creating a folder for your events, and create a file for each one. Name the files according to the name of the event. For example, for your client's message event, the file should be named message.js.

實現您現在對模塊的了解,您可以編寫事件文件.比如……

Implementing what you now know about modules, you can code the event files. For example...

message.js

module.exports = (client, message) => {
  // This code will be executed when
  // the 'message' event is emitted.
};

設置處理程序.

要制作實際的處理程序,您可以將以下代碼放在函數中以加載事件...

Setting up the handler.

To make the actual handler, you can place the following code in a function to load events...

const requireAll = require('require-all');   // Don't forget to install!

const files = requireAll({                   // Require all the files within your
  dirname: `${__dirname}/events`,            // event directory which have a name
  filter: /^(?!-)(.+).js$/                  // ending in '.js' NOT starting
});                                          // with '-' (a way to disable files).

client.removeAllListeners();                 // Prevent duplicate listeners on reload.
                                             // CAUTION: THIS REMOVES LISTENERS
                                             // ATTACHED BY DISCORD.JS!

for (const name in files) {                  // Iterate through the files object
  const event = files[name];                 // and attach listeners to each
                                             // event, passing 'client' as the
  client.on(name, event.bind(null, client)); // first parameter, and the rest
                                             // of the expected parameters
  console.log(`Event loaded: ${name}`);      // afterwards. Then, log the
}                                            // successful load to the console.

現在,當您的客戶端發出您有文件的事件之一時,其中的代碼就會運行.

Now, when your client emits one of the events you have a file for, the code inside of it is run.

就像事件處理程序一樣,您應該首先為您的命令創建一個單獨的文件夾,然后為每個單獨的命令創建文件.

Just like for the event handler, you should start by creating a separate folder for your commands, and create files for each individual command.

您可以導出一個運行"函數一個配置對象,而不是只導出一個函數.

Instead of exporting just one function, you can export a "run" function and a configuration object.

help.js

module.exports.run = async (client, message, args) => {
  // This code will be executed to
  // run the 'help' command.
};

module.exports.config = {
  name: 'help',
  aliases: ['h'] // Even if you don't want an alias, leave this as an array.
};

設置處理程序.

就像事件處理程序一樣,將此代碼放在一個函數中以加載命令...

Setting up the handler.

Just like the event handler, place this code in a function to load commands...

const requireAll = require('require-all');   // Using the same npm module...

const files = requireAll({                   // Require all the files within your
  dirname: `${__dirname}/commands`,          // command directory which have a name
  filter: /^(?!-)(.+).js$/                  // ending in '.js' NOT starting
});                                          // with '-' (a way to disable files).

client.commands = new Map();                 // Create new Maps for the corresponding
client.aliases = new Map();                  // command names/commands, and aliases.

for (const name in files) {                  // Iterate through the files object
  const cmd = files[name];                   // and set up the 'commands' and
                                             // 'aliases' Maps. Then, log the
  client.commands.set(cmd.config.name, cmd); // successful load to the console.
  for (const a of cmd.config.aliases) client.aliases.set(a, cmd.config.name);

  console.log(`Command loaded: ${cmd.config.name}`);
}

在您客戶端的 message 事件中,您可以使用以下代碼運行命令...

In your client's message event, you can use the following code to run the commands...

const prefix = '!'; // Example
const [cmd, ...args] = message.content.trim().slice(prefix.length).split(/s+/g);

const command = client.commands.get(cmd) || client.commands.get(client.aliases.get(cmd));
if (command) {
  command.run(client, message, args);
  console.log(`Executing ${command.config.name} command for ${message.author.tag}.`);
}

<小時>

常見問題.

如果我需要傳遞事件/命令的數據庫相關變量或其他變量怎么辦?

對于事件,您可以在 event.on(...) 中傳遞您的變量,在 client 之后.然后在您的實際事件中,您的函數必須在 client 之后包含該參數.

For events, you can pass your variable in event.on(...), following client. Then in your actual events, your function must include that parameter after client.

對于命令,您可以在 message 事件中調用時將變量傳遞給 run 函數.同樣,在您的函數中,您需要包含正確放置的參數.

For commands, you can pass your variable into the run function when calling it in the message event. Again, in your function, you need to include the properly placed parameter.

如果我想在子文件夾中包含命令/事件怎么辦?

查看此答案以進行遞歸搜索.

Check out this answer to search recursively.

如何將這些處理程序用于重新加載命令?

如果您將它們的代碼放在函數中,您可以設置一個重新加載"命令來調用這些函數,再次加載事件和命令.

If you placed the code for them inside of functions, you can set up a "reload" command that calls those functions, loading the events and commands again.

  • Node.js 文檔
  • MDN 文檔
  • W3Schools 教程
  • require-all
  • Discord.js 文檔
  • client.removeAllListeners() 將刪除附加到客戶端的所有 監聽器,包括那些源自客戶端實例化的監聽器.這可能會導致與語音連接相關的錯誤,特別是 語音連接未在 15 秒內建立 被拋出.為防止出現此問題,請跟蹤每個偵聽器函數并使用 client.removeListener(listener) 單獨刪除每個函數.
  • client.removeAllListeners() will remove all listeners attached to the client, including those originating from client instantiation. This can cause voice connection related errors, specifically Voice connection not established within 15 seconds being thrown. To prevent this issue, keep track of every listener function and remove each individually using client.removeListener(listener).

這篇關于如何為我的 Discord.js 機器人編寫事件/命令處理程序?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Using discord.js to detect image and respond(使用 discord.js 檢測圖像并響應)
Check if user ID exists in Discord server(檢查 Discord 服務器中是否存在用戶 ID)
Guild Member Add does not work (discordjs)(公會成員添加不起作用(discordjs))
Creating my first bot using REPLIT but always error Discord.JS(使用 REPLIT 創建我的第一個機器人,但總是錯誤 Discord.JS)
How to find a User ID from a Username in Discord.js?(如何從 Discord.js 中的用戶名中查找用戶 ID?)
DeprecationWarning: Collection#find: pass a function instead(DeprecationWarning: Collection#find: 傳遞一個函數)
主站蜘蛛池模板: 优考试_免费在线考试系统_培训考试系统_题库系统_组卷答题系统_匡优考试 | 锻造液压机,粉末冶金,拉伸,坩埚成型液压机定制生产厂家-山东威力重工官方网站 | 彩超机-黑白B超机-便携兽用B超机-多普勒彩超机价格「大为彩超」厂家 | 上海盐水喷雾试验机_两厢式冷热冲击试验箱-巨怡环试 | 淘趣英语网 - 在线英语学习,零基础英语学习网站 | 波纹补偿器_不锈钢波纹补偿器_巩义市润达管道设备制造有限公司 | 工业PH计|工业ph酸度计|在线PH计价格-合肥卓尔仪器仪表有限公司 济南画室培训-美术高考培训-山东艺霖艺术培训画室 | 石家庄律师_石家庄刑事辩护律师_石家庄取保候审-河北万垚律师事务所 | 交通信号灯生产厂家_红绿灯厂家_电子警察监控杆_标志杆厂家-沃霖电子科技 | 台湾HIWIN上银直线模组|导轨滑块|TBI滚珠丝杆丝杠-深圳汉工 | 电力测功机,电涡流测功机,磁粉制动器,南通远辰曳引机测试台 | 【直乐】河北石家庄脊柱侧弯医院_治疗椎间盘突出哪家医院好_骨科脊柱外科专业医院_治疗抽动症/关节病骨伤权威医院|排行-直乐矫形中医医院 | 混合气体腐蚀试验箱_盐雾/硫化氢/气体腐蚀试验箱厂家-北京中科博达 | 电磁流量计厂家_涡街流量计厂家_热式气体流量计-青天伟业仪器仪表有限公司 | 伟秀电气有限公司-10kv高低压开关柜-高低压配电柜-中置柜-充气柜-欧式箱变-高压真空断路器厂家 | 北京自然绿环境科技发展有限公司专业生产【洗车机_加油站洗车机-全自动洗车机】 | 短信通106短信接口验证码接口群发平台_国际短信接口验证码接口群发平台-速度网络有限公司 | 纸张环压仪-纸张平滑度仪-杭州纸邦自动化技术有限公司 | 安徽免检低氮锅炉_合肥燃油锅炉_安徽蒸汽发生器_合肥燃气锅炉-合肥扬诺锅炉有限公司 | 玉米深加工设备-玉米深加工机械-新型玉米工机械生产厂家-河南粮院机械制造有限公司 | 量子管通环-自清洗过滤器-全自动反冲洗过滤器-北京罗伦过滤技术集团有限公司 | 济南侦探调查-济南调查取证-山东私家侦探-山东白豹调查咨询公司 密集架|电动密集架|移动密集架|黑龙江档案密集架-大量现货厂家销售 | Safety light curtain|Belt Sway Switches|Pull Rope Switch|ultrasonic flaw detector-Shandong Zhuoxin Machinery Co., Ltd | 座椅式升降机_无障碍升降平台_残疾人升降平台-南京明顺机械设备有限公司 | 澳洁干洗店加盟-洗衣店干洗连锁「澳洁干洗免费一对一贴心服务」 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 成都亚克力制品,PVC板,双色板雕刻加工,亚克力门牌,亚克力标牌,水晶字雕刻制作-零贰捌广告 | 彩超机-黑白B超机-便携兽用B超机-多普勒彩超机价格「大为彩超」厂家 | 上海办公室装修_上海店铺装修公司_厂房装潢设计_办公室装修 | LHH药品稳定性试验箱-BPS系列恒温恒湿箱-意大利超低温冰箱-上海一恒科学仪器有限公司 | 新疆系统集成_新疆系统集成公司_系统集成项目-新疆利成科技 | 探伤仪,漆膜厚度测试仪,轮胎花纹深度尺厂家-淄博创宇电子 | 热镀锌槽钢|角钢|工字钢|圆钢|H型钢|扁钢|花纹板-天津千百顺钢铁贸易有限公司 | ?水马注水围挡_塑料注水围挡_防撞桶-常州瑞轩水马注水围挡有限公司 | 舞台木地板厂家_体育运动木地板_室内篮球馆木地板_实木运动地板厂家_欧氏篮球地板推荐 | 天津中都白癜风医院_天津白癜风医院_天津治疗白癜风 | 桨叶搅拌机_螺旋挤压/方盒旋切造粒机厂家-无锡市鸿诚输送机械有限公司 | 搬运设备、起重设备、吊装设备—『龙海起重成套设备』 | 石牌坊价格石牌坊雕刻制作_石雕牌坊牌楼石栏杆厂家_山东嘉祥石雕有限公司 | 次氯酸钠厂家,涉水级次氯酸钠,三氯化铁生产厂家-淄博吉灿化工 | 铝合金脚手架厂家-专注高空作业平台-深圳腾达安全科技 | 万博士范文网-您身边的范文参考网站Vanbs.com |