I'm trying to implement a command to delete the bot's N last messages in a channel. The bot does not have MANAGE_MESSAGES permission. When N = 1, the bot deletes its latest message, as intended. However, when N > 1, the bot throw the error:
D:\Seneca\7th_Term\DPS909\EarTensifier\node_modules\discord.js\src\rest\RequestHandler.js:170
return reject(new DiscordAPIError(request.path, data, request.method, res.status));
^
DiscordAPIError: Missing Permissions
at RequestHandler.execute (D:\Seneca\7th_Term\DPS909\EarTensifier\node_modules\discord.js\src\rest\RequestHandler.js:170:25)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
method: 'post',
path: '/channels/757408200973877277/messages/bulk-delete',
code: 50013,
httpStatus: 403
}
What is weird to me is that how come the bot be able to delete 1 of its message, but not more? When I tested the same code with MANAGE_MESSAGES enabled, it works perfectly. Is it possible to implement this functionality without the MANAGE_MESSAGES permission, since I only want the bot to delete its own messages, not others'?
Here is my implementation:
const botID = process.env.DISCORD_ID;
if (message.channel.type == 'text') {
await message.channel.messages.fetch({limit: 100}).then(messages => {
let botMessages = messages.filter(msg => msg.author == botID).array();
if (no_of_messages > 0){
botMessages.splice(no_of_messages);
}
message.channel.bulkDelete(botMessages);
client.log(`Deleted ${botMessages.length} messages from ${botID}`)
}).catch(err => {
client.log('Error while doing Bulk Delete');
client.log(err);
});
}