96 lines
4.2 KiB
JavaScript
96 lines
4.2 KiB
JavaScript
const { SlashCommandBuilder, PermissionFlagsBits, EmbedBuilder } = require('discord.js');
|
|
const db = require('../../functions/database/db.js'); // instance mysql2/promise
|
|
|
|
module.exports = {
|
|
category: 'moderation',
|
|
data: new SlashCommandBuilder()
|
|
.setName('ban')
|
|
.setDescription('Select a member and ban them.')
|
|
.addUserOption(option =>
|
|
option.setName('target')
|
|
.setDescription('The member to ban')
|
|
.setRequired(true))
|
|
.addStringOption(option =>
|
|
option.setName('reason')
|
|
.setDescription('Reason')
|
|
.setRequired(false))
|
|
.addStringOption(option =>
|
|
option.setName('duration')
|
|
.setDescription('Ban duration (ex: 1h, 2d, leave empty for permanent)')
|
|
.setRequired(false))
|
|
.setDefaultMemberPermissions(PermissionFlagsBits.BanMembers),
|
|
|
|
async execute(interaction) {
|
|
const target = interaction.options.getUser('target');
|
|
const reason = interaction.options.getString('reason') || 'No reason provided';
|
|
const durationInput = interaction.options.getString('duration');
|
|
|
|
await interaction.deferReply({ ephemeral: true });
|
|
|
|
if (!target) return interaction.editReply({ content: 'No user specified!' });
|
|
await interaction.guild.bans.create(target.id, { reason: `Banned by ${interaction.user.tag}: ${reason}` });
|
|
|
|
let type = 'Permanent';
|
|
let unbanDate = null;
|
|
|
|
if (durationInput) {
|
|
const regex = /^(\d+)([smhd])$/;
|
|
const match = durationInput.match(regex);
|
|
if (match) {
|
|
const value = parseInt(match[1]);
|
|
const unit = match[2];
|
|
let multiplier = 1000;
|
|
if (unit === 'm') multiplier *= 60;
|
|
if (unit === 'h') multiplier *= 60 * 60;
|
|
if (unit === 'd') multiplier *= 60 * 60 * 24;
|
|
unbanDate = Date.now() + value * multiplier;
|
|
type = 'Temporary';
|
|
}
|
|
}
|
|
|
|
try {
|
|
// Bannissement
|
|
interaction.guild.bans.create(target.id, { reason: `Banned by ${interaction.user.tag}: ${reason}` });
|
|
|
|
// Stockage dans MySQL (bans)
|
|
await db.query(
|
|
`INSERT INTO bans (userId, reason, modId, timestamp, type, unbanDate)
|
|
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]
|
|
);
|
|
|
|
// Stockage dans logs (ajout guildId)
|
|
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, 'ban', reason, type, interaction.guild.id, Date.now()]
|
|
);
|
|
|
|
await interaction.editReply(`✅ Successfully banned ${target.tag} (${type})`);
|
|
|
|
// Embed log
|
|
const logChannel = interaction.guild.channels.cache.find(ch => ch.name === 'logs');
|
|
if (logChannel) {
|
|
const embed = new EmbedBuilder()
|
|
.setTitle('User Banned')
|
|
.setColor('Red')
|
|
.addFields(
|
|
{ name: 'Banned User', value: `<@${target.id}> (${target.tag})`, inline: true },
|
|
{ name: 'Banned By', value: `<@${interaction.user.id}> (${interaction.user.tag})`, inline: true },
|
|
{ name: 'Reason', value: reason, inline: false },
|
|
{ name: 'Type', value: type, inline: true },
|
|
{ name: 'Date', value: new Date().toLocaleString(), inline: true },
|
|
{ name: 'Guild', value: interaction.guild.name, inline: true }
|
|
)
|
|
.setTimestamp();
|
|
logChannel.send({ embeds: [embed] });
|
|
}
|
|
|
|
} catch (err) {
|
|
console.error(err);
|
|
await interaction.editReply({ content: `❌ Failed to ban ${target.tag}` });
|
|
}
|
|
}
|
|
};
|