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

标签 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 之前等待。

谢谢你帮助我。

最佳答案

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

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

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

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

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

关于javascript - discord.js 在发送消息之前等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49627508/

相关文章:

javascript - 验证失败后 Unresolved promise

javascript - Angular 2 中组件的自定义用户模板

javascript - javascript marquee 中的间距元素,使用 php 循环元素

c - fork()、exec 和 waitpid()

java - 苹果。移动应用程序自动化。等到

javascript - DM'ing 一个特定的用户 ID

javascript - JavaScript 函数运行时 HTML 页面不更新

vb6 - 等待文件创建超时

javascript - Discord.js 如何知道用户是否阻止了机器人

javascript - 尝试为提到的用户添加角色