反应收集器,显示百分比(用于建议)

时间:2020-07-06 21:48:20

标签: javascript discord discord.js

因此,我基本上已经设置了我的建议机器人,这是一个非常基础的机器人,但是我希望添加一个很酷的功能,该功能可以收集正面和负面的反应并显示一个百分比。如果赞成票数更多,它将显示100%,如果赞成票为1,反对票为1,它将显示50%;如果赞成票数是负数,则为1,而其他则不显示-100%。这很简单,但是我很努力地了解如何去做。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

对于句柄反应,可以使用方法createReactionCollector,但第一个问题是:方法未在反应移除时触发。因此,您需要使用一些时间间隔来检查消息反应。

time: 120000-等待毫秒响应的时间,将其更改为所需的内容。 如果漫游器重新启动,哈希反应将停止...

client.on('message', message => {
    if (message.content.startsWith('test')) {
        let suggestion = message.content.substring(0, 4) //test length
        let embed = new Discord.MessageEmbed();
        embed.setAuthor(message.author.tag, message.author.displayAvatarURL({
            dynamic:true,
            format: "png"
        }))
        embed.setTitle('Suggestion')
        embed.setColor('GOLD')
        embed.setDescription(suggestion)
        embed.setTimestamp()
        message.channel.send(embed).then(msg => {
            msg.react('?').then(() => msg.react('?'))
            const filter = (reaction, user) => {
                return [`?`, '?'].includes(reaction.emoji.name)
            };
            let check = setInterval(handleReaction, 5000, message, msg, suggestion)
            const collector = msg.createReactionCollector(filter, {
                time: 120000,
            });
            collector.on('collect', (reaction, reactionCollector) => {
                handleReaction(message, msg, suggestion)
            });
            collector.on('end', (reaction, reactionCollector) => {;
                clearInterval(check)
            });
        })
    }
})

function handleReaction (message, msg, suggestion) {
    let embed = new Discord.MessageEmbed();
    let positiveReaction = msg.reactions.cache.get('?')
    let negativeReaction = msg.reactions.cache.get('?')
    let negativeCount = negativeReaction ? negativeReaction.count : 0
    let positiveCount = positiveReaction ? positiveReaction.count : 0
    embed.setAuthor(message.author.tag, message.author.displayAvatarURL({
        dynamic:true,
        format: "png"
    }))
    embed.setTitle('Suggestion')
    embed.setColor('GOLD')
    embed.setDescription(suggestion)
    embed.addField('Votes', `? - ${(positiveCount / (positiveCount + negativeCount) * 100).toFixed(2)}%\n? - ${(negativeCount / (positiveCount + negativeCount) * 100).toFixed(2)}%`)
    embed.setTimestamp()
    msg.edit(embed)
}

答案 1 :(得分:0)

如果您直接在找我,我现在真的不知道,但是这是我有一个旧机器人的旧代码

    const sayMessage = args.join(" ");
    if (!args.length) {
            return message.channel.send(`You didn't provide any text! <:angry:713422454688710717>`);
        }
    const sentMessage = await client.channels.get(< CHANNEL ID HERE >).send({embed: {
    color: 700000,
    author: {
      name: client.user.username,
    },
    title: "Suggestion",
    description: sayMessage,
    footer: {
      timestamp: new Date(),
      icon_url: client.user.avatarURL,
      text: "Suggestion"
    }
  }});
    message.react('?');
    sentMessage.react('?').then(sentMessage.react('?'))
    message.channel.send("To vote for a suggestion, join my server. It should just be ..server!")

它所要做的是获取用户消息并将其嵌入,并在您选择的渠道中以竖起大拇指和朝下做出反应 但是,如果您使用的是V.12,则可能需要进行一些更新