Skip to content

Commit

Permalink
chore: fixes I want and this is quicker than GH review
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Aug 13, 2022
1 parent d2eabd3 commit 72e3fad
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 118 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"dependencies": {
"@discordjs/builders": "^0.16.0",
"@sapphire/discord-utilities": "^2.11.5",
"@sapphire/discord.js-utilities": "^4.11.3",
"@sapphire/discord.js-utilities": "^4.12.0",
"@sapphire/lexure": "^1.0.1",
"@sapphire/pieces": "^3.5.0",
"@sapphire/ratelimits": "^2.4.4",
Expand All @@ -51,12 +51,12 @@
"@sapphire/prettier-config": "^1.4.3",
"@sapphire/ts-config": "^3.3.4",
"@types/jest": "^28.1.6",
"@types/node": "^18.6.4",
"@types/node": "^18.7.3",
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^5.32.0",
"@typescript-eslint/parser": "^5.32.0",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"cz-conventional-changelog": "^3.3.0",
"discord.js": "^13.9.2",
"discord.js": "^13.10.2",
"eslint": "^8.21.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
Expand All @@ -67,7 +67,7 @@
"pinst": "^3.0.0",
"prettier": "^2.7.1",
"pretty-quick": "^3.1.3",
"rollup": "^2.77.2",
"rollup": "^2.77.3",
"rollup-plugin-version-injector": "^1.3.3",
"ts-jest": "^28.0.7",
"typedoc": "^0.23.10",
Expand Down
10 changes: 8 additions & 2 deletions src/lib/resolvers/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ export function resolveBoolean(
customs?: { truths?: readonly string[]; falses?: readonly string[] }
): Result<boolean, Identifiers.ArgumentBooleanError> {
const boolean = parameter.toLowerCase();
if ([...baseTruths, ...(customs?.truths ?? [])].includes(boolean)) return Result.ok(true);
if ([...baseFalses, ...(customs?.falses ?? [])].includes(boolean)) return Result.ok(false);

if ([...baseTruths, ...(customs?.truths ?? [])].includes(boolean)) {
return Result.ok(true);
}

if ([...baseFalses, ...(customs?.falses ?? [])].includes(boolean)) {
return Result.ok(false);
}

return Result.err(Identifiers.ArgumentBooleanError);
}
9 changes: 6 additions & 3 deletions src/lib/resolvers/channel.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { ChannelMentionRegex, ChannelTypes } from '@sapphire/discord.js-utilities';
import { container } from '@sapphire/pieces';
import { Result } from '@sapphire/result';
import type { CommandInteraction, Message, Snowflake } from 'discord.js';
import type { BaseCommandInteraction, Message, Snowflake } from 'discord.js';
import { Identifiers } from '../errors/Identifiers';

export function resolveChannel(parameter: string, message: Message | CommandInteraction): Result<ChannelTypes, Identifiers.ArgumentChannelError> {
export function resolveChannel(
parameter: string,
messageOrInteraction: Message | BaseCommandInteraction
): Result<ChannelTypes, Identifiers.ArgumentChannelError> {
const channelId = (ChannelMentionRegex.exec(parameter)?.[1] ?? parameter) as Snowflake;
const channel = (message.guild ? message.guild.channels : container.client.channels).cache.get(channelId);
const channel = (messageOrInteraction.guild ? messageOrInteraction.guild.channels : container.client.channels).cache.get(channelId);

if (channel) {
return Result.ok(channel as ChannelTypes);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/resolvers/dmChannel.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { isDMChannel } from '@sapphire/discord.js-utilities';
import { Result } from '@sapphire/result';
import type { CommandInteraction, DMChannel, Message } from 'discord.js';
import type { BaseCommandInteraction, DMChannel, Message } from 'discord.js';
import { Identifiers } from '../errors/Identifiers';
import { resolveChannel } from './channel';

export function resolveDMChannel(
parameter: string,
message: Message | CommandInteraction
messageOrInteraction: Message | BaseCommandInteraction
): Result<DMChannel, Identifiers.ArgumentChannelError | Identifiers.ArgumentDMChannelError> {
const result = resolveChannel(parameter, message);
const result = resolveChannel(parameter, messageOrInteraction);
return result.mapInto((value) => {
if (isDMChannel(value) && !value.partial) {
return Result.ok(value);
Expand Down
9 changes: 2 additions & 7 deletions src/lib/resolvers/member.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { SnowflakeRegex, UserOrMemberMentionRegex } from '@sapphire/discord-utilities';
import { Result } from '@sapphire/result';
import type { BaseCommandInteraction, Guild, GuildMember, Snowflake } from 'discord.js';
import type { Guild, GuildMember, Snowflake } from 'discord.js';
import { Identifiers } from '../errors/Identifiers';
import resolveGuild from '../utils/resolvers/resolveGuild';

export async function resolveMember(
parameter: string,
guildOrInteraction: Guild | BaseCommandInteraction
): Promise<Result<GuildMember, Identifiers.ArgumentMemberError>> {
const guild = resolveGuild(guildOrInteraction);
export async function resolveMember(parameter: string, guild: Guild): Promise<Result<GuildMember, Identifiers.ArgumentMemberError>> {
if (guild) {
const member = (await resolveById(parameter, guild)) ?? (await resolveByQuery(parameter, guild));

Expand Down
13 changes: 7 additions & 6 deletions src/lib/resolvers/message.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { ChannelMessageRegex, MessageLinkRegex, SnowflakeRegex } from '@sapphire/discord-utilities';
import {
AnyInteraction,
GuildBasedChannelTypes,
isAnyInteraction,
isGuildBasedChannel,
isNewsChannel,
isTextBasedChannel,
isTextChannel,
runsOnInteraction,
TextBasedChannelTypes
} from '@sapphire/discord.js-utilities';
import { container } from '@sapphire/pieces';
import { Result } from '@sapphire/result';
import type { Awaitable } from '@sapphire/utilities';
import { CommandInteraction, Message, Permissions, Snowflake, User } from 'discord.js';
import { Message, Permissions, Snowflake, User } from 'discord.js';
import { Identifiers } from '../errors/Identifiers';

/**
Expand All @@ -24,9 +25,9 @@ export interface MessageResolverOptions {
*/
channel?: TextBasedChannelTypes;
/**
* Base {@link Message} or {@link CommandInteraction} to resolve the message from (e.g. pick the channel if not given).
* Base {@link Message} or {@link AnyInteraction} to resolve the message from (e.g. pick the channel if not given).
*/
messageOrInteraction: Message | CommandInteraction;
messageOrInteraction: Message | AnyInteraction;
/**
* Whether to scan the entire guild cache for the message.
* If channel is given with this option, this option is ignored.
Expand Down Expand Up @@ -91,7 +92,7 @@ async function resolveByLink(parameter: string, options: MessageResolverOptions)
return getMessageFromChannel(
channelId,
messageId,
runsOnInteraction(options.messageOrInteraction) ? options.messageOrInteraction.user : options.messageOrInteraction.author
isAnyInteraction(options.messageOrInteraction) ? options.messageOrInteraction.user : options.messageOrInteraction.author
);
}

Expand All @@ -105,7 +106,7 @@ async function resolveByChannelAndMessage(parameter: string, options: MessageRes
return getMessageFromChannel(
result.channelId,
result.messageId,
runsOnInteraction(options.messageOrInteraction) ? options.messageOrInteraction.user : options.messageOrInteraction.author
isAnyInteraction(options.messageOrInteraction) ? options.messageOrInteraction.user : options.messageOrInteraction.author
);
}

Expand Down
9 changes: 2 additions & 7 deletions src/lib/resolvers/role.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { RoleMentionRegex, SnowflakeRegex } from '@sapphire/discord-utilities';
import { Result } from '@sapphire/result';
import type { BaseCommandInteraction, Guild, Role, Snowflake } from 'discord.js';
import type { Guild, Role, Snowflake } from 'discord.js';
import { Identifiers } from '../errors/Identifiers';
import resolveGuild from '../utils/resolvers/resolveGuild';

export async function resolveRole(
parameter: string,
guildOrInteraction: Guild | BaseCommandInteraction
): Promise<Result<Role, Identifiers.ArgumentRoleError>> {
const guild = resolveGuild(guildOrInteraction);
export async function resolveRole(parameter: string, guild: Guild): Promise<Result<Role, Identifiers.ArgumentRoleError>> {
if (guild) {
const role = (await resolveById(parameter, guild)) ?? resolveByQuery(parameter, guild);

Expand Down
9 changes: 0 additions & 9 deletions src/lib/utils/resolvers/resolveGuild.ts

This file was deleted.

Loading

0 comments on commit 72e3fad

Please sign in to comment.