如何正确实现包含awaitMessages的循环?

问题描述 投票:1回答:1

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

我已经尝试过,但失败了,一堆不同的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个回答
0
投票

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

所以这是我正在使用的代码:

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.')
                            }
                        })
                    })

希望这可以帮助别人!

© www.soinside.com 2019 - 2024. All rights reserved.