Search code examples
javascriptwaitdiscord.js

discord.js wait before send message


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

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.

Thanks you for helping me.


Solution

  • 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(function(){ 
        doSomething(); 
    }, 3000);
    

    This will run doSomething() after 3 seconds (3000 milliseconds).
    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.