Skip to content

Add client-side validation (min/max for string and number options) #530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading