Calculating dates (Discord.js)

Asked

Viewed 48 times

-2

I’m trying to develop a kind of Mod record for my Discord Bot.

Ps: I do not believe it is necessary knowledge of Discord.js to solve the question

How it would work: The user with administrator permissions would review modstats, and the bot would return a/embed table with the information of when was the last time a moderator used a command ban/mute/kick/warn (are already ready) as follows:

warns (última semana): [número]
warns (último mês): [numero]
warns (ano inteiro): [numero]

I was wondering how to get the bot to record the day the last warning was made, and calculate the time interval between the previous one. I thought of something like:

const warn = require('link_do_arquivo_de_warn.js')
const hoje = new Date(); // Data de hoje
const ultimaVez = new Date('2014-07-07'); // Outra data no passado

let calcular = new CalcularIntevaloDeDatas() // função de calcular que eu não faço muita ideia de como montar

if (message.content == warn) //caso algum mod dê um warn

{ 
db.data.banidos.push(hoje)
db.data.banidos[0]            //salva a data de hoje no banco de dados (essa parte não é muito importante. Estou usando um banco chamado LOWDB)
}

if (message.content == "modstats") { return calcular }

console.log(calcular)

But the point is I don’t know how to calculate the difference between the dates saved, I mean, assuming the moderator gave a warn 3 days ago (warn 1), and one day today (warn 2). How do I let the bot know there were 2 Arns a week ago? Then I apply the same logic to the month and year...

Note: In addition to calculating dates, I must also save warn 2 as the last date, and warn 1 as the penultimate (that it is no longer saved as the last date), so I can calculate the next warn from warn 2 - and so on.

  • This answers your question? Difference between dates

  • It does not answer, because I also need to know how I can save the last date as the last, the last as the penultimate, and so on, automatically....

1 answer

0

Just create a array, since you say there are only three possibilities. Then use the solutions already mentioned in the comments.


let r = {
  "semana": 0,
  "mes": 0,
  "sempre": 0
};

let now = new Date();

let dates = [
  now - (1000 * 60 * 60 * 24), // semana
  now - (1000 * 60 * 60 * 24 * 6), // semana
  now - (1000 * 60 * 60 * 24 * 8), // mes 
  now - (1000 * 60 * 60 * 24 * 34), // sempre
  now - (1000 * 60 * 60 * 24 * 100), //sempre
  now - (1000 * 60 * 60 * 24 * 130), //sempre
];

for (i = 0; i < dates.length; i++) {
  diff = (Math.floor((now - dates[i]) / (1000 * 60 * 60 * 24)));
  let m = "semana";
  switch (true) {
    case diff > 30:
      m = "sempre";
      break;
    case diff > 7:
      m = "mes";
      break;
  }
  r[m]++;
}

console.log(r);

  • In this case, "now" is the "today" I put on top, right? ?

  • There is no reason for this, if you already have the date list, just compare the date list. Or, details are missing in your question. What it asks is "[...] how to calculate the difference between saved dates, I mean, assuming the moderator gave a warning 3 days ago (warn 1), and gave another one today (warn 2). How do I let the bot know that there were 2 Warns a week ago? Then I apply the same logic to the month and year...". In my example, the dates is the list of "warn", which will have the dates (of warn1 and warn2). If you want to ignore old dates, just check if the previous date is larger than the new one, it is the same logic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.