Skip to content

Commit

Permalink
feat(errors): expose all identifiers in an enum
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `'ArgumentBooleanInvalidBoolean'` -> `Identifiers.ArgumentBoolean`
BREAKING CHANGE: `'ArgumentCategoryChannelInvalidChannel'` -> `Identifiers.ArgumentCategoryChannel`
BREAKING CHANGE: `'ArgumentChannelMissingChannel'` -> `Identifiers.ArgumentChannel`
BREAKING CHANGE: `'ArgumentDateInvalidNumber'` -> `Identifiers.ArgumentDate`
BREAKING CHANGE: `'ArgumentDateTooSmall'` -> `Identifiers.ArgumentDateTooSmall`
BREAKING CHANGE: `'ArgumentDateTooBig'` -> `Identifiers.ArgumentDateTooBig`
BREAKING CHANGE: `'ArgumentDMChannelInvalidChannel'` -> `Identifiers.ArgumentDMChannel`
BREAKING CHANGE: `'ArgumentFloatInvalidFloat'` -> `Identifiers.ArgumentFloat`
BREAKING CHANGE: `'ArgumentFloatTooSmall'` -> `Identifiers.ArgumentFloatTooSmall`
BREAKING CHANGE: `'ArgumentFloatTooBig'` -> `Identifiers.ArgumentFloatTooBig`
BREAKING CHANGE: `'ArgumentGuildChannelMissingGuild'` -> `Identifiers.ArgumentGuildChannelMissingGuild`
BREAKING CHANGE: `'ArgumentGuildChannelUnknownChannel'` -> `Identifiers.ArgumentGuildChannel`
BREAKING CHANGE: `'ArgumentHyperlinkInvalidURL'` -> `Identifiers.ArgumentHyperlink`
BREAKING CHANGE: `'ArgumentIntegerInvalidNumber'` -> `Identifiers.ArgumentInteger`
BREAKING CHANGE: `'ArgumentIntegerTooSmall'` -> `Identifiers.ArgumentIntegerTooSmall`
BREAKING CHANGE: `'ArgumentIntegerTooBig'` -> `Identifiers.ArgumentIntegerTooBig`
BREAKING CHANGE: `'ArgumentMemberMissingGuild'` -> `Identifiers.ArgumentMemberMissingGuild`
BREAKING CHANGE: `'ArgumentMemberUnknownMember'` -> `Identifiers.ArgumentMember`
BREAKING CHANGE: `'ArgumentMessageUnknownMessage'` -> `Identifiers.Message`
BREAKING CHANGE: `'ArgumentNewsChannelInvalidChannel'` -> `Identifiers.NewsChannel`
BREAKING CHANGE: `'ArgumentNumberInvalidNumber'` -> `Identifiers.Number`
BREAKING CHANGE: `'ArgumentNumberTooSmall'` -> `Identifiers.ArgumentNumberTooSmall`
BREAKING CHANGE: `'ArgumentNumberTooBig'` -> `Identifiers.ArgumentNumberTooBig`
BREAKING CHANGE: `'ArgumentRoleMissingGuild'` -> `Identifiers.ArgumentRoleMissingGuild`
BREAKING CHANGE: `'ArgumentRoleUnknownRole'` -> `Identifiers.Role`
BREAKING CHANGE: `'ArgumentStringTooShort'` -> `Identifiers.ArgumentStringTooSmall`
BREAKING CHANGE: `'ArgumentStringTooLong'` -> `Identifiers.ArgumentStringTooBig`
BREAKING CHANGE: `'ArgumentTextChannelInvalidChannel'` -> `Identifiers.TextChannel`
BREAKING CHANGE: `'ArgumentUserUnknownUser'` -> `Identifiers.User`
BREAKING CHANGE: `'ArgumentVoiceChannelInvalidChannel'` -> `Identifiers.VoiceChannel`
BREAKING CHANGE: `'CommandDisabled'` -> `Identifiers.CommandDisabled`
BREAKING CHANGE: `'UnavailableArgument'` -> `Identifiers.ArgsUnavailable`
BREAKING CHANGE: `'MissingArguments'` -> `Identifiers.ArgsMissing`
BREAKING CHANGE: `'Cooldown'` -> `Identifiers.PreconditionCooldown`
BREAKING CHANGE: `'DMOnly'` -> `Identifiers.PreconditionDMOnly`
BREAKING CHANGE: `'GuildOnly'` -> `Identifiers.PreconditionGuildOnly`
BREAKING CHANGE: `'NSFW'` -> `Identifiers.PreconditionNSFW`
BREAKING CHANGE: `'Permissions'` -> `Identifiers.PreconditionPermissions`
  • Loading branch information
kyranet committed Feb 6, 2021
1 parent 836f1b8 commit 57e180c
Show file tree
Hide file tree
Showing 28 changed files with 109 additions and 104 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
5 changes: 3 additions & 2 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,7 +11,7 @@ export class CoreArgument extends Argument<string> {
if (typeof context.minimum === 'number' && parameter.length < context.minimum) {
return this.error({
parameter,
identifier: 'ArgumentStringTooShort',
identifier: Identifiers.ArgumentStringTooSmall,
message: `The argument must be greater than ${context.minimum} characters.`,
context
});
Expand All @@ -19,7 +20,7 @@ export class CoreArgument extends Argument<string> {
if (typeof context.maximum === 'number' && parameter.length > context.maximum) {
return this.error({
parameter,
identifier: 'ArgumentStringTooLong',
identifier: Identifiers.ArgumentStringTooBig,
message: `The argument must be less 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 57e180c

Please sign in to comment.