PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 11, 2022

[FIXED] How to make a bot send a message to a specific channel after receiving a reaction from a certain message

 July 11, 2022     async-await, discord.js, message     No comments   

Issue

So I'm trying to develop a bot for a very small project (I'm not a programmer or anything, just need to do this one thing). All I need it to do is to collect reactions from a specific message I sent and send another message to a channel as soon as it detects a reaction. The message would contain the reactor's tag and some text. I would need it to be activelly collecting reactions all the time, without any limit. I tried looking through the documentation but I don't really know how to implement the .awaitmessageReactions or whatever it's called. Can you help me out?


Solution

You can use method createReactionCollector for this. But when bot go down, this collector will stop.

const Discord = require('discord.js');
const bot = new Discord.Client();
let targetChannelId = '1232132132131231';
bot.on('ready', () => {
    console.log(`${bot.user.tag} is ready on ${bot.guilds.cache.size} servers!`);
});

bot.on('message', (message) => {
    if (message.content === 'test') {
        message.channel.send(`i\`m await of reactions on this message`).then((msg) => {
            const filter = (reaction, user) => !user.bot;
            const collector = msg.createReactionCollector(filter);
            collector.on('collect', (reaction, user) => {
                let channel = message.guild.channels.cache.get(targetChannelId);
                if (channel) {
                    let embed = new Discord.MessageEmbed();
                    embed.setAuthor(
                        user.tag,
                        user.displayAvatarURL({
                            dynamic: true,
                            format: 'png',
                        }),
                    );
                }
                embed.setDescription(`${user} (${user.tag}) has react a: ${reaction.emoji}`);
                channel.send(embed);
            });
            collector.on('end', (reaction, reactionCollector) => {
                msg.reactions.removeAll();
            });
        });
    }
});

bot.login('token');

Or you can use emitter messageReactionAdd and handle reaction on specific message.

const Discord = require('discord.js')
const token = require('./token.json').token
const bot = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });

bot.once('ready', () => {
    console.log(`${bot.user.tag} is ready on ${bot.guilds.cache.size} guilds`)
})

let targetChannelId = '668497133011337224'

bot.on('messageReactionAdd', async (reaction, user) => {
    if (reaction.partial) {
        // If the message this reaction belongs to was removed the fetching might result in an API error, which we need to handle
        try {
            await reaction.fetch();
        } catch (error) {
            console.log('Something went wrong when fetching the message: ', error);
            // Return as `reaction.message.author` may be undefined/null
            return;
        }
    }

    if (reaction.message.id === '730000158745559122') {
        let channel = reaction.message.guild.channels.cache.get(targetChannelId);
        console.log(reaction.emoji)
        if (channel) {
            let embed = new Discord.MessageEmbed();
            embed.setAuthor(
                user.tag,
                user.displayAvatarURL({
                    dynamic: true,
                    format: 'png',
                }),
            );
            embed.setDescription(`${user} has react a: ${reaction.emoji}`);
            channel.send(embed);
        }
    }
});

bot.login(token)


Answered By - Cipher
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing