Skip to content

Commit

Permalink
feat(args): add more data to error context for core arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Feb 7, 2021
1 parent 61f9c41 commit 0cfff79
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/arguments/CoreCategoryChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ 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, 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: { ...context, channel }
});
}
}
8 changes: 7 additions & 1 deletion src/arguments/CoreChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ 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);
return channel ? this.ok(channel) : this.error({ parameter, message: 'The argument did not resolve to a channel.', context });
return channel
? this.ok(channel)
: this.error({
parameter,
message: 'The argument did not resolve to a channel.',
context: { ...context, channel }
});
}
}
6 changes: 5 additions & 1 deletion src/arguments/CoreDMChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ 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, 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: { ...context, channel }
});
}
}
10 changes: 8 additions & 2 deletions src/arguments/CoreGuildChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ export class CoreArgument extends Argument<GuildChannel> {
parameter,
identifier: Identifiers.ArgumentGuildChannelMissingGuild,
message: 'The argument must be run in a guild.',
context
context: { ...context, guild }
});
}

const channel = this.resolveByID(parameter, guild) ?? this.resolveByQuery(parameter, guild);
return channel ? this.ok(channel) : this.error({ parameter, 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: { ...context, guild }
});
}

private resolveByID(argument: string, guild: Guild): GuildChannel | null {
Expand Down
10 changes: 8 additions & 2 deletions src/arguments/CoreMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ export class CoreArgument extends Argument<GuildMember> {
parameter,
identifier: Identifiers.ArgumentMemberMissingGuild,
message: 'The argument must be run on a guild.',
context
context: { ...context, guild }
});
}

const member = (await this.resolveByID(parameter, guild)) ?? (await this.resolveByQuery(parameter, guild));
return member ? this.ok(member) : this.error({ parameter, 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: { ...context, guild }
});
}

private async resolveByID(argument: string, guild: Guild): Promise<GuildMember | null> {
Expand Down
14 changes: 10 additions & 4 deletions src/arguments/CoreMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ 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, message: 'The argument did not resolve to a message.', context });
const channel = context.channel ?? context.message.channel;
const message = (await this.resolveByID(parameter, channel)) ?? (await this.resolveByLink(parameter, context));
return message
? this.ok(message)
: this.error({
parameter,
message: 'The argument did not resolve to a message.',
context: { ...context, channel }
});
}

private async resolveByID(argument: string, context: MessageArgumentContext): Promise<Message | null> {
const channel = context.channel ?? context.message.channel;
private async resolveByID(argument: string, channel: DMChannel | NewsChannel | TextChannel): Promise<Message | null> {
return SnowflakeRegex.test(argument) ? channel.messages.fetch(argument).catch(() => null) : null;
}

Expand Down
6 changes: 5 additions & 1 deletion src/arguments/CoreNewsChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ 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, 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: { ...context, channel }
});
}
}
6 changes: 5 additions & 1 deletion src/arguments/CoreTextChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ 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, 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: { ...context, channel }
});
}
}
6 changes: 5 additions & 1 deletion src/arguments/CoreVoiceChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ 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, 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: { ...context, channel }
});
}
}

0 comments on commit 0cfff79

Please sign in to comment.