对机器人发送的消息使用反应收集器

时间:2021-06-11 18:31:59

标签: javascript discord.js

在这个机器人上工作了一段时间,但我似乎被难住了。每次我运行它时,它都会说

UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“createReactionCollector”

这是由
引起的 const 收集器 = message.createReactionCollector(filter, { time: 15000 });
我不知道还能怎么做。大多数其他示例要么已经过时,要么是为特定目的而制作的,因此很难将它们实现到我的代码中。我非常感谢您能提供的任何帮助!

if (command === 'ping') {
    const pingEmbed = new Discord.MessageEmbed()
        .setColor('#03cffc')
        .setTitle('Ping!')
        .setDescription(`${message.author.username} is playing a game! \n \n Playing With: \n ` + isPlaying);
    message.channel.send(pingEmbed)
        .then(sentEmbed => {
            sentEmbed.react("?")
        }).then( async message => {
            const filter = (reaction, user) => {
                return reaction.emoji.name === '?' && user.id === message.author.id;
            };

            const collector = message.createReactionCollector(filter, { time: 15000 });

            collector.on('collect', (reaction, user) => {
                console.log(`Collected ${reaction.emoji.name} from ${user.tag}`);
            });

            collector.on('end', collected => {
                console.log(`Collected ${collected.size} items`);
            });
        })}

1 个答案:

答案 0 :(得分:0)

您不需要使用两个 then 方法。对于您的情况,只需一个就足够了。不必使用另一个 then 方法并传入 message,您只需将 message 替换为 sentEmbed

代码:

if (command === 'ping') {
    const pingEmbed = new Discord.MessageEmbed()
        .setColor('#03cffc')
        .setTitle('Ping!')
        .setDescription(`${message.author.username} is playing a game! \n \n Playing With: \n ` + isPlaying);
    message.channel.send(pingEmbed)
        .then(sentEmbed => {
            sentEmbed.react("?")
            const filter = (reaction, user) => {
                return reaction.emoji.name === '?' && user.id === message.author.id;
            };

            const collector = sentEmbed.createReactionCollector(filter, { time: 15000 });

            collector.on('collect', (reaction, user) => {
                console.log(`Collected ${reaction.emoji.name} from ${user.tag}`);
            });

            collector.on('end', collected => {
                console.log(`Collected ${collected.size} items`);
            });
        })
}