Node.js下Discord机器人如何导入运行2ndQ.js实现!setup指令功能
实现方案
Node.js采用CommonJS模块规范,每个文件是独立的模块作用域,跨文件调用代码需要通过module.exports导出待使用的内容,再在调用方用require导入即可。
步骤1:修改2ndQ.js为可导入的模块
将原有代码封装为可接收依赖参数的模块,导出触发setup流程的方法:
// 2ndQ.js 完整修改后代码 module.exports = (Discord, client, ping_char = '!') => { // 菜单Embed定义 const menu = new Discord.MessageEmbed() .setTitle("Options: ") .setDescription("Here are the different options for this bot!") .addField("--> 1 = Casino", "Change prices of items and payout amount!") .addField("--> 2 = Quotes", "Change around the quotes eg add a quote to the quote list") .addField("--> 3 = Moderation", "Kick, Ban members and much more") .addField("--> More to come in the future", "Leave some suggestions!") .setImage("https://www.bolero.net/wp-content/uploads/2020/11/inr-banner-sercure.jpg") // Casino分类Embed(已删除重复定义的冗余代码) const casino = new Discord.MessageEmbed() .setTitle("--> Casino:") .setDescription("Here are the different options under the Casino section!") .addField("----> Change payout range on **slot machine**", "Type [SLR = Min(number), Max(number)]") .addField("----> Change payout range on **blackjack**", "Type [BJR = Min(number), Max(number)]") .addField("----> Change payout range on **cock fight**", "Type [CFR = Min(number), Max(number)]") .addField("----> Change price of **chicken** for **cock fight**", "Type [CFP = Price]") .addField("----> Change price of **lottery tickets**", "Type [LTP = x1(number), x5(number), x10(number), x100(number)]") .setImage("https://www.startus.cc/sites/default/files/styles/company_profile_cover_crop/public/img-casino.jpg?itok=E1XXjp5b&sc=775a4769d2582602853a3c7b3efeed6f") // Quotes分类Embed(已修正原代码标题错误问题) const quote = new Discord.MessageEmbed() .setTitle("--> Quotes:") .setDescription("Here are the different options under the Quotes section!") .addField("----> Clear **quotes**", "Type [Clear quotes]") .addField("----> Add to quotes", "Type [Add (quote....- first name, last name)]") .addField("----> Remove a quote", "Type [Remove (quote...)]") .setImage("https://www.startus.cc/sites/default/files/styles/company_profile_cover_crop/public/img-casino.jpg?itok=E1XXjp5b&sc=775a4769d2582602853a3c7b3efeed6f") // Moderation分类Embed(已修正原代码标题错误问题) const mod = new Discord.MessageEmbed() .setTitle("--> Moderation:") .setDescription("Here are the different options under the Moderation section!") .addField("----> **Ban** a member", "Type [Ban @member]") .addField("----> **Warn** a member", "Type [Warn @member]") .addField("----> Create a **self role** message", "Type [Create ]") .setImage("https://www.startus.cc/sites/default/files/styles/company_profile_cover_crop/public/img-casino.jpg?itok=E1XXjp5b&sc=775a4769d2582602853a3c7b3efeed6f") // 新成员入群欢迎事件,仅注册一次 client.on("guildMemberAdd", (member) => { const channel = member.guild.channels.cache.find( (ch) => ch.name === "general" ); if (!channel) return; channel.send("Welcome to the server, I'm the resident bot."); }); // 导出setup触发方法 const triggerSetup = async (msg) => { // 发送配置菜单 await msg.channel.send(menu); // 创建消息收集器:仅响应触发setup的用户的1/2/3输入,超时时间5分钟 const filter = m => m.author.id === msg.author.id && ['1','2','3'].includes(m.content); const collector = msg.channel.createMessageCollector(filter, { time: 300000 }); collector.on('collect', m => { switch(m.content) { case '1': m.channel.send(casino); break; case '2': m.channel.send(quote); break; case '3': m.channel.send(mod); break; } }); } return { triggerSetup }; }
步骤2:修改主文件的触发逻辑
在主文件中导入2ndQ模块,调用对应的setup触发方法:
- 首先在主文件头部导入模块,初始化获取触发方法:
// 路径根据2ndQ.js的实际存放位置调整,此处假设和主文件同目录 const setupModule = require('./2ndQ.js'); // 传入主文件中的Discord实例、client实例、自定义的命令前缀,获取setup触发方法 const { triggerSetup } = setupModule(Discord, client, ping_char);
- 替换原有!setup的触发逻辑:
client.on('message', msg => { if (msg.content === ping_char + 'setup') { triggerSetup(msg); } });
注意事项
- 原有2ndQ.js存在重复定义
casino变量、quote和mod的Embed标题错误、self role语法错误的问题,上述修改代码已经全部修复 - 使用Discord内置的消息收集器替代全局监听1/2/3命令,避免和其他全局命令冲突,仅触发了!setup的用户的对应输入才会响应
- 若2ndQ.js存放在子目录中,需要对应调整
require的导入路径
内容的提问来源于stack exchange,提问作者QUICKGAM3R
相关产品推荐
相关产品推荐

