Skip to content

Commit

Permalink
feat(errors): expose all identifiers in an enum (#161)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Changed UserError identifier from `'ArgumentBooleanInvalidBoolean'` to `Identifiers.ArgumentBoolean`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentCategoryChannelInvalidChannel'` to `Identifiers.ArgumentCategoryChannel`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentChannelMissingChannel'` to `Identifiers.ArgumentChannel`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentDateInvalidNumber'` to `Identifiers.ArgumentDate`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentDateTooSmall'` to `Identifiers.ArgumentDateTooSmall`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentDateTooBig'` to `Identifiers.ArgumentDateTooBig`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentDMChannelInvalidChannel'` to `Identifiers.ArgumentDMChannel`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentFloatInvalidFloat'` to `Identifiers.ArgumentFloat`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentFloatTooSmall'` to `Identifiers.ArgumentFloatTooSmall`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentFloatTooBig'` to `Identifiers.ArgumentFloatTooBig`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentGuildChannelMissingGuild'` to `Identifiers.ArgumentGuildChannelMissingGuild`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentGuildChannelUnknownChannel'` to `Identifiers.ArgumentGuildChannel`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentHyperlinkInvalidURL'` to `Identifiers.ArgumentHyperlink`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentIntegerInvalidNumber'` to `Identifiers.ArgumentInteger`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentIntegerTooSmall'` to `Identifiers.ArgumentIntegerTooSmall`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentIntegerTooBig'` to `Identifiers.ArgumentIntegerTooBig`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentMemberMissingGuild'` to `Identifiers.ArgumentMemberMissingGuild`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentMemberUnknownMember'` to `Identifiers.ArgumentMember`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentMessageUnknownMessage'` to `Identifiers.Message`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentNewsChannelInvalidChannel'` to `Identifiers.NewsChannel`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentNumberInvalidNumber'` to `Identifiers.Number`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentNumberTooSmall'` to `Identifiers.ArgumentNumberTooSmall`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentNumberTooBig'` to `Identifiers.ArgumentNumberTooBig`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentRoleMissingGuild'` to `Identifiers.ArgumentRoleMissingGuild`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentRoleUnknownRole'` to `Identifiers.Role`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentStringTooShort'` to `Identifiers.ArgumentStringTooShort`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentStringTooLong'` to `Identifiers.ArgumentStringTooLong`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentTextChannelInvalidChannel'` to `Identifiers.TextChannel`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentUserUnknownUser'` to `Identifiers.User`.
BREAKING CHANGE: Changed UserError identifier from `'ArgumentVoiceChannelInvalidChannel'` to `Identifiers.VoiceChannel`.
BREAKING CHANGE: Changed UserError identifier from `'CommandDisabled'` to `Identifiers.CommandDisabled`.
BREAKING CHANGE: Changed UserError identifier from `'UnavailableArgument'` to `Identifiers.ArgsUnavailable`.
BREAKING CHANGE: Changed UserError identifier from `'MissingArguments'` to `Identifiers.ArgsMissing`.
BREAKING CHANGE: Changed UserError identifier from `'Cooldown'` to `Identifiers.PreconditionCooldown`.
BREAKING CHANGE: Changed UserError identifier from `'DMOnly'` to `Identifiers.PreconditionDMOnly`.
BREAKING CHANGE: Changed UserError identifier from `'GuildOnly'` to `Identifiers.PreconditionGuildOnly`.
BREAKING CHANGE: Changed UserError identifier from `'NSFW'` to `Identifiers.PreconditionNSFW`.
BREAKING CHANGE: Changed UserError identifier from `'Permissions'` to `Identifiers.PreconditionPermissions`.
  • Loading branch information
kyranet authored Feb 6, 2021
1 parent e866d04 commit 3371f35
Show file tree
Hide file tree
Showing 28 changed files with 111 additions and 106 deletions.
2 changes: 1 addition & 1 deletion src/arguments/CoreBoolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export class CoreArgument extends Argument<boolean> {
if (truths.includes(boolean)) return this.ok(true);
if (falses.includes(boolean)) return this.ok(false);

return this.error({ parameter, identifier: 'ArgumentBooleanInvalidBoolean', message: 'The argument did not resolve to a boolean.', context });
return this.error({ parameter, message: 'The argument did not resolve to a boolean.', context });
}
}
7 changes: 1 addition & 6 deletions src/arguments/CoreCategoryChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ export class CoreArgument extends ExtendedArgument<'guildChannel', CategoryChann
public handle(channel: GuildChannel, context: ExtendedArgumentContext): ArgumentResult<CategoryChannel> {
return isCategoryChannel(channel)
? this.ok(channel)
: this.error({
parameter: context.parameter,
identifier: 'ArgumentCategoryChannelInvalidChannel',
message: 'The argument did not resolve to a category channel.',
context
});
: this.error({ parameter: context.parameter, message: 'The argument did not resolve to a category channel.', context });
}
}
12 changes: 1 addition & 11 deletions src/arguments/CoreChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@ export class CoreArgument extends Argument<Channel> {

public run(parameter: string, context: ArgumentContext): ArgumentResult<Channel> {
const channel = (context.message.guild ? context.message.guild.channels : this.context.client.channels).cache.get(parameter);

if (!channel) {
return this.error({
parameter,
identifier: 'ArgumentChannelMissingChannel',
message: 'The argument did not resolve to a channel.',
context
});
}

return this.ok(channel);
return channel ? this.ok(channel) : this.error({ parameter, message: 'The argument did not resolve to a channel.', context });
}
}
7 changes: 1 addition & 6 deletions src/arguments/CoreDMChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ export class CoreArgument extends ExtendedArgument<'channel', DMChannel> {
public handle(channel: Channel, context: ExtendedArgumentContext): ArgumentResult<DMChannel> {
return isDMChannel(channel)
? this.ok(channel)
: this.error({
parameter: context.parameter,
identifier: 'ArgumentDMChannelInvalidChannel',
message: 'The argument did not resolve to a DM channel.',
context
});
: this.error({ parameter: context.parameter, message: 'The argument did not resolve to a DM channel.', context });
}
}
6 changes: 3 additions & 3 deletions src/arguments/CoreDate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PieceContext } from '@sapphire/pieces';
import { Identifiers } from '../lib/errors/Identifiers';
import { Argument, ArgumentContext, ArgumentResult } from '../lib/structures/Argument';

