discord.js在发送消息之前等待

时间:2018-04-03 10:13:15

标签: javascript wait discord.js

我的问题是我希望我的discordbot在赠品期间等待,直到赠品出来

这是我的代码:

        const Discord = require('discord.js');
        const client = new Discord.Client();
        const config = require("./config.json");
        var prefix = config.prefix;
        client.login(config.token);

    client.on('ready',() => {
        console.log('I\'m online');
    })

    client.on('message', message => {

      const args = message.content.slice(prefix.length).trim().split(/ +/g);
      const command = args.shift().toLowerCase();


      if (command === 'gstart') {
        var channel = args[0].slice(2).slice(0, -1);
        var price = args[1];
        var End = args[2];
        var EndMS = End*1000;
        client.channels.get(channel).send({embed: {
            color: 3447003,
            author: {
              name: '',
              icon_url: ''
            },
            title: "",
            url: "",
            description: "",
            fields: [{
                name: 'a Giveaway just started !',
                value: 'Put\'\' in react of this message to try to win \'' + price + '\'.'
          },     
        ],
        timestamp: '',
        footer: {
          icon_url: '',
          text: 'End in ' + End + ' secondes !'
        }
      }
    })
    .then(msg => {
      msg.react('');
      client.channels.get(channel).send('STUFF')(EndMS); // my problem is here
    })
  }

他必须等待EndMS milis,然后选择一名获胜者但是现在,他立即发送STUFF,选择获胜者,我不会问你。 我想要的只是在发送Stuff之前等待。

感谢您帮助我。

1 个答案:

答案 0 :(得分:2)

这取决于。如果您相信机器人的正常运行时间(如果机器人在赠品期间不会崩溃或重新启动),您可以使用setTimeout:

setTimeout(function(){ 
    doSomething(); 
}, 3000);

这将在3秒(3000毫秒)后运行doSomething() 所以你需要做的是:

.then(msg => {
  msg.react('');
  setTimeout(function(){
     client.channels.get(channel).send("It's finished!");
  }, EndMS);
})

注意:您可能需要调整EndMS部分。

相关问题