Skip to content

Commit

Permalink
add client-side validation (min/max for string and number options)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyper-neutrino committed Jul 14, 2024
1 parent 41a2dab commit 5e3808a
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions src/codeyCommand.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import {
ApplicationCommandOptionBase,
ApplicationCommandOptionWithChoicesAndAutocompleteMixin,
SlashCommandBuilder,
SlashCommandIntegerOption,
SlashCommandNumberOption,
SlashCommandStringOption,
SlashCommandSubcommandBuilder,
SlashCommandSubcommandsOnlyBuilder,
ApplicationCommandOptionWithChoicesAndAutocompleteMixin,
ApplicationCommandOptionBase,
} from '@discordjs/builders';
import {
ApplicationCommandRegistries,
Args,
ArgType,
Args,
ChatInputCommand,
Command as SapphireCommand,
container,
RegisterBehavior,
SapphireClient,
Command as SapphireCommand,
container,
} from '@sapphire/framework';
import { APIMessage, APIApplicationCommandOptionChoice } from 'discord-api-types/v9';
import { APIApplicationCommandOptionChoice, APIMessage } from 'discord-api-types/v9';
import {
ApplicationCommandOptionType,
BaseMessageOptions,
Expand All @@ -24,8 +27,8 @@ import {
MessagePayload,
User,
} from 'discord.js';
import { logger } from './logger/default';
import { CodeyUserError } from './codeyUserError';
import { logger } from './logger/default';

export type SapphireSentMessageType = Message | CommandInteraction;
export type SapphireMessageResponse =
Expand Down Expand Up @@ -85,6 +88,13 @@ export interface CodeyCommandOption {
required: boolean;
/** Mention choices for the field if needed */
choices?: APIApplicationCommandOptionChoice[];
/** Client-side validation options */
validation?: {
/** Minimum length or value */
min?: number;
/** Maximum length or value */
max?: number;
};
}

/** Sets the command option in the slash command builder */
Expand All @@ -93,10 +103,30 @@ const setCommandOption = (
option: CodeyCommandOption,
): SlashCommandBuilder | SlashCommandSubcommandBuilder => {
function setupCommand<T extends ApplicationCommandOptionBase>(commandOption: T): T {
return commandOption
let result = commandOption
.setName(option.name)
.setDescription(option.description)
.setRequired(option.required);

if (result instanceof SlashCommandStringOption && option.validation?.min !== undefined)
result = result.setMinLength(option.validation.min);

if (result instanceof SlashCommandStringOption && option.validation?.max !== undefined)
result = result.setMaxLength(option.validation.max);

if (
(result instanceof SlashCommandNumberOption || result instanceof SlashCommandIntegerOption) &&
option.validation?.min !== undefined
)
result = result.setMinValue(option.validation.min);

if (
(result instanceof SlashCommandNumberOption || result instanceof SlashCommandIntegerOption) &&
option.validation?.max !== undefined
)
result = result.setMaxValue(option.validation.max);

return result;
}

function setupChoices<
Expand All @@ -108,6 +138,7 @@ const setCommandOption = (
? commandOption.addChoices(...(option.choices as APIApplicationCommandOptionChoice<B>[]))
: commandOption;
}

switch (option.type) {
case CodeyCommandOptionType.STRING:
return <SlashCommandBuilder>builder.addStringOption((x) => setupCommand(setupChoices(x)));
Expand Down

0 comments on commit 5e3808a

Please sign in to comment.