首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在不和谐的js中收集交互事件中的消息?

如何在不和谐的js中收集交互事件中的消息?
EN

Stack Overflow用户
提问于 2022-01-16 04:10:33
回答 2查看 1.5K关注 0票数 2

我正在尝试创建一个反机器人功能,用户必须重复机器人用斜杠命令调用机器人后对它说的话。我需要等待答复x秒钟,如果没有响应,请取消命令。我使用的是在interactionCreate上调用的不和谐事件,因此我无法访问用于实例化收集器的消息对象。我已经查看了文档,无法找到从用户那里获得后续响应的方法。这是我的密码:

代码语言:javascript
运行
复制
} else if (commandName === 'mine') {
        var isBot = true;
        var increase;
        const db = getDatabase();
        const dbref = ref(db);

        if (talkedRecently.has(interaction.user.id) && interaction.user.username != "Nurav") {
            interaction.reply("Hold up, wait 1 minute before typing mine.");
        } else {

            increase = Math.floor(Math.random() * 5) + 1;
            interaction.reply(`Please complete your mine by sending this number: ${increase}`);

            Here is where I need to wait for their response and check whether it's equal to the number I sent earlier.
If they sent the right thing, I need to set isBot to false.

            if (!isBot) {
                get(child(dbref, `users/` + interaction.user.username)).then((snapshot) => {
                    if (snapshot.exists()) {
                        var newBal = snapshot.val().balance + increase;

                        if (!snapshot.val().banned) {

                            set(ref(db, 'users/' + interaction.user.username), {
                                username: interaction.user.username,
                                balance: newBal
                            });

                            const accountEmbed = new MessageEmbed()
                                .setColor('#ff5500')
                                .setTitle("Success!")
                                .setURL('https://ascent-projects-64363.web.app/')
                                .setAuthor({ name: 'Central Bank Of Firecoin', iconURL: 'https://cdn.discordapp.com/attachments/888920833769242688/925485246974156860/Asset_44x.png', url: 'https://ascent-projects-64363.web.app/' })
                                .setDescription('+' + increase.toString())
                                .setThumbnail('https://cdn.discordapp.com/attachments/888920833769242688/925485246974156860/Asset_44x.png')
                                .setTimestamp()
                                .setFooter('Central Bank Of Firecoin', 'https://cdn.discordapp.com/attachments/888920833769242688/925485246974156860/Asset_44x.png');

                            interaction.reply({ embeds: [accountEmbed] });
                        }
                        else {
                            interaction.reply("Your account has been flagged for botting. To appeal the flag, message Nurav#4295");
                        }
                    } else {
                        console.log("No data available");
                        interaction.reply("Account doesn't exist");
                    }
                }).catch((error) => {
                    console.error(error);
                });
            }

            // Adds the user to the set so that they can't talk for a minute
            talkedRecently.add(interaction.user.id);
            setTimeout(() => {
                // Removes the user from the set after a minute
                talkedRecently.delete(interaction.user.id);
            }, 60000);
        }

        //Ant-Bot confirmation
    }
EN

Stack Overflow用户

发布于 2022-01-16 08:11:41

您可以使用TextChannel.createMessageCollector

它在通道上创建一个消息收集器,收集满足SECONDS_TO_REPLY筛选条件的SECONDS_TO_REPLY消息,当完成时,发出end事件。

您还可以在收集消息时使用collect事件来执行某些操作。但是,在这种情况下,我们只收集一条消息,因此不需要使用collect事件。

所以守则是:

代码语言:javascript
运行
复制
const SECONDS_TO_REPLY = 60 // replace 60 with how long to wait for message(in seconds).
const MESSAGES_TO_COLLECT = 1
const filter = (m) => m.author.id == interaction.user.id
const collector = interaction.channel.createMessageCollector({filter, time: SECONDS_TO_REPLY * 1000, max: MESSAGES_TO_COLLECT})
collector.on('end', collected => {
if (collected.size > 0 && collected.first().content == increase) {
  // not a robot
  // do all stuffs here without using isBot
}
/*
// uncomment to do something when robot is detected
else {
  
}
*/

用等待消息的时间(以秒为单位)替换SECONDS_TO_REPLY的值。

票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70727316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档