UsingClient, __parseCommandLocales

This commit is contained in:
MARCROCK22 2024-03-22 13:34:31 -04:00
parent 51b8fd216c
commit b072e33fd6
23 changed files with 74 additions and 81 deletions

View File

@ -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<APIAttachment> {}
export class Attachment extends Base {
constructor(
client: BaseClient,
client: UsingClient,
public data: APIAttachment,
) {
super(client);

7
src/cache/index.ts vendored
View File

@ -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);

View File

@ -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<T = any> {
constructor(
protected cache: Cache,
client?: BaseClient,
client?: UsingClient,
) {
if (client) {
this.client = client;
@ -16,7 +17,7 @@ export class BaseResource<T = any> {
}
/** @internal */
__setClient(client: BaseClient) {
__setClient(client: UsingClient) {
this.client = client;
}

View File

@ -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<T = any> {
constructor(
protected cache: Cache,
client?: BaseClient,
client?: UsingClient,
) {
if (client) {
this.client = client;
@ -16,7 +17,7 @@ export class GuildBasedResource<T = any> {
}
/** @internal */
__setClient(client: BaseClient) {
__setClient(client: UsingClient) {
this.client = client;
}

View File

@ -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<T = any> {
constructor(
protected cache: Cache,
client?: BaseClient,
client?: UsingClient,
) {
if (client) {
this.client = client;
@ -16,7 +17,7 @@ export class GuildRelatedResource<T = any> {
}
/** @internal */
__setClient(client: BaseClient) {
__setClient(client: UsingClient) {
this.client = client;
}

View File

@ -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<typeof Command>(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<Command['options']>;
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<string>(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<string>(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;
}

View File

@ -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<T = {}> = Identify<{ client: BaseClient } & T>;
export type MethodContext<T = {}> = Identify<{ client: UsingClient } & T>;
export type MessageWebhookPayload<Body, Extra = {}> = Identify<{ body: Body } & Extra>;

View File

@ -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);
}

View File

@ -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<APIAutoModerationRule> {}
export class AutoModerationRule extends DiscordBase<APIAutoModerationRule> {
constructor(client: BaseClient, data: APIAutoModerationRule) {
constructor(client: UsingClient, data: APIAutoModerationRule) {
super(client, data);
}

View File

@ -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'],
) {

View File

@ -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<State extends StructStates = 'api'> extends (BaseGuild as unk
large!: StructPropState<boolean, State, 'create'>;
unavailable?: StructPropState<boolean, State, 'create'>;
constructor(client: BaseClient, data: APIGuild | GatewayGuildCreateDispatchData) {
constructor(client: UsingClient, data: APIGuild | GatewayGuildCreateDispatchData) {
super(client, data);
if ('joined_at' in data) {

View File

@ -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<Omit<APIEmoji, 'i
export class GuildEmoji extends DiscordBase {
constructor(
client: BaseClient,
client: UsingClient,
data: APIEmoji,
readonly guildId: string,
) {

View File

@ -22,7 +22,7 @@ export type GuildMemberData =
| GatewayGuildMemberAddDispatchData
| APIInteractionDataResolvedGuildMember;
import type { BaseClient } from '../client/base';
import type { UsingClient } from '../commands';
import type { ImageOptions, MethodContext } from '../common/types/options';
import type { GuildMemberResolvable } from '../common/types/resolvables';
import { User } from './User';
@ -38,7 +38,7 @@ export class BaseGuildMember extends DiscordBase {
joinedTimestamp?: number;
communicationDisabledUntilTimestamp?: number | null;
constructor(
client: BaseClient,
client: UsingClient,
data: GuildMemberData,
id: string,
/** the choosen guild id */
@ -133,7 +133,7 @@ export interface GuildMember extends ObjectToLower<Omit<APIGuildMember, 'user' |
export class GuildMember extends BaseGuildMember {
user: User;
constructor(
client: BaseClient,
client: UsingClient,
data: GuildMemberData,
user: APIUser | User,
/** the choosen guild id */
@ -212,7 +212,7 @@ export class InteractionGuildMember extends (GuildMember as unknown as ToClass<
>) {
permissions: PermissionsBitField;
constructor(
client: BaseClient,
client: UsingClient,
data: APIInteractionDataResolvedGuildMember,
user: APIUser | User,
/** the choosen guild id */

View File

@ -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);
}
}

View File

@ -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<Omit<APIRole, 'per
export class GuildRole extends DiscordBase {
permissions: PermissionsBitField;
constructor(
client: BaseClient,
client: UsingClient,
data: APIRole,
readonly guildId: string,
) {

View File

@ -1,4 +1,4 @@
import type { BaseClient } from '../client/base';
import type { UsingClient } from '../commands';
import type {
APITemplate,
MethodContext,
@ -11,7 +11,7 @@ import { Base } from './extra/Base';
export interface GuildTemplate extends Base, ObjectToLower<APITemplate> {}
export class GuildTemplate extends Base {
constructor(client: BaseClient, data: APITemplate) {
constructor(client: UsingClient, data: APITemplate) {
super(client);
this.__patchThis(data);
}

View File

@ -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<FromGuild extends boolean = boolean> extend
declare data: ObjectToLower<APIApplicationCommandAutocompleteInteraction['data']>;
options: OptionResolver;
constructor(
client: BaseClient,
client: UsingClient,
interaction: APIApplicationCommandAutocompleteInteraction,
protected __reply?: __InternalReplyFunction,
) {
@ -492,7 +491,7 @@ export class SelectMenuInteraction extends ComponentInteraction {
declare data: ObjectToLower<APIMessageComponentSelectMenuInteraction['data']>;
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,
) {

View File

@ -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<Omit<MessageData, 'timestamp' | 'author' | 'mentions' | 'components'>> {}
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,

View File

@ -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<Omit<APISticker, 'us
export class Sticker extends DiscordBase {
user?: User;
constructor(client: BaseClient, data: APISticker) {
constructor(client: UsingClient, data: APISticker) {
super(client, data);
if (data.user) {
this.user = new User(this.client, data.user);

View File

@ -1,4 +1,4 @@
import type { BaseClient } from '../client/base';
import type { UsingClient } from '../commands';
import type {
APIWebhook,
ImageOptions,
@ -22,7 +22,7 @@ export class Webhook extends DiscordBase {
user?: User;
sourceGuild?: Partial<AnonymousGuild>;
messages!: ReturnType<typeof Webhook.messages>;
constructor(client: BaseClient, data: APIWebhook) {
constructor(client: UsingClient, data: APIWebhook) {
super(client, data);
if (data.user) {

View File

@ -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<T extends ChannelType> extends DiscordBase<APIChannelBase<ChannelType>> {
declare type: T;
constructor(client: BaseClient, data: APIChannelBase<ChannelType>) {
constructor(client: UsingClient, data: APIChannelBase<ChannelType>) {
super(client, data);
}
@ -164,7 +164,7 @@ interface IChannelTypes {
export interface BaseGuildChannel extends ObjectToLower<Omit<APIGuildChannel<ChannelType>, 'permission_overwrites'>> {}
export class BaseGuildChannel extends BaseChannel<ChannelType> {
constructor(client: BaseClient, data: APIGuildChannel<ChannelType>) {
constructor(client: UsingClient, data: APIGuildChannel<ChannelType>) {
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<ChannelType>, client: BaseClient): AllChannels {
export default function channelFrom(data: APIChannelBase<ChannelType>, client: UsingClient): AllChannels {
switch (data.type) {
case ChannelType.GuildStageVoice:
return new StageChannel(client, data);

View File

@ -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 });
}

View File

@ -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<Data extends Record<string, any> = { id: string }> extends Base {
id: string;
constructor(
client: BaseClient,
client: UsingClient,
/** Unique ID of the object */
data: Data,
) {