Compare commits

...

2 Commits

Author SHA1 Message Date
d7a73334d4 feat(discord-bot): implement automute command
All checks were successful
Build and Deploy / typecheck (push) Successful in 1m44s
Build and Deploy / build (push) Successful in 57s
Build and Deploy / deploy (push) Successful in 1s
2026-10-23 16:10:54 +02:00
742bcab319 feat: delete quest message after sending the embed 2026-10-23 15:31:10 +02:00
6 changed files with 132 additions and 3 deletions

View File

@@ -0,0 +1,52 @@
import type { VoiceState } from "discord.js";
import { createLogger } from "@lbf-bot/utils";
import { redis } from "@lbf-bot/database";
const automuteLogger = createLogger({ prefix: "automute" });
const CHANNELS_KEY = "automute:channels";
const MUTED_KEY = "automute:muted";
export const isAutomuted = async (channelId: string): Promise<boolean> =>
(await redis.sismember(CHANNELS_KEY, channelId)) === 1;
export const enableAutomute = async (channelId: string): Promise<void> => {
await redis.sadd(CHANNELS_KEY, channelId);
};
export const disableAutomute = async (channelId: string): Promise<void> => {
await redis.srem(CHANNELS_KEY, channelId);
};
export const muteMember = async (state: VoiceState): Promise<void> => {
if (state.serverMute) return;
await state.setMute(true, "automute");
await redis.sadd(MUTED_KEY, state.id);
automuteLogger.info(`Muted ${state.member?.user.username ?? state.id} in ${state.channel?.name ?? state.channelId}`);
};
export const unmuteMember = async (state: VoiceState): Promise<void> => {
if (!(await redis.sismember(MUTED_KEY, state.id))) return;
if (state.serverMute) {
await state.setMute(false, "automute");
automuteLogger.info(`Unmuted ${state.member?.user.username ?? state.id}`);
}
await redis.srem(MUTED_KEY, state.id);
};
export const onVoiceStateUpdate = async (oldState: VoiceState, newState: VoiceState) => {
if (oldState.channelId === newState.channelId) return;
if (!newState.channelId) return;
try {
if (await isAutomuted(newState.channelId)) {
await muteMember(newState);
} else {
await unmuteMember(newState);
}
} catch (error: unknown) {
automuteLogger.error(`Failed to update ${newState.id}:`, error);
}
};

View File

