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.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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<MessageReplyOptions> {
|
||||
): Promise<TagReplyPayload> {
|
||||
const tag = await getTagByName(context, guildId, name);
|
||||
if (!tag) {
|
||||
throw new TagError('not_found');
|
||||
|
||||
Reference in New Issue
Block a user