-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
39 additions
and
19 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
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
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 |
---|---|---|
@@ -1,20 +1,22 @@ | ||
import { RoleMentionRegex, SnowflakeRegex } from '@sapphire/discord-utilities'; | ||
import { RoleMentionRegex } from '@sapphire/discord-utilities'; | ||
import { Result } from '@sapphire/result'; | ||
import type { Guild, Role, Snowflake } from 'discord.js'; | ||
import { Identifiers } from '../errors/Identifiers'; | ||
import { resolveId, IdResolvable } from '../utils/resolvers/resolveId'; | ||
|
||
export async function resolveRole(parameter: string, guild: Guild): Promise<Result<Role, Identifiers.ArgumentRoleError>> { | ||
export async function resolveRole(parameter: IdResolvable, guild: Guild): Promise<Result<Role, Identifiers.ArgumentRoleError>> { | ||
const role = (await resolveById(parameter, guild)) ?? resolveByQuery(parameter, guild); | ||
if (role) return Result.ok(role); | ||
return Result.err(Identifiers.ArgumentRoleError); | ||
} | ||
|
||
async function resolveById(argument: string, guild: Guild): Promise<Role | null> { | ||
const roleId = RoleMentionRegex.exec(argument) ?? SnowflakeRegex.exec(argument); | ||
async function resolveById(argument: IdResolvable, guild: Guild): Promise<Role | null> { | ||
const roleId = resolveId(argument, RoleMentionRegex, 'roles'); | ||
return roleId ? guild.roles.fetch(roleId[1] as Snowflake) : null; | ||
} | ||
|
||
function resolveByQuery(argument: string, guild: Guild): Role | null { | ||
function resolveByQuery(argument: IdResolvable, guild: Guild): Role | null { | ||
if (typeof argument !== 'string') return null; | ||
const lowerCaseArgument = argument.toLowerCase(); | ||
return guild.roles.cache.find((role) => role.name.toLowerCase() === lowerCaseArgument) ?? null; | ||
} |
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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import { SnowflakeRegex, UserOrMemberMentionRegex } from '@sapphire/discord-utilities'; | ||
import { UserOrMemberMentionRegex } from '@sapphire/discord-utilities'; | ||
import { container } from '@sapphire/pieces'; | ||
import { Result } from '@sapphire/result'; | ||
import type { Snowflake, User } from 'discord.js'; | ||
import type { User } from 'discord.js'; | ||
import { Identifiers } from '../errors/Identifiers'; | ||
import { resolveId, IdResolvable } from '../utils/resolvers/resolveId'; | ||
|
||
export async function resolveUser(parameter: string): Promise<Result<User, Identifiers.ArgumentUserError>> { | ||
const userId = UserOrMemberMentionRegex.exec(parameter) ?? SnowflakeRegex.exec(parameter); | ||
const user = userId ? await container.client.users.fetch(userId[1] as Snowflake).catch(() => null) : null; | ||
export async function resolveUser(parameter: IdResolvable): Promise<Result<User, Identifiers.ArgumentUserError>> { | ||
const userId = resolveId(parameter, UserOrMemberMentionRegex, 'users'); | ||
const user = userId ? await container.client.users.fetch(userId).catch(() => null) : null; | ||
if (user) return Result.ok(user); | ||
return Result.err(Identifiers.ArgumentUserError); | ||
} |
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,13 @@ | ||
import { SnowflakeRegex } from '@sapphire/discord-utilities'; | ||
import type { CommandInteraction, CommandInteractionResolvedData, Snowflake } from 'discord.js'; | ||
|
||
export type IdResolvable = string | CommandInteraction; | ||
|
||
export function resolveId(parameter: IdResolvable, mentionRegex: RegExp, key: keyof CommandInteractionResolvedData): Snowflake | null { | ||
if (typeof parameter == 'string') return mentionRegex.exec(parameter)?.[1] ?? SnowflakeRegex.exec(parameter)?.[1] ?? null; | ||
if (!parameter.inCachedGuild()) { | ||
// TODO: figure out how to get uncached members | ||
return key === 'members' ? null : parameter.options.resolved[key]!.first()!.id; | ||
} | ||
return parameter.options.resolved[key]!.first()!.id; | ||
} |