discord.jsfivem

DiscordAPIError: Cannot send empty message


Im trying to make a discord bot for my fivem server. but when i try to send a message embed i get an error. Full error: DiscordAPIError: Cannot send an empty message at RequestHandler.execute (D:\DiscordBots\MoonRP\node_modules\discord.js\src\rest\RequestHandler.js:154:13) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async RequestHandler.push (D:\DiscordBots\MoonRP\node_modules\discord.js\src\rest\RequestHandler.js:39:14) { method: 'post', path: '/channels/941736918104821820/messages', code: 50006, httpStatus: 400 }

The new full error: DiscordAPIError: Invalid Form Body embed.description: This field is required embeds[0].description: This field is required at RequestHandler.execute (D:\DiscordBots\MoonRP\node_modules\discord.js\src\rest\RequestHandler.js:154:13) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async RequestHandler.push (D:\DiscordBots\MoonRP\node_modules\discord.js\src\rest\RequestHandler.js:39:14) { method: 'post', path: '/channels/941736918104821820/messages', code: 50035, httpStatus: 400 }

The index.js code is here:

const {MessageEmbed} = require('discord.js')
const fs = require('fs')
const fivereborn = require('fivereborn-query');
const bot = new Client({
    disableEveryone: true
})
const config = require('./config.json')
const prefix = config.prefix
const token = config.token
bot.commands = new Collection();
bot.aliases = new Collection();
bot.categories = fs.readdirSync("./commands/");
["command"].forEach(handler => {
    require(`./handlers/${handler}`)(bot);
});
bot.on('ready', () => {
    console.log(`${bot.user.username} ✅`)
})
bot.on('message', async message =>{
    if(message.author.bot) return;
    if(!message.content.startsWith(prefix)) return;
    if(!message.guild) return;
    if(!message.member) message.member = await message.guild.fetchMember(message);
    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const cmd = args.shift().toLowerCase();
    if(cmd.length == 0 ) return;
    let command = bot.commands.get(cmd)
    if(!command) command = bot.commands.get(bot.aliases.get(cmd));
    if(command) command.run(bot, message, args)
})

const ip = config.serverIp
const port = config.serverPort
const sName = config.serverName
var MoonRPEmbed = new MessageEmbed()


function activity(){ // Defines the function
    setTimeout(() => { // Starts a loop
        fivereborn.query(ip, port, (err, data) => { // Starts a function that allowes us to retrive data from our fivem server
            if (err) { // Checks for errors
                return console.log(err); // If a error is true then this will log that error and then stop it from going by
            } else { // If a error is not true then
              if(bot.user!= null)
                bot.user.setActivity(`${data.clients} / ${data.maxclients} i byen ${sName}`, { type: "WATCHING" }); // Serts the Status
            }
        });
        activity(); // Runs the function we defined at line 45
    }, 1000); // Waits 1 second
}
activity(); // Runs the function again


function status(){
    setTimeout(() => {
        fivereborn.query(ip, port, (err, data) => {
            if (!err) {
                console.log(data)
              bot.channels.fetch('941736918104821820').then(ch => {
                if (ch.isText())
                {
                  ch.bulkDelete(10);
                  }
              })
                .catch(console.error);

                 if (err) { // Checks for errors
                return console.log(err); // If a error is true then this will log that error and then stop it from going by
            } else { // If a error is not true then
              if(bot.user!= null)
                bot.user.setActivity(`${data.clients} / ${data.maxclients} i byen ${sName}`, { type: "WATCHING" }); // Serts the Status

              MoonRPEmbed = {
                color: 'ff0000',
                description: '📊MoonRP Status📊',
                author: {
                name: '📊MoonRP Status📊',
                },
                 fields: [
                {
                  name: 'Server Åben',
                  value: "```   ✅ ```",
                  inline: true,
                },
                {
                  name: 'Spillere inde',
                  value: "```"+data.clients+ " / "+ data.maxclients+"```",
                  inline: true,
                },
                {
                  name: 'Server IP',
                  value: "```cfx.re/join/ybrgv5```",
                  inline: false,
                }
              ],
            timestamp: Math.floor(new Date().getTime() / 1000),//new Date(),
              footer: {
                text: 'MoonRP Bot',
                icon_url: 'https://cdn.discordapp.com/attachments/703252325686575124/934779887112302652/Moonrp_logo-min.png'
              },
            };
            }

              bot.channels.fetch('941736918104821820')
                .then(channel => {
                  console.log(MoonRPEmbed);
                  channel.send({embed:[MoonRPEmbed]})
                    .then(console.log)
                    .catch(console.error);
                })
                .catch(console.error)
                //channel.send({ embeds: [MoonRPEmbed] });
            } else {
                return console.log(err)
            }
        });
        status();
    }, 60000); // Waits 10 mins
}
status()

bot.login(token)

The discord.js version is: 12.5.3


Solution

  • I Fixed it. The error came because the code where the embed is had to be in a async function. i don`t know why but it worked.