2017-11-08 12 views
1

私はbottingとjsを戸惑わせるのが初めてです。私は自分のbotで遊んでいます。私はタイピングのミニゲームをしたい。チャットで?typeと入力すると、ボットはチャットで何かを発言し、カウントダウン中にそのメッセージを編集します。カウントダウンが終了すると、ランダムに生成された単語が表示されます。プレイヤーはチャットに正確なランダムな単語を入力する必要があり、ボットには合計時間が表示されます。それはまで0 私はmessage.channel.send(randomWord)が動作しない理由を理解しないを数えた後、現在のコードが停止しsetTimeout()の2番目の関数が実行されません

case "type": 
     let randomWord = Math.random().toString(36).replace(/[^a-z]+/g, ''); 
     let timer = 3; 
     message.channel.send("Generating a new word..") 
      .then((msg)=> { 
      var interval = setInterval(function() { 
       msg.edit(`Starting in **${timer--}**..`) 
      }, 1000) 
      }); 
     setTimeout(function() { 
      clearInterval(interval); 
      message.channel.send(randomWord) 
      .then(() => { 
      message.channel.awaitMessages(response => response.content == randomWord, { 
       max: 1, 
       time: 10000, 
       errors: ['time'], 
      }) 
      .then(() => { 
       message.channel.send(`Your time was ${(msg.createdTimestamp - message.createdTimestamp)/1000} seconds.`); 
      }) 
      .catch(() => { 
       message.channel.send('There was no collected message that passed the filter within the time limit!'); 
      }); 
      }); 
     }, 5000); 
     break; 

は、ここに私のコードです。また、誰かが私にこのコードを変更してasynchを使うのを手伝ってもらえれば、それが大好きです。

答えて

0

私はあなたの問題を調査し始めました。ここで私が考え出したシステムがあります。

ここには、さまざまなユーザーからのメッセージをリッスンするボットがあります。ユーザーが'?type'と入力すると、メッセージのチャンネルを渡して関数runWordGameが呼び出されます。

// set message listener 
client.on('message', message => { 
    switch(message.content.toUpperCase()) { 
     case '?type': 
      runWordGame(message.channel); 
      break; 
    } 
}); 
runWordGame

ここで、ボットは、ランダム・ワードを作成し、次いで、(以下displayMessageCountdownを参照)がユーザにカウントダウンを表示します。カウントダウンが終了すると、メッセージはランダムな単語で編集されます。次に、ボットは10秒間1つのメッセージを待って、ユーザがランダムな単語を入力するのを待ちます。成功すると、成功メッセージが送信されます。それ以外の場合は、エラーメッセージが送信されます。

// function runs word game 
function runWordGame(channel) { 
    // create random string 
    let randomWord = Math.random().toString(36).replace(/[^a-z]+/g, ''); 

    channel.send("Generating a new word..") 
    .then(msg => { 
     // display countdown with promise function :) 
     return displayMessageCountdown(channel); 
    }) 
    .then(countdownMessage => { 
     // chain promise - sending message to channel 
     return countdownMessage.edit(randomWord); 
    }) 
    .then(randomMsg => { 
     // setup collector 
     channel.awaitMessages(function(userMsg) { 
      // check that user created msg matches randomly generated word :) 
      if (userMsg.id !== randomMsg.id && userMsg.content === randomWord) 
       return true; 
     }, { 
      max: 1, 
      time: 10000, 
      errors: ['time'], 
     }) 
     .then(function(collected) { 
      // here, a message passed the filter! 
      let successMsg = 'Success!\n'; 

      // turn collected obj into array 
      let collectedArr = Array.from(collected.values()); 

      // insert time it took user to respond 
      for (let msg of collectedArr) { 
       let timeDiff = (msg.createdTimestamp - randomMsg.createdTimestamp)/1000; 

       successMsg += `Your time was ${timeDiff} seconds.\n`; 
      } 

      // send success message to channel 
      channel.send(successMsg); 
     }) 
     .catch(function(collected) { 
      // here, no messages passed the filter 
      channel.send(
       `There were no messages that passed the filter within the time limit!` 
      ); 
     }); 
    }) 
    .catch(function(err) { 
     console.log(err); 
    }); 
} 

この関数は、カウントダウンメッセージの表示を外挿します。タイマーがゼロになるまで同じメッセージオブジェクトが編集され、Promiseが解決され、次の.then(...メソッドがrunWordGameの内側にトリガーされます。

// Function displays countdown message, then passes Message obj back to caller 
function displayMessageCountdown(channel) { 
    let timer = 3; 

    return new Promise((resolve, reject) => { 
     channel.send("Starting in..") 
     .then(function(msg) { 
      var interval = setInterval(function() { 
       msg.edit(`Starting in **${timer--}**..`); 

       // clear timer when it gets to 0 
       if (timer === 0) { 
        clearInterval(interval); 
        resolve(msg); 
       } 
      }, 1000); 
     }) 
     .catch(reject); 
    }); 
} 

ご質問がある場合や、別の最終結果をお探しの場合はお知らせください。

関連する問題