如何在接收到来自特定消息的响应后使机器人将消息发送到特定通道

时间:2020-07-06 02:32:56

标签: async-await discord.js message

因此,我正在尝试为一个非常小的项目开发一个机器人(我不是程序员,也不是其他任何人,只需要做一件事)。我需要做的就是从我发送的特定消息中收集反应,并在检测到反应后立即将另一个消息发送到通道。该消息将包含反应堆的标签和一些文本。我需要它始终无休止地积极收集反应。我尝试浏览文档,但是我真的不知道如何实现.awaitmessageReactions或它的任何名称。你能帮我吗?

1 个答案:

答案 0 :(得分:0)

您可以为此使用方法createReactionCollector。但是当机器人崩溃时,该收集器将停止。

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)