Skip to content

Commit c6a2b95

Browse files
committed
fix: handle correctly options
1 parent b1e66f0 commit c6a2b95

File tree

3 files changed

+98
-101
lines changed

3 files changed

+98
-101
lines changed

src/commands/applications/shared.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ export type MessageCommandOptionErrors =
5454
| ['STRING_MIN_LENGTH', min: number]
5555
| ['STRING_MAX_LENGTH', max: number]
5656
| ['STRING_INVALID_CHOICE', choices: readonly { name: string; value: string }[]]
57-
| ['NUMBER_NAN', value: string | undefined]
57+
| ['NUMBER_NAN', value: string]
5858
| ['NUMBER_MIN_VALUE', min: number]
5959
| ['NUMBER_MAX_VALUE', max: number]
6060
| ['NUMBER_INVALID_CHOICE', choices: readonly { name: string; value: number }[]]
61+
| ['NUMBER_OUT_OF_BOUNDS', value: number]
6162
| ['OPTION_REQUIRED']
6263
| ['UNKNOWN', error: unknown];
6364

src/commands/handle.ts

Lines changed: 94 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import type { Client, WorkerClient } from '../client';
2323
import { type MessageStructure, type OptionResolverStructure, Transformers } from '../client/transformers';
2424
import type { MakeRequired } from '../common';
25+
import { INTEGER_OPTION_VALUE_LIMIT } from '../common/it/constants';
2526
import { ComponentContext, ModalContext } from '../components';
2627
import {
2728
AutocompleteInteraction,
@@ -635,7 +636,7 @@ export class HandleCommand {
635636
async argsOptionsParser(
636637
command: Command | SubCommand,
637638
message: GatewayMessageCreateDispatchData,
638-
args: Partial<Record<string, string>>,
639+
args: Record<string, string>,
639640
resolved: MakeRequired<ContextOptionsResolved>,
640641
) {
641642
const options: APIApplicationCommandInteractionDataOption[] = [];
@@ -649,6 +650,7 @@ export class HandleCommand {
649650
type: ApplicationCommandOptionType;
650651
})[]) {
651652
try {
653+
if (!args[i.name] && i.type !== ApplicationCommandOptionType.Attachment) continue;
652654
let value: string | boolean | number | undefined;
653655
switch (i.type) {
654656
case ApplicationCommandOptionType.Attachment:
@@ -658,9 +660,7 @@ export class HandleCommand {
658660
}
659661
break;
660662
case ApplicationCommandOptionType.Boolean:
661-
if (args[i.name]) {
662-
value = ['yes', 'y', 'true', 'treu'].includes(args[i.name]!.toLowerCase());
663-
}
663+
value = ['yes', 'y', 'true', 'treu'].includes(args[i.name].toLowerCase());
664664
break;
665665
case ApplicationCommandOptionType.Channel:
666666
{
@@ -670,24 +670,22 @@ export class HandleCommand {
670670
if (!rawQuery) continue;
671671
const channel =
672672
(await this.client.cache.channels?.raw(rawQuery)) ?? (await this.fetchChannel(i, rawQuery));
673-
if (channel) {
674-
if ('channel_types' in i) {
675-
if (!(i as SeyfertChannelOption).channel_types!.includes(channel.type)) {
676-
if (i.required)
677-
errors.push({
678-
name: i.name,
679-
error: `The entered channel type is not one of ${(i as SeyfertChannelOption)
680-
.channel_types!.map(t => ChannelType[t])
681-
.join(', ')}`,
682-
fullError: ['CHANNEL_TYPES', (i as SeyfertChannelOption).channel_types!],
683-
});
684-
break;
685-
}
673+
if (!channel) break;
674+
if ('channel_types' in i) {
675+
if (!(i as SeyfertChannelOption).channel_types!.includes(channel.type)) {
676+
errors.push({
677+
name: i.name,
678+
error: `The entered channel type is not one of ${(i as SeyfertChannelOption)
679+
.channel_types!.map(t => ChannelType[t])
680+
.join(', ')}`,
681+
fullError: ['CHANNEL_TYPES', (i as SeyfertChannelOption).channel_types!],
682+
});
683+
break;
686684
}
687-
value = channel.id;
688-
//discord funny memoentnt!!!!!!!!
689-
resolved.channels[channel.id] = channel as APIInteractionDataResolvedChannel;
690685
}
686+
value = channel.id;
687+
//discord funny memoentnt!!!!!!!!
688+
resolved.channels[channel.id] = channel as APIInteractionDataResolvedChannel;
691689
}
692690
break;
693691
case ApplicationCommandOptionType.Mentionable:
@@ -754,115 +752,111 @@ export class HandleCommand {
754752
break;
755753
case ApplicationCommandOptionType.String:
756754
{
757-
value = args[i.name];
758755
const option = i as SeyfertStringOption;
759-
if (!value) break;
760-
if (option.min_length) {
761-
if (value.length < option.min_length) {
762-
value = undefined;
763-
if (i.required)
764-
errors.push({
765-
name: i.name,
766-
error: `The entered string has less than ${option.min_length} characters. The minimum required is ${option.min_length} characters.`,
767-
fullError: ['STRING_MIN_LENGTH', option.min_length],
768-
});
756+
if (option.choices?.length) {
757+
const choice = option.choices.find(x => x.name === args[i.name]);
758+
if (!choice) {
759+
errors.push({
760+
name: i.name,
761+
error: `The entered choice is invalid. Please choose one of the following options: ${option.choices
762+
.map(x => x.name)
763+
.join(', ')}`,
764+
fullError: ['STRING_INVALID_CHOICE', option.choices],
765+
});
769766
break;
770767
}
768+
value = choice.value;
769+
break;
771770
}
772-
if (option.max_length) {
773-
if (value.length > option.max_length) {
774-
value = undefined;
775-
if (i.required)
776-
errors.push({
777-
name: i.name,
778-
error: `The entered string has more than ${option.max_length} characters. The maximum required is ${option.max_length} characters.`,
779-
fullError: ['STRING_MAX_LENGTH', option.max_length],
780-
});
771+
if (option.min_length !== undefined) {
772+
if (args[i.name].length < option.min_length) {
773+
errors.push({
774+
name: i.name,
775+
error: `The entered string has less than ${option.min_length} characters. The minimum required is ${option.min_length} characters`,
776+
fullError: ['STRING_MIN_LENGTH', option.min_length],
777+
});
781778
break;
782779
}
783780
}
784-
if (option.choices?.length) {
785-
const choice = option.choices.find(x => x.name === value);
786-
if (!choice) {
787-
value = undefined;
788-
if (i.required)
789-
errors.push({
790-
name: i.name,
791-
error: `The entered choice is invalid. Please choose one of the following options: ${option.choices
792-
.map(x => x.name)
793-
.join(', ')}.`,
794-
fullError: ['STRING_INVALID_CHOICE', option.choices],
795-
});
781+
if (option.max_length !== undefined) {
782+
if (args[i.name].length > option.max_length) {
783+
errors.push({
784+
name: i.name,
785+
error: `The entered string has more than ${option.max_length} characters. The maximum required is ${option.max_length} characters`,
786+
fullError: ['STRING_MAX_LENGTH', option.max_length],
787+
});
796788
break;
797789
}
798-
value = choice.value;
799790
}
791+
value = args[i.name];
800792
}
801793
break;
802794
case ApplicationCommandOptionType.Number:
803795
case ApplicationCommandOptionType.Integer:
804796
{
805797
const option = i as SeyfertNumberOption | SeyfertIntegerOption;
806-
if (!option.choices?.length) {
807-
value = Number(args[i.name]);
808-
if (args[i.name] === undefined) {
809-
value = undefined;
798+
if (option.choices?.length) {
799+
const choice = option.choices.find(x => x.name === args[i.name]);
800+
if (!choice) {
801+
errors.push({
802+
name: i.name,
803+
error: `The entered choice is invalid. Please choose one of the following options: ${option.choices
804+
.map(x => x.name)
805+
.join(', ')}`,
806+
fullError: ['NUMBER_INVALID_CHOICE', option.choices],
807+
});
810808
break;
811809
}
812-
if (Number.isNaN(value)) {
810+
value = choice.value;
811+
break;
812+
}
813+
value =
814+
i.type === ApplicationCommandOptionType.Integer
815+
? Math.trunc(Number(args[i.name]))
816+
: Number(args[i.name]);
817+
if (Number.isNaN(value)) {
818+
value = undefined;
819+
errors.push({
820+
name: i.name,
821+
error: 'The entered choice is an invalid number',
822+
fullError: ['NUMBER_NAN', args[i.name]],
823+
});
824+
break;
825+
}
826+
if (value <= -INTEGER_OPTION_VALUE_LIMIT || value >= INTEGER_OPTION_VALUE_LIMIT) {
827+
value = undefined;
828+
errors.push({
829+
name: i.name,
830+
error: 'The entered number must be between -2^53 and 2^53',
831+
fullError: ['NUMBER_OUT_OF_BOUNDS', INTEGER_OPTION_VALUE_LIMIT],
832+
});
833+
break;
834+
}
835+
if (option.min_value !== undefined) {
836+
if (value < option.min_value) {
813837
value = undefined;
814-
if (i.required)
815-
errors.push({
816-
name: i.name,
817-
error: 'The entered choice is an invalid number.',
818-
fullError: ['NUMBER_NAN', args[i.name]],
819-
});
838+
errors.push({
839+
name: i.name,
840+
error: `The entered number is less than ${option.min_value}. The minimum allowed is ${option.min_value}`,
841+
fullError: ['NUMBER_MIN_VALUE', option.min_value],
842+
});
820843
break;
821844
}
822-
if (option.min_value) {
823-
if (value < option.min_value) {
824-
value = undefined;
825-
if (i.required)
826-
errors.push({
827-
name: i.name,
828-
error: `The entered number is less than ${option.min_value}. The minimum allowed is ${option.min_value}`,
829-
fullError: ['NUMBER_MIN_VALUE', option.min_value],
830-
});
831-
break;
832-
}
833-
}
834-
if (option.max_value) {
835-
if (value > option.max_value) {
836-
value = undefined;
837-
if (i.required)
838-
errors.push({
839-
name: i.name,
840-
error: `The entered number is greater than ${option.max_value}. The maximum allowed is ${option.max_value}`,
841-
fullError: ['NUMBER_MAX_VALUE', option.max_value],
842-
});
843-
break;
844-
}
845-
}
846-
break;
847845
}
848-
const choice = option.choices.find(x => x.name === args[i.name]);
849-
if (!choice) {
850-
value = undefined;
851-
if (i.required)
846+
if (option.max_value !== undefined) {
847+
if (value > option.max_value) {
848+
value = undefined;
852849
errors.push({
853850
name: i.name,
854-
error: `The entered choice is invalid. Please choose one of the following options: ${option.choices
855-
.map(x => x.name)
856-
.join(', ')}.`,
857-
fullError: ['NUMBER_INVALID_CHOICE', option.choices],
851+
error: `The entered number is greater than ${option.max_value}. The maximum allowed is ${option.max_value}`,
852+
fullError: ['NUMBER_MAX_VALUE', option.max_value],
858853
});
854+
break;
855+
}
859856
break;
860857
}
861-
value = choice.value;
862858
}
863859
break;
864-
default:
865-
break;
866860
}
867861
if (value !== undefined) {
868862
options.push({

src/common/it/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ export const DiscordEpoch = 1420070400000n;
3838
export const BASE_HOST = 'https://discord.com';
3939
export const BASE_URL = `${BASE_HOST}/api`;
4040
export const CDN_URL = 'https://cdn.discordapp.com';
41+
42+
export const INTEGER_OPTION_VALUE_LIMIT = 2 ** 53;

0 commit comments

Comments
 (0)