diff options
Diffstat (limited to 'src/lib/extensions/discord-akairo')
-rw-r--r-- | src/lib/extensions/discord-akairo/BushClient.ts | 11 | ||||
-rw-r--r-- | src/lib/extensions/discord-akairo/BushClientUtil.ts | 66 |
2 files changed, 48 insertions, 29 deletions
diff --git a/src/lib/extensions/discord-akairo/BushClient.ts b/src/lib/extensions/discord-akairo/BushClient.ts index 43ae139..e46e701 100644 --- a/src/lib/extensions/discord-akairo/BushClient.ts +++ b/src/lib/extensions/discord-akairo/BushClient.ts @@ -42,6 +42,7 @@ import { } from 'discord.js'; import EventEmitter from 'events'; import { google } from 'googleapis'; +import snakeCase from 'lodash.snakecase'; import path from 'path'; import readline from 'readline'; import type { Options as SequelizeOptions, Sequelize as SequelizeType } from 'sequelize'; @@ -213,7 +214,9 @@ export class BushClient<Ready extends boolean = boolean> extends AkairoClient<Re allowedMentions: AllowedMentions.users(), // No everyone or role mentions by default makeCache: Options.cacheWithLimits({}), failIfNotExists: false, - rest: { api: 'https://canary.discord.com/api' } + rest: { api: 'https://canary.discord.com/api' }, + // todo: remove this when https://github.com/discordjs/discord.js/pull/7497 is merged + jsonTransformer }); patch(this); @@ -542,3 +545,9 @@ enum GatewayIntentBits { DirectMessageTyping = 16384, GuildScheduledEvents = 65536 } + +function jsonTransformer(obj: any): any { + if (typeof obj !== 'object' || !obj) return obj; + if (Array.isArray(obj)) return obj.map(jsonTransformer); + return Object.fromEntries(Object.entries(obj).map(([key, value]) => [snakeCase(key), jsonTransformer(value)])); +} diff --git a/src/lib/extensions/discord-akairo/BushClientUtil.ts b/src/lib/extensions/discord-akairo/BushClientUtil.ts index 4184723..ecfa360 100644 --- a/src/lib/extensions/discord-akairo/BushClientUtil.ts +++ b/src/lib/extensions/discord-akairo/BushClientUtil.ts @@ -1,5 +1,6 @@ import { Arg, + BaseBushArgumentType, BushConstants, Global, Shared, @@ -266,31 +267,18 @@ export class BushClientUtil extends ClientUtil { * @returns The default options combined with the specified options. */ #getDefaultInspectOptions(options?: BushInspectOptions): BushInspectOptions { - const { - showHidden = false, - depth = 2, - colors = false, - customInspect = true, - showProxy = false, - maxArrayLength = Infinity, - maxStringLength = Infinity, - breakLength = 80, - compact = 3, - sorted = false, - getters = true - } = options ?? {}; return { - showHidden, - depth, - colors, - customInspect, - showProxy, - maxArrayLength, - maxStringLength, - breakLength, - compact, - sorted, - getters + showHidden: options?.showHidden ?? false, + depth: options?.depth ?? 2, + colors: options?.colors ?? false, + customInspect: options?.customInspect ?? true, + showProxy: options?.showProxy ?? false, + maxArrayLength: options?.maxArrayLength ?? Infinity, + maxStringLength: options?.maxStringLength ?? Infinity, + breakLength: options?.breakLength ?? 80, + compact: options?.compact ?? 3, + sorted: options?.sorted ?? false, + getters: options?.getters ?? true }; } @@ -556,7 +544,7 @@ export class BushClientUtil extends ClientUtil { * @returns The {@link ParsedDuration}. */ public parseDuration(content: string, remove = true): ParsedDuration { - if (!content) return { duration: 0, contentWithoutTime: null }; + if (!content) return { duration: 0, content: null }; // eslint-disable-next-line prefer-const let duration: number | null = null; @@ -574,7 +562,7 @@ export class BushClientUtil extends ClientUtil { } // remove the space added earlier if (contentWithoutTime.startsWith(' ')) contentWithoutTime.replace(' ', ''); - return { duration, contentWithoutTime }; + return { duration, content: contentWithoutTime }; } /** @@ -716,7 +704,7 @@ export class BushClientUtil extends ClientUtil { .catch(() => undefined)) as { pronouns: PronounCode } | undefined; if (!apiRes) return undefined; - if (!apiRes.pronouns) throw new Error('apiRes.pronouns is undefined'); + assert(apiRes.pronouns); return client.constants.pronounMapping[apiRes.pronouns!]!; } @@ -923,6 +911,23 @@ export class BushClientUtil extends ClientUtil { } } + public async castDurationContent( + arg: string | ParsedDuration | null, + message: BushMessage | BushSlashMessage + ): Promise<ParsedDurationRes> { + const res = typeof arg === 'string' ? await util.arg.cast('contentWithDuration', message, arg) : arg; + + return { duration: res?.duration ?? 0, content: res?.content ?? '' }; + } + + public async cast<T extends keyof BaseBushArgumentType>( + type: T, + arg: BaseBushArgumentType[T] | string, + message: BushMessage | BushSlashMessage + ) { + return typeof arg === 'string' ? await util.arg.cast(type, message, arg) : arg; + } + /** * A wrapper for the Argument class that adds custom typings. */ @@ -989,5 +994,10 @@ export interface HasteResults { export interface ParsedDuration { duration: number | null; - contentWithoutTime: string | null; + content: string | null; +} + +export interface ParsedDurationRes { + duration: number; + content: string; } |