From 45d1ba227e11aa4c401b45df9db7402d239fae5b Mon Sep 17 00:00:00 2001 From: IRONM00N <64110067+IRONM00N@users.noreply.github.com> Date: Sun, 13 Mar 2022 21:24:18 -0400 Subject: feat: improve inspect wrapper --- .../extensions/discord-akairo/BushClientUtil.ts | 47 +++++++++------------- 1 file changed, 19 insertions(+), 28 deletions(-) (limited to 'src/lib/extensions') diff --git a/src/lib/extensions/discord-akairo/BushClientUtil.ts b/src/lib/extensions/discord-akairo/BushClientUtil.ts index 04014ef..9b63736 100644 --- a/src/lib/extensions/discord-akairo/BushClientUtil.ts +++ b/src/lib/extensions/discord-akairo/BushClientUtil.ts @@ -26,12 +26,10 @@ import { APIEmbed, APIMessage, OAuth2Scopes } from 'discord-api-types/v9'; import { Constants as DiscordConstants, Embed, - GuildMember, Message, PermissionFlagsBits, PermissionsBitField, PermissionsString, - ThreadMember, User, Util as DiscordUtil, type CommandInteraction, @@ -251,17 +249,6 @@ export class BushClientUtil extends ClientUtil { return code3; } - /** - * Uses {@link inspect} with custom defaults. - * @param object - The object you would like to inspect. - * @param options - The options you would like to use to inspect the object. - * @returns The inspected object. - */ - public inspect(object: any, options?: BushInspectOptions): string { - const optionsWithDefaults = this.#getDefaultInspectOptions(options); - return inspect(object, optionsWithDefaults); - } - /** * Generate defaults for {@link inspect}. * @param options The options to create defaults with. @@ -321,6 +308,20 @@ export class BushClientUtil extends ClientUtil { return text; } + /** + * Uses {@link inspect} with custom defaults. + * @param object - The object you would like to inspect. + * @param options - The options you would like to use to inspect the object. + * @returns The inspected object. + */ + public inspect(object: any, options?: BushInspectOptions): string { + const optionsWithDefaults = this.#getDefaultInspectOptions(options); + + if (!optionsWithDefaults.inspectStrings && typeof object === 'string') return object; + + return inspect(object, optionsWithDefaults); + } + /** * Takes an any value, inspects it, redacts credentials, and puts it in a codeblock * (and uploads to hast if the content is too long). @@ -333,11 +334,10 @@ export class BushClientUtil extends ClientUtil { public async inspectCleanRedactCodeblock( input: any, language?: CodeBlockLang | '', - inspectOptions?: BushInspectOptions & { inspectStrings?: boolean }, + inspectOptions?: BushInspectOptions, length = 1024 ) { - input = - !inspectOptions?.inspectStrings && typeof input === 'string' ? input : this.inspect(input, inspectOptions ?? undefined); + input = this.inspect(input, inspectOptions ?? undefined); if (inspectOptions) inspectOptions.inspectStrings = undefined; input = this.discord.cleanCodeBlockContent(input); input = this.redact(input); @@ -351,7 +351,7 @@ export class BushClientUtil extends ClientUtil { * @returns The {@link HasteResults}. */ public async inspectCleanRedactHaste(input: any, inspectOptions?: BushInspectOptions): Promise { - input = typeof input !== 'string' ? this.inspect(input, inspectOptions ?? undefined) : input; + input = this.inspect(input, inspectOptions ?? undefined); input = this.redact(input); return this.haste(input, true); } @@ -363,7 +363,7 @@ export class BushClientUtil extends ClientUtil { * @returns The redacted and inspected object. */ public inspectAndRedact(input: any, inspectOptions?: BushInspectOptions): string { - input = typeof input !== 'string' ? this.inspect(input, inspectOptions ?? undefined) : input; + input = this.inspect(input, inspectOptions ?? undefined); return this.redact(input); } @@ -694,16 +694,7 @@ export class BushClientUtil extends ClientUtil { */ public async resolveNonCachedUser(user: UserResolvable | undefined | null): Promise { if (user == null) return undefined; - const resolvedUser = - user instanceof User - ? user - : user instanceof GuildMember - ? user.user - : user instanceof ThreadMember - ? user.user - : user instanceof Message - ? user.author - : undefined; + const resolvedUser = client.users.resolve(user); return resolvedUser ?? (await client.users.fetch(user as Snowflake).catch(() => undefined)); } -- cgit