所以基本上,我正在为我的不和谐机器人创建一个快速设置命令。这个想法是通过一系列提示让他们选择允许他们选择想要使用哪些命令的角色的角色。问题是,如果(出于某种原因,由于他们提到了角色,这并没有多大意义,但是当涉及到错误时,没有任何障碍),他们选择了一个不存在的角色,它允许它们在命令的“阶段”重新启动。我想这样做是需要一个循环的,因为理想情况下,如果他们继续选择的角色不存在,它可以让他们无限地重试。
我已经尝试并失败了许多不同的for/while
循环和while
循环,但是它们都用光了内存,我相信这表明它不断无限地生成新的{{1} }实例。
这是我目前可以使用的代码(没有“捕获”错误)
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')
}
})
})
部分,并且如果角色无效,请使其循环播放并重试。
答案 0 :(得分: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.')
}
})
})
希望这可以帮助某人!