如何正确实现包含 awaitMessages 的循环?
How to properly implement a loop that includes awaitMessages?
基本上我正在为我的 discord 机器人创建一个快速设置命令。这个想法是,它会引导他们通过一系列提示,让他们选择他们想要的角色有权使用哪些命令。问题是,如果(出于某种原因,这没有多大意义,因为他们提到了这个角色,但在涉及错误时不遗余力,)他们选择了一个不存在的角色,它允许他们重新启动命令的 'stage'。我想这样做我需要一个循环,因为理想情况下,如果他们继续选择的角色不存在,它允许他们无限重试。
我尝试了一堆不同的 for/while
循环和 while
循环,但都失败了,但它们都 运行 内存不足,我相信这表明它会无限地保持生成新的 awaitMessages
个实例。
这是我目前可用的代码(没有 'catching' 错误)
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')
}
})
})
我知道提示和消息每次都需要一段时间,在那个时间范围内循环可能 运行 数百万次,但老实说我不知道如何实现无限重试每个 'stage.'
我希望每次发送 "Choose your moderator role" 消息,并在选择角色(成功或不成功)后将其删除,如果角色有效,则将其转到 if (Found)
部分,如果角色无效,则返回并重试。
所以我通过一些工作弄明白了,因为其他人似乎也有这个问题,所以我不删除,而是回答。
这是我的有效代码:
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.')
}
})
})
希望这可以帮助到别人!
基本上我正在为我的 discord 机器人创建一个快速设置命令。这个想法是,它会引导他们通过一系列提示,让他们选择他们想要的角色有权使用哪些命令。问题是,如果(出于某种原因,这没有多大意义,因为他们提到了这个角色,但在涉及错误时不遗余力,)他们选择了一个不存在的角色,它允许他们重新启动命令的 'stage'。我想这样做我需要一个循环,因为理想情况下,如果他们继续选择的角色不存在,它允许他们无限重试。
我尝试了一堆不同的 for/while
循环和 while
循环,但都失败了,但它们都 运行 内存不足,我相信这表明它会无限地保持生成新的 awaitMessages
个实例。
这是我目前可用的代码(没有 'catching' 错误)
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')
}
})
})
我知道提示和消息每次都需要一段时间,在那个时间范围内循环可能 运行 数百万次,但老实说我不知道如何实现无限重试每个 'stage.'
我希望每次发送 "Choose your moderator role" 消息,并在选择角色(成功或不成功)后将其删除,如果角色有效,则将其转到 if (Found)
部分,如果角色无效,则返回并重试。
所以我通过一些工作弄明白了,因为其他人似乎也有这个问题,所以我不删除,而是回答。
这是我的有效代码:
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.')
}
})
})
希望这可以帮助到别人!