-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Args): add guild argument (#626)
* feat(Args): add guild argument * fix: rename guild argument * fix: some fixes Co-authored-by: Jeroen Claassens <[email protected]>
- Loading branch information
Showing
4 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters