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

203
Views
How to check in an interaval if x time has passed since the creation of a channel in discord.js?

I've been having some hard time writing this and I searched a lot for any similar questions but didn't find any.

So I have this Schema:

new mongoose.Schema({
       Guild: String,
       Username: String,
       UserID: String,
       Channel: String,
       Type: String,
       Reason: String,
       Time: Date,
   }) 

And I want to automatically delete the channel after x time of its creation, the Time is saved as <channel>.createdTimestamp

And here is the condition of the event:

setInterval(async () => {
        hSchema.find().then((data) => {
            if (!data && !data.length) return
            
            data.forEach(async (value) => {
            const user = value.UserID;
            var time = value.Time; // time of the creation of channel
            var res = time.getTime(); // time in ms  

            // check if user is premium
            uSchema.findOne({ User: user}, async (err, userData) => {
                // if user is not premium
                if (!userData) {
                    const destruct = ms('1m'); // time to pass before deleting 
                    var date = res + destruct; 
                if () { // I can't figure out how to check if x time has passed since the creation of channel
                    // Do stuff here
                    const ch = data.Channel;
                    const channel = client.channels.cache.get(ch)
                     channel.delete()
                     value.delete()
                }
            }
            })
        })
    });
    }, ms("10 seconds"));

So as I said I just need some help on how to check if x ms has passed to use it in an if() statement!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use Date.now() to get the current time in milliseconds, then subtract whatever time you need (in milliseconds), and check that versus your created timestamp, if the number is greater it's been more than a minute!

const minuteInMs = 60000;

if (Date.now() - minuteInMs > channel.createdTimestamp) {
  console.log("more than a minute");
} else {
  console.log("less than a minute");
}
about 4 years ago · Juan Pablo Isaza Report

0

You can use moment to subtract the diff in seconds pretty easy.

var now = moment(new Date());
var end = moment(discordChannelCreationTimestamp);

var duration = moment.duration(now.diff(end));

var seconds = duration.asSeconds();
about 4 years ago · Juan Pablo Isaza 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!