Discord.js物品搜索功能无响应问题求助
Discord机器人!search命令无响应排查问题
我正在开发一款Discord机器人,期望用户输入!search 物品名称时,机器人能在聊天中发送对应物品的属性信息。但执行!search Animal Skull时无任何反应,以下是相关的Discord.js代码及JSON数据,恳请协助排查问题。
原Discord.js代码
const { EmbedBuilder } = require("discord.js"); const fs = require('fs') const path = require('path') datapath = path.resolve('data.json') module.exports = { config: { name: "search", description: "Search for items!", }, permissions: ['SendMessages'], owner: false, run: async (client, message, args, prefix, config, db) => { fs.readFile(datapath, 'utf-8', (err, data) => { if (err) { throw err; } const jsondata = JSON.parse(data.toString()); var needle = args; for (var i = 0; i < jsondata.length; i++){ if (jsondata[i].names == needle){ message.reply({ embeds: [ new EmbedBuilder() .setTitle(jsondata[i].names) .setThumbnail(jsondata[i].image) .setDescription(`Item type: **${jsondata[i].itemtype}** Item ID: **${jsondata[i].itemid}** Rarity: **${jsondata[i].itemrarity}** Farmable: **${jsondata[i].itemfarmable}** Droppable: **${jsondata[i].itemdroppable}**`) .setColor("Green") ] }) }} }) }, };
原JSON数据
[ { "names": "Animal Skull", "itemtype": "Block", "itemid": "32", "itemrarity": "4", "itemfarmable": "No", "itemdroppable": "Yes", "image": "https://static.wikia.nocookie.net/cubixworlds/images/4/4c/Animal_Skull.png/revision/latest?cb=20220720212850" }, { "names": "Asteroid Block", "itemtype": "Block", "itemid": "1", "itemrarity": "1", "itemfarmable": "No", "itemdroppable": "Yes", "image": "https://static.wikia.nocookie.net/cubixworlds/images/f/fe/Asteroid_Block.png/revision/latest?cb=20220725063800" }, { "names": "Bathtub", "itemtype": "Block", "itemid": "129", "itemrarity": "8", "itemfarmable": "No", "itemdroppable": "Yes", "image": "https://static.wikia.nocookie.net/cubixworlds/images/5/5a/Bathtub.png/revision/latest?cb=20220722112339" } ]
问题根源
args是数组而非字符串:用户输入!search Animal Skull时,args的值是["Animal", "Skull"],直接用它和JSON里的字符串"Animal Skull"做全等比较,永远不会匹配成功。- 无匹配反馈缺失:没找到对应物品时,机器人没有任何输出,无法判断是匹配失败还是代码其他问题。
- 异常处理不严谨:
throw err会直接导致进程崩溃,没有给用户错误提示,也不利于排查问题。
修复后的代码
const { EmbedBuilder } = require("discord.js"); const fs = require('fs').promises; // 用Promise版本适配async函数 const path = require('path') const datapath = path.resolve('data.json') // 补全变量声明 module.exports = { config: { name: "search", description: "搜索物品信息!", }, permissions: ['SendMessages'], owner: false, run: async (client, message, args, prefix, config, db) => { try { // 把args数组拼接成完整物品名 const needle = args.join(' '); if (!needle) { return message.reply('请输入要搜索的物品名称!'); } const data = await fs.readFile(datapath, 'utf-8'); const jsondata = JSON.parse(data); // 查找匹配的物品 const item = jsondata.find(item => item.names === needle); if (item) { // 生成并发送物品信息Embed const embed = new EmbedBuilder() .setTitle(item.names) .setThumbnail(item.image) .setDescription(`物品类型: **${item.itemtype}** 物品ID: **${item.itemid}** 稀有度: **${item.itemrarity}** 可种植: **${item.itemfarmable}** 可掉落: **${item.itemdroppable}**`) .setColor("Green"); await message.reply({ embeds: [embed] }); } else { // 未找到物品时提示用户 await message.reply(`未找到名为"${needle}"的物品!`); } } catch (err) { console.error(err); await message.reply('读取物品数据时发生错误,请联系管理员!'); } }, };
修复说明
- 用
args.join(' ')将数组转为完整字符串,解决带空格物品名的匹配问题。 - 改用
fs.promises结合async/await处理文件读取,代码更简洁且符合异步逻辑。 - 增加输入校验:用户未输入物品名时直接提示。
- 用
Array.find()替代for循环,代码更易读。 - 补充未找到物品的反馈,以及异常捕获后的错误提示,避免进程崩溃同时明确告知用户问题。
- 补全变量声明,符合JS规范。
内容的提问来源于stack exchange,提问作者alimertiskender
相关产品推荐
相关产品推荐

