【问题标题】:need to know how to code a countdown in javascript需要知道如何在 javascript 中编写倒计时代码
【发布时间】:2019-04-29 09:50:47
【问题描述】:

总的来说,我对编码还很陌生,但我对事物的掌握得很好。我正在为 Discord 编写一个机器人,我需要的主要命令是从 5 开始倒计时。例如,有人说 !startqueue 它将从 5 开始计数,一旦达到零就会停止。如果不必发送单独的消息,我将能够在其他地方找到答案。我不知道这是否有意义,所以如果需要,请要求澄清。

这是控制 !roll 功能的代码。它会从 1 到 6 滚动一个随机数(这只是为了说明如何编写代码以查找有效的不和谐命令)。

    const commando = require('discord.js-commando');
class DiceRollCommand extends commando.Command {
    constructor(client) {
        super(client, {
            name: 'roll',
            group: 'random',
            memberName: 'roll',
            description: 'Rolls a die' ,
        });
    }
    async run(message, args) {
        var roll = Math.floor(Math.random() * 6) + 1;
        message.reply("You Rolled a " + roll);
    }
}
module.exports = DiceRollCommand;

下面是我为 !queue 命令设置的基本代码

const commando = require('discord.js-commando');
class QueueCommand extends commando.Commando {
    constructor(client) {
        super(client, {
            name: 'Queue Start',
            group: 'random',
            memberName: 'startQueue',
            description: 'Starts the queue' ,
        });
    }
}

这只是我所拥有的其余代码,您可以看到我也看到的所有代码。

const commando = require('discord.js-commando') ;
const bot = new commando.CommandoClient();

bot.registry.registerGroup('random', 'Random') ;
bot.registry.registerDefaults();
bot.registry.registerCommandsIn(__dirname + "/commands") ;

bot.login('no token');

【问题讨论】:

    标签: javascript discord


    【解决方案1】:

    简单的倒计时:

    let count = 5
    
    const counter = setInterval(() => {
      if (count > 0) {
        console.log(count)
        count--
      } else {
        clearInterval(counter)
      }
    }, 1000)

    【讨论】:

    • 我怎样才能让它编辑消息而不是一一编辑
    猜你喜欢
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 2011-05-01
    • 2016-08-22
    • 2017-02-06
    • 1970-01-01
    • 2012-05-22
    相关资源
    最近更新 更多