【问题标题】:Limit user reaction to message限制用户对消息的反应
【发布时间】:2020-06-26 15:25:47
【问题描述】:

我正在为 JavaScript 中的 Discord 构建一个简单的 投票机器人,现在我正在尝试实现每个用户对消息的最大反应数。

例如,假设我们有以下投票问题的选项:

问题?

  1. 选项 A

  2. 选项 B

  3. 选项 C

  4. 选项 D

  5. 选项 E

每个“选项”都是对机器人给出的消息的反应,我想确保用户不能对超过做出反应 em> 3 这些选项。

  • 我的思路是创建一个messageReactionAdd 听众和 然后当用户对4th time 做出反应时,删除最后一个 反应,给他发一条消息,比如“你已经投票了3 times, 请删除回复以再次投票”。
  • 不过,我仍然无法在对象中导航以找到 每个用户的总反应数 我可以找到总反应数 每个表情符号但这不是我需要的。

有人能给我一些见解吗?

编辑

用于发送消息的代码:

Embed = new Discord.MessageEmbed()
                .setColor(0x6666ff)
                .setTitle(question)
                .setDescription(optionsList);

                message.channel.send(Embed).then(messageReaction => {

                for (var i = 0; i < options.length; i++){
                    messageReaction.react(emojiAlphabet[i][0]);
                }

                message.delete().catch(console.error);
              });

【问题讨论】:

    标签: javascript discord.js


    【解决方案1】:

    试试这个:

    const {Collection} = require('discord.js')
    
    // the messages that users can only react 3 times with
    const polls = new Set()
    // Collection<Message, Collection<User, number>>: stores how many times a user has reacted on a message
    const reactionCount = new Collection()
    
    // when you send a poll add the message the bot sent to the set:
    polls.add(message)
    
    client.on('messageReactionAdd', (reaction, user) => {
      // edit: so that this does not run when the bot reacts
      if (user.id === client.user.id) return
    
      const {message} = reaction
    
      // only do the following if the message is one of the polls
      if (polls.has(message)) {
        // if message hasn't been added to collection add it
        if (!reactionCount.get(message)) reactionCount.set(message, new Collection())
        // reaction counts for this message
        const userCount = reactionCount.get(message)
        // add 1 to the user's reaction count
        userCount.set(user, (userCount.get(user) || 0) + 1)
    
        if (userCount.get(user) > 3) {
          reaction.users.remove(user)
          // <@!id> mentions the user (using their nickname if they have one)
          message.channel.send(`<@!${user.id}>, you've already voted 3 times, please remove a reaction to vote again.`)
        }
      }
    })
    
    client.on('messageReactionRemove', (reaction, user) => {
      // edit: so that this does not run when the bot reacts
      if (user.id === client.user.id) return
    
      const {message} = reaction
      const userCount = reactionCount.get(message)
      // subtract 1 from user's reaction count
      if (polls.has(message)) userCount.set(user, reactionCount.get(message).get(user) - 1)
    })
    

    【讨论】:

    • 非常感谢cherryblossom :-) 我尝试实现代码时的另一个问题是,使用client.on('messageReactionAdd') 我得到了机器人添加的反应调用的两倍,我可以在 messageReactionAdd 和 messageReactionRemove 的解析中以任何方式排除它们?提前致谢
    • @ManuelCelli 是的,您可以使用if (user.id === client.user.id) return。我编辑了我的帖子。
    • 再次感谢您的帮助 :-) 我在将消息添加到投票 const 时遇到了一些问题,我已使用用于创建消息的代码更新了问题。跨度>
    • 设法解决它,使用 polls.add(client.user.lastMessage)
    • @ManuelCelli 你也可以message.channel.send(Embed).then(messageReaction =&gt; polls.add(messageReaction))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 2021-06-04
    • 2018-04-03
    • 2021-08-08
    • 2010-10-10
    相关资源
    最近更新 更多