feat(moderation): implement the moderation service and commands
Every action checks the role hierarchy before touching Discord, then records the sanction in SQLite and mirrors it in the moderation log channel, so the history outlives the audit log.
This commit is contained in:
9 files changed
+460
-30
No files matched your search
@@ -0,0 +1,118 @@
|
||||
import { COLORS, MODERATION_ACTIONS } from '../../config/constants.js';
|
||||
import { env } from '../../config/env.js';
|
||||
import { UserFacingError } from '../../core/errors.js';
|
||||
import { canActOn } from '../../core/permissions.js';
|
||||
import { logEvent } from '../../database/repositories/event-log-repository.js';
|
||||
import { recordAction } from '../../database/repositories/moderation-repository.js';
|
||||
import { baseEmbed } from '../../ui/embeds.js';
|
||||
|
||||
/**
|
||||
* Enforces the role hierarchy before any moderation action (M3-T05 / M8-T08).
|
||||
* @param {import('discord.js').ChatInputCommandInteraction} interaction
|
||||
* @param {import('discord.js').GuildMember} target
|
||||
*/
|
||||
export function assertModerationTarget(interaction, target) {
|
||||
if (!target) throw new UserFacingError('Ce membre n’est pas présent sur le serveur.');
|
||||
if (target.id === interaction.user.id) {
|
||||
throw new UserFacingError('Tu ne peux pas t’appliquer cette action à toi-même.');
|
||||
}
|
||||
if (!canActOn(interaction.member, target)) {
|
||||
throw new UserFacingError('Tu ne peux pas agir sur ce membre : son rôle est égal ou supérieur au tien.');
|
||||
}
|
||||
|
||||
const bot = interaction.guild.members.me;
|
||||
if (bot && bot.roles.highest.comparePositionTo(target.roles.highest) <= 0) {
|
||||
throw new UserFacingError(`Mon rôle est trop bas pour agir sur ${target.user.tag}.`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Records the action in SQLite and mirrors it in the moderation log channel (M8-T06).
|
||||
* Call it only once the Discord action has succeeded.
|
||||
*/
|
||||
export async function logModerationAction(
|
||||
interaction,
|
||||
{ action, targetId, reason = null, durationMs = null },
|
||||
) {
|
||||
recordAction({
|
||||
guildId: interaction.guildId,
|
||||
action,
|
||||
moderatorId: interaction.user.id,
|
||||
targetId,
|
||||
reason,
|
||||
durationMs,
|
||||
});
|
||||
logEvent({
|
||||
guildId: interaction.guildId,
|
||||
type: `moderation.${action}`,
|
||||
actorId: interaction.user.id,
|
||||
payload: { targetId, reason, durationMs },
|
||||
});
|
||||
|
||||
const meta = MODERATION_ACTIONS[action];
|
||||
await sendModerationLog(interaction.client, interaction.guild, {
|
||||
action,
|
||||
color: meta?.color ?? COLORS.moderation,
|
||||
targetId,
|
||||
moderatorId: interaction.user.id,
|
||||
reason,
|
||||
durationMs,
|
||||
});
|
||||
}
|
||||
|
||||
/** Posts an entry in `CHANNEL_MOD_LOGS_ID`, if configured (M8-T06). */
|
||||
export async function sendModerationLog(
|
||||
client,
|
||||
guild,
|
||||
{ action, targetId, moderatorId, reason, durationMs },
|
||||
) {
|
||||
if (!env.channels.modLogs) return;
|
||||
|
||||
const channel = await client.channels.fetch(env.channels.modLogs).catch(() => null);
|
||||
if (!channel?.isSendable()) return;
|
||||
|
||||
const meta = MODERATION_ACTIONS[action];
|
||||
const embed = baseEmbed(meta?.color)
|
||||
.setTitle(`Modération — ${meta?.label ?? action}`)
|
||||
.addFields(
|
||||
{ name: 'Cible', value: targetId ? `<@${targetId}> (${targetId})` : 'Salon', inline: true },
|
||||
{ name: 'Modérateur', value: `<@${moderatorId}>`, inline: true },
|
||||
{ name: 'Raison', value: reason || 'Non précisée' },
|
||||
);
|
||||
if (durationMs) {
|
||||
embed.addFields({ name: 'Durée', value: formatDuration(durationMs), inline: true });
|
||||
}
|
||||
if (guild) embed.setFooter({ text: `${guild.name} · ${guild.id}` });
|
||||
|
||||
await channel.send({ embeds: [embed] }).catch(() => {});
|
||||
}
|
||||
|
||||
/** Direct message sent to the member who is being moderated (M8-T07). */
|
||||
export function targetNoticeEmbed({ action, guildName, reason = null, durationMs = null }) {
|
||||
const meta = MODERATION_ACTIONS[action];
|
||||
const embed = baseEmbed(meta?.color)
|
||||
.setTitle(`Sanction sur ${guildName}`)
|
||||
.setDescription(`Action : **${meta?.label ?? action}**`)
|
||||
.addFields({ name: 'Raison', value: reason || 'Non précisée' });
|
||||
if (durationMs) embed.addFields({ name: 'Durée', value: formatDuration(durationMs) });
|
||||
return embed;
|
||||
}
|
||||
|
||||
/** Human readable duration for embeds: `2 h 30 min`. */
|
||||
export function formatDuration(durationMs) {
|
||||
const minutes = Math.round(durationMs / 60_000);
|
||||
if (minutes < 60) return `${minutes} min`;
|
||||
const hours = Math.floor(minutes / 60);
|
||||
const rest = minutes % 60;
|
||||
if (hours < 24) return rest === 0 ? `${hours} h` : `${hours} h ${rest} min`;
|
||||
const days = Math.floor(hours / 24);
|
||||
const remainingHours = hours % 24;
|
||||
return remainingHours === 0 ? `${days} j` : `${days} j ${remainingHours} h`;
|
||||
}
|
||||
|
||||
/** Fetches the member targeted by a command, with a clear error if absent. */
|
||||
export async function fetchTargetMember(interaction, userId) {
|
||||
const member = await interaction.guild.members.fetch(userId).catch(() => null);
|
||||
if (!member) throw new UserFacingError('Ce membre n’est pas présent sur le serveur.');
|
||||
return member;
|
||||
}
|
||||
Reference in new issue
Block a user