0

Description: Rename the channel every 5 seconds creating Server Time

This code works at first then It won't update after

module.exports = async (client) => {
    const guild = client.guilds.cache.get(Guild_ID);
    const channel = guild.channels.cache.get(Channel_ID);
    setInterval(() => {
        var date = new Date();
        var hours = date.getHours();
        var minutes = date.getMinutes();
        
        channel.setName(`Server Time:${hours}:${minutes}`)
        console.log(hours)
        console.log(minutes)
    }, 5000);
}

Why does it not update after quite sometime?

  • Are you sending the server time every 5 seconds? It's difficult to find your error if you don't include some sort of description of what you are attempting to do. – Joe Moore Feb 27 '21 at 10:20
  • I am sorry, yeah the goal was to send it every 5 seconds – Aegis Zero Feb 27 '21 at 10:22

1 Answers1

1

You seem to be missing the looping feature of your function.

setInterval(() => {
 message.channel.send(`hy`).then(() => count++);
}, 10000);

The count++ means that the function will repeat incrementally.

Please note, setInterval() is highly inaccurate and it is likely that after a period of time your bot will fall out of sync with the actual flow of time.

Please look at this interesting post on the subject of timers.

Joe Moore
  • 2,031
  • 2
  • 8
  • 29
  • I put the console.log to track the data and it seems that the getHours and getMinutes is on point. But the update to rename the channel is not responding – Aegis Zero Feb 27 '21 at 11:10
  • hmm, try fetching the channel inside the loop, and then run error trapping like `if (!channel) console.log("cannot locate channel")` or something similar – Joe Moore Feb 27 '21 at 11:57