如何让机器人在收到来自某个消息的反应后将消息发送到特定频道

Posted

技术标签:

【中文标题】如何让机器人在收到来自某个消息的反应后将消息发送到特定频道【英文标题】:How to make a bot send a message to a specific channel after receiving a reaction from a certain message 【发布时间】:2020-10-26 03:06:09 【问题描述】:

所以我正在尝试为一个非常小的项目开发一个机器人(我不是程序员或任何东西,只需要做这件事)。我需要它做的就是从我发送的特定消息中收集反应,并在检测到反应后立即将另一条消息发送到通道。该消息将包含反应器的标签和一些文本。我需要它一直积极地收集反应,没有任何限制。我尝试查看文档,但我真的不知道如何实现 .awaitmessageReactions 或任何它的名称。你能帮帮我吗?

【问题讨论】:

【参考方案1】:

您可以为此使用方法createReactionCollector。但是当 bot 宕机时,这个收集器就会停止。

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');

或者您可以使用发射器messageReactionAdd 并处理对特定消息的反应。

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)

【讨论】:

我试过了。我发送消息后,第一个确实回复了,但似乎没有收集我添加的任何反应。第二个(这几乎是我需要的)并没有真正做任何事情,即使我要求其他人添加反应。 我很确定我做到了。我将频道 ID 放在“targetChannelId”上,将消息 ID 放在“SOME ID”上,并将我的令牌放在令牌括号中。我还有什么需要填写的吗? 对不起,忘记了部分,那不需要对非缓存消息的句柄反应。我正在编辑我的解决方案,现在它可以正常工作了 非常感谢!它现在完美运行。我希望我不会占用您太多时间,但是是否可以将嵌入(很像那个)发送到特定频道,而不是在同一个频道上回复? 当然,用 let anotherChannel = react.message.guild.channels.cache.get('ID HERE'); 获取另一个频道anotherChannel.send(embed)

以上是关于如何让机器人在收到来自某个消息的反应后将消息发送到特定频道的主要内容,如果未能解决你的问题,请参考以下文章

如何让我的机器人在特定频道中发送消息,然后在一段时间后将其删除

试图让不和谐机器人对某个频道内的所有消息做出反应

收到表情符号反应后的 Discord.js 消息

重启后如何使机器人注册反应

如何让一个不和谐的 js 机器人在某人获得某个角色后向他发送消息

如何让不和谐机器人使用 discord.py 向消息添加表情符号反应?