Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

194
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda