From b072e33fd6b32cc3ed356beb9fe7d7414d7bb840 Mon Sep 17 00:00:00 2001 From: MARCROCK22 <57925328+MARCROCK22@users.noreply.github.com> Date: Fri, 22 Mar 2024 13:34:31 -0400 Subject: [PATCH] UsingClient, __parseCommandLocales --- src/builders/Attachment.ts | 5 ++- src/cache/index.ts | 7 ++-- src/cache/resources/default/base.ts | 5 +-- src/cache/resources/default/guild-based.ts | 5 +-- src/cache/resources/default/guild-related.ts | 5 +-- src/commands/handler.ts | 36 ++++++++------------ src/common/types/options.ts | 4 +-- src/components/handler.ts | 4 +-- src/structures/AutoModerationRule.ts | 4 +-- src/structures/ClientUser.ts | 4 +-- src/structures/Guild.ts | 4 +-- src/structures/GuildEmoji.ts | 4 +-- src/structures/GuildMember.ts | 8 ++--- src/structures/GuildPreview.ts | 4 +-- src/structures/GuildRole.ts | 4 +-- src/structures/GuildTemplate.ts | 4 +-- src/structures/Interaction.ts | 17 +++++---- src/structures/Message.ts | 8 ++--- src/structures/Sticker.ts | 4 +-- src/structures/Webhook.ts | 4 +-- src/structures/channels.ts | 8 ++--- src/structures/extra/Base.ts | 3 +- src/structures/extra/DiscordBase.ts | 4 +-- 23 files changed, 74 insertions(+), 81 deletions(-) diff --git a/src/builders/Attachment.ts b/src/builders/Attachment.ts index dd2fa2b..2da3f11 100644 --- a/src/builders/Attachment.ts +++ b/src/builders/Attachment.ts @@ -2,8 +2,7 @@ import type { APIAttachment, RESTAPIAttachment } from 'discord-api-types/v10'; import { randomBytes } from 'node:crypto'; import { readFile, stat } from 'node:fs/promises'; import path from 'node:path'; -import { throwError, type RawFile } from '..'; -import type { BaseClient } from '../client/base'; +import { type UsingClient, throwError, type RawFile } from '..'; import type { ImageResolvable, ObjectToLower } from '../common'; import { Base } from '../structures/extra/Base'; @@ -27,7 +26,7 @@ export interface AttachmentData { export interface Attachment extends ObjectToLower {} export class Attachment extends Base { constructor( - client: BaseClient, + client: UsingClient, public data: APIAttachment, ) { super(client); diff --git a/src/cache/index.ts b/src/cache/index.ts index c1676d9..2983f58 100644 --- a/src/cache/index.ts +++ b/src/cache/index.ts @@ -22,8 +22,7 @@ import { Stickers } from './resources/stickers'; import { Threads } from './resources/threads'; import { VoiceStates } from './resources/voice-states'; -import type { BaseClient } from '../client/base'; -import type { InternalOptions } from '../commands'; +import type { InternalOptions, UsingClient } from '../commands'; import { ChannelType, GatewayIntentBits } from '../common'; import { Overwrites } from './resources/overwrites'; @@ -103,7 +102,7 @@ export class Cache { public intents: number, public adapter: Adapter, readonly disabledCache: (NonGuildBased | GuildBased | GuildRelated)[] = [], - client?: BaseClient, + client?: UsingClient, ) { // non-guild based if (!this.disabledCache.includes('users')) { @@ -149,7 +148,7 @@ export class Cache { } /** @internal */ - __setClient(client: BaseClient) { + __setClient(client: UsingClient) { this.users?.__setClient(client); this.guilds?.__setClient(client); diff --git a/src/cache/resources/default/base.ts b/src/cache/resources/default/base.ts index a199695..4ec86ee 100644 --- a/src/cache/resources/default/base.ts +++ b/src/cache/resources/default/base.ts @@ -1,4 +1,5 @@ import type { BaseClient } from '../../../client/base'; +import type { UsingClient } from '../../../commands'; import { fakePromise, type GatewayIntentBits } from '../../../common'; import type { Cache, ReturnCache } from '../../index'; @@ -8,7 +9,7 @@ export class BaseResource { constructor( protected cache: Cache, - client?: BaseClient, + client?: UsingClient, ) { if (client) { this.client = client; @@ -16,7 +17,7 @@ export class BaseResource { } /** @internal */ - __setClient(client: BaseClient) { + __setClient(client: UsingClient) { this.client = client; } diff --git a/src/cache/resources/default/guild-based.ts b/src/cache/resources/default/guild-based.ts index d5608db..c298f25 100644 --- a/src/cache/resources/default/guild-based.ts +++ b/src/cache/resources/default/guild-based.ts @@ -1,4 +1,5 @@ import type { BaseClient } from '../../../client/base'; +import type { UsingClient } from '../../../commands'; import { fakePromise, type GatewayIntentBits } from '../../../common'; import type { Cache, ReturnCache } from '../../index'; @@ -8,7 +9,7 @@ export class GuildBasedResource { constructor( protected cache: Cache, - client?: BaseClient, + client?: UsingClient, ) { if (client) { this.client = client; @@ -16,7 +17,7 @@ export class GuildBasedResource { } /** @internal */ - __setClient(client: BaseClient) { + __setClient(client: UsingClient) { this.client = client; } diff --git a/src/cache/resources/default/guild-related.ts b/src/cache/resources/default/guild-related.ts index 25283a6..2fea398 100644 --- a/src/cache/resources/default/guild-related.ts +++ b/src/cache/resources/default/guild-related.ts @@ -1,4 +1,5 @@ import type { BaseClient } from '../../../client/base'; +import type { UsingClient } from '../../../commands'; import { fakePromise, type GatewayIntentBits } from '../../../common'; import type { Cache, ReturnCache } from '../../index'; @@ -8,7 +9,7 @@ export class GuildRelatedResource { constructor( protected cache: Cache, - client?: BaseClient, + client?: UsingClient, ) { if (client) { this.client = client; @@ -16,7 +17,7 @@ export class GuildRelatedResource { } /** @internal */ - __setClient(client: BaseClient) { + __setClient(client: UsingClient) { this.client = client; } diff --git a/src/commands/handler.ts b/src/commands/handler.ts index 8279898..6481075 100644 --- a/src/commands/handler.ts +++ b/src/commands/handler.ts @@ -1,9 +1,9 @@ import { basename, dirname } from 'node:path'; -import type { BaseClient } from '../client/base'; import type { Logger } from '../common'; import { BaseHandler, Locale, type LocaleString } from '../common'; import { Command, SubCommand } from './applications/chat'; import { ContextMenuCommand } from './applications/menu'; +import type { UsingClient } from './applications/shared'; export class CommandHandler extends BaseHandler { values: (Command | ContextMenuCommand)[] = []; @@ -11,7 +11,7 @@ export class CommandHandler extends BaseHandler { constructor( protected logger: Logger, - protected client: BaseClient, + protected client: UsingClient, ) { super(logger); } @@ -35,7 +35,7 @@ export class CommandHandler extends BaseHandler { } } - async load(commandsDir: string, client: BaseClient) { + async load(commandsDir: string, client: UsingClient) { const result = (await this.loadFilesK(await this.getFiles(commandsDir))).filter(x => x.file); this.values = []; @@ -65,6 +65,7 @@ export class CommandHandler extends BaseHandler { commandInstance.__filePath = command.path; commandInstance.options ??= [] as NonNullable; if (commandInstance.__d) { + //@AutoLoad const options = await this.getFiles(dirname(command.path)); for (const option of options) { if (command.name === basename(option)) { @@ -98,11 +99,11 @@ export class CommandHandler extends BaseHandler { } this.values.push(commandInstance); - this.__parseLocales(commandInstance, client); + this.__parseCommandLocales(commandInstance, client); for (const i of commandInstance.options ?? []) { if (i instanceof SubCommand) { - this.__parseLocales(i, client); + this.__parseCommandLocales(i, client); } } } @@ -110,30 +111,23 @@ export class CommandHandler extends BaseHandler { return this.values; } - private __parseLocales(command: Command | SubCommand, client: BaseClient) { + private __parseCommandLocales(command: Command | SubCommand, client: UsingClient) { if (command.__t) { command.name_localizations = {}; command.description_localizations = {}; for (const locale of Object.keys(client.langs.values)) { - const aliases = this.client.langs.aliases.find(x => x[0] === locale)?.[1] ?? []; - if (Object.values(Locale).includes(locale)) { - if (command.__t.name) { - const valueName = client.langs.getKey(locale, command.__t.name!); - if (valueName) command.name_localizations[locale as LocaleString] = valueName; - } - if (command.__t.description) { - const valueKey = client.langs.getKey(locale, command.__t.description!); - if (valueKey) command.description_localizations[locale as LocaleString] = valueKey; - } - } - for (const i of aliases) { - if (command.__t.name) { + const locales = this.client.langs.aliases.find(x => x[0] === locale)?.[1] ?? []; + if (Object.values(Locale).includes(locale)) locales.push(locale as LocaleString); + + if (command.__t.name) { + for (const i of locales) { const valueName = client.langs.getKey(locale, command.__t.name!); if (valueName) command.name_localizations[i] = valueName; } } - for (const i of aliases) { - if (command.__t.description) { + + if (command.__t.description) { + for (const i of locales) { const valueKey = client.langs.getKey(locale, command.__t.description!); if (valueKey) command.description_localizations[i] = valueKey; } diff --git a/src/common/types/options.ts b/src/common/types/options.ts index 8b3df29..86bdd99 100644 --- a/src/common/types/options.ts +++ b/src/common/types/options.ts @@ -1,9 +1,9 @@ import type { Identify } from '..'; import type { ImageURLOptions } from '../../api'; -import type { BaseClient } from '../../client/base'; +import type { UsingClient } from '../../commands'; export type ImageOptions = ImageURLOptions; -export type MethodContext = Identify<{ client: BaseClient } & T>; +export type MethodContext = Identify<{ client: UsingClient } & T>; export type MessageWebhookPayload = Identify<{ body: Body } & Extra>; diff --git a/src/components/handler.ts b/src/components/handler.ts index 67e3204..3fffbdd 100644 --- a/src/components/handler.ts +++ b/src/components/handler.ts @@ -1,6 +1,6 @@ import type { ComponentCallback, ListenerOptions, ModalSubmitCallback } from '../builders/types'; -import type { BaseClient } from '../client/base'; import { LimitedCollection } from '../collection'; +import type { UsingClient } from '../commands'; import { BaseHandler, magicImport, type Logger, type OnFailCallback } from '../common'; import type { ComponentInteraction, ModalSubmitInteraction } from '../structures'; import { ComponentCommand, InteractionCommandType, ModalCommand } from './command'; @@ -24,7 +24,7 @@ export class ComponentHandler extends BaseHandler { constructor( logger: Logger, - protected client: BaseClient, + protected client: UsingClient, ) { super(logger); } diff --git a/src/structures/AutoModerationRule.ts b/src/structures/AutoModerationRule.ts index c7ba8b2..a858c81 100644 --- a/src/structures/AutoModerationRule.ts +++ b/src/structures/AutoModerationRule.ts @@ -1,4 +1,4 @@ -import type { BaseClient } from '../client/base'; +import type { UsingClient } from '../commands'; import type { APIAutoModerationRule, MethodContext, @@ -11,7 +11,7 @@ import { DiscordBase } from './extra/DiscordBase'; export interface AutoModerationRule extends ObjectToLower {} export class AutoModerationRule extends DiscordBase { - constructor(client: BaseClient, data: APIAutoModerationRule) { + constructor(client: UsingClient, data: APIAutoModerationRule) { super(client, data); } diff --git a/src/structures/ClientUser.ts b/src/structures/ClientUser.ts index 8df6e2c..5b5e9aa 100644 --- a/src/structures/ClientUser.ts +++ b/src/structures/ClientUser.ts @@ -1,11 +1,11 @@ -import type { BaseClient } from '../client/base'; +import type { UsingClient } from '../commands'; import type { GatewayReadyDispatchData, RESTPatchAPICurrentUserJSONBody } from '../common'; import { User } from './User'; export class ClientUser extends User { bot = true; constructor( - client: BaseClient, + client: UsingClient, data: GatewayReadyDispatchData['user'], public application: GatewayReadyDispatchData['application'], ) { diff --git a/src/structures/Guild.ts b/src/structures/Guild.ts index 62b892c..2f7ff01 100644 --- a/src/structures/Guild.ts +++ b/src/structures/Guild.ts @@ -1,4 +1,4 @@ -import type { BaseClient } from '../client/base'; +import type { UsingClient } from '../commands'; import type { APIGuild, APIPartialGuild, GatewayGuildCreateDispatchData, ObjectToLower } from '../common'; import type { StructPropState, StructStates, ToClass } from '../common/types/util'; import { AutoModerationRule } from './AutoModerationRule'; @@ -21,7 +21,7 @@ export class Guild extends (BaseGuild as unk large!: StructPropState; unavailable?: StructPropState; - constructor(client: BaseClient, data: APIGuild | GatewayGuildCreateDispatchData) { + constructor(client: UsingClient, data: APIGuild | GatewayGuildCreateDispatchData) { super(client, data); if ('joined_at' in data) { diff --git a/src/structures/GuildEmoji.ts b/src/structures/GuildEmoji.ts index 70924b5..e99cea3 100644 --- a/src/structures/GuildEmoji.ts +++ b/src/structures/GuildEmoji.ts @@ -1,5 +1,5 @@ import type { BaseImageURLOptions } from '../api'; -import type { BaseClient } from '../client/base'; +import type { UsingClient } from '../commands'; import type { APIEmoji, EmojiShorter, @@ -14,7 +14,7 @@ export interface GuildEmoji extends DiscordBase, ObjectToLower) { permissions: PermissionsBitField; constructor( - client: BaseClient, + client: UsingClient, data: APIInteractionDataResolvedGuildMember, user: APIUser | User, /** the choosen guild id */ diff --git a/src/structures/GuildPreview.ts b/src/structures/GuildPreview.ts index 2a35279..bc53553 100644 --- a/src/structures/GuildPreview.ts +++ b/src/structures/GuildPreview.ts @@ -1,4 +1,4 @@ -import type { BaseClient } from '../client/base'; +import type { UsingClient } from '../commands'; import type { APIGuildPreview, APIPartialGuild } from '../common'; import { AnonymousGuild } from './AnonymousGuild'; @@ -7,7 +7,7 @@ import { AnonymousGuild } from './AnonymousGuild'; * @link https://discord.com/developers/docs/resources/guild#guild-preview-object */ export class GuildPreview extends AnonymousGuild { - constructor(client: BaseClient, data: APIGuildPreview) { + constructor(client: UsingClient, data: APIGuildPreview) { super(client, data as APIPartialGuild); } } diff --git a/src/structures/GuildRole.ts b/src/structures/GuildRole.ts index 6f62e4c..718c244 100644 --- a/src/structures/GuildRole.ts +++ b/src/structures/GuildRole.ts @@ -1,4 +1,4 @@ -import type { BaseClient } from '../client/base'; +import type { UsingClient } from '../commands'; import type { APIRole, MethodContext, @@ -15,7 +15,7 @@ export interface GuildRole extends DiscordBase, ObjectToLower {} export class GuildTemplate extends Base { - constructor(client: BaseClient, data: APITemplate) { + constructor(client: UsingClient, data: APITemplate) { super(client); this.__patchThis(data); } diff --git a/src/structures/Interaction.ts b/src/structures/Interaction.ts index fd9ba89..b3c0d2c 100644 --- a/src/structures/Interaction.ts +++ b/src/structures/Interaction.ts @@ -1,8 +1,7 @@ import { mix } from 'ts-mixer'; import type { RawFile } from '../api'; import { ActionRow, Embed, Modal, resolveAttachment, resolveFiles } from '../builders'; -import type { BaseClient } from '../client/base'; -import { type ContextOptionsResolved, OptionResolver, type UsingClient } from '../commands'; +import { OptionResolver, type ContextOptionsResolved, type UsingClient } from '../commands'; import type { APIActionRowComponent, APIApplicationCommandAutocompleteInteraction, @@ -209,7 +208,7 @@ export class BaseInteraction< }); } - static from(client: BaseClient, gateway: GatewayInteractionCreateDispatchData, __reply?: __InternalReplyFunction) { + static from(client: UsingClient, gateway: GatewayInteractionCreateDispatchData, __reply?: __InternalReplyFunction) { switch (gateway.type) { case InteractionType.ApplicationCommandAutocomplete: return new AutocompleteInteraction(client, gateway, __reply); @@ -293,7 +292,7 @@ export class AutocompleteInteraction extend declare data: ObjectToLower; options: OptionResolver; constructor( - client: BaseClient, + client: UsingClient, interaction: APIApplicationCommandAutocompleteInteraction, protected __reply?: __InternalReplyFunction, ) { @@ -492,7 +491,7 @@ export class SelectMenuInteraction extends ComponentInteraction { declare data: ObjectToLower; constructor( - client: BaseClient, + client: UsingClient, interaction: APIMessageComponentSelectMenuInteraction, protected __reply?: __InternalReplyFunction, ) { @@ -517,7 +516,7 @@ export class StringSelectMenuInteraction< export class ChannelSelectMenuInteraction extends SelectMenuInteraction { channels: AllChannels[]; constructor( - client: BaseClient, + client: UsingClient, interaction: APIMessageComponentSelectMenuInteraction, protected __reply?: __InternalReplyFunction, ) { @@ -532,7 +531,7 @@ export class MentionableSelectMenuInteraction extends SelectMenuInteraction { members: InteractionGuildMember[]; users: User[]; constructor( - client: BaseClient, + client: UsingClient, interaction: APIMessageComponentSelectMenuInteraction, protected __reply?: __InternalReplyFunction, ) { @@ -559,7 +558,7 @@ export class MentionableSelectMenuInteraction extends SelectMenuInteraction { export class RoleSelectMenuInteraction extends SelectMenuInteraction { roles: GuildRole[]; constructor( - client: BaseClient, + client: UsingClient, interaction: APIMessageComponentSelectMenuInteraction, protected __reply?: __InternalReplyFunction, ) { @@ -573,7 +572,7 @@ export class UserSelectMenuInteraction extends SelectMenuInteraction { members: InteractionGuildMember[]; users: User[]; constructor( - client: BaseClient, + client: UsingClient, interaction: APIMessageComponentSelectMenuInteraction, protected __reply?: __InternalReplyFunction, ) { diff --git a/src/structures/Message.ts b/src/structures/Message.ts index 3510c37..51929e4 100644 --- a/src/structures/Message.ts +++ b/src/structures/Message.ts @@ -1,5 +1,5 @@ import type { ListenerOptions } from '../builders'; -import type { BaseClient } from '../client/base'; +import type { UsingClient } from '../commands'; import type { APIChannelMention, APIGuildMember, @@ -35,7 +35,7 @@ export class BaseMessage extends DiscordBase { users: (GuildMember | User)[]; }; - constructor(client: BaseClient, data: MessageData) { + constructor(client: UsingClient, data: MessageData) { super(client, data); this.mentions = { roles: data.mention_roles ?? [], @@ -119,7 +119,7 @@ export interface Message ObjectToLower> {} export class Message extends BaseMessage { - constructor(client: BaseClient, data: MessageData) { + constructor(client: UsingClient, data: MessageData) { super(client, data); } @@ -161,7 +161,7 @@ export type WriteMessageWebhook = MessageWebhookMethodWriteParams; export class WebhookMessage extends BaseMessage { constructor( - client: BaseClient, + client: UsingClient, data: MessageData, readonly webhookId: string, readonly webhookToken: string, diff --git a/src/structures/Sticker.ts b/src/structures/Sticker.ts index a4f153c..ec7feff 100644 --- a/src/structures/Sticker.ts +++ b/src/structures/Sticker.ts @@ -1,5 +1,5 @@ +import type { UsingClient } from '..'; import type { Attachment } from '../builders'; -import type { BaseClient } from '../client/base'; import type { APISticker, MethodContext, @@ -14,7 +14,7 @@ export interface Sticker extends DiscordBase, ObjectToLower; messages!: ReturnType; - constructor(client: BaseClient, data: APIWebhook) { + constructor(client: UsingClient, data: APIWebhook) { super(client, data); if (data.user) { diff --git a/src/structures/channels.ts b/src/structures/channels.ts index 0746820..f27c45b 100644 --- a/src/structures/channels.ts +++ b/src/structures/channels.ts @@ -15,7 +15,7 @@ import { } from 'discord-api-types/v10'; import { mix } from 'ts-mixer'; import { Embed, resolveAttachment } from '../builders'; -import type { BaseClient } from '../client/base'; +import type { UsingClient } from '../commands'; import type { APIChannelBase, APIGuildChannel, @@ -43,7 +43,7 @@ import { channelLink } from './extra/functions'; export class BaseChannel extends DiscordBase> { declare type: T; - constructor(client: BaseClient, data: APIChannelBase) { + constructor(client: UsingClient, data: APIChannelBase) { super(client, data); } @@ -164,7 +164,7 @@ interface IChannelTypes { export interface BaseGuildChannel extends ObjectToLower, 'permission_overwrites'>> {} export class BaseGuildChannel extends BaseChannel { - constructor(client: BaseClient, data: APIGuildChannel) { + constructor(client: UsingClient, data: APIGuildChannel) { const { permission_overwrites, ...rest } = data; super(client, rest); } @@ -265,7 +265,7 @@ export interface TextBaseGuildChannel @mix(MessagesMethods) export class TextBaseGuildChannel extends BaseGuildChannel {} -export default function channelFrom(data: APIChannelBase, client: BaseClient): AllChannels { +export default function channelFrom(data: APIChannelBase, client: UsingClient): AllChannels { switch (data.type) { case ChannelType.GuildStageVoice: return new StageChannel(client, data); diff --git a/src/structures/extra/Base.ts b/src/structures/extra/Base.ts index 0a0d8e7..e3308ea 100644 --- a/src/structures/extra/Base.ts +++ b/src/structures/extra/Base.ts @@ -1,11 +1,10 @@ import { Router } from '../../api'; -import type { BaseClient } from '../../client/base'; import type { UsingClient } from '../../commands'; import { toCamelCase } from '../../common'; /** */ export abstract class Base { - constructor(client: BaseClient) { + constructor(client: UsingClient) { Object.assign(this, { client }); } diff --git a/src/structures/extra/DiscordBase.ts b/src/structures/extra/DiscordBase.ts index 4300849..0ced9b2 100644 --- a/src/structures/extra/DiscordBase.ts +++ b/src/structures/extra/DiscordBase.ts @@ -1,11 +1,11 @@ -import type { BaseClient } from '../../client/base'; +import type { UsingClient } from '../../commands'; import { Base } from './Base'; import { snowflakeToTimestamp } from './functions'; export class DiscordBase = { id: string }> extends Base { id: string; constructor( - client: BaseClient, + client: UsingClient, /** Unique ID of the object */ data: Data, ) {