Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
151 lines (138 loc) · 5.07 KB
/
Copy pathindex.js
File metadata and controls
151 lines (138 loc) · 5.07 KB
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
const fs = require("fs");
const path = require("path");
const {
Client,
GatewayIntentBits,
Collection,
REST,
Routes,
} = require("discord.js");
require("dotenv").config({ path: path.resolve(__dirname, ".env") });
// Importer et attendre l'initialisation de la base de données
const { initializeDatabase } = require("./utils/dbInit");
const BanExpirationHandler = require("./utils/banExpirationHandler");
const SimpleModerationSystem = require("./utils/simpleModerationSystem");
const ConfigManager = require("./utils/configManager");
(async () => {
try {
// Attendre l'initialisation de la base (création des tables, etc.)
await initializeDatabase();
// Créez une instance du bot
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
// Collection des commandes
client.commands = new Collection();
const commands = [];
// Charger toutes les commandes depuis le dossier "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);
if (command.data && command.data.name) {
client.commands.set(command.data.name, command);
commands.push(command.data.toJSON());
console.log(
`📜\x1b[38;5;4m Chargement de la commande ${command.data.name} \x1b[0m`,
);
} else {
console.error(
`⚠️ \x1b[38;5;1m Erreur: La commande dans le fichier ${file} est invalide ou n'a pas de nom. \x1b[0m`,
);
}
}
// Charger les événements
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, client));
console.log(
`📜\x1b[38;5;5m Chargement de l'événement ${event.name} (once) \x1b[0m`,
);
} else {
client.on(event.name, (...args) => event.execute(...args, client));
console.log(
`📜\x1b[38;5;5m Chargement de l'événement ${event.name} \x1b[0m`,
);
}
}
// Préparer l'API REST avec le token du bot
const rest = new REST({ version: "10" }).setToken(
process.env.DISCORD_TOKEN,
);
const clientId = process.env.CLIENT_ID;
// Récupérer les commandes actuellement enregistrées sur Discord
let registeredCommands = [];
try {
registeredCommands = await rest.get(Routes.applicationCommands(clientId));
} catch (error) {
console.error(
"⚠️\x1b[38;5;1m Erreur lors de la récupération des commandes enregistrées: \x1b[0m",
error,
);
}
// Supprimer les commandes enregistrées qui ne sont plus présentes localement
for (const registeredCommand of registeredCommands) {
if (!client.commands.has(registeredCommand.name)) {
try {
await rest.delete(
`${Routes.applicationCommands(clientId)}/${registeredCommand.id}`,
);
console.log(
`📍\x1b[38;5;3m Suppression de la commande obsolète: ${registeredCommand.name} \x1b[0m`,
);
} catch (error) {
console.error(
`⚠️\x1b[38;5;1m Erreur lors de la suppression de la commande ${registeredCommand.name}: \x1b[0m`,
error,
);
}
}
}
// Enregistrer (ou mettre à jour) les commandes auprès de Discord
try {
await rest.put(Routes.applicationCommands(clientId), { body: commands });
console.log(
"📩\x1b[38;5;2m Commandes enregistrées avec succès. \x1b[0m",
);
} catch (error) {
console.error(
"⚠️\x1b[38;5;1m Erreur lors de l'enregistrement des commandes: \x1b[0m",
error,
);
} // Lancer le bot
client.login(process.env.DISCORD_TOKEN);
// Initialiser les systèmes après la connexion
client.once("ready", () => {
console.log("🤖 Bot connecté et prêt !");
// Initialiser le gestionnaire de configuration avec rechargement automatique
const configManager = ConfigManager.init(client);
client.configManager = configManager;
// Gestionnaire de bans expirés
const banExpirationHandler = new BanExpirationHandler(client);
banExpirationHandler.start();
// Système de modération simplifié
const simpleModerationSystem = new SimpleModerationSystem(client);
client.simpleModerationSystem = simpleModerationSystem;
});
} catch (err) {
console.error(
"⚠️\x1b[38;5;1m Erreur lors de l'initialisation de la base de données ou du bot:",
err,
);
process.exit(1);
}
})();