export class CoreArgument extends Argument<Date> {
Expand All @@ -13,18 +14,17 @@ export class CoreArgument extends Argument<Date> {
if (Number.isNaN(time)) {
return this.error({
parameter,
identifier: 'ArgumentDateInvalidNumber',
message: 'The argument did not resolve to a valid date.',
context
});
}

if (typeof context.minimum === 'number' && time < context.minimum) {
return this.error({ parameter, identifier: 'ArgumentDateTooSmall', message: 'The argument is too small.', context });
return this.error({ parameter, identifier: Identifiers.ArgumentDateTooSmall, message: 'The argument is too small.', context });
}

if (typeof context.maximum === 'number' && time > context.maximum) {
return this.error({ parameter, identifier: 'ArgumentDateTooBig', message: 'The argument is too big.', context });
return this.error({ parameter, identifier: Identifiers.ArgumentDateTooBig, message: 'The argument is too big.', context });
}

return this.ok(parsed);
Expand Down
12 changes: 4 additions & 8 deletions src/arguments/CoreFloat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PieceContext } from '@sapphire/pieces';
import { Identifiers } from '../lib/errors/Identifiers';
import { Argument, ArgumentContext, ArgumentResult } from '../lib/structures/Argument';

export class CoreArgument extends Argument<number> {
Expand All @@ -10,18 +11,13 @@ export class CoreArgument extends Argument<number> {
const parsed = Number(parameter);

if (Number.isNaN(parsed)) {
return this.error({
parameter,
identifier: 'ArgumentFloatInvalidFloat',
message: 'The argument did not resolve to a valid floating point number.',
context
});
return this.error({ parameter, message: 'The argument did not resolve to a valid floating point number.', context });
}

if (typeof context.minimum === 'number' && parsed < context.minimum) {
return this.error({
parameter,
identifier: 'ArgumentFloatTooSmall',
identifier: Identifiers.ArgumentFloatTooSmall,
message: `The argument must be greater than ${context.minimum}.`,
context
});
Expand All @@ -30,7 +26,7 @@ export class CoreArgument extends Argument<number> {
if (typeof context.maximum === 'number' && parsed > context.maximum) {
return this.error({
parameter,
identifier: 'ArgumentFloatTooBig',
identifier: Identifiers.ArgumentFloatTooBig,
message: `The argument must be less than ${context.maximum}.`,
context
});
Expand Down
12 changes: 3 additions & 9 deletions src/arguments/CoreGuildChannel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ChannelMentionRegex, SnowflakeRegex } from '@sapphire/discord-utilities';
import type { PieceContext } from '@sapphire/pieces';
import type { Guild, GuildChannel } from 'discord.js';
import { Identifiers } from '../lib/errors/Identifiers';
import { Argument, ArgumentContext, ArgumentResult } from '../lib/structures/Argument';

export class CoreArgument extends Argument<GuildChannel> {
Expand All @@ -13,21 +14,14 @@ export class CoreArgument extends Argument<GuildChannel> {
if (!guild) {
return this.error({
parameter,
identifier: 'ArgumentGuildChannelMissingGuild',
identifier: Identifiers.ArgumentGuildChannelMissingGuild,
message: 'The argument must be run in a guild.',
context
});
}

const channel = this.resolveByID(parameter, guild) ?? this.resolveByQuery(parameter, guild);
return channel
? this.ok(channel)
: this.error({
parameter,
identifier: 'ArgumentGuildChannelUnknownChannel',
message: 'The argument did not resolve to a guild channel.',
context
});
return channel ? this.ok(channel) : this.error({ parameter, message: 'The argument did not resolve to a guild channel.', context });
}

private resolveByID(argument: string, guild: Guild): GuildChannel | null {
Expand Down
7 changes: 1 addition & 6 deletions src/arguments/CoreHyperlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ export class CoreArgument extends Argument<URL> {
try {
return this.ok(new URL(parameter));
} catch {
return this.error({
parameter,
identifier: 'ArgumentHyperlinkInvalidURL',
message: 'The argument did not resolve to a valid URL.',
context
});
return this.error({ parameter, message: 'The argument did not resolve to a valid URL.', context });
}
}
}
6 changes: 3 additions & 3 deletions src/arguments/CoreInteger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PieceContext } from '@sapphire/pieces';
import { Identifiers } from '../lib/errors/Identifiers';
import { Argument, ArgumentContext, ArgumentResult } from '../lib/structures/Argument';

export class CoreArgument extends Argument<number> {
Expand All @@ -12,7 +13,6 @@ export class CoreArgument extends Argument<number> {
if (!Number.isInteger(parsed)) {
return this.error({
parameter,
identifier: 'ArgumentIntegerInvalidNumber',
message: 'The argument did not resolve to an integer.',
context
});
Expand All @@ -21,7 +21,7 @@ export class CoreArgument extends Argument<number> {
if (typeof context.minimum === 'number' && parsed < context.minimum) {
return this.error({
parameter,
identifier: 'ArgumentIntegerTooSmall',
identifier: Identifiers.ArgumentIntegerTooSmall,
message: `The argument must be greater than ${context.minimum}.`,
context
});
Expand All @@ -30,7 +30,7 @@ export class CoreArgument extends Argument<number> {
if (typeof context.maximum === 'number' && parsed > context.maximum) {
return this.error({
parameter,
identifier: 'ArgumentIntegerTooBig',
identifier: Identifiers.ArgumentIntegerTooBig,
message: `The argument must be less than ${context.maximum}.`,
context
});
Expand Down
12 changes: 8 additions & 4 deletions src/arguments/CoreMember.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SnowflakeRegex, UserOrMemberMentionRegex } from '@sapphire/discord-utilities';
import type { PieceContext } from '@sapphire/pieces';
import type { Guild, GuildMember } from 'discord.js';
import { Identifiers } from '../lib/errors/Identifiers';
import { Argument, ArgumentContext, AsyncArgumentResult } from '../lib/structures/Argument';

export class CoreArgument extends Argument<GuildMember> {
Expand All @@ -11,13 +12,16 @@ export class CoreArgument extends Argument<GuildMember> {
public async run(parameter: string, context: ArgumentContext): AsyncArgumentResult<GuildMember> {
const { guild } = context.message;
if (!guild) {
return this.error({ parameter, identifier: 'ArgumentMemberMissingGuild', message: 'The argument must be run on a guild.', context });
return this.error({
parameter,
identifier: Identifiers.ArgumentMemberMissingGuild,
message: 'The argument must be run on a guild.',
context
});
}

const member = (await this.resolveByID(parameter, guild)) ?? (await this.resolveByQuery(parameter, guild));
return member
? this.ok(member)
: this.error({ parameter, identifier: 'ArgumentMemberUnknownMember', message: 'The argument did not resolve to a member.', context });
return member ? this.ok(member) : this.error({ parameter, message: 'The argument did not resolve to a member.', context });
}

private async resolveByID(argument: string, guild: Guild): Promise<GuildMember | null> {
Expand Down
4 changes: 1 addition & 3 deletions src/arguments/CoreMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ export class CoreArgument extends Argument<Message> {

public async run(parameter: string, context: MessageArgumentContext): AsyncArgumentResult<Message> {
const message = (await this.resolveByID(parameter, context)) ?? (await this.resolveByLink(parameter, context));
return message
? this.ok(message)
: this.error({ parameter, identifier: 'ArgumentMessageUnknownMessage', message: 'The argument did not resolve to a message.', context });
return message ? this.ok(message) : this.error({ parameter, message: 'The argument did not resolve to a message.', context });
}

private async resolveByID(argument: string, context: MessageArgumentContext): Promise<Message | null> {
Expand Down
7 changes: 1 addition & 6 deletions src/arguments/CoreNewsChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ export class CoreArgument extends ExtendedArgument<'guildChannel', NewsChannel>
public handle(channel: GuildChannel, context: ExtendedArgumentContext): ArgumentResult<NewsChannel> {
return isNewsChannel(channel)
? this.ok(channel)
: this.error({
parameter: context.parameter,
identifier: 'ArgumentNewsChannelInvalidChannel',
message: 'The argument did not resolve to a news channel.',
context
});
: this.error({ parameter: context.parameter, message: 'The argument did not resolve to a news channel.', context });
}
}
6 changes: 3 additions & 3 deletions src/arguments/CoreNumber.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PieceContext } from '@sapphire/pieces';
import { Identifiers } from '../lib/errors/Identifiers';
import { Argument, ArgumentContext, ArgumentResult } from '../lib/structures/Argument';

export class CoreArgument extends Argument<number> {
Expand All @@ -12,7 +13,6 @@ export class CoreArgument extends Argument<number> {
if (Number.isNaN(parsed)) {
return this.error({
parameter,
identifier: 'ArgumentNumberInvalidNumber',
message: 'The argument did not resolve to a valid number.',
context
});
Expand All @@ -21,7 +21,7 @@ export class CoreArgument extends Argument<number> {
if (typeof context.minimum === 'number' && parsed < context.minimum) {
return this.error({
parameter,
identifier: 'ArgumentNumberTooSmall',
identifier: Identifiers.ArgumentNumberTooSmall,
message: `The argument must be greater than ${context.minimum}.`,
context
});
Expand All @@ -30,7 +30,7 @@ export class CoreArgument extends Argument<number> {
if (typeof context.maximum === 'number' && parsed > context.maximum) {
return this.error({
parameter,
identifier: 'ArgumentNumberTooBig',
identifier: Identifiers.ArgumentNumberTooBig,
message: `The argument must be smaller than ${context.maximum}.`,
context
});
Expand Down
12 changes: 8 additions & 4 deletions src/arguments/CoreRole.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RoleMentionRegex, SnowflakeRegex } from '@sapphire/discord-utilities';
import type { PieceContext } from '@sapphire/pieces';
import type { Guild, Role } from 'discord.js';
import { Identifiers } from '../lib/errors/Identifiers';
import { Argument, ArgumentContext, AsyncArgumentResult } from '../lib/structures/Argument';

export class CoreArgument extends Argument<Role> {
Expand All @@ -11,13 +12,16 @@ export class CoreArgument extends Argument<Role> {
public async run(parameter: string, context: ArgumentContext): AsyncArgumentResult<Role> {
const { guild } = context.message;
if (!guild) {
return this.error({ parameter, identifier: 'ArgumentRoleMissingGuild', message: 'The argument must be run on a guild.', context });
return this.error({
parameter,
identifier: Identifiers.ArgumentRoleMissingGuild,
message: 'The argument must be run on a guild.',
context
});
}

const role = (await this.resolveByID(parameter, guild)) ?? this.resolveByQuery(parameter, guild);
return role
? this.ok(role)
: this.error({ parameter, identifier: 'ArgumentRoleUnknownRole', message: 'The argument did not resolve to a role.', context });
return role ? this.ok(role) : this.error({ parameter, message: 'The argument did not resolve to a role.', context });
}

private async resolveByID(argument: string, guild: Guild): Promise<Role | null> {
Expand Down
9 changes: 5 additions & 4 deletions src/arguments/CoreString.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PieceContext } from '@sapphire/pieces';
import { Identifiers } from '../lib/errors/Identifiers';
import { Argument, ArgumentContext, ArgumentResult } from '../lib/structures/Argument';

export class CoreArgument extends Argument<string> {
Expand All @@ -10,17 +11,17 @@ export class CoreArgument extends Argument<string> {
if (typeof context.minimum === 'number' && parameter.length < context.minimum) {
return this.error({
parameter,
identifier: 'ArgumentStringTooShort',
message: `The argument must be greater than ${context.minimum} characters.`,
identifier: Identifiers.ArgumentStringTooShort,
message: `The argument must be longer than ${context.minimum} characters.`,
context
});
}

if (typeof context.maximum === 'number' && parameter.length > context.maximum) {
return this.error({
parameter,
identifier: 'ArgumentStringTooLong',
message: `The argument must be less than ${context.maximum} characters.`,
identifier: Identifiers.ArgumentStringTooLong,
message: `The argument must be shorter than ${context.maximum} characters.`,
context
});
}
Expand Down
7 changes: 1 addition & 6 deletions src/arguments/CoreTextChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ export class CoreArgument extends ExtendedArgument<'guildChannel', TextChannel>
public handle(channel: GuildChannel, context: ExtendedArgumentContext): ArgumentResult<TextChannel> {
return isTextChannel(channel)
? this.ok(channel)
: this.error({
parameter: context.parameter,
identifier: 'ArgumentTextChannelInvalidChannel',
message: 'The argument did not resolve to a text channel.',
context
});
: this.error({ parameter: context.parameter, message: 'The argument did not resolve to a text channel.', context });
}
}
4 changes: 1 addition & 3 deletions src/arguments/CoreUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ export class CoreArgument extends Argument<User> {
public async run(parameter: string, context: ArgumentContext): AsyncArgumentResult<User> {
const userID = UserOrMemberMentionRegex.exec(parameter) ?? SnowflakeRegex.exec(parameter);
const user = userID ? await this.context.client.users.fetch(userID[1]).catch(() => null) : null;
return user
? this.ok(user)
: this.error({ parameter, identifier: 'ArgumentUserUnknownUser', message: 'The argument did not resolve to a user.', context });
return user ? this.ok(user) : this.error({ parameter, message: 'The argument did not resolve to a user.', context });
}
}
7 changes: 1 addition & 6 deletions src/arguments/CoreVoiceChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ export class CoreArgument extends ExtendedArgument<'guildChannel', VoiceChannel>
public handle(channel: GuildChannel, context: ExtendedArgumentContext): ArgumentResult<VoiceChannel> {
return isVoiceChannel(channel)
? this.ok(channel)
: this.error({
parameter: context.parameter,
identifier: 'ArgumentVoiceChannelInvalidChannel',
message: 'The argument did not resolve to a voice channel.',
context
});
: this.error({ parameter: context.parameter, message: 'The argument did not resolve to a voice channel.', context });
}
}
12 changes: 4 additions & 8 deletions src/events/command-handler/CorePreCommandRun.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PieceContext } from '@sapphire/pieces';
import { Identifiers } from '../../lib/errors/Identifiers';
import { UserError } from '../../lib/errors/UserError';
import { isErr } from '../../lib/parsers/Result';
import { Event } from '../../lib/structures/Event';
Expand All @@ -10,17 +11,12 @@ export class CoreEvent extends Event<Events.PreCommandRun> {
}

public async run(payload: PreCommandRunPayload) {
const { message, command, parameters, context } = payload;
const { message, command } = payload;
if (!command.enabled) {
message.client.emit(
Events.CommandDenied,
new UserError({ identifier: 'CommandDisabled', message: 'This command is disabled.', context: payload }),
{
message,
command,
parameters,
context
}
new UserError({ identifier: Identifiers.CommandDisabled, message: 'This command is disabled.', context: payload }),
payload
);
return;
}
Expand Down
Loading

0 comments on commit 3371f35

Please sign in to comment.