-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
127 lines (103 loc) · 4 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
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
const { Client, GatewayIntentBits, Collection, REST, Routes } = require('discord.js');
const fs = require('fs');
const path = require('path');
require('dotenv').config();
const { initializeServices, getEmbedInfo } = require('./database/database');
const { startEmbedUpdates } = require('./commands/statuspanel');
const config = require('./config.json');
const { monitorServices } = require('./utils/statusChange');
const { updateActivity } = require('./utils/activity');
initializeServices(config.services);
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
function loadCommands(dir) {
const files = fs.readdirSync(dir, { withFileTypes: true });
for (const file of files) {
const fullPath = path.join(dir, file.name);
if (file.isDirectory()) {
loadCommands(fullPath);
} else if (file.isFile() && file.name.endsWith('.js')) {
const command = require(fullPath);
if (command.data && command.data.name) {
client.commands.set(command.data.name, command);
console.log(`Loaded command: ${command.data.name}`);
} else {
console.error(`Command file ${fullPath} is not properly formatted.`);
}
}
}
}
const commandsPath = path.join(__dirname, 'commands');
loadCommands(commandsPath);
async function deployCommands() {
const commands = [];
client.commands.forEach(command => {
if (command.data && typeof command.data.toJSON === 'function') {
commands.push(command.data.toJSON());
}
});
const rest = new REST({ version: '10' }).setToken(process.env.TOKEN);
try {
console.log('Started registering slash commands.');
if (config.guildId) {
await rest.put(
Routes.applicationGuildCommands(config.clientId, config.guildId),
{ body: commands }
);
} else {
await rest.put(
Routes.applicationCommands(config.clientId),
{ body: commands }
);
}
console.log('Successfully registered slash commands.');
} catch (error) {
console.error('Error registering slash commands:', error);
}
}
deployCommands();
client.once('ready', async () => {
console.log(`${client.user.tag} is online!`);
getEmbedInfo((err, embedInfo) => {
if (err || !embedInfo) {
console.warn('No embed info found or error retrieving it:', err?.message || 'No data');
return;
}
startEmbedUpdates(client, embedInfo.channel_id, embedInfo.message_id);
});
monitorServices(client)
updateActivity(client);
});
client.on('interactionCreate', async (interaction) => {
if (interaction.isAutocomplete()) {
const command = client.commands.get(interaction.commandName);
if (command && command.autocomplete) {
try {
await command.autocomplete(interaction);
} catch (error) {
console.error('Error in autocomplete:', error);
}
}
}
if (!interaction.isCommand()) return;
const command = 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 executing command ${interaction.commandName}:`, error);
await interaction.reply({
content: 'There was an error executing this command!',
ephemeral: true,
});
}
});
if (!process.env.TOKEN || process.env.TOKEN === '<YOUR-TOKEN-HERE>') {
console.error('Error: Invalid token. Please set a valid token in the .env file.');
process.exit(1);
}
client.login(process.env.TOKEN);
require("./webserver/webserver.js");