I'm currently creating a discord bot. One of the functions that I want it to have is to send 'scheduled messages', for which admins of that guild will be able to set the time they'll get sent.
I am using enmap
for a per-server configuration system and cron
for the scheduled message.
Here is my code so far:
bot.guilds.forEach((guild) => {
let banquetTime;
banquetTime = bot.settings.get(guild.id, "banquetTime");
cron.schedule(banquetTime, () => {
bot.settings.ensure(guild.id, defaultSettings);
let banquetChannel = bot.settings.get(guild.id, "banquetChannel");
let banquetMessage = bot.settings.get(guild.id, "banquetMessage");
guild.channels
.find(channel => channel.name === banquetChannel)
.send(banquetMessage)
.catch(console.error);
});
}, {
scheduled: true,
timeZone: 'America/Los_Angeles'
});
When I run this, nothing happens. Does anyone know why?
I have another function where the .forEach()
runs INSIDE the cron.schedule
, and it works, but I'm not sure why when the cron.schedule
is inside .forEach()
, it doesn't work.
Here is the code where it works:
cron.schedule('00 45 12,20 * * *', () => {
bot.guilds.forEach((guild) => {
bot.settings.ensure(guild.id, defaultSettings);
let expoChannel = bot.settings.get(guild.id, "expoChannel");
let expoMessage = bot.settings.get(guild.id, "expoMessage");
guild.channels
.find(channel => channel.name === expoChannel)
.send(expoMessage)
.catch(console.error);
});
});
Now, I can't run the .forEach()
inside the cron for the top function because I need to grab the variable banquetTime
from the .forEach(guild)
first or else I won't be able to use it in cron.