How to make bot send message on specific date

by Best Movies   Last Updated June 30, 2020 02:26 AM

i'm trying to make my discord bot send message on specific date like a birthday date i'm new to (Node.js) or (Discord.js)

this is what im trying to do

 client.on("ready", async () => {
  var d = new Date();
if(d.toLocaleDateString()=="7/1/2020")
birthdaychannel.send("birthday test")
 })

i know everything is wrong because i have no idea how to do it but is there any code similar to what im trying to do?



Answers 1


If you want to execute code at a certain date, you'll need to ensure that either

A) Your program is running forever. Run your bot indefinitely on a server, for example, and then check the date every few seconds, and send the message on the right date. Or,

B) You can configure your computer to start your program at a certain date and just program the bot to send the message and end. CRON Job is the search term you want here. Of course, your computer still needs to be running to launch your program.

To help you with actual discord.js API here's an example to send a user a DM

const { Client } = require("discord.js");

const token = process.env.token;
const client = new Client();

client.login(token);


client.on("ready", async () => {
   const id = "3474477473288573848"; // Discord User IDs look like a long string of random numbers

  const user = await client.users.fetch(id);

  // Create/access a DM thread between the bot account and the user
  const dms = await user.createDM();

  dms.send("Happy Birthday!");

});

MayorMonty
MayorMonty
June 30, 2020 01:12 AM

Related Questions


TypeError: client.guilds is not iterable?

Updated May 28, 2020 21:26 PM

discord.js args default separated by ,

Updated October 04, 2019 22:26 PM