Add per-guild moderation settings and records
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const { SlashCommandBuilder, PermissionFlagsBits } = require("discord.js");
|
||||
const { recordModerationAction } = require('../../services/moderationRecords');
|
||||
|
||||
module.exports = {
|
||||
permissionGroup: 'moderation',
|
||||
@@ -31,6 +32,14 @@ module.exports = {
|
||||
}
|
||||
|
||||
await member.kick(reason);
|
||||
await recordModerationAction({
|
||||
guildId: interaction.guildId,
|
||||
userId: user.id,
|
||||
action: 'kick',
|
||||
moderatorId: interaction.user.id,
|
||||
reason,
|
||||
metadata: {},
|
||||
});
|
||||
|
||||
await interaction.reply(`Kicked ${user.tag} for: ${reason}`);
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const { SlashCommandBuilder, PermissionFlagsBits } = require("discord.js");
|
||||
const { recordModerationAction } = require('../../services/moderationRecords');
|
||||
|
||||
module.exports = {
|
||||
permissionGroup: 'moderation',
|
||||
@@ -39,6 +40,15 @@ module.exports = {
|
||||
}
|
||||
|
||||
await member.timeout(durationMs, reason);
|
||||
await recordModerationAction({
|
||||
guildId: interaction.guildId,
|
||||
userId: target.id,
|
||||
action: 'mute',
|
||||
moderatorId: interaction.user.id,
|
||||
reason,
|
||||
metadata: { durationMinutes: duration },
|
||||
stateUpdates: { mutedUntil: new Date(Date.now() + durationMs) },
|
||||
});
|
||||
|
||||
return interaction.reply({
|
||||
content: `Muted ${target.tag} for **${duration} minutes**. Reason: ${reason}`,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits } = require("discord.js");
|
||||
const { connectToDatabase } = require('../../db.js');
|
||||
const { recordModerationAction } = require('../../services/moderationRecords');
|
||||
|
||||
module.exports = {
|
||||
mod: true,
|
||||
@@ -73,11 +74,24 @@ module.exports = {
|
||||
const tempBanCollection = db.collection("tempban");
|
||||
|
||||
// 3. Insert the ban record
|
||||
await tempBanCollection.insertOne({
|
||||
const banRecord = {
|
||||
guildId: interaction.guild.id,
|
||||
userId: user.id,
|
||||
banTime: banUntil,
|
||||
reason: reason,
|
||||
};
|
||||
await tempBanCollection.insertOne(banRecord);
|
||||
await recordModerationAction({
|
||||
guildId: interaction.guild.id,
|
||||
userId: user.id,
|
||||
action: 'temp-ban',
|
||||
moderatorId: interaction.user.id,
|
||||
reason,
|
||||
metadata: { durationMs: durationms },
|
||||
stateUpdates: {
|
||||
isBanned: true,
|
||||
bannedUntil: new Date(banUntil),
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
|
||||
+101
-1
@@ -1,5 +1,8 @@
|
||||
const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');
|
||||
const { addWarning } = require('../../services/warnings');
|
||||
const { getGuildSettings } = require('../../services/guildSettings');
|
||||
const { recordModerationAction } = require('../../services/moderationRecords');
|
||||
const { connectToDatabase } = require('../../db');
|
||||
|
||||
module.exports = {
|
||||
permissionGroup: 'moderation',
|
||||
@@ -29,13 +32,110 @@ module.exports = {
|
||||
});
|
||||
}
|
||||
|
||||
const { warnings } = await addWarning({
|
||||
const { warnings, strikeCount } = await addWarning({
|
||||
guildId: interaction.guildId,
|
||||
userId: target.id,
|
||||
moderatorId: interaction.user.id,
|
||||
reason,
|
||||
});
|
||||
|
||||
const settings = await getGuildSettings(interaction.guildId);
|
||||
const thresholds = settings.moderation?.strikeThresholds ?? {};
|
||||
const muteThreshold = Number.isInteger(thresholds.mute) ? thresholds.mute : null;
|
||||
const kickThreshold = Number.isInteger(thresholds.kick) ? thresholds.kick : null;
|
||||
const banThreshold = Number.isInteger(thresholds.ban) ? thresholds.ban : null;
|
||||
const muteDurationMinutes = settings.moderation?.muteDurationMinutes ?? 0;
|
||||
const banDurationMs = settings.moderation?.banDurationMs ?? 0;
|
||||
|
||||
let actionTaken = null;
|
||||
|
||||
if (banThreshold && strikeCount === banThreshold) {
|
||||
actionTaken = 'ban';
|
||||
} else if (kickThreshold && strikeCount === kickThreshold) {
|
||||
actionTaken = 'kick';
|
||||
} else if (muteThreshold && strikeCount === muteThreshold) {
|
||||
actionTaken = 'mute';
|
||||
}
|
||||
|
||||
if (actionTaken === 'mute') {
|
||||
try {
|
||||
const member = await interaction.guild.members.fetch(target.id);
|
||||
if (member.moderatable && muteDurationMinutes > 0) {
|
||||
const durationMs = muteDurationMinutes * 60 * 1000;
|
||||
await member.timeout(durationMs, 'Strike threshold reached');
|
||||
await recordModerationAction({
|
||||
guildId: interaction.guildId,
|
||||
userId: target.id,
|
||||
action: 'mute',
|
||||
moderatorId: interaction.user.id,
|
||||
reason: 'Strike threshold reached',
|
||||
metadata: { strikeCount, durationMinutes: muteDurationMinutes },
|
||||
stateUpdates: { mutedUntil: new Date(Date.now() + durationMs) },
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to auto-mute member:', error);
|
||||
}
|
||||
}
|
||||
|
||||
if (actionTaken === 'kick') {
|
||||
try {
|
||||
const member = await interaction.guild.members.fetch(target.id);
|
||||
if (member.kickable) {
|
||||
await member.kick('Strike threshold reached');
|
||||
await recordModerationAction({
|
||||
guildId: interaction.guildId,
|
||||
userId: target.id,
|
||||
action: 'kick',
|
||||
moderatorId: interaction.user.id,
|
||||
reason: 'Strike threshold reached',
|
||||
metadata: { strikeCount },
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to auto-kick member:', error);
|
||||
}
|
||||
}
|
||||
|
||||
if (actionTaken === 'ban') {
|
||||
try {
|
||||
const banReason = 'Strike threshold reached';
|
||||
const shouldTempBan = Number.isInteger(banDurationMs) && banDurationMs > 0;
|
||||
|
||||
await interaction.guild.bans.create(target.id, {
|
||||
reason: banReason,
|
||||
});
|
||||
|
||||
if (shouldTempBan) {
|
||||
const dbClient = await connectToDatabase();
|
||||
const db = dbClient.db('discord');
|
||||
const tempBanCollection = db.collection('tempban');
|
||||
const banUntil = Date.now() + banDurationMs;
|
||||
await tempBanCollection.insertOne({
|
||||
guildId: interaction.guildId,
|
||||
userId: target.id,
|
||||
banTime: banUntil,
|
||||
reason: banReason,
|
||||
});
|
||||
}
|
||||
|
||||
await recordModerationAction({
|
||||
guildId: interaction.guildId,
|
||||
userId: target.id,
|
||||
action: 'ban',
|
||||
moderatorId: interaction.user.id,
|
||||
reason: banReason,
|
||||
metadata: { strikeCount, banDurationMs },
|
||||
stateUpdates: {
|
||||
isBanned: true,
|
||||
bannedUntil: shouldTempBan ? new Date(Date.now() + banDurationMs) : null,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to auto-ban member:', error);
|
||||
}
|
||||
}
|
||||
|
||||
return interaction.reply({
|
||||
content: `${target.tag} has been warned. Total strikes: **${warnings.length}**.`,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user