0

Let's say I wanted my bot to greet me at my birthday and have my bot greet me in a direct message every single year. How can we achieve it ?

Code :

const { Client, MessageEmbed, Intents } = require('discord.js');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

const d = new Date();
const minutes = d.getMinutes();

client.once('ready', () => {
    console.log('beep boop thirdy is online');
    }
)

client.on('messageCreate', message => {

    
    if(message.content === '!dm')
    client.users.fetch('471298666586570762', false).then((user) => {
        
            user.send("test")
              
      
        });
        

})

client.login('token');
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
Yoshi
  • 1
  • 1

1 Answers1

0

This can be achieved by an answer in this post, You can use a similar method but you will need to account for a possible leap year every 4 years You also need to update the datestr1 each year after a check and save it in a database

const datestr1 = "7/13/2015"
const datestr2 = "7/13/2016"
const date1 = new Date(datestr1);
const date2 = new Date(datestr2);
const diffTime = Math.abs(date2 - date1);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
const currdateyear = parseInt(datestr2.substring(datestr2.length - 4))
if (currdateyear % 4 === 0) {
  if (diffDays === 366) console.log("happy bday")
} else if (diffDays === 365) console.log("happy bday")
Arnav Mishra
  • 488
  • 2
  • 15
  • also should i put the code on ready or client on messageCreate or do i put it somewhere different for the messsage to be sent every year?? – Yoshi Mar 01 '22 at 06:53
  • in the ready even would suffice tbh any other place besides any event except ready – Arnav Mishra Mar 01 '22 at 12:03
  • so if i replace my bday date on the datestr 1 and 2 theoretically it should send me the message every year. What happens if i dont replace datestr1 every year?? (sorry if i have too many questions im just a beginner and i dont really understand the code you've given me but it did work when i tested it so thank you still) – Yoshi Mar 01 '22 at 13:37
  • datestr2 must be the current date. – Arnav Mishra Mar 01 '22 at 13:56
  • oh so maybe i could do is make a variable that gets the current date and update it every few seconds then replace the date in datestr2 with the variable that i made if that makes sense. If that works somehow then how do i update the the variable every few seconds as i mentioned before? – Yoshi Mar 02 '22 at 14:21
  • Using a setInterval() and isn't checking every few seconds bit too excessive? considering it will only happen once per year – Arnav Mishra Mar 02 '22 at 15:22
  • hi so i tried to make a code that theoretically would send me a message everytime it passes by that certain date. `client.on('ready', () => { console.log('beep boop thirdy is online'); setInterval(() => { const d = new Date(); const hr = d.getMonth(); console.log(hr); if(hr == 2){ client.users.fetch('471298666586570762', false).then((user) => { user.send('test'); }); } }, 5000) } )` @Arnav Mishra is this correct? – Yoshi Mar 03 '22 at 02:45