[英]How to properly implement a loop that includes awaitMessages?
因此,基本上,我正在為我的Discord機器人創建一個快速設置命令。 這個想法是通過一系列提示讓他們選擇允許他們選擇想要使用哪些命令的角色的角色。 問題是,如果(出於某種原因,由於他們提到了角色,這並沒有多大意義,但是當涉及到錯誤時,沒有任何障礙),他們選擇了一個不存在的角色,它允許它們在命令的“階段”重新啟動。 我想這樣做,我需要一個循環,因為理想情況下,如果他們繼續選擇的角色不存在,它可以使他們無限地重試。
我已經嘗試了很多不同的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)
部分,如果該角色無效,因為它會循環播放並重試。
因此,我能夠通過一些工作來解決這個問題,並且由於似乎其他人也存在此問題,因此我將回答而不是刪除。
所以這是我正在工作的代碼:
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.')
}
})
})
希望這可以幫助某人!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.