discord.js 机器人在尝试在无法发送消息的频道中发送消息后崩溃

discord.js bot crashes after trying to send a message in a channel it can't send messages

好吧 discord.js 机器人在尝试在它看到但无法发送消息的频道中发送消息后崩溃

好吧,在那之后我遇到了崩溃,我该如何解决这个问题?这是它显示的其他命令也显示的错误

 /home/pi/Desktop/floppa_gaming/node_modules/discord.js/src/rest/RequestHandler.js:298
      throw new DiscordAPIError(data, res.status, request);
            ^

DiscordAPIError: Missing Permissions
    at RequestHandler.execute (/home/pi/Desktop/floppa_gaming/node_modules/discord.js/src/rest/RequestHandler.js:298:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (/home/pi/Desktop/floppa_gaming/node_modules/discord.js/src/rest/RequestHandler.js:50:14)
    at async TextChannel.send (/home/pi/Desktop/floppa_gaming/node_modules/discord.js/src/structures/interfaces/TextBasedChannel.js:171:15) {
  method: 'post',
  path: '/channels/785243249072930869/messages',
  code: 50013,
  httpStatus: 403,
  requestData: {
    json: {
      content: 'You can use get help by saying "floppa help" in the chat :wink:',
      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: []
  }
}

如果有人想在这里看到我糟糕的代码,那就是

 const { Client, Intents, MessageEmbed, Permissions } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES,] });


var fs = require('fs');
var files = fs.readdirSync('./floppa/');



client.on('ready', () => {
  client.user.setStatus('invisible') //You can set idle, dnd or invisible
  client.user.setActivity("use floppa to get a image!",  { type: 'PLAYING' }) // PLAYING , LISTENING , WATCHING , STREAMING
  console.log("on discord lol");
});



 const { AutoPoster } = require('topgg-autoposter')

 const poster = AutoPoster('token', client) // tells topgg amount of servers ur bot is in 

 // optional
 poster.on('posted', (stats) => { // ran when succesfully posted
  console.log(`Posted stats to Top.gg | ${stats.serverCount} servers`)
})


 client.on('message', msg => {
    if (msg.content === 'floppa') 
    {
        imageNumber = files[Math.floor(Math.random()*files.length)]
        msg.channel.send ( {files: ["./floppa/" + imageNumber]} )
    }
        
// finds a random image in a folder
 

显示的错误是:DiscordAPIError: Missing Permissions

这意味着您的机器人没有在该频道中发送消息的权限。为避免您的机器人在发生这种情况时崩溃,您需要执行以下操作之一:

  • 捕获错误:
    msg.channel.send("stuff").catch((err) => {/* handle errors */});
    
  • 在 posting 之前验证您的机器人是否具有 post 的权限:
    if (msg.guild && msg.channel.permissionsFor(msg.guild.me).has("SEND_MESSAGES"))) {
      msg.channel.send("stuff");
    }