DiscordAPIError: Cannot send an empty message at RequestHandler.execute

DiscordAPIError: Cannot send an empty message at RequestHandler.execute

作为第一个更大的 js/node 项目之一,我决定使用 discord.js 制作一个 discord 机器人。每个用户都能够将新消息添加到 repl.it(我托管机器人的网站)数据库并随机读取消息。该机器人工作正常,所以我想让它用嵌入回复,因为它看起来更好,这就是问题所在。

我在进行嵌入时没有收到任何错误,但是当我尝试将其发送到聊天时,它说它无法在 RequestHandler.execute 发送空消息。这是完整的错误消息:

/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: []
  }
}

这里是完整代码:

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

我在网上找不到这个问题的任何答案,所以我希望你能回答。提前致谢!

由于您正在使用(我假设)discord.js v13,发送嵌入的方式是这样的:

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

这应该可以解决您的问题。