0

I am getting back into javascript and coding and stuff because I have a passion project I wanna work on for a guild in a game I play. So I wanna figure out how I could get my bot to send a message at a specific time every day and then delete it after 5 hours. I've tried multiple things and its either two things that don't work well together or something that just doesn't work outright.

  • Possible duplicate of [Send message and shortly delete it](https://stackoverflow.com/questions/46907207/send-message-and-shortly-delete-it) – Lorddirt Nov 19 '19 at 09:04
  • Flagged as duplicate of https://stackoverflow.com/questions/46907207/send-message-and-shortly-delete-it/46918813. Answer in that question is to return a "then" promise after a timeout. – Lorddirt Nov 19 '19 at 09:04
  • `message.channel.send("Your message").then(m => m.delete(time in milliseconds))` – Cursed Nov 19 '19 at 10:26
  • Hi, welcome to SO. Please when asking questions, make sure that you provide whatever solution you have tried already. For any more reference please check [MCVE]. – Barrosy Nov 19 '19 at 10:32

1 Answers1

2

Set an interval to check the time and then send the message when the time is right

Set up a timer to call a function periodically. We'll also set up a variable to store the date's day, this is to avoid sending a second message in the same day

var day = 0;
setInterval(tick, 30000);

function tick() {};

The function will get today's date (and time) and compare it to see if it matches with the desired time, if it does, it will send the message and delete it after 5 hours. It will also store the day into our day variable to prevent it from sending it again in the same day.

function tick() {
  let now = new Date();
  if (day === now.getDate()) return;

  if (now.toISOString().slice(11,16) === "14:30") { // 2:30PM UTC
    day = now.getDate();

    channel.send("This message will self-destruct in 5 hours.")
    .then(msg => msg.delete(1000*60*60*5));
    .catch(e => /* handle error */);
  }
}

Notes:

  • I've used now.toISOString().slice(11,16) to get the time, which would be equivalent to just using now.getHours() + ":" + now.getMinutes(). (Taken from this answer)
  • Use Promises to get the message after sending it and call .delete() with the number of milliseconds to wait before deleting, like .delete(1000*60*60*5). (Taken from this answer)
  • If you'd rather not use UTC, you may want to use things like moment-timezone, but then you may also want to use moment.js objects instead of vanilla JS date objects
luxmiyu
  • 585
  • 2
  • 13
  • Thank you! I appreciate the help so much! Also no UTC works great since the game I'm using it for runs off of UTC. – Felinephenom Nov 19 '19 at 17:00
  • But also I have a question how would I input the id to get it to run in a specific channel? – Felinephenom Nov 19 '19 at 17:12
  • Would I just change the channel.send to client.channel.get("Channels id").send("the message I want to send") – Felinephenom Nov 19 '19 at 17:29
  • also maybe I'm just forgetting something from when I used to code a lot more often but every time I try and insert that code I get: [90m at Module._compile (internal/modules/cjs/loader.js:718:23)[39m [90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)[39m [90m at Module.load (internal/modules/cjs/loader.js:641:32)[39m [90m at Function.Module._load (internal/modules/cjs/loader.js:556:12)[39m [90m at Function.Module.runMain (internal/modules/cjs/loader.js:837:10)[39m [90m at internal/main/run_main_module.js:17:11[39m – Felinephenom Nov 19 '19 at 17:59