I wanna use an bot to react to every single message in an channel using discord.js f.e. i got an emoji contest channel and i wanna ad an ✅ and an ✖ reaction on every post in there ofc, all the unnecesary messages are cleaned up so that there are like 50 messages
Asked
Active
Viewed 5,225 times
2 Answers
3
- Fetch the messages already sent in a channel with
TextChannel.fetchMessages()
. - Iterate through the Collection.
- Add reactions with
Message.react()
. - When a new message is sent in the channel, you should also add the reactions.
const emojiChannelID = 'ChannelIDHere';
client.on('ready', async () => {
try {
const channel = client.channels.get(emojiChannelID);
if (!channel) return console.error('Invalid ID or missing channel.');
const messages = await channel.fetchMessages({ limit: 100 });
for (const [id, message] of messages) {
await message.react('✅');
await message.react('✖');
}
} catch(err) {
console.error(err);
}
});
client.on('message', async message => {
if (message.channel.id === emojiChannelID) {
try {
await message.react('✅');
await message.react('✖');
} catch(err) {
console.error(err);
}
}
});
In this code, you'll notice I'm using a for...of
loop rather than Map.forEach()
. The reasoning behind this is that the latter will simply call the methods and move on. This would cause any rejected promises not to be caught. I've also used async/await
style rather than then()
chains which could easily get messy.

slothiful
- 5,548
- 3
- 12
- 36
-
got it working like that, thx, but im pretty new to js coding, what does this => i figure it doesnt mean sammerl or equal than – ScarVite Jul 08 '19 at 16:09
-
`=>` is actually part of the [arrow function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) syntax. `<=` and `>=` are [relational operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Relational_operators)s. `=>` and `>=` can be easily confused, but they're very different. – slothiful Jul 08 '19 at 16:12
-
can you develop on the rejected promises part? I used the `Map.forEach()` in one of my previous answer and didn't think/know about it. You're talking about the `message.react` promise, aren't you? Because if a promise if rejected, the `forEach` will stop so it will be stopped and go in the catch as you for, no? – JackRed Jul 09 '19 at 05:42
-
1The way `forEach()` works, no. It simply invokes the Promise and continues to iterate. By the time it's done, Promises will be still be in their pending state. Even if you use the `await` keyword, it's not going to be effective towards this issue. See [here](https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop/45006966) for a much more thorough explanation of the workings and the problem. – slothiful Jul 09 '19 at 11:37
1
According to https://discord.js.org/#/docs/main/stable/class/TextChannel
you can use fetchMessages
to get all messages from a specific channel, which then returns a collection of Message
Then you can use .react
function to apply your reactions to this collection of message by iterating over it and calling .react
on each.
Edit:
channelToFetch.fetchMessages()
.then(messages => {
messages.tap(message => {
message.react(`CHARACTER CODE OR EMOJI CODE`).then(() => {
// Do what ever or use async/await syntax if you don't care
about Promise handling
})
})
})

Thomas Billot
- 163
- 1
- 10