【问题标题】:How to make an loop for awaiting messages in discord.js如何在 discord.js 中创建一个等待消息的循环
【发布时间】:2020-12-25 07:52:03
【问题描述】:

顺便说一句,我正在尝试制作游戏。

我有一张大地图和一张小地图。

一张小地图专注于 100x100 地图的 5x5 打印,当您发送“w a s d”之一时,它会根据键执行必要的操作。

我的代码是这样的:

message.channel
  .awaitMessages((msg) => msg.author.id == message.author.id, {
    max: 1,
    time: 30000,
  })
  .then((collected) => {
    if (collected.first().content.toLowerCase() == "w") {
      simdiX--;
    } else if (collected.first().content.toLowerCase() == "s") {
      simdiX++;
    } else if (collected.first().content.toLowerCase() == "d") {
      simdiY++;
    } else if (collected.first().content.toLowerCase() == "a") {
      simdiY--;
    } else if (collected.first().content.toLowerCase() == "cancel") {
      message.channel.bulkDelete(2);
      return message.channel.send("Canceled:ok_hand:");
    } else {
      async function del() {
        let ms = await message.channel.send("That's not an option");
        wait(5000);
        ms.delete();
      }
      del();
    }

    tasi(simdiX, simdiY, 5, 5);
    message.channel.bulkDelete(2);
    myPrint(5, 5);
  })
  .catch(() => {
    message.reply("U lost ur chance");
  });

好的,我的格式有点奇怪

【问题讨论】:

    标签: javascript node.js loops discord discord.js


    【解决方案1】:

    您可以使用TextBasedChannel#createMessageCollector 创建一个MessageCollector,它是一个异步可迭代对象。您可以将for await...of 与此收集器一起使用:

    const collector = message.channel.createMessageCollector(
      msg => msg.author.id == message.author.id,
      {time: 30000}
    );
    for await (const msg of collector) {
      if (msg.content.toLowerCase() == "w") {
        simdiX--;
      }
      // etc...
    }
    // This will be sent when the collector ends (when time runs out or if you
    // manually stop it with collector.stop())
    message.reply("U lost ur chance");
    

    请注意,您只能在 async function 中使用 for await...of

    【讨论】:

    • 所以它确实做了一个循环,但它不理解它认为内容不是 w a s d 字符之一的内容。
    • @cinoez “不理解内容”是什么意思?你能试试调试看看msg.content是什么吗?
    猜你喜欢
    • 2020-07-02
    • 2011-06-09
    • 2021-07-23
    • 1970-01-01
    • 2019-12-24
    • 2020-12-11
    • 2020-10-18
    • 1970-01-01
    • 2012-08-07
    相关资源
    最近更新 更多