135 lines
5.6 KiB
JavaScript
135 lines
5.6 KiB
JavaScript
const { SlashCommandBuilder, PermissionFlagsBits, EmbedBuilder } = require('discord.js');
|
|
const db = require('../../functions/database/db.js');
|
|
|
|
module.exports = {
|
|
category: 'moderation',
|
|
data: new SlashCommandBuilder()
|
|
.setName('ban')
|
|
.setDescription('Bannir un membre du serveur.')
|
|
.addUserOption(option =>
|
|
option.setName('target')
|
|
.setDescription('Le membre à bannir')
|
|
.setRequired(true))
|
|
.addStringOption(option =>
|
|
option.setName('reason')
|
|
.setDescription('Raison du bannissement')
|
|
.setRequired(false))
|
|
.addStringOption(option =>
|
|
option.setName('duration')
|
|
.setDescription('Durée du ban (ex: 1h, 2d, laisser vide pour permanent)')
|
|
.setRequired(false))
|
|
.setDefaultMemberPermissions(PermissionFlagsBits.BanMembers),
|
|
|
|
async execute(interaction) {
|
|
const target = interaction.options.getUser('target');
|
|
const reason = interaction.options.getString('reason') || 'Aucune raison fournie';
|
|
const durationInput = interaction.options.getString('duration');
|
|
|
|
await interaction.deferReply({ ephemeral: true });
|
|
|
|
if (!target) {
|
|
return interaction.editReply({ content: '❌ Aucun utilisateur spécifié !' });
|
|
}
|
|
|
|
// Vérifier que l'utilisateur peut être banni
|
|
const member = await interaction.guild.members.fetch(target.id).catch(() => null);
|
|
if (member) {
|
|
if (member.roles.highest.position >= interaction.member.roles.highest.position && interaction.guild.ownerId !== interaction.user.id) {
|
|
return interaction.editReply({ content: '❌ Tu ne peux pas bannir cet utilisateur car il a un rôle supérieur ou égal au tien.' });
|
|
}
|
|
if (member.id === interaction.guild.ownerId) {
|
|
return interaction.editReply({ content: '❌ Tu ne peux pas bannir le propriétaire du serveur.' });
|
|
}
|
|
}
|
|
|
|
let type = 'Permanent';
|
|
let unbanDate = null;
|
|
let durationText = 'Permanent';
|
|
|
|
if (durationInput) {
|
|
const { parseDuration, formatDuration } = require('../../utils/helpers');
|
|
const durationMs = parseDuration(durationInput);
|
|
if (durationMs) {
|
|
unbanDate = Date.now() + durationMs;
|
|
type = 'Temporary';
|
|
durationText = formatDuration(durationMs);
|
|
}
|
|
}
|
|
|
|
try {
|
|
// Bannissement
|
|
await interaction.guild.bans.create(target.id, { reason: `Banni par ${interaction.user.tag}: ${reason}` });
|
|
|
|
// Stockage dans MySQL (bans)
|
|
await db.query(
|
|
`INSERT INTO bans (userId, reason, modId, timestamp, type, unbanDate, guildId)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
ON DUPLICATE KEY UPDATE reason=VALUES(reason), modId=VALUES(modId), timestamp=VALUES(timestamp), type=VALUES(type), unbanDate=VALUES(unbanDate)`,
|
|
[target.id, reason, interaction.user.id, Date.now(), type, unbanDate, interaction.guild.id]
|
|
);
|
|
|
|
await db.query(
|
|
`INSERT INTO logs (userId, userTag, modId, modTag, action, reason, type, guildId, timestamp)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
[target.id, target.tag, interaction.user.id, interaction.user.tag, 'Bannissement', reason, type, interaction.guild.id, Date.now()]
|
|
);
|
|
|
|
const { colors, emojis } = require('../../utils/constants');
|
|
const { sendLog } = require('../../utils/helpers');
|
|
|
|
const embed = new EmbedBuilder()
|
|
.setAuthor({
|
|
name: `${target.displayName}`,
|
|
iconURL: target.displayAvatarURL({ dynamic: true })
|
|
})
|
|
.setTitle(`${emojis.ban} Utilisateur Banni`)
|
|
.setColor(colors.ban)
|
|
.setThumbnail(target.displayAvatarURL({ dynamic: true }))
|
|
.setDescription(`${target.toString()} a été banni du serveur.`)
|
|
.addFields(
|
|
{ name: '👤 Utilisateur', value: `${target.toString()}\n\`${target.tag}\``, inline: true },
|
|
{ name: '👮 Modérateur', value: `${interaction.user.toString()}\n\`${interaction.user.tag}\``, inline: true },
|
|
{ name: '⏱️ Durée', value: `\`${durationText}\``, inline: true },
|
|
{ name: '📝 Raison', value: reason, inline: false },
|
|
{ name: '📅 Date', value: `<t:${Math.floor(Date.now() / 1000)}:F>`, inline: true },
|
|
{ name: '🏷️ Type', value: `\`${type}\``, inline: true }
|
|
)
|
|
.setFooter({ text: `ID: ${target.id} • ${interaction.guild.name}` })
|
|
.setTimestamp();
|
|
|
|
await interaction.editReply({ embeds: [embed] });
|
|
|
|
// Log dans le canal de logs
|
|
const logEmbed = new EmbedBuilder()
|
|
.setAuthor({
|
|
name: `${interaction.user.displayName}`,
|
|
iconURL: interaction.user.displayAvatarURL({ dynamic: true })
|
|
})
|
|
.setTitle(`${emojis.ban} Bannissement`)
|
|
.setColor(colors.ban)
|
|
.setThumbnail(target.displayAvatarURL({ dynamic: true }))
|
|
.setDescription(`${target.toString()} a été banni du serveur.`)
|
|
.addFields(
|
|
{ name: '👤 Utilisateur', value: `${target.toString()}\n\`${target.tag}\``, inline: true },
|
|
{ name: '👮 Modérateur', value: `${interaction.user.toString()}\n\`${interaction.user.tag}\``, inline: true },
|
|
{ name: '⏱️ Durée', value: `\`${durationText}\``, inline: true },
|
|
{ name: '📝 Raison', value: reason, inline: false },
|
|
{ name: '📅 Date', value: `<t:${Math.floor(Date.now() / 1000)}:F>`, inline: true }
|
|
)
|
|
.setFooter({ text: `ID: ${target.id} • ${interaction.guild.name}` })
|
|
.setTimestamp();
|
|
await sendLog(interaction.guild, { embeds: [logEmbed] });
|
|
|
|
} catch (err) {
|
|
console.error('Erreur lors du bannissement:', err);
|
|
if (err.code === 50013) {
|
|
await interaction.editReply({ content: '❌ Je n\'ai pas les permissions nécessaires pour bannir cet utilisateur.', ephemeral: true });
|
|
} else if (err.code === 50035) {
|
|
await interaction.editReply({ content: '❌ Cet utilisateur est déjà banni.', ephemeral: true });
|
|
} else {
|
|
await interaction.editReply({ content: `❌ Échec du bannissement de ${target.tag}: ${err.message}`, ephemeral: true });
|
|
}
|
|
}
|
|
},
|
|
};
|