简体   繁体   English

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

[英]discord.js wait before send message

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.他必须等待 EndMS milis,然后选择一个获胜者,但现在,他会立即发送 STUFF,为了选择获胜者,我不会问你。 All I want is he wait before sending Stuff.我想要的只是他在发送 Stuff 之前等待。

Thanks you for helping me.谢谢你帮助我。

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).这将在 3 秒(3000 毫秒)后运行doSomething() )。
So what you need to basically do is:所以你基本上需要做的是:

.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 部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM