I’m working on a Discord bot using Discord.js Node V12 and I want to show how long the bot has been active in its status. I’ve been searching for a way to display the elapsed time since the bot started playing a game or doing any activity, but I haven’t found any helpful info. Here’s what I’ve got so far:
This sets the bot’s status, but I’m not sure how to add a dynamic timestamp. Any ideas on how to implement this feature? I’d really appreciate some guidance on updating the status with the bot’s uptime.
I’ve implemented this feature in my bot recently. Here’s what worked for me:
First, store the bot’s start time when it initializes. Then, use setInterval to update the status periodically. Calculate the uptime by subtracting the start time from the current time.
You’ll need a function to format the uptime into a readable string. Something like:
I’ve tackled this issue in my projects before. One effective approach is to use the moment.js library for time calculations and formatting. It simplifies the process significantly.
First, install moment.js with npm. Then, in your code:
This updates every minute, which is a good balance between accuracy and performance. Remember to handle potential disconnects by resetting the startTime if needed. Also, consider adding error handling to ensure your bot doesn’t crash if setting the activity fails for any reason.
hey there! i’ve done something similar before. you could use the setInterval() function to update the status every minute or so. calculate the uptime by subtracting the bot’s login time from the current time, then format it nicely. heres a rough idea: