【问题标题】:how i can use async function in discord js to make a question game?我如何在 discord js 中使用异步功能来制作问题游戏?
【发布时间】:2021-04-25 07:59:14
【问题描述】:

我想制作一个不和谐的机器人,如果我在特定时间输入完成,它会问我一个问题,如果它的答案应该说你赢了,但如果它达到 7 秒,它应该说时间到但我没有知道有时它会按时间向频道发送垃圾邮件,我该如何解决,请我被困在那里 8 小时?


/* === countries image === */

var   kurdistan = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/35/Flag_of_Kurdistan.svg/800px-Flag_of_Kurdistan.svg.png";
var   catalonia = "https://upload.wikimedia.org/wikipedia/commons/thumb/c/ce/Flag_of_Catalonia.svg/810px-Flag_of_Catalonia.svg.png";
var   palastine = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Flag_of_Palestine.svg/800px-Flag_of_Palestine.svg.png";


/* === countries names === */

const obj = [
 kurdistan,
 catalonia,
 palastine
];

/* === code === */



client.on('message', async message => {
    if(message.author.bot) return;
  
          const ranImg = Math.floor(Math.random() * obj.length); 
          if(message.content === 'flag') {
             const embed = new Discord.MessageEmbed();
             embed.setTitle('Flag guesser');
             embed.setDescription('which countries flag does this belong?');
             embed.setImage(obj[ranImg]);
             embed.setColor(0x3aeb34);
             embed.setFooter('this game provided by by me');
             embed.setTimestamp();
             message.channel.send(embed).then(() => {
              let filter = m => m.author.id === message.author.id;
  
              message.channel.awaitMessages(filter, { max: 1, time: '7000', errors: ['time'] })
               .then(collected => {  
//when it gives me the image and I write the correct answer of the obj[ranImg] it doesn't says you win all the time it says try again ???????????????????????????????? 
                  if (collected.first().content === obj[ranImg]) {
                    return message.channel.send(':white_check_mark: You win!') //they won 
                  } else {
                    return message.channel.send(`:x: Oooh close! Try again!`); // they lost 
                  }
               })
               .catch(collected => {
                  return message.channel.send('times up! no answers were collected!');
               });
            });
        }
    }
  );

【问题讨论】:

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


    【解决方案1】:

    消息收集器很难,而且你真的很接近。你不需要定义 awaitMessages,而且 await 已经是异步的,所以你不需要在 #awaitMessages 之前输入 async。此外,您需要一个#then 来引导到#awaitMessages

    此外,如果抛出由errors: ['time'] obj 定义的错误,则收集器有#catch 系统的概念。

    maxMatches 已过时,在 v12 中已更改为 max

    最后,我将结合以上所有内容来修复您的代码:

    client.on('message', async message => {
      if(message.author.bot) return; //have this here so the command does as little as possible before returning
    
            const ranImg = Math.floor(Math.random() * obj.length); 
            if(message.content === 'flag') {
               const embed = new Discord.MessageEmbed();
               embed.setTitle('Flag guesser');
               embed.setDescription('which countries flag does this belong?');
               embed.setImage(obj[ranImg]);
               embed.setColor(0x3aeb34);
               embed.setFooter('this game provided by by me');
               embed.setTimestamp();
               message.channel.send(embed).then(() => {
                let filter = m => m.author.id === message.author.id;
    
                message.channel.awaitMessages(filter, { max: 1, time: '7000', errors: ['time'] })
                 .then(collected => {
                    if (collected.first().content === obj[ranImg]) {
                      return message.channel.send(':white_check_mark: You win!') //they won 
                    } else {
                      return message.channel.send(`:x: Oooh close! Try again!`); // they lost 
                    }
                 })
                 .catch(collected => {
                    return message.channel.send('times up! no answers were collected!'); // if the timer of 7000ms elapses, catch here
                 });
              });
          }
      }
    );
    

    Here 是您需要更多帮助的指南

    【讨论】:

    • 哦,谢谢它对我有用,多亏了你,我学到了很多东西,但我还有一个问题是 ran[obj] 没有给我正确的答案,所以它一直都是错误答案我的对象和为我提供随机图像的变量是 /* /* === countries image === / `` var kurdistan = // kurdistan var palastine 的链接 = // palastine var 的链接Catalonia = // 加泰罗尼亚链接 / === 国家名称 === */ const obj = [ kurdistan, catalonia, Palestine ]; */ ```
    • 请把我的回答标记为正确,至于你的问题我不明白你的意思,你得再详细解释一下
    • 我做到了,我的意思是当我得到随机图像时,我应该猜到当我写下正确答案(如对象键)时,我没有收到说你赢了的消息,就像你在`` ` if (collected.first().content === obj[ranImg]) // 随机对象 ```
    • 我不知道 obj 是在哪里定义的,所以你必须更新上面的代码才能正确解释
    • 我做了,请看我在.then(collected)之后添加的评论
    猜你喜欢
    • 2022-01-15
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 2020-06-29
    • 2013-09-07
    • 2021-12-04
    相关资源
    最近更新 更多