@@ -0,0 +1,70 @@
import { ChannelType, PermissionFlagsBits } from "discord.js";
import { env } from "~/env";
import { replyError, replySuccess } from "~/discord";
import { disableAutomute, enableAutomute, isAutomuted, muteMember, unmuteMember } from "~/automute";
import type { Command } from "./index";
const USAGE = "Usage: `lbf automute <#salon | ID> on/off`";
export const automuteCommand: Command = {
help: "Mute automatiquement toute personne rejoignant le salon vocal spécifié",
handler: async (message, args) => {
if (!message.member?.roles.cache.has(env.DISCORD_STAFF_ROLE_ID)) {
await replyError(message, "Tu t'es cru chez mémé ou quoi faut être staff");
return;
}
const [rawChannel, state] = args;
if (!rawChannel || !state) {
await replyError(message, USAGE);
return;
}
const channelId = rawChannel.match(/^<#(\d+)>$/)?.[1] ?? rawChannel;
const channel = message.guild?.channels.cache.get(channelId);
if (!channel) {
await replyError(message, `Salon introuvable: \`${rawChannel}\``);
return;
}
if (channel.type !== ChannelType.GuildVoice && channel.type !== ChannelType.GuildStageVoice) {
await replyError(message, `<#${channel.id}> n'est pas un salon vocal`);
return;
}
switch (state.toLowerCase()) {
case "on": {
if (await isAutomuted(channel.id)) {
await replyError(message, `Automute déjà activé pour <#${channel.id}>`);
return;
}
const me = message.guild?.members.me;
if (!me || !channel.permissionsFor(me).has(PermissionFlagsBits.MuteMembers)) {
await replyError(message, `Je n'ai pas la permission de mute dans <#${channel.id}>`);
return;
}
await enableAutomute(channel.id);
await Promise.allSettled(channel.members.map((member) => muteMember(member.voice)));
await replySuccess(message, `Automute activé pour <#${channel.id}>`);
break;
}
case "off": {
if (!(await isAutomuted(channel.id))) {
await replyError(message, `Automute pas activé pour <#${channel.id}>`);
return;
}
await disableAutomute(channel.id);
await Promise.allSettled(channel.members.map((member) => unmuteMember(member.voice)));
await replySuccess(message, `Automute désactivé pour <#${channel.id}>`);
break;
}
default: {
await replyError(message, `Option inconnue \`${state}\`. ${USAGE}`);
}
}
},
};

View File

@@ -9,6 +9,7 @@ import { queteCommand } from "./quete";
import { tgCommand } from "./tg"; import { tgCommand } from "./tg";
import { reportmsgCommand } from "./reportmsg"; import { reportmsgCommand } from "./reportmsg";
import { reportsCommand } from "./reports"; import { reportsCommand } from "./reports";
import { automuteCommand } from "./automute";
export type CommandHandler = ( export type CommandHandler = (
message: OmitPartialGroupDMChannel<Message<boolean>>, message: OmitPartialGroupDMChannel<Message<boolean>>,
@@ -31,4 +32,5 @@ export const commands: Record<string, Command> = {
tg: tgCommand, tg: tgCommand,
reportmsg: reportmsgCommand, reportmsg: reportmsgCommand,
reports: reportsCommand, reports: reportsCommand,
automute: automuteCommand,
}; };

View File

@@ -24,5 +24,7 @@ export const queteCommand: Command = {
.setColor(color) .setColor(color)
], ],
}); });
await message.delete();
}, },
}; };

View File

@@ -40,6 +40,7 @@ const client = new Client({
GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent, GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages, GatewayIntentBits.DirectMessages,
GatewayIntentBits.GuildVoiceStates,
], ],
partials: [Partials.Message, Partials.Channel], partials: [Partials.Message, Partials.Channel],
}); });

View File

@@ -4,6 +4,7 @@ import { env } from "~/env";
import { questCheckCron } from "~/quests"; import { questCheckCron } from "~/quests";
import { trackingCron } from "~/tracking"; import { trackingCron } from "~/tracking";
import { commands } from "~/commands"; import { commands } from "~/commands";
import { onVoiceStateUpdate } from "~/automute";
import { handleReportButton, handleReportModal, handleEditButton, handleDeleteButton, handleEditModal, REPORT_BUTTON_ID, REPORT_MODAL_ID, REPORT_EDIT_BUTTON_PREFIX, REPORT_DELETE_BUTTON_PREFIX, REPORT_EDIT_MODAL_PREFIX } from "~/reporting"; import { handleReportButton, handleReportModal, handleEditButton, handleDeleteButton, handleEditModal, REPORT_BUTTON_ID, REPORT_MODAL_ID, REPORT_EDIT_BUTTON_PREFIX, REPORT_DELETE_BUTTON_PREFIX, REPORT_EDIT_MODAL_PREFIX } from "~/reporting";
const PREFIX = env.NODE_ENV === "development" ? "lbf_" : "lbf"; const PREFIX = env.NODE_ENV === "development" ? "lbf_" : "lbf";
@@ -63,7 +64,8 @@ const onInteraction = async (interaction: Interaction, client: Client) => {
}; };
export const setupBotMode = (client: Client) => { export const setupBotMode = (client: Client) => {
client.on("clientReady", (client) => { void onReady(client); }); client.on("clientReady", (client) => { void onReady(client); });
client.on("messageCreate", (message) => { void onMessage(message); }); client.on("messageCreate", (message) => { void onMessage(message); });
client.on("interactionCreate", (interaction) => { void onInteraction(interaction, client); }); client.on("interactionCreate", (interaction) => { void onInteraction(interaction, client); });
client.on("voiceStateUpdate", (oldState, newState) => { void onVoiceStateUpdate(oldState, newState); });
}; };