简体   繁体   English

Discord.js awaitReactions 或在 messageReactionAdd 私信

[英]Discord.js awaitReactions or on messageReactionAdd in private message

I've created a system which allows users to send a poll in a channel and want to give them extra options in a private message.我创建了一个系统,它允许用户在频道中发送投票,并希望在私人消息中为他们提供额外的选项。 For this, I'm trying to toggle a poll option when the user reacts to this private message with an emoji.为此,当用户使用表情符号对此私人消息做出反应时,我尝试切换投票选项。 The server part works perfectly fine, but I can't get the private message reactions to work as intended.服务器部分工作得很好,但我无法让私人消息反应按预期工作。

In summary: I'm trying to do something when the user reacts to a private message with an emoji.总结:当用户对带有表情符号的私人消息做出反应时,我正在尝试做一些事情。 I've got this working perfectly fine for server channels.对于服务器频道,我已经完全正常工作了。

To test this I've tried the following:为了测试这一点,我尝试了以下方法:

This first one doesn't seem to get called with private messages.第一个似乎没有被私人消息调用。

client.on('messageReactionAdd', (reaction, user) => {
    console.log("added response");
});

client.on('messageReactionRemove', (reaction, user) => {
    console.log("removed response");
});

This second one is test code that runs in response to the "!beep" command.第二个是响应“!beep”命令而运行的测试代码。 Yet shows a few problems:然而显示出一些问题:

  • The filter never passes and "collected" never gets logged.过滤器永远不会通过,“收集”永远不会被记录。
  • The filter only works after the reactions are added (I assume it's because of the async, but I want users to be able to toggle the option whenever they press on the reaction)过滤器仅在添加反应后才起作用(我认为这是因为异步,但我希望用户能够在按下反应时切换选项)
  • The code doesn't get triggered when a reaction is removed (I assume this is because awaitReactions doesn't support that, but I want users to be able to toggle the option whenever they press on the reaction, regardless of whether they are adding or removing it)删除反应时不会触发代码(我认为这是因为 awaitReactions 不支持,但我希望用户能够在按下反应时切换选项,无论他们是添加还是删除它)
  • I tried removing the error bit, which logs "collected" after the time's up.我尝试删除错误位,该位在时间结束后记录“已收集”。 But I want it to be responsive, and thus instant, and I also want it to be reusable so users can toggle it on and off.但我希望它具有响应性,因此是即时的,而且我还希望它可以重复使用,以便用户可以打开和关闭它。
const reactions = ["0️⃣", "1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣", "🔟"];

        message.channel.send('@here Boop :robot:')
            .then(async (sentMessage) => {
                for (var i=0; i < reactions.length; i++) {
                    await sentMessage.react(reactions[i]);
                }

                ///////////////   TEST CODE!!!!
                const filter = (reaction, user) => {
                    console.log("filter called");
                    return true;
                };
                sentMessage.awaitReactions(filter, { time: 33000, errors: ['time'] })
                    .then(collected => {
                        console.log("collected");

                        const reaction = collected.first();
                        console.log(reaction.emoji);
                    })
                    .catch(collected => {
                        console.log("Time's up");
                    });

            });;

TLDR: I want to give a poll config panel to a user in a private message, that allows said user to toggle an option by clicking on a reaction to said config panel. TLDR:我想在私人消息中向用户提供一个轮询配置面板,允许所述用户通过单击对所述配置面板的反应来切换选项。

The stuff I tried didn't work.我试过的东西没有用。 Am I doing something wrong?难道我做错了什么? Is there some other way to do this?有没有其他方法可以做到这一点? Or is this simply not possible?或者这根本不可能?

After debugging a bit more I found the problem, so in case anyone runs into an issue here;经过更多调试后,我发现了问题,以防万一有人在这里遇到问题;

These do work, I just had an if statement that got in my way XD.这些确实有效,我只是有一个 if 语句妨碍了我的方式 XD。

client.on('messageReactionAdd', (reaction, user) => {
    console.log("added response");
});

client.on('messageReactionRemove', (reaction, user) => {
    console.log("removed response");
});

In case they don't work, the message isn't cached.如果它们不起作用,则不会缓存消息。 For which the following give a good starting point (I haven't fully tried them though):下面给出了一个很好的起点(虽然我还没有完全尝试过):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM