Skip to content

Commit

Permalink
feat(Args): add guild argument (#626)
Browse files Browse the repository at this point in the history
* feat(Args): add guild argument

* fix: rename guild argument

* fix: some fixes
Co-authored-by: Jeroen Claassens <[email protected]>
  • Loading branch information
swiizyy authored Apr 10, 2023
1 parent 37b1ac5 commit ed414f4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/arguments/CoreGuild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { PieceContext } from '@sapphire/pieces';
import type { Guild } from 'discord.js';
import { resolveGuild } from '../lib/resolvers/guild';
import { Argument } from '../lib/structures/Argument';

export class CoreArgument extends Argument<Guild> {
public constructor(context: PieceContext) {
super(context, { name: 'guild' });
}

public async run(parameter: string, context: Argument.Context): Argument.AsyncResult<Guild> {
const resolved = await resolveGuild(parameter);
return resolved.mapErrInto((identifier) =>
this.error({
parameter,
identifier,
message: 'The given argument did not resolve to a Discord guild.',
context
})
);
}
}
1 change: 1 addition & 0 deletions src/lib/errors/Identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum Identifiers {
ArgumentFloatError = 'floatError',
ArgumentFloatTooLarge = 'floatTooLarge',
ArgumentFloatTooSmall = 'floatTooSmall',
ArgumentGuildError = 'guildError',
ArgumentGuildCategoryChannelError = 'categoryChannelError',
ArgumentGuildChannelError = 'guildChannelError',
ArgumentGuildChannelMissingGuildError = 'guildChannelMissingGuildError',
Expand Down
16 changes: 16 additions & 0 deletions src/lib/resolvers/guild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { SnowflakeRegex } from '@sapphire/discord-utilities';
import { container } from '@sapphire/pieces';
import { Result } from '@sapphire/result';
import type { Guild } from 'discord.js';
import { Identifiers } from '../errors/Identifiers';

export async function resolveGuild(parameter: string): Promise<Result<Guild, Identifiers.ArgumentGuildError>> {
const guildId = SnowflakeRegex.exec(parameter)?.groups?.id;
const guild = guildId ? await container.client.guilds.fetch(guildId).catch(() => null) : null;

if (guild) {
return Result.ok(guild);
}

return Result.err(Identifiers.ArgumentGuildError);
}
1 change: 1 addition & 0 deletions src/lib/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './dmChannel';
export { resolveEmoji } from './emoji';
export * from './enum';
export * from './float';
export * from './guild';
export * from './guildCategoryChannel';
export * from './guildChannel';
export * from './guildNewsChannel';
Expand Down

0 comments on commit ed414f4

Please sign in to comment.