Hey everyone! I’m working on a Discord bot and I’m stuck on something. I want my bot to remove exactly two messages from a channel. But I can’t figure out the right command to make this happen.
Here’s what I’ve got so far:
const botHelper = require('botHelper')({secretKey: process.env.BOT_SECRET_KEY});
async function removeMessage(messageInfo) {
await botHelper.discord.channel.message.erase({
messageId: messageInfo.id,
channelId: messageInfo.channelId
});
}
This code only deletes one message though. How can I modify it to remove two messages instead? Any help would be awesome! Thanks in advance!
Hey Dave, I’ve been working with Discord bots for a while, and I think I can help you out. Instead of deleting messages one by one, you can use the bulkDelete method. It’s much more efficient. Here’s how you can modify your code:
async function removeMessages(channelId, count) {
const channel = await botHelper.discord.channel.get(channelId);
await channel.bulkDelete(count);
}
Then you can call it like this:
removeMessages(channelId, 2);
This will delete the last two messages in the channel. Just remember, you can’t delete messages older than 14 days with this method. Also, make sure your bot has the ‘Manage Messages’ permission. Hope this helps!
yo dave, i think i got a solution for ya. try using a loop to delete multiple messages. something like:
for (let i = 0; i < 2; i++) {
await removeMessage(messageInfo);
}
this should delete 2 messages. hope it helps bro!
Hey Dave, I’ve actually run into this issue before when building my own Discord bot. Here’s a solution that worked for me:
async function removeMessages(channelId, count) {
const channel = await botHelper.discord.channel.get(channelId);
const messages = await channel.messages.fetch({ limit: count });
await channel.bulkDelete(messages);
}
This function fetches the specified number of messages and then bulk deletes them. It’s way more efficient than deleting one by one, especially if you need to remove more messages in the future.
Just call it like removeMessages(channelId, 2)
to delete two messages. Remember though, Discord has a 14-day limit on bulk deletions. Also, make sure your bot has ‘Manage Messages’ permission.
Let me know if you run into any issues implementing this. I’d be happy to help troubleshoot!