【问题标题】:Message on Reaction discord.js关于反应 discord.js 的消息
【发布时间】:2021-03-23 21:18:44
【问题描述】:

我一直试图让我的机器人在用户对机器人表情符号做出反应时回复他们,但没有运气我希望有人能在这里帮助我,我只是 discord.js 的初学者。

这是我的代码,我只需要在用户对其做出反应后机器人发送回消息的部分获得帮助。

bot.on('message', message => {
    if(message.author.bot) 
    {
        if(message.embeds)
        {
            const embedMsg = message.embeds.find(msg => msg.title === 'Next step');
            if(embedMsg)
            {
                message.react('708923041928839169')
                .then(reaction => reaction.message.react('708923028846805073'))
                .catch(err => console.error);
            }
        }
        return;
    }

    if(message.content.toLowerCase() === 'done'){
      
        const embed = new Discord.MessageEmbed();
        embed.setTitle('Next step')
        embed.setColor(colors.blue);
        embed.setDescription('Please react to Agree or Disagree');
        message.channel.send(embed)
    }

  });

bot.login(token);

【问题讨论】:

    标签: discord.js bots


    【解决方案1】:

    您可以简单地使用ReactionCollector 来执行此操作。这方面的文档可以在 discord.js docs 上轻松找到,我建议您在不确定如何做某事之前查看那里,然后再在 StackOverflow 上提问。

    这是使用您的代码的示例:

    bot.on('message', message => {
        if(message.author.bot) return;
    
        if(message.content.toLowerCase() === 'done'){
      
            const embed = new Discord.MessageEmbed();
            embed.setTitle('Next step')
            embed.setColor(colors.blue);
            embed.setDescription('Please react to Agree or Disagree');
            message.channel.send(embed).then(embedMsg => {
                embedMsg.react('708923041928839169')
                .then(reaction => embedMsg.react('708923028846805073'))
                .catch(err => console.error);
    
                const filter = (r, u) => r.emoji.id == '708923041928839169' || r.emoji.id == "708923028846805073";
                const collector = message.createReactionCollector(filter, {time: 60000});
                collector.on('collect', (r, u) => {
                   //Put your response to reactions here
                   message.channel.send("Reply with something to " + u.tag + " because they reacted with " + r.emoji.name);
                });
            })
        }
    
    });
    
    bot.login(token);
    

    我还移动了您将反应添加到嵌入的代码,因为您这样做的方式是不必要的额外步骤。

    相关资源:
    https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=createReactionCollector

    【讨论】:

    • 嘿@Cannicide 你的例子有效,但对反应的反应不起作用你能检查一下吗??
    • 抱歉@SinKnight,我不小心导致ReactionCollector 只检查ID 为“708923041928839169”的反应,而不是ID 为“708923028846805073”的反应,因为我的回答中有错字。我假设这就是问题所在,并且我已经通过更正更新了我的答案。另请注意,此示例设置了 60 秒的时间限制,因此在发送嵌入后一分钟将不再发生响应(调整收集器选项中的 time 以更改它)。
    猜你喜欢
    • 2021-03-31
    • 2021-05-13
    • 2020-06-25
    • 2021-01-03
    • 2021-04-15
    • 2021-08-11
    • 1970-01-01
    • 2021-06-21
    • 2020-11-30
    相关资源
    最近更新 更多