A few months ago, I had a problem with this code too, but I fixed the problem after some solutions found here. But, since yesterday, the bot is having the same issue as before, but I don't understand why, I don't even have changed something in the code, but it keeps having the same error.
It's a bot showing the weather for a specified location when you ask. It works for some locations, but otherwise, it has this error:
/home/container/node_modules/discord.js/src/rest/RequestHandler.js:350
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) {
method: 'post',
path: '/channels/959726696393748519/messages',
code: 50006,
httpStatus: 400,
requestData: {
json: {
content: undefined,
tts: false,
nonce: undefined,
embeds: undefined,
components: undefined,
username: undefined,
avatar_url: undefined,
allowed_mentions: undefined,
flags: undefined,
message_reference: undefined,
attachments: undefined,
sticker_ids: undefined
},
files: []
}
}
I don't know if it's useful to say that, but there is some time between when I run the command, and when the error message is displayed.
Here is the full code:
index.js
:
const Discord = require("discord.js");
var weather = require('weather-js');
const { Client, GuildMember, Message } = require("discord.js");
const config = require("./config.json")
const client = new Client({
intents: ["GUILD_MESSAGES", "GUILD_MEMBERS", "GUILDS"]
});
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);
}
const prefix = "!";
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)
}
});
client.login(config.token);
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 || "NO DATA"}`)
.setDescription(`${current.skytext || "NO DATA"}`) //Using a direct call can cause an error.
.setThumbnail(current.imageUrl) //If this part is the culprit let me know
.setColor("RANDOM")
.setTimestamp()
.addField("Temperature: ", `${current.temperature || "NO DATA"} °C`, true)
.addField("Wind Speed: ", `${current.winddisplay || "NO DATA"}`, true)
.addField("Humidity: ", `${current.humidity || "NO DATA"}%`, true)
.addField("Timezone: ", `UTC${location.timezone || "NO DATA"}`, true)
message.channel.send({ embeds: [embed]});
})
}
}
I was searching on some websites about how I could solve the "Cannot send an empty message" error, but nothing worked for me. I also tried to type another code using weather-js
, but it didn't worked either.
CodePudding user response:
It's because the error
in if (error) return message.channel.send(error)
is an object and you can only send a string as the message content.
Change it to error.message
(that's a string) and it should work as expected:
if (error) return message.channel.send(error.message)