From 7e8270f7d260322e1950e058b221ab088bd595d0 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Mon, 24 Jun 2024 20:25:16 +0700 Subject: [PATCH] fix(bots/discord/utils/discord): add `moderation` module related functions --- bots/discord/src/utils/discord/moderation.ts | 45 ++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 bots/discord/src/utils/discord/moderation.ts diff --git a/bots/discord/src/utils/discord/moderation.ts b/bots/discord/src/utils/discord/moderation.ts new file mode 100644 index 0000000..d74563b --- /dev/null +++ b/bots/discord/src/utils/discord/moderation.ts @@ -0,0 +1,45 @@ +import { config, logger } from '$/context' +import type { ChatInputCommandInteraction, EmbedBuilder, Guild, User } from 'discord.js' +import { applyReferenceToModerationActionEmbed, createModerationActionEmbed } from './embeds' + +const PresetLogAction = { + apply: 'Applied role preset to', + remove: 'Removed role preset from', +} as const + +export const sendPresetReplyAndLogs = ( + action: keyof typeof PresetLogAction, + interaction: ChatInputCommandInteraction, + user: User, + preset: string, + expires?: number | null, +) => + sendModerationReplyAndLogs( + interaction, + createModerationActionEmbed(PresetLogAction[action], user, interaction.user, undefined, expires, [ + [{ name: 'Preset', value: preset, inline: true }], + ]), + ) + +export const sendModerationReplyAndLogs = async (interaction: ChatInputCommandInteraction, embed: EmbedBuilder) => { + const reply = await interaction.reply({ embeds: [embed] }).then(it => it.fetch()) + const logChannel = await getLogChannel(interaction.guild!) + await logChannel?.send({ embeds: [applyReferenceToModerationActionEmbed(embed, reply.url)] }) +} + +export const getLogChannel = async (guild: Guild) => { + const logConfig = config.moderation?.log + if (!logConfig) return + + try { + const channel = await guild.channels.fetch(logConfig.thread ?? logConfig.channel) + if (!channel || !channel.isTextBased()) + return void logger.warn('The moderation log channel does not exist, skipping logging') + + return channel + } catch (error) { + logger.warn('Failed to fetch the moderation log channel:', error) + } + + return +}