File tree 4 files changed +40
-0
lines changed
4 files changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ import type { PieceContext } from '@sapphire/pieces' ;
2
+ import type { Guild } from 'discord.js' ;
3
+ import { resolveGuild } from '../lib/resolvers/guild' ;
4
+ import { Argument } from '../lib/structures/Argument' ;
5
+
6
+ export class CoreArgument extends Argument < Guild > {
7
+ public constructor ( context : PieceContext ) {
8
+ super ( context , { name : 'guild' } ) ;
9
+ }
10
+
11
+ public async run ( parameter : string , context : Argument . Context ) : Argument . AsyncResult < Guild > {
12
+ const resolved = await resolveGuild ( parameter ) ;
13
+ return resolved . mapErrInto ( ( identifier ) =>
14
+ this . error ( {
15
+ parameter,
16
+ identifier,
17
+ message : 'The given argument did not resolve to a Discord guild.' ,
18
+ context
19
+ } )
20
+ ) ;
21
+ }
22
+ }
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ export enum Identifiers {
12
12
ArgumentFloatError = 'floatError' ,
13
13
ArgumentFloatTooLarge = 'floatTooLarge' ,
14
14
ArgumentFloatTooSmall = 'floatTooSmall' ,
15
+ ArgumentGuildError = 'guildError' ,
15
16
ArgumentGuildCategoryChannelError = 'categoryChannelError' ,
16
17
ArgumentGuildChannelError = 'guildChannelError' ,
17
18
ArgumentGuildChannelMissingGuildError = 'guildChannelMissingGuildError' ,
Original file line number Diff line number Diff line change
1
+ import { SnowflakeRegex } from '@sapphire/discord-utilities' ;
2
+ import { container } from '@sapphire/pieces' ;
3
+ import { Result } from '@sapphire/result' ;
4
+ import type { Guild } from 'discord.js' ;
5
+ import { Identifiers } from '../errors/Identifiers' ;
6
+
7
+ export async function resolveGuild ( parameter : string ) : Promise < Result < Guild , Identifiers . ArgumentGuildError > > {
8
+ const guildId = SnowflakeRegex . exec ( parameter ) ?. groups ?. id ;
9
+ const guild = guildId ? await container . client . guilds . fetch ( guildId ) . catch ( ( ) => null ) : null ;
10
+
11
+ if ( guild ) {
12
+ return Result . ok ( guild ) ;
13
+ }
14
+
15
+ return Result . err ( Identifiers . ArgumentGuildError ) ;
16
+ }
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ export * from './dmChannel';
5
5
export { resolveEmoji } from './emoji' ;
6
6
export * from './enum' ;
7
7
export * from './float' ;
8
+ export * from './guild' ;
8
9
export * from './guildCategoryChannel' ;
9
10
export * from './guildChannel' ;
10
11
export * from './guildNewsChannel' ;
You can’t perform that action at this time.
0 commit comments