简体   繁体   中英

Discord.js embed countdown

I need some help: How am I able to create a embed that update itself every 2 seconds?

Info: The embed should include a countdown of 3 days. Every two seconds the embed should update itself and goes down to 00:00:00. I would like to use the following method to get the message and update itself:

bot.guilds.cache.get('').channels.cache.get('').messages.fetch('');

Im really new to discord.js development and need some help:D

You can use the setInterval() method to do this. What this does is run a block of code every ___ seconds. Using this, and a variable to store a Unix timestamp of when the timer will finish, we can update a message using the .edit() method, so it shows how much time is left.

bot.on("ready", () => {
    // other code
    const message = bot.guilds.cache.get('').channels.cache.get('').messages.fetch('');
    let timeLeft = INSERT TIMESTAMP HERE;
    setInterval(() => {
        timeLeft -= 2000;
        message.edit(`There is ${timeLeft} time left!`):
    }, 2000)
})

Use markdown timestamp so you don't need to send requests from server every second.

Markup: <t:1655196925:R>

Check here: https://discord.com/developers/docs/reference#message-formatting-formats

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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