Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
How to make Timed role command with database, so after bot restart, time won't reset?

I am making a timed role command. I know that if I use setInterveral(), it will be deleted after restarting bot. Therefore, I tried using quick.db to store user IDs and also the time into the database. I tried fetching db when a user type anything, it works, but what I want is that I don't need to type any command, if time ended, role will be removed from the user of database. Here is my code:

message.reply({ embeds: [
        new Discord.MessageEmbed()
        .setTitle(`Role added`)
        .setDescription(`You got the role for a 1 week`)
      ]})
      db.set(`role_${message.author.id}`, Date.now())
      let role = message.guild.roles.cache.find(role => role.id === '873283387144691723')
      message.member.roles.add(role)
      db.push(`role_${message.guild.id}`, { week:`${message.member.id}`})

index.js:

client.on('messageCreate', message => {
    let members = db.fetch(`role_${message.guild.id}`)
    if(members !== null && members.find(x => x.week === message.author.id).week) {
      let timeout = 6048;
      let duration = db.fetch(`role_${message.author.id}`)
      
      if(timeout - (Date.now() - duration) <= 0) {
        let role = message.member.roles.cache.find(role => role.id === '873283387144691723')
        message.member.roles.remove(role)
      } else {
        let timeout = 6048;
        let date = db.fetch(`role_${message.member.id}`)
        message.channel.send(`${ms(timeout - (Date.now() - date), { long: true })}`)
      }
    } else {
      return;
    }
}

What should I do if I want the bot to auto remove the role after the time of users in database ended?

Note: I want to add multiple user in db also.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!