0

How do I make it send a message to a specific user at a specific time? The message will be sent via DMs, not channel, not tagging. Basically, I want the bot to DM an user (for example user id: 163640941431881728) a message (for example: hi) at a certain system time (for example 2PM).

Code so far:

const Discord = require('discord.js');
const bot = new Discord.Client();
var currentHour = new Date().getHours();
var currentMin = new Date().getMinutes();

bot.on('message', (message) => {

    if(message.content == '~~test') {
        message.channel.send(currentHour + ':' + currentMin);
    }

});
James Westgate
  • 11,306
  • 8
  • 61
  • 68
Andrew
  • 1
  • 1
  • 3

1 Answers1

0

Assuming your bot will be running continuously, you will probably do this using a standard setTimeout call. Simply calculate the millisecond difference between the current time and the time you want the message to be sent -- new Date().getTime() - scheduledTime

If you want the ability to cancel the message later, you should store the value returned by setTimeout which is that timer's ID. You can then pass it to clearTimeout to stop it. Example on W3Schools.

If your bot needed to restart for some reason, you'd need to find a way to persist your list of scheduled messages to disk and load them when starting up.

Community
  • 1
  • 1
BoffinBrain
  • 6,337
  • 6
  • 33
  • 59
  • The deal is I want it at a time when I will *not* be awake, hence why I'm doing this. – Andrew Dec 21 '17 at 17:40
  • It's more about the bot being awake! As I mentioned, if the bot is not going to be running continuously, you'll need to write your reminders to a file and load them up when you restart it. You'd then use the same method to calculate how far in advance to set the timeout. If you really care about the persistence factor then you would've needed to specify that in the original question. Looks like it was marked as a duplicate now. – BoffinBrain Dec 22 '17 at 10:20