-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (65 loc) · 2.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const fs = require('node:fs');
const { Client, Collection, Intents, MessageEmbed } = require('discord.js');
const config = require('./config.json');
const path = require('path');
const cron = require('node-cron');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
}
// Scheduler options
const cronSchedule = (config.moodle.schedule && cron.validate(config.moodle.schedule)) ? config.moodle.schedule : "0 0 6,18 * *";
console.log(`Starting scheduler using cron expression: ${cronSchedule}`)
const scheduler = cron.schedule(cronSchedule, () => client.emit('scheduledGetAssignments', client),{scheduled: false, timezone: 'Europe/Budapest'},false,'Europe/Budapest')
// Login and setup
// + Start Scheduler
client.once('ready', () => {
scheduler.start();
//client.emit('scheduledGetAssignments',client);
console.log('Ready! I\'m active as of ' + client.readyAt + ', as ' + client.user.tag);
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
try{
await interaction.reply({ embeds: [
new MessageEmbed()
.setColor(config.colors.red)
.setTitle('Oh no! An error occurred!')
.setTimestamp()
.setDescription(`I'm so sorry, an error has occured!`)
.setFooter({ text: `Please try again later. ${error}` })
], ephemeral: true });
}
catch(INTERACTION_ALREADY_REPLIED){
await interaction.editReply({ embeds: [
new MessageEmbed()
.setColor(config.colors.red)
.setTitle('Oh no! An error occurred!')
.setTimestamp()
.setDescription(`I'm so sorry, an error has occured!`)
.setFooter({ text: `Please try again later. ${error}` })
], ephemeral: true });
}
}
});
client.login(config.discord.token);