javascript - 如何收集嵌入消息的总 react ? |不和谐.js v13

标签 javascript discord.js

我正在尝试创建一个嵌入,让机器人对消息使用react,并且我已经成功做到了这一点。不过,我现在正在尝试收集该消息的两个表情符号的总 react 。我在网上看到了多种方法,但似乎没有一个能正常工作。有些会抛出错误,而另一些则不会给出任何响应,就好像代码一开始就不存在一样。

这是我的代码:

const discord = require('discord.js');
const db = require('quick.db');

module.exports = {
    name: 'command',
    run: async (message, args) => {

        const suggestionEmbed = new discord.MessageEmbed()
            .setColor('#000000')
            .setTitle('[!] Hello')
            .setTimestamp()
        
        message.channel.send({ embeds: [suggestionEmbed] }).then(sEmbed => {
            sEmbed.react('⬆️');
            sEmbed.react('⬇️');
        });

    }
}

注意:我想也许 module.exports可能是问题所在,所以我将所有尝试的整个代码移至主 index.js 中文件,但仍然没有运气。没有输出,没有错误。

这是我的尝试:

尝试1:

    message.channel.send({ embeds: [suggestionEmbed] })
     .then(sEmbed => {
         sEmbed.react("⬆️");
         sEmbed.react("⬇️");
     });
    const filter = reaction => reaction.emoji.name === '⬆️';
    message.awaitReactions(filter, { time: 10000 })
    .then(collected => collected.map(s => message.reply(`we have ${s.count}`)));

尝试2:

    message.channel.send({ embeds: [suggestionEmbed] })
     .then(sEmbed => {
         sEmbed.react("⬆️");
         sEmbed.react("⬇️");
     });
     console.log(suggestionEmbed.reactions.cache.get("⬆️").count);

尝试 2 的输出:

console.log(suggestionEmbed.reactions.cache.get("⬆️").count);
TypeError: Cannot read property 'cache' of undefined

如何收集已发送的特定消息 ID 的总数?所以我可以检查它是否有 5 个赞成票、10 个反对票等...

编辑:

尝试 3 也没有输出或出现错误:

    const emojis = ["⬆️", "⬇️"]; //the emojis to react
       
    message.channel.send({ embeds: [suggestionEmbed] }) 
        .then(async function (message) {
    for (let emoji of emojis) { await message.react(emoji) }
    const filter = (reaction, user) => {
        return reaction.emoji.name === '⬆️' && user.id === message.author.id;
    };
    
    message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
        .then(collected => {
            const reaction = collected.first();

            if (reaction.emoji.name === '⬆️') {
                message.reply('you reacted with up.');
            } else {
                message.reply('you reacted with down.');
            }
        })
        .catch(collected => {
            console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
            message.reply('you didn\'t react with neither a thumbs up, nor a thumbs down.');
        });

尝试 4,没有输出或错误:

    const time = 1000 //amount of time to collect for in milliseconds
    const emojis = ["⬆️", "⬇️"]; //the emojis to react
    
    message.channel.send({ embeds: [suggestionEmbed] }) 
    .then(async function (message) {
        for (let emoji of emojis) { await message.react(emoji) }
    const filter = (reaction, user) => {
        return reaction.emoji.name === '⬆️' && user.id === message.author.id;
    };
    
    const collector = message.createReactionCollector(filter, { time: time });
    
    collector.on('collect', (reaction, reactionCollector) => {
        console.log(reaction.count) //or do whatever you want with the reactions
        });
    });

尝试 5,没有错误或输出:

    const time = 1000 //amount of time to collect for in milliseconds
    const emojis = ["⬆️", "⬇️"]; //the emojis to react
    
    message.channel.send({ embeds: [suggestionEmbed] }) 
    .then(async function (message) {
        for (let emoji of emojis) { await message.react(emoji) }
    const filter = (reaction, user) => {
        return reaction.emoji.name === '⬆️' && user.id === message.author.id;
    };
    
    const collector = message.createReactionCollector(filter, { time: time });
    
    collector.on('collect', (reaction, user) => {
        console.log(reaction.count) //or do whatever you want with the reactions
        });
    });

尝试 6,没有错误,但输出奇怪:

    message.channel.send({ embeds: [suggestionEmbed] }).then(sEmbed => {
    sEmbed.react('⬆️');
    sEmbed.react('⬇️');
    //db.set(`${suggestionID}.ChannelID`, sEmbed.ID);
});

const filter = (reaction, user) => {
    return reaction.emoji.name === '⬆️' && user.id === message.author.id;
};

message.awaitReactions({ filter, max: 5, time: 60000, errors: ['time'] })
    .then(collected => console.log(collected.size))
    .catch(collected => {
        console.log(`After a minute, only ${collected.size} out of 5 reacted.`);
});

这个似乎只输出 0一旦我运行命令,一分钟后它不会输出 collected.size.catch陈述。我从discord.js文档中取出的这段代码链接hereawait reactions部分。

尝试 7,输出错误但没有错误:

    const embedSend = await message.channel.send({ embeds: [suggestionEmbed] }).then(async sEmbed => {
        await sEmbed.react('⬆️');
        await sEmbed.react('⬇️');
        //db.set(`${suggestionID}.ChannelID`, sEmbed.ID);
        const filter = (reaction, user) => {
            return reaction.emoji.name === '⬆️' && user.id === message.author.id;
        };
        
        const reactions = await sEmbed.awaitReactions({ filter, max: 5, time: 10000, errors: ['time'] })
            .then(collected => console.log(collected.size))
            .catch(collected => {
                console.log(`After a minute, only ${collected.size} out of 5 reacted.`);
        });
    });

这个给我的输出是 After a minute, only 0 out of 5 reacted 。不过我不明白为什么是0?它不计算机器人的 react ,当我在这 10 秒内使用react时,总 react 是 2但输出仍然是 0尽管它本来应该是 1。知道为什么吗?

最佳答案

我找到了解决方案并混合了问题,我的绝大多数尝试都是正确的。唯一的问题是,在使用 Discord.js v13 时,我忘记在意图中包含 Intents.FLAGS.GUILD_MESSAGE_REACTIONS :) 我只花了 23 个多小时才弄清楚这一点。

关于javascript - 如何收集嵌入消息的总 react ? |不和谐.js v13,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69273419/

相关文章:

javascript - 如何在 JS 的 setInterval 中等待?

javascript - 在 React 中读取 CSV 文件

javascript - 类型错误 : Cannot read property 'startsWith' of undefined node. js V12.18.2

javascript - 使循环在变量为 false 时停止

javascript - 如何创建一个发送消息的函数

javascript - 如何在 Math.floor 中使用变量

javascript - Angularjs 下拉菜单在 Internet Explorer 11 的第一个选项中出现空白

javascript - 谁能解释一下这里发生了什么?两个相等的字符串比较时返回 false

javascript - 事件处理时的内存泄漏

javascript - 统计命令显示嵌入中未定义的数据