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

233
Views
Is There a Way To Store a Timer In The DataBase Discord js

im kind of a rookie at coding and I don't know how to store something into the database, spefically I need to store a timer in the database. Basically, the thing I have has a timer so the user can't use a certain thing again until that timer is off. I need the timer to be in the database so if the bot goes offline the timers will be saved and not reset/the user being able to use that thing again. I am currently running 9the bot on discord v13.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

There are various db's for you to achieve this

Here are few:

• Quick.db

• MongoDB

Read docs and you're all good to go

The below is an example of quick.db

const db = require('quick.db')
db.set(`foo`, "bar")//An example on how to set

//You can set the timestamp and the userid, read docs to help yourself out there 

const db = require('quick.db')
const ms = require('ms')
const prefix = "!"
client.on("messageCreate", async message=>{
    if(message.content.startsWith('!disable')){
        let target = message.mentions.users.first()//Getting the user who was mentioned
        if(!target) return; //If no user is mentioned return
        target = target.id//User id of the mentioned user
        let args = message.content.slice(prefix.length).trim().split(' ')
        args.shift()
        
        
        let duration = ms(args[1])
        if(isNaN(duration)) return message.reply({content: `Not a valid duration`})
        await db.set(`disabled_${target}`, duration)//Setting the user and the timestamp in the db

    }
})
//Checking on start
client.on("ready", ()=>{
    setInterval(()=> {
    let all = db.all()
    all = all.filter(data => data.ID.startsWith(`disabled_`))
    for(var i in all){
        if(Date.now() > all[i].data) { //Checking if it's the time to remove the user as disabled
            let id = all[i].ID.split('_')[1]
            
            db.delete(`disabled_${id}`)//Removing the user from the database
    console.log(`Removed user with ID ${id}`)

        }
    }
}, 5000)//checking every 5 seconds
})
})

This is just for your reference, the above can be simplified if you use slash commands

Incase you opt for legacy/slash commands, do use a commandhandler, there is an example for the same in the discord.js guide to have everything well organised

about 4 years ago · Santiago Trujillo Report
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!