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



嗯,discord.js bot试图在它看到但无法发送消息的通道中发送消息后崩溃

好吧,我得到一个崩溃之后,我怎么能解决这个问题?下面是它显示的错误它也显示了其他命令

/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

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

  • 捕获错误:
    msg.channel.send("stuff").catch((err) => {/* handle errors */});
    
  • 在发帖前验证你的bot是否有发帖的权限:
    if (msg.guild && msg.channel.permissionsFor(msg.guild.me).has("SEND_MESSAGES"))) {
    msg.channel.send("stuff");
    }
    

最新更新