【问题标题】:How to properly implement a loop that includes awaitMessages?如何正确实现包含 awaitMessages 的循环?
【发布时间】:2019-09-14 09:26:27
【问题描述】:

所以基本上我正在为我的不和谐机器人创建一个快速设置命令。这个想法是让他们通过一系列提示,让他们选择他们想要拥有使用哪些命令的权限的角色。问题是,如果(出于某种原因,这没有多大意义,因为他们提到了角色,但在涉及错误时不遗余力,)他们选择了一个不存在的角色,它允许他们在命令的那个“阶段”重新启动。我想这样做我需要一个循环,因为理想情况下,如果他们一直选择的角色不存在,它允许他们无限重试。

我尝试了一堆不同的for/while 循环和while 循环,但都失败了,但它们都耗尽了内存,我相信这表明它会不断生成新的awaitMessages 实例。

这是我目前工作的代码(没有“捕捉”错误)

message.channel.send('Choose your moderator role.').then(async (modQ) => {
                        message.channel.awaitMessages(filter, {maxMatches: 1, time: 60000, errors: ['time']}).then(async (modC) => {
                            await modQ.delete()
                            await modC.first().delete()
                            let Found = modC.first().mentions.roles.first()
                            if (Found) {
                                let chosen = String(modC.first().mentions.roles.first().id)
                                setArgs(chosen, 'generalRoles', 'generalRole_id')
                            } else {
                                message.channel.send('No')
                            }
                        })
                    })

我知道提示和消息每次都需要一段时间,在那个时间范围内,循环可能会运行数百万次,但老实说,我不知道如何在每个“阶段”实现无限重试。

我希望每次都发送“选择您的主持人角色”消息,并在选择角色(成功或不成功)后删除,如果角色有效,则将其转到 if (Found) 部分,如果角色无效,则循环返回并重试。

【问题讨论】:

    标签: javascript node.js visual-studio-code discord.js


    【解决方案1】:

    所以我能够通过一些工作解决它,并且由于似乎其他人也有这个问题,而不是删除,我会回答。

    所以这是我正在工作的代码:

    message.channel.send(mod).then(async (modQ) => {
                            message.channel.awaitMessages(filter, {maxMatches: 1, time: 60000, errors: ['time']}).then(async (modC) => {
                                await modQ.delete()
                                await modC.first().delete()
                                let Found = modC.first().mentions.roles.first()
                                let found = false;
                                if (Found) {
                                    found = true;
                                    let chosen = String(modC.first().mentions.roles.first().id)
                                    setArgs(chosen, 'generalRoles', 'generalRole_id')
                                    console.log('worked1')
                                } else {
                                    while (found === false) {
                                        await message.channel.send('Hey').then(async (modQ) => {
                                            await message.channel.awaitMessages(filter, {maxMatches: 1, time: 60000, errors: ['time']}).then(async (modC) => {
                                                await modQ.delete()
                                                await modC.first().delete()
                                                let Found = modC.first().mentions.roles.first()
                                                if (Found) {
                                                    let chosen = String(modC.first().mentions.roles.first().id)
                                                    setArgs(chosen, 'generalRoles', 'generalRole_id')
                                                    console.log('worked2')
                                                    found = true
                                                }
                                            })
                                        })
                                    }
                                }
                                if (found === true) {
                                    message.channel.send('We here now.')
                                }
                            })
                        })
    

    希望这可以帮助别人!

    【讨论】:

      猜你喜欢
      • 2021-04-26
      • 2021-12-02
      • 2016-11-22
      • 1970-01-01
      • 2022-09-26
      • 2019-02-15
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多