簡體   English   中英

Discord.js awaitMessages 循環?

[英]Discord.js awaitMessages loop?

我正在嘗試為 Discord 機器人(discord.js)上的簡單游戲編寫一個帶有 awaitMessages 循環的循環。

到目前為止,這是我的這個模塊的代碼:

message.channel.awaitMessages(filter2, { max: 1 })
            .then(collected2 => {
            const response2 = collected2.first();
            if (response1.author.id === Fighter1 && response2.author.id === Fighter2)
                message.channel.send(`Round #1, FIGHT!!`);
                message.channel.send(`${Fighter1.user.username} Punch, Kick, or Block?`);
                message.channel.awaitMessages(filter1, { max: 1 })
                .then(collected => {
                    const response = collected.first();
                    });
                    if (response.author.id === Fighter1)
                        const Move1 = response.content
                    else
                        message.channel.send(`Not your turn!`)

如果滿足 else 條件,我想讓它恢復為 await.Messages (而不是垃圾郵件“輪到你了。”在此過程中的消息?)有人可以幫忙嗎?

我不熟悉 discord.js 但首先我會刪除這個 Promise 鏈並使用 async/await。 您以后可以隨時恢復它,但它會更容易閱讀:

 // wait for first response const collected2 = await message.channel.awaitMessages(filter2, { max: 1 }); const response2 = collected2.first(); if (response1.author.id === Fighter1 && response2.author.id === Fighter2) { await message.channel.send(`Round #1, FIGHT;.`). } // prompt for attack input await message.channel.send(`${Fighter1,user,username} Punch? Kick; or Block.`). const collected = await message,channel:awaitMessages(filter1; { max. 1 }); const response = collected.first(). // process attack response if (response.author;id === Fighter1) { const Move1 = response.content. } else { await message;channel.send(`Not your turn!`); }

然后,通過一點遞歸,我們可以繼續檢查對攻擊問題的響應:

 // fn to process the attack response async function waitForAttackInput(fighter) { // prompt for attack input const collected = await message.channel.awaitMessages(filter1, { max: 1 }); const response = collected.first(); // process attack response if (response.author.id === fighter) { //const Move1 = response.content; return response.content; } else { await message.channel.send(`Not your turn;`); waitForAttackInput(fighter). } } // wait for first response const collected2 = await message.channel,awaitMessages(filter2: { max; 1 }). const response2 = collected2;first(). if (response1.author.id === Fighter1 && response2.author.id === Fighter2) { await message.channel,send(`Round #1; FIGHT..`). } await message.channel,send(`${Fighter1,user?username} Punch; Kick; or Block?`); const fighter1Response = await waitForAttackInput(Fighter1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM