【问题标题】:How can I resolve this problem (node.js, discord.js)我该如何解决这个问题(node.js,discord.js)
【发布时间】:2023-03-20 21:06:01
【问题描述】:
            if (!args[1]) return message.channel.send('You need to specify a person!')
            if (!args[2]) return message.channel.send('Please specify a time for how long the ban council will last.')
            var tim = args[2]
            const sweden = message.mentions.users.first()
            message.react('????').then(() => message.react('????'))
            const mappo = ['????', '????']
            if (!args[1]) return message.channel.send('Please specify a person!')
            if(message.guild){ //this will check if the command is being called from a server
                const embad = new Discord.MessageEmbed()
                    .setTitle('Ban Council')
                    .addField('The Convicted:', `${sweden.tag}`)
                    .addField('Rules:', 'Vote on whether the convicted is guilty or not with the prompted reactions. The ban will end automatically in 5 seconds.')
                message.channel.send(embad)
                setTimeout(function(){
                if(sweden){
                    const lyft = message.guild.member(sweden)
                    if(lyft){
                        if(message.reactions.cache.map(r => `${'????'} ${r.users.cache.size}`)[0] > message.reactions.cache.map(r => `${'????'} ${r.users.cache.size}`)[1]){
                            lyft.ban({ ression: 'Majority has exiled you from server. '}).then(() => {
                            message.reply(`The user ${december.tag} was banned as a result of a majority vote.`)
                            })
                        } else {
                            message.channel.send('The ban was cancelled.')
                        }
                    }else{
                        message.reply('The user is not in this server.')
                    }
                    }else{
                        message.reply('You need to specify a person!')
                    }
                }, tim)
                } else {
                message.channel.send('Banning does not work here!')
            }

它在实际有机会接受输入之前发送“Ban cancelled”。我已经尝试过收集器,但由于 max: 部分,它不起作用,我该如何解决这个问题? (也不会返回错误)

这是一个案例。 (附加到我得到的反馈)

【问题讨论】:

  • 另外,顶部有一个案例,虽然它只是说案例'ban':
  • 尝试使用 awaitReactions
  • @Pepe_W​​orm : awaitReactions 只在我没有的异步函数中工作。
  • 它适用于任何函数,它只是一个承诺,如果你使用 await 那么它需要在一个异步函数中
  • @Karizma 所以你建议我重写它吗?效果很好,只是需要时间

标签: node.js discord.js


【解决方案1】:

首先你应该在你发布到 stackoverflow 之前美化你的代码,你可以删除 break 关键字并解释它是在一个 switch case 里面

它不起作用的原因是因为您正在检查 message 的反应,而不是您发送的嵌入,因此要解决此问题,您需要为 message.channel.send(embad) 分配一个变量,但这是一个承诺所以你需要await它,这需要一个异步函数

最后awaitReactionscreateReactionCollector 可能是更好的选择,

所以这是新代码:

(async () => {
    if (!args[1]) return message.channel.send('You need to specify a person!');
    if (!args[2]) return message.channel.send('Please specify a time for how long the ban council will last.')
    if (!message.guild) return message.channel.send('Banning does not work here');

    var tim = args[2]
    const sweden = message.mentions.member.first()
    const mappo = ['?', '?']
    message.react('?').then(() => message.react('?'))

    const embad = new Discord.MessageEmbed()
        .setTitle('Ban Council')
        .addField('The Convicted:', `${sweden.tag}`)
        .addField('Rules:', 'Vote on whether the convicted is guilty or not with the prompted reactions. The ban will end automatically in 5 seconds.')
    const embedMessage = await message.channel.send(embad);

    setTimeout(function () {
        if (!sweden) return message.reply('You need to mention a person');

        const filter = (reaction) => mappo.includes(reaction.emoji.name);
        embad.awaitReactions(filter, { time: 5000 })
            .then(collection => {
                //not the most optimal way to do it
                const emoji1 = collection.find(e => e.emoji.name === '?');
                const emoji2 = collection.find(e => e.emoji.name === '?');
                
                //both default to 0
                const upvotes = emoji1 && emoji1.users.size || 0;
                const downvotes = emoji2 && emoji2.users.size || 0;

                if (upvotes > downvotes) {
                    lyft.ban({ reason: 'Majority has exiled you from server. ' })
                        .then(() => message.reply(`The user ${december.tag} was banned as a result of a majority vote.`));
                } else {
                    message.channel.send('The ban was cancelled.');
                }

            })
            .catch(console.error);
    }, tim);
})();

【讨论】:

  • 非常感谢,这真的很有帮助。感谢您解释我做错的所有事情,我做这件事不到一年,所以像这样的东西非常有帮助。抱歉,我没有提供更简洁的代码,感谢您的帮助。希望你有一个美好的一天。
  • 没问题,很高兴你阅读它而不是仅仅复制代码,我做了一些我没有解释的更改,所以如果你不明白,请评论,不用担心干净的代码部分。最后我应该指出,SO 团队反对“谢谢”评论,所以如果你想对正确答案表示感谢,你应该接受这个帖子。
  • 好的,我遇到了一个问题,它说它无法读取未定义的“用户”属性,不知道如何解决。过去一个小时我一直在研究它,但它仍然无法正常工作。希望你能帮忙,如果没有,我会继续努力。
  • 这可能是因为其中一个表情符号没有反应,所以你必须检查它们是否存在,然后调用.users.size,否则默认为 0。编辑我的帖子以获得差异。另外,您不应该将它们命名为emoji1emoji2,我只是这样做了,因为我目前想不出任何名称
  • 是的!我用我最初拥有的代码替换了 const upvotes 和 const upvotes 的区域。谢谢你的一切!
【解决方案2】:

自从我发表这篇文章已经过去了大约 8 个月,我找到了答案,并且找到了一种更有效的方法。我不会发布整个代码,因为它很长而且不是很整洁。然而,这是一种更有效的计算反应的方法。

我的原始代码是:

const upvotes = message.reactions.cache.map(r => ${'?'} ${r.users.cache.size})[0]

const downvotes = message.reactions.cache.map(r => ${'?'} ${r.users.cache.size})[1]

也不是很好用。

const [upvoteReaction, downvoteReaction] = message.reactions.cache.first(2);

     const upvotes = upvoteReaction.users.cache.size;
     const downvotes = downvoteReaction.users.cache.size;

它从消息的前两个反应中获取反应数。看到唯一的反应是竖起大拇指和竖起大拇指,它会从他们俩那里得到数字。从那里,您可以比较两者。

【讨论】:

  • 感谢堆栈溢出成员帮助我解决了另一个问题,因此为这个问题找到了更好的策略。
  • 还有其他几种方法可以做到这一点,但这是我发现的最通用和最简单的方法。如果有的话,在 cmets 中添加更多!
猜你喜欢
  • 2021-05-16
  • 2021-04-13
  • 2020-03-25
  • 2020-02-14
  • 2021-10-23
  • 1970-01-01
相关资源
最近更新 更多