From 1ceb8c3574653425dfa4cd28531a5165a4e3da36 Mon Sep 17 00:00:00 2001 From: smueller Date: Wed, 22 Jul 2026 13:09:06 +0200 Subject: [PATCH] Refactor tag editing command to improve option handling and embed structure - Simplified the retrieval of command options for editing tags, enhancing readability and maintainability. - Updated embed handling to use an object structure instead of null, ensuring consistency in the updateTag function. - Improved overall clarity by using descriptive variable names for command options. --- apps/bot/src/modules/selfroles/commands.ts | 5 ++- apps/bot/src/modules/tags/commands.ts | 36 ++++++++++++---------- apps/bot/src/modules/tags/service.ts | 13 +++++--- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/apps/bot/src/modules/selfroles/commands.ts b/apps/bot/src/modules/selfroles/commands.ts index c8681b0..ee68ee8 100644 --- a/apps/bot/src/modules/selfroles/commands.ts +++ b/apps/bot/src/modules/selfroles/commands.ts @@ -1,4 +1,4 @@ -import { PermissionFlagsBits } from 'discord.js'; +import { ChannelType, PermissionFlagsBits } from 'discord.js'; import { SelfRoleBehaviorSchema, SelfRoleModeSchema, @@ -71,8 +71,7 @@ const selfrolesCommand: SlashCommand = { ); const mode = SelfRoleModeSchema.parse(interaction.options.getString('mode', true)); const channel = interaction.options.getChannel('channel', true); - - if (!channel.isTextBased() || channel.isDMBased()) { + if (channel.type !== ChannelType.GuildText && channel.type !== ChannelType.GuildAnnouncement) { await interaction.reply({ content: t(locale, 'selfroles.error.invalid_channel'), ephemeral: true diff --git a/apps/bot/src/modules/tags/commands.ts b/apps/bot/src/modules/tags/commands.ts index 4587a8f..c0bdc9c 100644 --- a/apps/bot/src/modules/tags/commands.ts +++ b/apps/bot/src/modules/tags/commands.ts @@ -99,28 +99,32 @@ const tagCommand: SlashCommand = { } if (sub === 'edit') { + const contentValue = interaction.options.getString('content'); + const triggerValue = interaction.options.getString('trigger_word'); + const rolesValue = interaction.options.getString('allowed_roles'); + const channelsValue = interaction.options.getString('allowed_channels'); + const embedTitle = interaction.options.getString('embed_title'); + const embedDescription = interaction.options.getString('embed_description'); + const embedColor = interaction.options.getInteger('embed_color'); + const embedProvided = + embedTitle !== null || embedDescription !== null || embedColor !== null; + const tag = await updateTag(context, interaction.guildId!, interaction.options.getString('name', true), { newName: interaction.options.getString('new_name') ?? undefined, responseType: interaction.options.getString('response_type') ? TagResponseTypeSchema.parse(interaction.options.getString('response_type')) : undefined, - content: interaction.options.has('content') - ? interaction.options.getString('content') + content: contentValue !== null ? contentValue : undefined, + embed: embedProvided + ? { + title: embedTitle ?? undefined, + description: embedDescription ?? undefined, + color: embedColor ?? undefined + } : undefined, - embed: interaction.options.has('embed_title') || - interaction.options.has('embed_description') || - interaction.options.has('embed_color') - ? buildEmbedPayload(interaction) ?? null - : undefined, - triggerWord: interaction.options.has('trigger_word') - ? interaction.options.getString('trigger_word') - : undefined, - allowedRoleIds: interaction.options.has('allowed_roles') - ? parseIdList(interaction.options.getString('allowed_roles')) - : undefined, - allowedChannelIds: interaction.options.has('allowed_channels') - ? parseIdList(interaction.options.getString('allowed_channels')) - : undefined + triggerWord: triggerValue !== null ? triggerValue : undefined, + allowedRoleIds: rolesValue !== null ? parseIdList(rolesValue) : undefined, + allowedChannelIds: channelsValue !== null ? parseIdList(channelsValue) : undefined }); await interaction.reply({ diff --git a/apps/bot/src/modules/tags/service.ts b/apps/bot/src/modules/tags/service.ts index d9a05ce..c0a5da7 100644 --- a/apps/bot/src/modules/tags/service.ts +++ b/apps/bot/src/modules/tags/service.ts @@ -1,4 +1,4 @@ -import { EmbedBuilder, type Guild, type GuildMember, type InteractionReplyOptions } from 'discord.js'; +import { EmbedBuilder, type Guild, type GuildMember } from 'discord.js'; import { applyTagPlaceholders, TagResponseTypeSchema, @@ -64,12 +64,17 @@ export function parseTagEmbed(raw: unknown) { return parsed.success ? parsed.data : null; } +export type TagReplyPayload = { + content?: string; + embeds?: EmbedBuilder[]; +}; + export function buildTagReply( tag: Tag, member: GuildMember | null, guild: Guild, args: string -): InteractionReplyOptions { +): TagReplyPayload { const vars = buildTagPlaceholderVars(member, guild, args); const responseType = TagResponseTypeSchema.parse(tag.responseType); @@ -208,7 +213,7 @@ export async function updateTag( name: newName ?? existing.name, responseType, content, - embed: embed ?? null, + embed: embed ?? undefined, triggerWord: updates.triggerWord !== undefined ? updates.triggerWord?.trim() || null : existing.triggerWord, allowedRoleIds: updates.allowedRoleIds ?? existing.allowedRoleIds, @@ -240,7 +245,7 @@ export async function executeTag( guild: Guild, channelId: string, args: string -): Promise { +): Promise { const tag = await getTagByName(context, guildId, name); if (!tag) { throw new TagError('not_found');