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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49627508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:40:12  来源:igfitidea点击:

discord.js wait before send message

javascriptwaitdiscord.js

提问by Dillgo

My problem is that I want my discordbot wait during the giveaway until the giveway is out

我的问题是我希望我的 discordbot 在赠品期间等待直到赠品结束

Here's my code :

这是我的代码:

        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
    })
  }

He must wait EndMS milis and, after, choose a winner but for now, he send instantly STUFF, for choosing the winner, I will not ask it for you. All I want is he wait before sending Stuff.

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

Thanks you for helping me.

谢谢你帮助我。

回答by André

Well it depends. If you trust on the bot's uptime (if the bot wont ever crash or restart during a giveaway) you can use a setTimeout:

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

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

This will run doSomething()after 3 seconds (3000 milliseconds).
So what you need to basically do is:

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

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

Note: You might have to tweak the EndMS part.

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