diff --git a/apps/discord-bot/src/automute.ts b/apps/discord-bot/src/automute.ts new file mode 100644 index 0000000..1e7125b --- /dev/null +++ b/apps/discord-bot/src/automute.ts @@ -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 => + (await redis.sismember(CHANNELS_KEY, channelId)) === 1; + +export const enableAutomute = async (channelId: string): Promise => { + await redis.sadd(CHANNELS_KEY, channelId); +}; + +export const disableAutomute = async (channelId: string): Promise => { + await redis.srem(CHANNELS_KEY, channelId); +}; + +export const muteMember = async (state: VoiceState): Promise => { + 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 => { + 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); + } +}; diff --git a/apps/discord-bot/src/commands/automute.ts b/apps/discord-bot/src/commands/automute.ts new file mode 100644 index 0000000..4ac4771 --- /dev/null +++ b/apps/discord-bot/src/commands/automute.ts @@ -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}`); + } + } + }, +}; diff --git a/apps/discord-bot/src/commands/index.ts b/apps/discord-bot/src/commands/index.ts index 56c793b..1269d0a 100644 --- a/apps/discord-bot/src/commands/index.ts +++ b/apps/discord-bot/src/commands/index.ts @@ -9,6 +9,7 @@ import { queteCommand } from "./quete"; import { tgCommand } from "./tg"; import { reportmsgCommand } from "./reportmsg"; import { reportsCommand } from "./reports"; +import { automuteCommand } from "./automute"; export type CommandHandler = ( message: OmitPartialGroupDMChannel>, @@ -31,4 +32,5 @@ export const commands: Record = { tg: tgCommand, reportmsg: reportmsgCommand, reports: reportsCommand, + automute: automuteCommand, }; diff --git a/apps/discord-bot/src/index.ts b/apps/discord-bot/src/index.ts index 2d8417f..f03957b 100644 --- a/apps/discord-bot/src/index.ts +++ b/apps/discord-bot/src/index.ts @@ -40,6 +40,7 @@ const client = new Client({ GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.DirectMessages, + GatewayIntentBits.GuildVoiceStates, ], partials: [Partials.Message, Partials.Channel], }); diff --git a/apps/discord-bot/src/modes/bot.ts b/apps/discord-bot/src/modes/bot.ts index 42ba7fb..d339bef 100644 --- a/apps/discord-bot/src/modes/bot.ts +++ b/apps/discord-bot/src/modes/bot.ts @@ -4,6 +4,7 @@ import { env } from "~/env"; import { questCheckCron } from "~/quests"; import { trackingCron } from "~/tracking"; 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"; const PREFIX = env.NODE_ENV === "development" ? "lbf_" : "lbf"; @@ -63,7 +64,8 @@ const onInteraction = async (interaction: Interaction, client: Client) => { }; export const setupBotMode = (client: Client) => { - client.on("clientReady", (client) => { void onReady(client); }); - client.on("messageCreate", (message) => { void onMessage(message); }); - client.on("interactionCreate", (interaction) => { void onInteraction(interaction, client); }); + client.on("clientReady", (client) => { void onReady(client); }); + client.on("messageCreate", (message) => { void onMessage(message); }); + client.on("interactionCreate", (interaction) => { void onInteraction(interaction, client); }); + client.on("voiceStateUpdate", (oldState, newState) => { void onVoiceStateUpdate(oldState, newState); }); };