Home > other >  DiscordAPIError: Cannot send an empty message at RequestHandler.execute
DiscordAPIError: Cannot send an empty message at RequestHandler.execute

Time:05-03

As one of the first bigger js/node projects I decided to make a discord bot using discord.js. Every user is able to add new messages to the repl.it (the website I host the bot on) database and read random ones. The bot is working fine so I wanted to make it reply with embeds because it looks better and that is where the problem is.

I don't get any errors when making the embed but when I try to send it to the chat it says that it can't send an empty message at the RequestHandler.execute. Here is the full error message:

/home/runner/anonymous-message/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/runner/anonymous-message/node_modules/discord.js/src/rest/RequestHandler.js:350:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (/home/runner/anonymous-message/node_modules/discord.js/src/rest/RequestHandler.js:51:14) {
  method: 'post',
  path: '/channels/880447741283692609/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: 0,
      message_reference: { message_id: '970404904537567322', fail_if_not_exists: true },
      attachments: undefined,
      sticker_ids: undefined
    },
    files: []
  }
}

And here is the full code:

const Discord = require("discord.js")
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })
const keepAlive = require("./server.js") //just a webserver that I combine with uptimerobot.com so the bot doesn't go offline

const Database = require("@replit/database")
const db = new Database()
client.login(process.env.TOKEN) //since all replits are public (unless you buy the premium) you can use the environment variables to keep somestuff private

let prefix = "msg "
let channel

let starterMessages = ["a random test message!", "Hello World!", "why is this not working"]

db.get("msgs", messages => {
  if (!messages || messages.length < 1) {
    db.set("msgs", starterMessages)
  }
})

client.on("ready", () => {
  console.log("bot is working")
})

client.on("messageCreate", (msg) => {
  channel = msg.channel.guild.channels.cache.get(msg.channel.id)

  switch (true) {
    case msg.content.toLowerCase().startsWith(prefix   "ping"):
      channel.send("the bot is working")
      break

    case msg.content.toLowerCase().startsWith(prefix   "send"):
      let exists = false
      let tempMsg = msg.content.slice(prefix.length   5, msg.length)
      if (tempMsg.length > 0) {
        db.get("msgs").then(messages => {
          for (i = 0; i < messages.length; i  ) {
            if (tempMsg.toLowerCase() === messages[i].toLowerCase()) { exists = true }
          }
          if (exists === false) {
            console.log(tempMsg)
            messages.push(tempMsg)
            db.set("msgs", messages)
            msg.delete() //deletes the message so no one knows who added it
          } else {
            console.log("message already exists") //all console logs are for debugging purposes
          }
        });
      }
      break

    case msg.content.toLowerCase().startsWith(prefix   "read"):
      db.get("msgs").then(messages => {
        let rndMsg = messages[Math.floor(Math.random() * messages.length)] //gets the random message from the database

        //Here I make the embed, it doesn't give any errors
        let newEmbed = new Discord.MessageEmbed()
          .setColor("#"   Math.floor(Math.random() * 16777215).toString(16))
          .setTitle("a message from a random stranger:")
          .setURL("https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstley")
          .addFields({ name: rndMsg, value: `if you want to send one of these for other people to read? Type "${prefix}send [MESSAGE]"` })
          .setFooter({ text: `type "${prefix}help" in the chat for more info!` })

        msg.channel.send(newEmbed) //and here is the problem. Without this line it runs with no errors
      });                          
      break
  }

})

keepAlive() //function from the webserver

I couldn't find any answers to this problem on the internet so I'm hoping you can answer it. Thanks in advance!

CodePudding user response:

Since you're using (I assume) discord.js v13, the way to send embeds is this:

msg.channel.send({embeds: [newEmbed]});

That should solve your problem.

  • Related