Files
src
.gitignore
config.json
package.json
README.md
Code
14 793
src/index.js
const { Client, GatewayIntentBits, Partials } = require('discord.js');
const config = require('./config');
const logger = require('./logger');
const permissions = require('./permissions');
const { parseArgs, truncate } = require('./utils');
const utility = require('./utility');
const moderation = require('./moderation');
const tickets = require('./tickets');
const permissionCommands = require('./permissions-command');
if (!config.token) {
console.error('DISCORD_TOKEN est manquant dans le fichier .env.');
process.exit(1);
}
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildBans,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates
],
partials: [Partials.Message, Partials.Channel, Partials.GuildMember, Partials.User]
});
const commandList = [
...utility.createCommands(),
...moderation.createCommands(),
...tickets.createCommands(),
...permissionCommands.createCommands()
];
const commands = new Map();
for (const command of commandList) {
commands.set(command.name, command);
for (const alias of command.aliases || []) commands.set(alias, command);
}
client.once('ready', () => {
console.log(`Bot prêt : ${client.user.tag} | préfixe ${config.prefix}`);
client.user.setActivity(`${config.prefix}help`, { type: 0 });
});
client.on('messageCreate', async (message) => {
if (message.author.bot || !message.guild) return;
await logger.logMessage(message);
if (!message.content.startsWith(config.prefix)) return;
const content = message.content.slice(config.prefix.length).trim();
if (!content) return;
const args = parseArgs(content);
const name = args.shift().toLowerCase();
const command = commands.get(name);
if (!command) return;
if (command.permission && !(await permissions.requirePermission(message, command.permission))) {
await logger.logEvent(message.guild, 'PERMISSION DENIED', `${message.author.tag} a tenté la commande ${config.prefix}${name}`, message.author);
return;
}
try {
await command.execute({ message, args, client, commands: commandList, commandName: name });
await logger.logEvent(message.guild, 'COMMAND', `${message.author.tag} : ${truncate(message.content, 500)}`);
} catch (error) {
console.error(`Erreur commande ${name}:`, error);
await logger.logEvent(message.guild, 'ERROR', `Commande ${name} : ${error.stack || error.message}`);
if (!message.replied && !message.deferred) await message.reply('Une erreur est survenue pendant l’exécution de la commande.');
}
});
client.on('interactionCreate', async (interaction) => {
try {
await tickets.handleInteraction(interaction);
} catch (error) {
console.error('Erreur interaction:', error);
if (!interaction.replied && !interaction.deferred) await interaction.reply({ content: 'Une erreur est survenue.', ephemeral: true });
}
});
client.on('messageDelete', async (message) => {
if (message.guild && !message.author?.bot) await logger.logEvent(message.guild, 'MESSAGE DELETE', `#${message.channel?.name || 'inconnu'} — ${message.author?.tag || 'inconnu'}\n${truncate(message.content || '[sans texte]', 1800)}`);
});
client.on('messageUpdate', async (oldMessage, newMessage) => {
if (!newMessage.guild || newMessage.author?.bot || oldMessage.content === newMessage.content) return;
await logger.logEvent(newMessage.guild, 'MESSAGE EDIT', `#${newMessage.channel?.name || 'inconnu'} — ${newMessage.author?.tag || 'inconnu'}\nAvant : ${truncate(oldMessage.content, 800)}\nAprès : ${truncate(newMessage.content, 800)}`);
});
client.on('guildMemberAdd', (member) => logger.logEvent(member.guild, 'MEMBER JOIN', `${member.user.tag} (${member})`));
client.on('guildMemberRemove', (member) => logger.logEvent(member.guild, 'MEMBER LEAVE', `${member.user?.tag || member.id}`));
client.on('guildMemberUpdate', (oldMember, newMember) => {
if (oldMember.nickname !== newMember.nickname) logger.logEvent(newMember.guild, 'MEMBER UPDATE', `${newMember.user.tag} — pseudo : ${oldMember.nickname || 'aucun'} → ${newMember.nickname || 'aucun'}`);
});
client.on('guildBanAdd', (ban) => logger.logEvent(ban.guild, 'BAN ADD', `${ban.user.tag}`));
client.on('guildBanRemove', (ban) => logger.logEvent(ban.guild, 'BAN REMOVE', `${ban.user.tag}`));
client.on('channelCreate', (channel) => channel.guild && logger.logEvent(channel.guild, 'CHANNEL CREATE', `${channel}`));
client.on('channelDelete', (channel) => channel.guild && logger.logEvent(channel.guild, 'CHANNEL DELETE', `#${channel.name}`));
client.on('channelUpdate', (oldChannel, newChannel) => newChannel.guild && oldChannel.name !== newChannel.name && logger.logEvent(newChannel.guild, 'CHANNEL UPDATE', `${oldChannel.name} → ${newChannel.name}`));
client.on('roleCreate', (role) => logger.logEvent(role.guild, 'ROLE CREATE', `${role}`));
client.on('roleDelete', (role) => logger.logEvent(role.guild, 'ROLE DELETE', `${role.name}`));
client.on('roleUpdate', (oldRole, newRole) => newRole.guild && oldRole.name !== newRole.name && logger.logEvent(newRole.guild, 'ROLE UPDATE', `${oldRole.name} → ${newRole.name}`));
client.on('voiceStateUpdate', (oldState, newState) => {
if (!newState.guild || oldState.channelId === newState.channelId) return;
const action = newState.channelId ? 'VOICE JOIN' : 'VOICE LEAVE';
logger.logEvent(newState.guild, action, `${newState.member?.user.tag || newState.id} — ${newState.channel?.name || oldState.channel?.name || 'inconnu'}`);
});
client.on('error', (error) => console.error('Discord client error:', error));
client.on('warn', (warning) => console.warn('Discord warning:', warning));
client.login(config.token);