so there is an answer for this:
There are two ways to do that, with cron (or something else on different platforms) and setInterval
1) Cron
Create a new file, goodmorning.js
with this:
const Discord = require('discord.js');
const client = new Discord.Client();
client.login("token").then(() => {
console.log("I am ready");
var guild = client.guilds.get('guildid');
if(guild && guild.channels.get('channelid')){
guild.channels.get('channelid').send("Good Morning").then(() => client.destroy());
} else {
console.log("nope");
//if the bot doesn't have guild with the id guildid
// or if the guild doesn't have the channel with id channelid
}
client.destroy();
});
(edit all the needed values: token, guildid and channelid)
And add a cronjob to run everyday at 8am.
This script will attempt to login into Discord and after successful login proceeds to find a guild and a channel, then just send the message, then finally logout (client.destroy()
). If it wasn't found a guild or channel, just simply destroy.
2) setInterval
The first problem with this would be that you need to start the script at the exact time you want the code to run, or get a setTimeout to start the setInterval to repeat the code over and over.
untested but should work with possibly some tweaking needed:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('message', message => {
//...
});
client.on('ready', () => {
setTimeout(function(){ // in leftToEight() milliseconds run this:
sendMessage(); // send the message once
var dayMillseconds = 1000 * 60 * 60 * 24;
setInterval(function(){ // repeat this every 24 hours
sendMessage();
}, dayMillseconds)
}, leftToEight())
})
function leftToEight(){
var d = new Date();
return (-d + d.setHours(8,0,0,0));
}
function sendMessage(){
var guild = client.guilds.get('guildid');
if(guild && guild.channels.get('channelid')){
guild.channels.get('channelid').send("Good Morning");
}
}
client.login("token");
I would definitely go for the cron option, doesn't require you to have the process running all the time (unless you already have it)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…