-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
156 lines (132 loc) · 5.56 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits, ActivityType, REST, Routes } = require('discord.js');
const { token, clientId, guildId, reminderchannel, activityText } = require('./config.json');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
const commands = [];
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
console.log(`[INFO] Loaded command ${command.data.name} from ${filePath}.`)
client.commands.set(command.data.name, command);
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
// Construct and prepare an instance of the REST module
const rest = new REST({ version: '10' }).setToken(token);
(async () => {
try {
console.log(`-> Started refreshing ${commands.length} application (/) commands.`);
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);
console.log(`-> Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
})();
client.on("ready", () => {
client.user.setPresence({
activities: [{ name: activityText, type: ActivityType.Watching }],
});
console.log(`Logged in as ${client.user.tag}!`);
// Check if any reminders are due
const reminders = require('./reminders.json');
const now = new Date();
for (const reminder of reminders.Reminders) {
const reminderDate = new Date(reminder.timestamp);
if (reminderDate < now) {
console.log("[INFO] Reminder Was Due Before Bot Started. Removing Reminder...");
// Remove the reminder from the list
const index = reminders.Reminders.indexOf(reminder);
if (index > -1) {
reminders.Reminders.splice(index, 1);
}
}
}
fs.writeFileSync('./reminders.json', JSON.stringify(reminders, null, 2));
// Set up the reminder interval
setInterval(() => {
// Check if any reminders are due
const reminders = require('./reminders.json');
const now = new Date().getTime();
for (const reminder of reminders.Reminders) {
const reminderDate = reminder.timestamp;
// Check for reminders happening now
if (reminderDate < now && !reminder.reminders[0].now) {
client.channels.cache.get(reminderchannel).send(`<@&${reminder.roleid}> Meeting is Starting Now!`);
reminder.reminders[0].now = true;
// Remove the reminder from the list
const index = reminders.Reminders.indexOf(reminder);
if (index > -1) {
reminders.Reminders.splice(index, 1);
}
}
// Check for reminders happening in 5 minutes
else if (reminderDate < now + 300000 && !reminder.reminders[0].five) {
client.channels.cache.get(reminderchannel).send(`<@&${reminder.roleid}> Meeting in 5 minutes!`);
reminder.reminders[0].five = true;
// Set all others to true so they don't get sent
reminder.reminders[0].fifteen = true;
reminder.reminders[0].thirty = true;
reminder.reminders[0].hour = true;
reminder.reminders[0].day = true;
}
// Check for reminders happening in 15 minutes
else if (reminderDate < now + 900000 && !reminder.reminders[0].fifteen) {
client.channels.cache.get(reminderchannel).send(`<@&${reminder.roleid}> Meeting in 15 minutes!`);
reminder.reminders[0].fifteen = true;
// Set all others to true so they don't get sent
reminder.reminders[0].thirty = true;
reminder.reminders[0].hour = true;
reminder.reminders[0].day = true;
}
// Check for reminders happening in 30 minutes
else if (reminderDate < now + 1800000 && !reminder.reminders[0].thirty) {
client.channels.cache.get(reminderchannel).send(`<@&${reminder.roleid}> Meeting in 30 minutes!`);
reminder.reminders[0].thirty = true;
// Set all others to true so they don't get sent
reminder.reminders[0].hour = true;
reminder.reminders[0].day = true;
}
// Check for reminders happening in a hour
else if (reminderDate < now + 3600000 && !reminder.reminders[0].hour) {
client.channels.cache.get(reminderchannel).send(`<@&${reminder.roleid}> Meeting in 1 hour!`);
reminder.reminders[0].hour = true;
// Set all others to true so they don't get sent
reminder.reminders[0].day = true;
}
// Check for reminders happening in a day
else if (reminderDate < now + 86400000 && !reminder.reminders[0].day) {
client.channels.cache.get(reminderchannel).send(`<@&${reminder.roleid}> Meeting in 1 day!`);
reminder.reminders[0].day = true;
}
}
fs.writeFileSync('./reminders.json', JSON.stringify(reminders, null, 2));
}
, 60000);
});
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
client.login(token)