【问题标题】:Discord.js awaitMessages loop?Discord.js awaitMessages 循环?
【发布时间】:2021-04-26 06:56:35
【问题描述】:

我正在尝试为 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,(并且在此过程中不要发送垃圾邮件“轮到你了!”消息。)有人可以帮忙吗?

【问题讨论】:

  • 当你说“而不是垃圾邮件'轮到你了!'进程中的消息。”,您的意思是您只想发送该消息一次,而不管人们在轮到他们时写了多少次消息?
  • 我的意思是,如果它转到 else 子句,当它围绕 if 子句循环时,直到另一个用户发布某些内容时才会完成。我们不希望它在等待另一条消息时向该消息发送垃圾邮件。

标签: javascript loops discord.js bots


【解决方案1】:

我不熟悉 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!`);
}

然后,通过一点点recursion,我们可以继续检查对攻击问题的响应:

// 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);

【讨论】:

  • 所以我没有包含代码的第一部分,它标识了两个战斗机。开始游戏的命令是 f$fight@user1@user2 然后代码的第一位等待两个命名用户响应开始比赛。 (我决定故意只检查用户响应的 ID 以确认它是正确的两个用户,而不是确保他们使用正确的关键字,因此他们可以说任何话继续前进。这将转到循环部分它要求两名玩家轮流选择、出拳、踢或阻挡。这两个输入将使用 d20 RNG 脚本进行处理。
  • 所以你的大部分代码都很棒,但我没有得到 Move1 = response.content 部分。我仍然需要编写一个模块来处理两个输入的动作(每个玩家一个)。这不仅仅是向所选动作发布响应消息。它必须使用 d20 RNG 来确定响应。
  • @Startropic1, const Move1 = response.content; 只是从您的原始问题中逐字复制。如果你能提供更多关于你想用waitForAttackInput 的输入做什么的信息,那么我希望能提供更多帮助。
  • 我已更新我的答案以删除 const Move1 = response.content; 部分,因为它似乎对您没有意义。这对你@Startropic1 是否更清楚?
  • 哎呀,现在我记得 Move1 = response.content 的用途了。我将变量 Move1 定义为玩家选择的任何词,(拳打、踢或阻挡)。然后使用 d20 RNG 检查 Move1(以及第二个玩家输入响应后的 Move2)。然后,机器人会根据两次 d20 掷骰结果发回一条消息。我知道 const 是一个永久变量,但是在循环中,每次运行循环时都可以重新定义它。因此,据我所知,使用 const 应该没问题。
猜你喜欢
  • 2020-08-16
  • 2020-09-03
  • 2019-10-20
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 2019-09-14
  • 2020-11-11
  • 1970-01-01
相关资源
最近更新 更多