简体   繁体   中英

How to properly implement a loop that includes awaitMessages?

So basically I am creating a quick setup command for my discord bot. The idea is that it takes them through a series of prompts that allows them to choose the roles they want to have permissions to use which commands. The issue is that if (for some reason, it doesn't make a ton of sense because they are mentioning the role, but no stone left unturned when it comes to errors,) they choose a role that doesn't exist, it allows them to restart on that 'stage' of the command. I figured to do this I would need a loop, since ideally it allows them to infinitely retry if the role they keep choosing doesn't exist.

I have tried, and failed, a bunch of different for/while loops and while loops, but they all run out of memory, which I believe indicates that it keeps infinitely generating new awaitMessages instances.

This is the code that I have that currently works (without 'catching' the error)

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

I am aware that the prompts and messages would take awhile each time, and in that time frame the loop could have run millions of times, but I honestly am out of ideas on how to achieve infinite retries on each 'stage.'

I would want the "Choose your moderator role" message sent each time, and deleted after a role has been chosen (successfully or unsuccessfully,) and if the role is valid, for it to go to the if (Found) part, and if the role is invalid, for it to loop back around and try again.

So I was able to figure it out through some work, and since it seems others have this issue as well, instead of deleting, I will answer.

So here is the code I have that is working:

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

Hope this can help someone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM