Discord Bot在线但无响应:附ping/fee命令代码求助
Discord Bot无响应问题排查与修复
我用自定义语法开发了一个Discord Bot,集成了fee等命令。Bot已经在线,但对Discord里的输入完全没反应,控制台也没报错。测试了简单的ping命令(返回嵌入消息“Pong!”),同样没有响应。以下是相关代码:
原代码
index.js
const Discord = require("discord.js"); const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_MESSAGE_REACTIONS"] }, { partials: ["MESSAGE", "CHANNEL", "REACTION", "MANAGE_ROLES"] }) require("dotenv").config(); const prefix = "-"; const fs = require("fs"); client.commands = new Discord.Collection(); const commandFiles = fs.readdirSync("./commands/").filter(file => file.endsWith('.js')); for (const file of commandFiles) { const command = require(`./commands/${file}`); client.commands.set(command.name, command); } client.on("ready", () => { console.log(`Logged in as ${client.user.tag}!`); }); client.on("messageCreate", message => { if (message.author.bot) return; if (message.content === prefix + 'reactionrole') { if (!message.member.roles.cache.has('885603457938108476')) { return message.channel.send('You dont have access to this!'); } if (!message.content.startsWith(prefix) || message.author.bot) return; const args = message.content.slice(prefix.length).split(/ +/); const command = args.shift().toLowerCase(); if (command === "ping") { client.commands.get("ping").execute(message, args, Discord, client); } } }); client.login('Token');
fees.js
module.exports = { name: 'fee', description: 'This command will calculate the payouts for every major platform\n`!fee <amount>`\nexample: `!fee 100`', async execute(message) { const fees = { 'StockX Level 1 (9.5%)': n => .095 * n, 'StockX Level 2 (9%)': n => .09 * n, 'StockX Level 3 (8.5%)': n => .085 * n, 'StockX Level 4 (8%)': n => .08 * n, 'Goat 90+ (9.5% + $5.00 + 2.9%)': n => 0.095 * n + 5 + (0.905 * n * 0.029), 'Goat 70-89 (15% + $5.00 + 2.9%)': n => 0.15 * n + 5 + (0.85 * n * 0.029), 'Goat 50-69 (20% + $5.00 + 2.9%)': n => 0.20 * n + 5 + (0.80 * n * 0.029), 'Ebay (12.9% + $0.30': n => 0.129 * n + 0.3, 'Paypal (2.9% + $0.30)': n => (0.029 * n) + 0.3, 'Grailed (9% + 2.9%)': n => 0.089 * n + 0.911 * n * 0.029, } const embed = new Discord.MessageEmbed(); embed.setTitle("Fee Calculator") if (msg.content.split(" ").length == 2) { if (isNaN(message.content.split(" ")[1])) { embed.setDescription('Please input a number'); } else { const [, number] = message.content.split(' '); Object.keys(fees).forEach(fee => { embed.addField(`${fee} Payout`, `$${Number(number - fees[fee](number)).toFixed(2)}`); }); } } else if (message.content.split(" ").length > 2) { embed.setDescription("This command takes only 1 argument"); } else if (message.content.split(" ").length == 1) { embed.setDescription("Please put a price to calculate fees"); } else { embed.setDescription("Please input a price") } message.channel.send(embed); } }
ping.js
module.exports = { name: 'ping', description: 'This command will display the latency between Discord and our servers\n`!ping`\nexample: `!ping`', permission: 'USE_APPLICATION_COMMANDS', async execute(message, args, Discord, client) { const embed = new Discord.MessageEmbed() .setTitle("Ping") .setColor('#5104DB') .setDescription(`Pong!`) message.channel.send({ embeds: [embed] }); } }
问题排查
- index.js事件逻辑错误:
messageCreate事件中,ping命令的处理逻辑被包裹在message.content === prefix + 'reactionrole'的判断分支里,只有当消息完全等于-reactionrole时才会检查ping命令,这导致其他命令根本不会被触发。同时存在重复的message.author.bot判断,冗余且无意义。 - fees.js变量错误:代码中使用了未定义的
msg变量(应为message);Ebay的键名存在语法错误,括号未闭合;命令描述中使用的前缀是!,但实际Bot前缀是-,会误导用户。 - ping.js描述错误:命令描述中的前缀是
!,与实际前缀-不符;permission字段是针对Slash命令的,前缀命令不需要这个字段。
修复后的代码
index.js
const Discord = require("discord.js"); // 修正Client初始化参数格式,将intents和partials放在同一个配置对象中 const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_MESSAGE_REACTIONS"], partials: ["MESSAGE", "CHANNEL", "REACTION"] // MANAGE_ROLES不属于partial类型,移除 }) require("dotenv").config(); const prefix = "-"; const fs = require("fs"); client.commands = new Discord.Collection(); const commandFiles = fs.readdirSync("./commands/").filter(file => file.endsWith('.js')); for (const file of commandFiles) { const command = require(`./commands/${file}`); client.commands.set(command.name, command); } client.on("ready", () => { console.log(`Logged in as ${client.user.tag}!`); }); client.on("messageCreate", message => { if (message.author.bot) return; // 先判断消息是否以指定前缀开头,否则直接返回 if (!message.content.startsWith(prefix)) return; const args = message.content.slice(prefix.length).split(/ +/); const commandName = args.shift().toLowerCase(); // 处理reactionrole命令 if (commandName === 'reactionrole') { if (!message.member.roles.cache.has('885603457938108476')) { return message.channel.send('You dont have access to this!'); } // 可在此添加reactionrole的具体逻辑 } // 处理其他已注册命令 else if (client.commands.has(commandName)) { try { client.commands.get(commandName).execute(message, args, Discord, client); } catch (error) { console.error(error); message.reply('执行命令时出错!'); } } }); client.login(process.env.TOKEN); // 建议用环境变量存储Token,避免硬编码泄露
fees.js
module.exports = { name: 'fee', description: '计算各平台的到手金额\n`-fee <amount>`\n示例: `-fee 100`', async execute(message, args, Discord) { const fees = { 'StockX Level 1 (9.5%)': n => .095 * n, 'StockX Level 2 (9%)': n => .09 * n, 'StockX Level 3 (8.5%)': n => .085 * n, 'StockX Level 4 (8%)': n => .08 * n, 'Goat 90+ (9.5% + $5.00 + 2.9%)': n => 0.095 * n + 5 + (0.905 * n * 0.029), 'Goat 70-89 (15% + $5.00 + 2.9%)': n => 0.15 * n + 5 + (0.85 * n * 0.029), 'Goat 50-69 (20% + $5.00 + 2.9%)': n => 0.20 * n + 5 + (0.80 * n * 0.029), 'Ebay (12.9% + $0.30)': n => 0.129 * n + 0.3, // 修复括号闭合问题 'Paypal (2.9% + $0.30)': n => (0.029 * n) + 0.3, 'Grailed (9% + 2.9%)': n => 0.089 * n + 0.911 * n * 0.029, } const embed = new Discord.MessageEmbed(); embed.setTitle("手续费计算器") .setColor('#5104DB'); // 用args获取参数更简洁,避免重复split操作 if (args.length === 1) { const amount = Number(args[0]); if (isNaN(amount)) { embed.setDescription('请输入有效的数字!'); } else { Object.keys(fees).forEach(feeType => { const payout = amount - fees[feeType](amount); embed.addField(`${feeType} 到手金额`, `$${payout.toFixed(2)}`); }); } } else if (args.length > 1) { embed.setDescription("该命令仅接受1个参数!"); } else { embed.setDescription("请输入要计算的金额!"); } message.channel.send({ embeds: [embed] }); } }
ping.js
module.exports = { name: 'ping', description: '显示Discord与服务器的延迟\n`-ping`\n示例: `-ping`', async execute(message, args, Discord, client) { const embed = new Discord.MessageEmbed() .setTitle("Ping") .setColor('#5104DB') .setDescription(`Pong! 延迟: ${client.ws.ping}ms`); // 添加实际延迟数据 message.channel.send({ embeds: [embed] }); } }
内容的提问来源于stack exchange,提问作者senpai
相关产品推荐
相关产品推荐

