Compare commits
2 Commits
21ff5454f1
...
d7a73334d4
| Author | SHA1 | Date | |
|---|---|---|---|
| d7a73334d4 | |||
| 742bcab319 |
52
apps/discord-bot/src/automute.ts
Normal file
52
apps/discord-bot/src/automute.ts
Normal 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);
|
||||
}
|
||||
};
|
||||
70
apps/discord-bot/src/commands/automute.ts
Normal file
70
apps/discord-bot/src/commands/automute.ts
Normal 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}`);
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
@@ -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<Message<boolean>>,
|
||||
@@ -31,4 +32,5 @@ export const commands: Record<string, Command> = {
|
||||
tg: tgCommand,
|
||||
reportmsg: reportmsgCommand,
|
||||
reports: reportsCommand,
|
||||
automute: automuteCommand,
|
||||
};
|
||||
|
||||
@@ -24,5 +24,7 @@ export const queteCommand: Command = {
|
||||
.setColor(color)
|
||||
],
|
||||
});
|
||||
|
||||
await message.delete();
|
||||
},
|
||||
};
|
||||
|
||||
@@ -40,6 +40,7 @@ const client = new Client({
|
||||
GatewayIntentBits.GuildMessages,
|
||||
GatewayIntentBits.MessageContent,
|
||||
GatewayIntentBits.DirectMessages,
|
||||
GatewayIntentBits.GuildVoiceStates,
|
||||
],
|
||||
partials: [Partials.Message, Partials.Channel],
|
||||
});
|
||||
|
||||
@@ -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";
|
||||
@@ -66,4 +67,5 @@ 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("voiceStateUpdate", (oldState, newState) => { void onVoiceStateUpdate(oldState, newState); });
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user