使用weather-js的Discord天气机器人出现不稳定及空消息报错问题
Discord天气机器人间歇性失效问题排查
几个月前我跟随教程开发了一款可根据指定地点显示天气预报的Discord机器人,该机器人基于weather-js包开发。但近期无论托管运行还是本地控制台启动,机器人均出现间歇性失效问题——部分地点无法查询天气,甚至此前可用的地点也突然无法正常使用。
失效时总会触发以下报错:
throw new DiscordAPIError(data, res.status, request); ^ DiscordAPIError: Cannot send an empty message at RequestHandler.execute (/home/container/node_modules/discord.js/src/rest/RequestHandler.js:350:13) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async RequestHandler.push (/home/container/node_modules/discord.js/src/rest/RequestHandler.js:51:14) at async TextChannel.send (/home/container/node_modules/discord.js/src/structures/interfaces/TextBasedChannel.js:175:15) {
核心代码展示
index.js
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").... ..... client.on('messageCreate', async message => { if (!message.content.startsWith(prefix)) return; let args = message.content.slice(prefix.length).split(" "); let command = args.shift().toLowerCase() if (command === "weather") { client.commands.get("weather").execute(message, args, Discord, weather) } });
weather.js
var weather = require('weather-js'); const Discord = require('discord.js'); module.exports = { name: 'weather', description: "Used to get the weather of a place", execute(message, args, Discord, weather) { const city = args[0] weather.find({search: args.join(" "), degreeType: "C"}, function(error, result){ if (error) return message.channel.send(error) if (!city) return message.channel.send("You must specify a location please!") if (result === undefined || result.length === 0) return message.channel.send("**Invalid location**") let current = result[0].current let location = result[0].location const embed = new Discord.MessageEmbed() .setTitle(`Weather forecast for ${current.observationpoint}`) .setDescription(current.skytext) .setThumbnail(current.imageUrl) .setColor("RANDOM") .setTimestamp() .addField("Temperature: ", current.temperature + "°C", true) .addField("Wind Speed: ", current.winddisplay, true) .addField("Humidity: ", `${current.humidity}%`, true) .addField("Timezone: ", `UTC${location.timezone}`, true) message.channel.send({ embeds: [embed]}); }) } }
想请教:是否有用户遇到过weather-js的同类问题?还是我的代码本身存在逻辑漏洞?
内容的提问来源于stack exchange,提问作者FireFenrir
相关产品推荐
相关产品推荐

