diff --git a/src/client/base.ts b/src/client/base.ts index 4473c4c..8699711 100644 --- a/src/client/base.ts +++ b/src/client/base.ts @@ -40,6 +40,7 @@ import { } from '../common'; import { promises } from 'node:fs'; +import { isBufferLike } from '../api/utils/utils'; import { HandleCommand } from '../commands/handle'; import { BanShorter } from '../common/shorters/bans'; import { VoiceStateShorter } from '../common/shorters/voiceStates'; @@ -55,7 +56,7 @@ import type { ModalSubmitInteraction, UserCommandInteraction, } from '../structures'; -import type { LocaleString, RESTPostAPIChannelMessageJSONBody } from '../types'; +import type { APIInteraction, APIInteractionResponse, LocaleString, RESTPostAPIChannelMessageJSONBody } from '../types'; import type { MessageStructure } from './transformers'; export class BaseClient { @@ -253,20 +254,17 @@ export class BaseClient { } async start( - options: Pick, 'langsDir' | 'commandsDir' | 'connection' | 'token' | 'componentsDir'> = { - token: undefined, - langsDir: undefined, - commandsDir: undefined, - connection: undefined, - componentsDir: undefined, - }, + options: Pick< + DeepPartial, + 'langsDir' | 'commandsDir' | 'connection' | 'token' | 'componentsDir' + > = {}, ) { await this.loadLangs(options.langsDir); await this.loadCommands(options.commandsDir); await this.loadComponents(options.componentsDir); const { token: tokenRC, debug } = await this.getRC(); - const token = options?.token ?? tokenRC; + const token = options.token ?? tokenRC; BaseClient.assertString(token, 'token is not a string'); if (this.rest.options.token === 'INVALID') this.rest.options.token = token; @@ -283,17 +281,56 @@ export class BaseClient { throw new Error('Function not implemented'); } - private shouldUploadCommands(cachePath: string, guildId?: string) { - return this.commands!.shouldUpload(cachePath, guildId).then(should => { - this.logger.debug( - should - ? `[${guildId ?? 'global'}] Change(s) detected, uploading commands` - : `[${guildId ?? 'global'}] commands seems to be up to date`, - ); - return should; + /** + * + * @param rawBody body of interaction + * @returns + */ + protected async onInteractionRequest(rawBody: APIInteraction): Promise<{ + headers: { 'Content-Type'?: string }; + response: APIInteractionResponse | FormData; + }> { + return new Promise(async r => { + await this.handleCommand.interaction(rawBody, -1, async ({ body, files }) => { + let response: FormData | APIInteractionResponse; + const headers: { 'Content-Type'?: string } = {}; + + if (files) { + response = new FormData(); + for (const [index, file] of files.entries()) { + const fileKey = file.key ?? `files[${index}]`; + if (isBufferLike(file.data)) { + response.append(fileKey, new Blob([file.data], { type: file.contentType }), file.filename); + } else { + response.append(fileKey, new Blob([`${file.data}`], { type: file.contentType }), file.filename); + } + } + if (body) { + response.append('payload_json', JSON.stringify(body)); + } + } else { + response = body ?? {}; + headers['Content-Type'] = 'application/json'; + } + + return r({ + headers, + response, + }); + }); }); } + private async shouldUploadCommands(cachePath: string, guildId?: string) { + const should = await this.commands!.shouldUpload(cachePath, guildId); + this.logger.debug( + should + ? `[${guildId ?? 'global'}] Change(s) detected, uploading commands` + : `[${guildId ?? 'global'}] commands seems to be up to date`, + ); + return should; + } + private syncCachePath(cachePath: string) { return promises.writeFile( cachePath, @@ -431,7 +468,7 @@ export interface BaseClientOptions { | ModalSubmitInteraction | EntryPointInteraction | When, - ) => object; + ) => Record; globalMiddlewares?: readonly (keyof RegisteredMiddlewares)[]; commands?: { defaults?: { @@ -516,7 +553,7 @@ export type RuntimeConfigHTTP = Omit; }; -export type InternalRuntimeConfig = Omit, 'publicKey' | 'port'>; +export type InternalRuntimeConfig = MakeRequired; export type RuntimeConfig = OmitInsert< InternalRuntimeConfig, 'intents', diff --git a/src/client/httpclient.ts b/src/client/httpclient.ts index 5989c6b..dbd30fb 100644 --- a/src/client/httpclient.ts +++ b/src/client/httpclient.ts @@ -1,6 +1,4 @@ -import { isBufferLike } from '../api/utils/utils'; import type { DeepPartial } from '../common'; -import type { APIInteraction, APIInteractionResponse } from '../types'; import type { BaseClientOptions, StartOptions } from './base'; import { BaseClient } from './base'; @@ -13,39 +11,4 @@ export class HttpClient extends BaseClient { await super.start(options); return this.execute(options.httpConnection ?? {}); } - - async onPacket(rawBody: APIInteraction): Promise<{ - headers: { 'Content-Type'?: string }; - response: APIInteractionResponse | FormData; - }> { - return new Promise(async r => { - await this.handleCommand.interaction(rawBody, -1, async ({ body, files }) => { - let response: FormData | APIInteractionResponse; - const headers: { 'Content-Type'?: string } = {}; - - if (files) { - response = new FormData(); - for (const [index, file] of files.entries()) { - const fileKey = file.key ?? `files[${index}]`; - if (isBufferLike(file.data)) { - response.append(fileKey, new Blob([file.data], { type: file.contentType }), file.filename); - } else { - response.append(fileKey, new Blob([`${file.data}`], { type: file.contentType }), file.filename); - } - } - if (body) { - response.append('payload_json', JSON.stringify(body)); - } - } else { - response = body ?? {}; - headers['Content-Type'] = 'application/json'; - } - - return r({ - headers, - response, - }); - }); - }); - } } diff --git a/src/client/types.ts b/src/client/types.ts index 864f1c5..df94b17 100644 --- a/src/client/types.ts +++ b/src/client/types.ts @@ -1,6 +1,6 @@ -import type { HttpClient } from './httpclient'; +import type { UsingClient } from '../commands'; export interface HttpServerAdapter { - client: HttpClient; + client: UsingClient; start?(path: `/${string}`): any; } diff --git a/src/commands/applications/chatcontext.ts b/src/commands/applications/chatcontext.ts index 88bde37..81ed43b 100644 --- a/src/commands/applications/chatcontext.ts +++ b/src/commands/applications/chatcontext.ts @@ -224,7 +224,7 @@ export class CommandContext< } export interface GuildCommandContext - extends Omit, 'guildId'>, 'guild'> { + extends Omit, 'guildId' | 'member'>, 'guild'> { guild(mode?: 'rest' | 'flow'): Promise>; guild(mode?: 'cache'): ReturnCache | undefined>; } diff --git a/src/commands/applications/entrycontext.ts b/src/commands/applications/entrycontext.ts index 9b9db3e..3868606 100644 --- a/src/commands/applications/entrycontext.ts +++ b/src/commands/applications/entrycontext.ts @@ -130,7 +130,7 @@ export class EntryPointContext ex } export interface GuildEntryPointContext - extends Omit, 'guildId'>, 'guild'> { + extends Omit, 'guildId' | 'member'>, 'guild'> { guild(mode?: 'rest' | 'flow'): Promise>; guild(mode?: 'cache'): ReturnCache | undefined>; } diff --git a/src/commands/applications/menucontext.ts b/src/commands/applications/menucontext.ts index 74fe6a0..4214024 100644 --- a/src/commands/applications/menucontext.ts +++ b/src/commands/applications/menucontext.ts @@ -169,7 +169,7 @@ export class MenuCommandContext< export interface GuildMenuCommandContext< T extends MessageCommandInteraction | UserCommandInteraction, M extends keyof RegisteredMiddlewares = never, -> extends Omit, 'guildId'>, 'guild'> { +> extends Omit, 'guildId' | 'member'>, 'guild'> { guild(mode?: 'rest' | 'flow'): Promise>; guild(mode?: 'cache'): ReturnCache | undefined>; } diff --git a/src/components/componentcontext.ts b/src/components/componentcontext.ts index 5d2eba6..f11dbfd 100644 --- a/src/components/componentcontext.ts +++ b/src/components/componentcontext.ts @@ -260,7 +260,7 @@ export interface ContextComponentCommandInteractionMap { export interface GuildComponentContext< Type extends keyof ContextComponentCommandInteractionMap, M extends keyof RegisteredMiddlewares = never, -> extends Omit, 'guildId'>, 'guild'> { +> extends Omit, 'guildId' | 'member'>, 'guild'> { guild(mode?: 'rest' | 'flow'): Promise>; guild(mode?: 'cache'): ReturnCache | undefined>; } diff --git a/src/components/modalcontext.ts b/src/components/modalcontext.ts index 1eff411..44d3efa 100644 --- a/src/components/modalcontext.ts +++ b/src/components/modalcontext.ts @@ -196,7 +196,7 @@ export class ModalContext extends } export interface GuildModalContext - extends Omit, 'guildId'>, 'guild'> { + extends Omit, 'guildId' | 'member'>, 'guild'> { guild(mode?: 'rest' | 'flow'): Promise>; guild(mode?: 'cache'): ReturnCache | undefined>; }