From f012bf17b7737346974bdac1d19490df2cebfb01 Mon Sep 17 00:00:00 2001 From: S222em Date: Tue, 26 Apr 2022 10:33:58 +0200 Subject: [PATCH 1/2] refactor: use @sapphire/shapeshift --- package-lock.json | 22 ++++- package.json | 4 +- src/lib/structures/Argument.ts | 165 +++++++++++-------------------- src/lib/structures/Command.ts | 170 +++++++++++++------------------- src/lib/structures/Component.ts | 122 ++++++++++------------- src/lib/structures/Listener.ts | 57 +++++------ src/lib/structures/Plugin.ts | 38 ++++--- 7 files changed, 242 insertions(+), 336 deletions(-) diff --git a/package-lock.json b/package-lock.json index fbc50d1b5..82fbcdcae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,10 +10,10 @@ "license": "ISC", "dependencies": { "@discordjs/rest": "^0.4.0", + "@sapphire/shapeshift": "^2.1.0", "discord-api-types": "^0.26.1", "ms": "^2.1.3", - "tslib": "^2.3.1", - "zod": "^3.11.6" + "tslib": "^2.3.1" }, "devDependencies": { "@commitlint/cli": "^16.2.3", @@ -997,6 +997,15 @@ "npm": ">=7.0.0" } }, + "node_modules/@sapphire/shapeshift": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@sapphire/shapeshift/-/shapeshift-2.1.0.tgz", + "integrity": "sha512-Z1ISLP9pNI3jpKJxx82xtDpEsfomhJ3iPqcKdYLjAHmQP2X5yqQF6phA0Wd+MmdXp081wepOiT59XEN0xdTVvQ==", + "engines": { + "node": ">=v15.0.0", + "npm": ">=7.0.0" + } + }, "node_modules/@sapphire/snowflake": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/@sapphire/snowflake/-/snowflake-3.2.1.tgz", @@ -6980,6 +6989,7 @@ "version": "3.11.6", "resolved": "https://registry.npmjs.org/zod/-/zod-3.11.6.tgz", "integrity": "sha512-daZ80A81I3/9lIydI44motWe6n59kRBfNzTuS2bfzVh1nAXi667TOTWWtatxyG+fwgNUiagSj/CWZwRRbevJIg==", + "dev": true, "funding": { "url": "https://github.com/sponsors/colinhacks" } @@ -7748,6 +7758,11 @@ "resolved": "https://registry.npmjs.org/@sapphire/async-queue/-/async-queue-1.3.1.tgz", "integrity": "sha512-FFTlPOWZX1kDj9xCAsRzH5xEJfawg1lNoYAA+ecOWJMHOfiZYb1uXOI3ne9U4UILSEPwfE68p3T9wUHwIQfR0g==" }, + "@sapphire/shapeshift": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@sapphire/shapeshift/-/shapeshift-2.1.0.tgz", + "integrity": "sha512-Z1ISLP9pNI3jpKJxx82xtDpEsfomhJ3iPqcKdYLjAHmQP2X5yqQF6phA0Wd+MmdXp081wepOiT59XEN0xdTVvQ==" + }, "@sapphire/snowflake": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/@sapphire/snowflake/-/snowflake-3.2.1.tgz", @@ -12218,7 +12233,8 @@ "zod": { "version": "3.11.6", "resolved": "https://registry.npmjs.org/zod/-/zod-3.11.6.tgz", - "integrity": "sha512-daZ80A81I3/9lIydI44motWe6n59kRBfNzTuS2bfzVh1nAXi667TOTWWtatxyG+fwgNUiagSj/CWZwRRbevJIg==" + "integrity": "sha512-daZ80A81I3/9lIydI44motWe6n59kRBfNzTuS2bfzVh1nAXi667TOTWWtatxyG+fwgNUiagSj/CWZwRRbevJIg==", + "dev": true } } } diff --git a/package.json b/package.json index 16cef7589..c8078fa0c 100644 --- a/package.json +++ b/package.json @@ -50,10 +50,10 @@ "license": "ISC", "dependencies": { "@discordjs/rest": "^0.4.0", + "@sapphire/shapeshift": "^2.1.0", "discord-api-types": "^0.26.1", "ms": "^2.1.3", - "tslib": "^2.3.1", - "zod": "^3.11.6" + "tslib": "^2.3.1" }, "devDependencies": { "@commitlint/cli": "^16.2.3", diff --git a/src/lib/structures/Argument.ts b/src/lib/structures/Argument.ts index e23d686a7..6ed5fd02e 100644 --- a/src/lib/structures/Argument.ts +++ b/src/lib/structures/Argument.ts @@ -1,9 +1,9 @@ import type { ApplicationCommandOptionType } from 'discord-api-types/v9'; -import { z } from 'zod'; import type { AutocompleteContext } from './contexts/AutocompleteContext'; -import { Locale, LocaleString } from '../util/common'; +import type { LocaleString } from '../util/common'; import { Logger } from '../util/logger/Logger'; import { commandAndOptionNameRegexp } from '../util/regexes'; +import { s } from '@sapphire/shapeshift'; export enum ArgumentType { 'SUB_COMMAND' = 1, @@ -61,77 +61,42 @@ export interface ArgumentOptions { run?: (ctx: AutocompleteContext) => any; } -const validationSchema = z - .object({ - name: z.string().max(32).regex(commandAndOptionNameRegexp), - nameLocalizations: z - .record( - z - .union([z.string(), z.nativeEnum(Locale)]) - .transform(arg => - typeof arg === 'string' && Object.keys(Locale).includes(arg) - ? Locale[arg] - : arg, - ), - z.string().max(32).regex(commandAndOptionNameRegexp), - ) - .optional(), - description: z.string().max(100), - descriptionLocalizations: z - .record( - z - .union([z.string(), z.nativeEnum(Locale)]) - .transform(arg => - typeof arg === 'string' && Object.keys(Locale).includes(arg) - ? Locale[arg] - : arg, - ), - z.string().max(100), - ) - .optional(), - type: z - .union([z.string(), z.nativeEnum(ArgumentType)]) - .transform(arg => - typeof arg === 'string' && Object.keys(ArgumentType).includes(arg) - ? ArgumentType[arg] - : arg, - ), - required: z.boolean().optional(), - choices: z - .object({ - name: z.string(), - nameLocalizations: z - .record( - z - .union([z.string(), z.nativeEnum(Locale)]) - .transform(arg => - typeof arg === 'string' && Object.keys(Locale).includes(arg) - ? Locale[arg] - : arg, - ), - z.string().max(32).regex(commandAndOptionNameRegexp), - ) - .optional(), - value: z.union([z.string(), z.number()]), - }) - .array() - .optional(), - options: z.any().array().optional(), - arguments: z.any().array().optional(), - channelTypes: z - .union([z.string(), z.nativeEnum(ChannelType)]) - .transform(arg => - typeof arg === 'string' && Object.keys(ChannelType).includes(arg) - ? ChannelType[arg] - : arg, - ) - .array() - .optional(), - minValue: z.number().optional(), - maxValue: z.number().optional(), - run: z.function().optional(), - }) - .passthrough(); +const parser = s.object({ + name: s.string.lengthLe(32).regex(commandAndOptionNameRegexp), + nameLocalizations: s.record( + s.string.lengthLe(32).regex(commandAndOptionNameRegexp), + ).optional, + description: s.string.lengthLe(100), + descriptionLocalizations: s.record(s.string.lengthLe(100)), + type: s.union(s.string, s.nativeEnum(ArgumentType)).transform(value => { + return typeof value === 'string' && + Object.keys(ArgumentType).includes(value) + ? ArgumentType[value] + : value; + }), + required: s.boolean.optional, + choices: s.array( + s.object({ + name: s.string, + nameLocalizations: s.record( + s.string.lengthLe(32).regex(commandAndOptionNameRegexp), + ).optional, + value: s.union(s.string, s.number), + }), + ).optional, + arguments: s.any.optional, + channelTypes: s.array( + s.union(s.string, s.nativeEnum(ChannelType)).transform(value => { + return typeof value === 'string' && + Object.keys(ChannelType).includes(value) + ? ChannelType[value] + : value; + }), + ), + minValue: s.number.optional, + maxValue: s.number.optional, + run: s.any, +}); export class Argument { public name: string; @@ -153,42 +118,30 @@ export class Argument { public run?: (ctx: AutocompleteContext) => any; constructor(options: ArgumentOptions) { - if (options.options) { + try { + const parsed = parser.parse({ ...options, ...this }); + this.name = parsed.name; + this.nameLocalizations = parsed.nameLocalizations; + this.description = parsed.description; + this.descriptionLocalizations = parsed.descriptionLocalizations; + this.type = parsed.type; + this.required = parsed.required; + this.choices = parsed.choices as Array; + this.arguments = parsed.arguments?.map(argument => { + if (argument instanceof Argument) return argument; + else return new Argument(argument); + }); + this.channelTypes = parsed.channelTypes; + this.minValue = parsed.minValue; + this.maxValue = parsed.maxValue; + this.run = parsed.run; + } catch (error) { Logger.warn( - 'The use of ArgumentOptions#options is depracted. Please use ArgumentOptions#arguments instead', + typeof error.code !== 'undefined' ? error.code : '', + error.message, ); - options.arguments = options.options; + if (error.stack) Logger.trace(error.stack); } - - if (this.run) options.run = this.run; - - validationSchema - .parseAsync({ ...options, ...this }) - .then(options => { - this.name = options.name; - this.nameLocalizations = options.nameLocalizations; - this.description = options.description; - this.descriptionLocalizations = options.descriptionLocalizations; - this.type = options.type; - this.required = options.required; - this.choices = options.choices as Array; - this.arguments = options.arguments?.map(argument => { - if (argument instanceof Argument) return argument; - else return new Argument(argument); - }); - this.options = this.arguments; - this.channelTypes = options.channelTypes; - this.minValue = options.minValue; - this.maxValue = options.maxValue; - this.run = options.run; - }) - .catch(error => { - Logger.warn( - typeof error.code !== 'undefined' ? error.code : '', - error.message, - ); - if (error.stack) Logger.trace(error.stack); - }); } public toJSON(): Record { diff --git a/src/lib/structures/Command.ts b/src/lib/structures/Command.ts index e5e110368..72120cd2f 100644 --- a/src/lib/structures/Command.ts +++ b/src/lib/structures/Command.ts @@ -1,9 +1,9 @@ -import { z } from 'zod'; +import { s } from '@sapphire/shapeshift'; import { Argument, ArgumentOptions } from './Argument'; import type { CommandContext } from './contexts/CommandContext'; import { AutoDeferType, GClient } from '../GClient'; import { Commands } from '../managers/CommandManager'; -import { Locale, LocaleString } from '../util/common'; +import type { LocaleString } from '../util/common'; import { Logger } from '../util/logger/Logger'; import { commandAndOptionNameRegexp } from '../util/regexes'; @@ -47,60 +47,32 @@ export interface CommandOptions { onError?: (ctx: CommandContext, error: any) => any; } -const validationSchema = z - .object({ - name: z.string().max(32).regex(commandAndOptionNameRegexp), - nameLocalizations: z - .record( - z - .union([z.string(), z.nativeEnum(Locale)]) - .transform(arg => - typeof arg === 'string' && Object.keys(Locale).includes(arg) - ? Locale[arg] - : arg, - ), - z.string().max(32).regex(commandAndOptionNameRegexp), - ) - .optional(), - description: z.string().max(100).optional(), - descriptionLocalizations: z - .record( - z - .union([z.string(), z.nativeEnum(Locale)]) - .transform(arg => - typeof arg === 'string' && Object.keys(Locale).includes(arg) - ? Locale[arg] - : arg, - ), - z.string().max(100), - ) - .optional(), - type: z - .union([z.string(), z.nativeEnum(CommandType)]) - .transform(arg => - typeof arg === 'string' && Object.keys(CommandType).includes(arg) - ? CommandType[arg] - : arg, - ) - .array() - .nonempty(), - arguments: z.any().array().optional(), - inhibitors: z.any().array().optional(), - guildId: z.string().optional(), - cooldown: z.string().optional(), - autoDefer: z - .union([z.string(), z.nativeEnum(AutoDeferType)]) - .transform(arg => - typeof arg === 'string' && Object.keys(AutoDeferType).includes(arg) - ? AutoDeferType[arg] - : arg, - ) - .optional(), - fileName: z.string().optional(), - run: z.function(), - onError: z.function().optional(), - }) - .passthrough(); +const parser = s.object({ + name: s.string.lengthLe(32).regex(commandAndOptionNameRegexp), + nameLocalizations: s.record( + s.string.lengthLe(32).regex(commandAndOptionNameRegexp), + ).optional, + description: s.string.lengthLe(100), + descriptionLocalizations: s.record(s.string.lengthLe(100)), + type: s.union(s.string, s.nativeEnum(CommandType)).transform(value => { + return typeof value === 'string' && Object.keys(CommandType).includes(value) + ? CommandType[value] + : value; + }), + arguments: s.array(s.any).optional, + inhibitors: s.array(s.any).optional, + guildId: s.string.optional, + cooldown: s.string.optional, + autoDefer: s.union(s.string, s.nativeEnum(AutoDeferType)).transform(value => { + return typeof value === 'string' && + Object.keys(AutoDeferType).includes(value) + ? AutoDeferType[value] + : value; + }), + fileName: s.string.optional, + run: s.any, + onError: s.any, +}); export class Command { public client: GClient; @@ -122,41 +94,37 @@ export class Command { public autoDefer?: AutoDeferType | keyof typeof AutoDeferType; public constructor(options: CommandOptions) { - if (this.run) options.run = this.run; - if (this.onError) options.onError = this.onError; - - validationSchema - .parseAsync({ ...options, ...this }) - .then(options => { - this.name = options.name || Command.defaults?.name; - this.nameLocalizations = - options.nameLocalizations || Command.defaults?.nameLocalizations; - this.description = options.description || Command.defaults?.description; - this.descriptionLocalizations = - options.descriptionLocalizations || - Command.defaults?.descriptionLocalizations; - this.type = options.type || Command.defaults?.type; - this.arguments = options.arguments?.map(argument => { - if (argument instanceof Argument) return argument; - else return new Argument(argument); - }); - this.inhibitors = options.inhibitors || Command.defaults?.inhibitors; - this.guildId = options.guildId || Command.defaults?.guildId; - this.cooldown = options.cooldown || Command.defaults?.cooldown; - this.fileName = options.fileName || Command.defaults?.fileName; - this.run = options.run || Command.defaults?.run; - this.onError = options.onError || Command.defaults?.onError; - this.autoDefer = options.autoDefer || Command.defaults?.autoDefer; - - Commands.register(this); - }) - .catch(error => { - Logger.warn( - typeof error.code !== 'undefined' ? error.code : '', - error.message, - ); - if (error.stack) Logger.trace(error.stack); + try { + const parsed = parser.passthrough.parse({ ...options, ...this }); + + this.name = parsed.name || Command.defaults?.name; + this.nameLocalizations = + parsed.nameLocalizations || Command.defaults?.nameLocalizations; + this.description = parsed.description || Command.defaults?.description; + this.descriptionLocalizations = + parsed.descriptionLocalizations || + Command.defaults?.descriptionLocalizations; + this.type = parsed.type || Command.defaults?.type; + this.arguments = parsed.arguments?.map(argument => { + if (argument instanceof Argument) return argument; + else return new Argument(argument); }); + this.inhibitors = parsed.inhibitors || Command.defaults?.inhibitors; + this.guildId = parsed.guildId || Command.defaults?.guildId; + this.cooldown = parsed.cooldown || Command.defaults?.cooldown; + this.fileName = parsed.fileName || Command.defaults?.fileName; + this.run = parsed.run || Command.defaults?.run; + this.onError = parsed.onError || Command.defaults?.onError; + this.autoDefer = parsed.autoDefer || Command.defaults?.autoDefer; + + Commands.register(this); + } catch (error) { + Logger.warn( + typeof error.code !== 'undefined' ? error.code : '', + error.message, + ); + if (error.stack) Logger.trace(error.stack); + } } public initialize(client: GClient): void { @@ -231,18 +199,14 @@ export class Command { } public static setDefaults(defaults: Partial): void { - validationSchema - .partial() - .parseAsync(defaults) - .then(defaults => { - Command.defaults = defaults as Partial; - }) - .catch(error => { - Logger.warn( - typeof error.code !== 'undefined' ? error.code : '', - error.message, - ); - if (error.stack) Logger.trace(error.stack); - }); + try { + Command.defaults = parser.partial.passthrough.parse(defaults); + } catch (error) { + Logger.warn( + typeof error.code !== 'undefined' ? error.code : '', + error.message, + ); + if (error.stack) Logger.trace(error.stack); + } } } diff --git a/src/lib/structures/Component.ts b/src/lib/structures/Component.ts index d66e14b67..fd7150f90 100644 --- a/src/lib/structures/Component.ts +++ b/src/lib/structures/Component.ts @@ -1,8 +1,9 @@ -import { z } from 'zod'; import type { ComponentContext } from './contexts/ComponentContext'; import { AutoDeferType, GClient } from '../GClient'; import { Components } from '../managers/ComponentManager'; import { Logger } from '../util/logger/Logger'; +import { s } from '@sapphire/shapeshift'; +import { commandAndOptionNameRegexp } from '../util/regexes'; export enum ComponentType { 'BUTTON' = 1, @@ -26,37 +27,27 @@ export interface ComponentOptions { onError?: (interaction: ComponentContext, error: any) => any; } -const validationSchema = z - .object({ - name: z - .string() - .max(32) - .regex(/^[a-z1-9]/), - type: z - .union([z.string(), z.nativeEnum(ComponentType)]) - .transform(arg => - typeof arg === 'string' && Object.keys(ComponentType).includes(arg) - ? ComponentType[arg] - : arg, - ) - .array() - .nonempty(), - inhibitors: z.any().optional(), - guildId: z.string().optional(), - cooldown: z.string().optional(), - autoDefer: z - .union([z.string(), z.nativeEnum(AutoDeferType)]) - .transform(arg => - typeof arg === 'string' && Object.keys(AutoDeferType).includes(arg) - ? AutoDeferType[arg] - : arg, - ) - .optional(), - fileName: z.string().optional(), - run: z.function(), - onError: z.function().optional(), - }) - .passthrough(); +const parser = s.object({ + name: s.string.lengthLe(32).regex(commandAndOptionNameRegexp), + type: s.union(s.string, s.nativeEnum(ComponentType)).transform(value => { + return typeof value === 'string' && + Object.keys(ComponentType).includes(value) + ? ComponentType[value] + : value; + }), + inhibitors: s.array(s.any).optional, + guildId: s.string.optional, + cooldown: s.string.optional, + autoDefer: s.union(s.string, s.nativeEnum(AutoDeferType)).transform(value => { + return typeof value === 'string' && + Object.keys(AutoDeferType).includes(value) + ? AutoDeferType[value] + : value; + }), + fileName: s.string.optional, + run: s.any, + onError: s.any, +}); export class Component { public client: GClient; @@ -74,31 +65,26 @@ export class Component { public autoDefer?: AutoDeferType | keyof typeof AutoDeferType; public constructor(options: ComponentOptions) { - if (this.run) options.run = this.run; - if (this.onError) options.onError = this.onError; - - validationSchema - .parseAsync({ ...options, ...this }) - .then(options => { - this.name = options.name || Component.defaults?.name; - this.type = options.type || Component.defaults?.type; - this.inhibitors = options.inhibitors || Component.defaults?.inhibitors; - this.guildId = options.guildId || Component.defaults?.guildId; - this.cooldown = options.cooldown || Component.defaults?.cooldown; - this.fileName = options.fileName || Component.defaults?.fileName; - this.run = options.run || Component.defaults?.run; - this.onError = options.onError || Component.defaults?.onError; - this.autoDefer = options.autoDefer || Component.defaults?.autoDefer; - - Components.register(this); - }) - .catch(error => { - Logger.warn( - typeof error.code !== 'undefined' ? error.code : '', - error.message, - ); - if (error.stack) Logger.trace(error.stack); - }); + try { + const parsed = parser.passthrough.parse({ ...options, ...this }); + this.name = parsed.name || Component.defaults?.name; + this.type = parsed.type || Component.defaults?.type; + this.inhibitors = parsed.inhibitors || Component.defaults?.inhibitors; + this.guildId = parsed.guildId || Component.defaults?.guildId; + this.cooldown = parsed.cooldown || Component.defaults?.cooldown; + this.fileName = parsed.fileName || Component.defaults?.fileName; + this.run = parsed.run || Component.defaults?.run; + this.onError = parsed.onError || Component.defaults?.onError; + this.autoDefer = parsed.autoDefer || Component.defaults?.autoDefer; + + Components.register(this); + } catch (error) { + Logger.warn( + typeof error.code !== 'undefined' ? error.code : '', + error.message, + ); + if (error.stack) Logger.trace(error.stack); + } } public initialize(client: GClient): void { @@ -151,18 +137,14 @@ export class Component { } public static setDefaults(defaults: Partial): void { - validationSchema - .partial() - .parseAsync(defaults) - .then(defaults => { - Component.defaults = defaults as Partial; - }) - .catch(error => { - Logger.warn( - typeof error.code !== 'undefined' ? error.code : '', - error.message, - ); - if (error.stack) Logger.trace(error.stack); - }); + try { + Component.defaults = parser.partial.passthrough.parse(defaults); + } catch (error) { + Logger.warn( + typeof error.code !== 'undefined' ? error.code : '', + error.message, + ); + if (error.stack) Logger.trace(error.stack); + } } } diff --git a/src/lib/structures/Listener.ts b/src/lib/structures/Listener.ts index 80fd4ddab..6b46174e0 100644 --- a/src/lib/structures/Listener.ts +++ b/src/lib/structures/Listener.ts @@ -1,5 +1,5 @@ import type { ClientEvents, WSEventType } from 'discord.js'; -import { z } from 'zod'; +import { s } from '@sapphire/shapeshift'; import type { GClient } from '../GClient'; import { Listeners } from '../managers/ListenerManager'; import { Logger } from '../util/logger/Logger'; @@ -18,16 +18,15 @@ export interface ListenerOptions< ) => any; } -const validationSchema = z - .object({ - event: z.string(), - name: z.string(), - once: z.boolean().optional(), - ws: z.boolean().optional().default(false), - fileName: z.string().optional(), - run: z.function(), - }) - .passthrough(); +const parser = s.object({ + event: s.string, + name: s.string, + once: s.boolean.optional.default(false), + ws: s.boolean.optional.default(false), + fileName: s.string.optional, + run: s.any, +}); + export class Listener< WS extends boolean = boolean, Event extends WS extends true @@ -45,27 +44,23 @@ export class Listener< public reloading = false; public constructor(options: ListenerOptions) { - if (this.run) options.run = this.run; - - validationSchema - .parseAsync({ ...options, ...this }) - .then(options => { - this.event = options.event; - this.name = options.name; - this.once = options.once; - this.ws = options.ws as WS; - this.fileName = options.fileName; - this.run = options.run; + try { + const parsed = parser.passthrough.parse({ ...options, ...this }); + this.event = parsed.event; + this.name = parsed.name; + this.once = parsed.once; + this.ws = parsed.ws as WS; + this.fileName = parsed.fileName; + this.run = parsed.run; - Listeners.register(this); - }) - .catch(error => { - Logger.warn( - typeof error.code !== 'undefined' ? error.code : '', - error.message, - ); - if (error.stack) Logger.trace(error.stack); - }); + Listeners.register(this); + } catch (error) { + Logger.warn( + typeof error.code !== 'undefined' ? error.code : '', + error.message, + ); + if (error.stack) Logger.trace(error.stack); + } } public initialize(client: GClient): void { diff --git a/src/lib/structures/Plugin.ts b/src/lib/structures/Plugin.ts index c9ab5a932..f79a6223f 100644 --- a/src/lib/structures/Plugin.ts +++ b/src/lib/structures/Plugin.ts @@ -1,34 +1,30 @@ -import { z } from 'zod'; +import { s } from '@sapphire/shapeshift'; import type { GClient } from '../GClient'; import { Plugins } from '../managers/PluginManager'; import { Logger } from '../util/logger/Logger'; -const validationSchema = z - .object({ - name: z.string(), - run: z.function(), - }) - .passthrough(); +const parser = s.object({ + name: s.string, + run: s.any, +}); export class Plugin { public name: string; public run: (client: GClient) => any; public constructor(name: string, run: (client: GClient) => any) { - validationSchema - .parseAsync({ name, run, ...this }) - .then(options => { - this.name = options.name; - this.run = options.run; + try { + const parsed = parser.passthrough.parse({ name, run, ...this }); + this.name = parsed.name; + this.run = parsed.run; - Plugins.register(this); - }) - .catch(error => { - Logger.warn( - typeof error.code !== 'undefined' ? error.code : '', - error.message, - ); - if (error.stack) Logger.trace(error.stack); - }); + Plugins.register(this); + } catch (error) { + Logger.warn( + typeof error.code !== 'undefined' ? error.code : '', + error.message, + ); + if (error.stack) Logger.trace(error.stack); + } } } From 2b7cb0c6f09a37533a7c6e2ca390454546b7c25d Mon Sep 17 00:00:00 2001 From: S222em Date: Fri, 13 May 2022 14:20:30 +0200 Subject: [PATCH 2/2] chore: update @sapphire/shapeshift to v3 --- package-lock.json | 14 +++++++------- package.json | 2 +- src/lib/structures/Argument.ts | 10 +++++----- src/lib/structures/Command.ts | 8 ++++---- src/lib/structures/Component.ts | 2 +- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/package-lock.json b/package-lock.json index 82fbcdcae..ca2658fe1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "ISC", "dependencies": { "@discordjs/rest": "^0.4.0", - "@sapphire/shapeshift": "^2.1.0", + "@sapphire/shapeshift": "^3.0.0", "discord-api-types": "^0.26.1", "ms": "^2.1.3", "tslib": "^2.3.1" @@ -998,9 +998,9 @@ } }, "node_modules/@sapphire/shapeshift": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/@sapphire/shapeshift/-/shapeshift-2.1.0.tgz", - "integrity": "sha512-Z1ISLP9pNI3jpKJxx82xtDpEsfomhJ3iPqcKdYLjAHmQP2X5yqQF6phA0Wd+MmdXp081wepOiT59XEN0xdTVvQ==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@sapphire/shapeshift/-/shapeshift-3.0.0.tgz", + "integrity": "sha512-LTVj/a70UDzjOFaViMGpVzSzKYD2pBk0TmZIHnBf4vnytV7TK/L6XVN6hslq7R+qwRZyL/mzIldu6mAV6r7vzA==", "engines": { "node": ">=v15.0.0", "npm": ">=7.0.0" @@ -7759,9 +7759,9 @@ "integrity": "sha512-FFTlPOWZX1kDj9xCAsRzH5xEJfawg1lNoYAA+ecOWJMHOfiZYb1uXOI3ne9U4UILSEPwfE68p3T9wUHwIQfR0g==" }, "@sapphire/shapeshift": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/@sapphire/shapeshift/-/shapeshift-2.1.0.tgz", - "integrity": "sha512-Z1ISLP9pNI3jpKJxx82xtDpEsfomhJ3iPqcKdYLjAHmQP2X5yqQF6phA0Wd+MmdXp081wepOiT59XEN0xdTVvQ==" + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@sapphire/shapeshift/-/shapeshift-3.0.0.tgz", + "integrity": "sha512-LTVj/a70UDzjOFaViMGpVzSzKYD2pBk0TmZIHnBf4vnytV7TK/L6XVN6hslq7R+qwRZyL/mzIldu6mAV6r7vzA==" }, "@sapphire/snowflake": { "version": "3.2.1", diff --git a/package.json b/package.json index c8078fa0c..f50c018e2 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "license": "ISC", "dependencies": { "@discordjs/rest": "^0.4.0", - "@sapphire/shapeshift": "^2.1.0", + "@sapphire/shapeshift": "^3.0.0", "discord-api-types": "^0.26.1", "ms": "^2.1.3", "tslib": "^2.3.1" diff --git a/src/lib/structures/Argument.ts b/src/lib/structures/Argument.ts index 6ed5fd02e..09aaf3f7e 100644 --- a/src/lib/structures/Argument.ts +++ b/src/lib/structures/Argument.ts @@ -62,12 +62,12 @@ export interface ArgumentOptions { } const parser = s.object({ - name: s.string.lengthLe(32).regex(commandAndOptionNameRegexp), + name: s.string.lengthLessThanOrEqual(32).regex(commandAndOptionNameRegexp), nameLocalizations: s.record( - s.string.lengthLe(32).regex(commandAndOptionNameRegexp), + s.string.lengthLessThanOrEqual(32).regex(commandAndOptionNameRegexp), ).optional, - description: s.string.lengthLe(100), - descriptionLocalizations: s.record(s.string.lengthLe(100)), + description: s.string.lengthLessThanOrEqual(100), + descriptionLocalizations: s.record(s.string.lengthLessThanOrEqual(100)), type: s.union(s.string, s.nativeEnum(ArgumentType)).transform(value => { return typeof value === 'string' && Object.keys(ArgumentType).includes(value) @@ -79,7 +79,7 @@ const parser = s.object({ s.object({ name: s.string, nameLocalizations: s.record( - s.string.lengthLe(32).regex(commandAndOptionNameRegexp), + s.string.lengthLessThanOrEqual(32).regex(commandAndOptionNameRegexp), ).optional, value: s.union(s.string, s.number), }), diff --git a/src/lib/structures/Command.ts b/src/lib/structures/Command.ts index 72120cd2f..a9d61f1b0 100644 --- a/src/lib/structures/Command.ts +++ b/src/lib/structures/Command.ts @@ -48,12 +48,12 @@ export interface CommandOptions { } const parser = s.object({ - name: s.string.lengthLe(32).regex(commandAndOptionNameRegexp), + name: s.string.lengthLessThanOrEqual(32).regex(commandAndOptionNameRegexp), nameLocalizations: s.record( - s.string.lengthLe(32).regex(commandAndOptionNameRegexp), + s.string.lengthLessThanOrEqual(32).regex(commandAndOptionNameRegexp), ).optional, - description: s.string.lengthLe(100), - descriptionLocalizations: s.record(s.string.lengthLe(100)), + description: s.string.lengthLessThanOrEqual(100), + descriptionLocalizations: s.record(s.string.lengthLessThanOrEqual(100)), type: s.union(s.string, s.nativeEnum(CommandType)).transform(value => { return typeof value === 'string' && Object.keys(CommandType).includes(value) ? CommandType[value] diff --git a/src/lib/structures/Component.ts b/src/lib/structures/Component.ts index fd7150f90..97bc8777a 100644 --- a/src/lib/structures/Component.ts +++ b/src/lib/structures/Component.ts @@ -28,7 +28,7 @@ export interface ComponentOptions { } const parser = s.object({ - name: s.string.lengthLe(32).regex(commandAndOptionNameRegexp), + name: s.string.lengthLessThanOrEqual(32).regex(commandAndOptionNameRegexp), type: s.union(s.string, s.nativeEnum(ComponentType)).transform(value => { return typeof value === 'string' && Object.keys(ComponentType).includes(value)