Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
87 views
in Technique[技术] by (71.8m points)

javascript - 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

question from:https://stackoverflow.com/questions/65626621/discord-js-embed-countdown

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...