fix: types

This commit is contained in:
MARCROCK22 2024-08-28 01:40:13 +00:00
parent a9d14c4c01
commit 0c1ef3777f
5 changed files with 31 additions and 12 deletions

View File

@ -9,6 +9,20 @@ import type { RestArguments } from '../api';
export interface InteractionRoutes {
interactions: (id: string) => (token: string) => {
callback: {
post(
args: RestArguments<
ProxyRequestMethod.Post,
RESTPostAPIInteractionCallbackJSONBody,
Omit<RESTPostAPIInteractionCallbackQuery, 'with_response'> & { with_response: true }
>,
): Promise<RESTPostAPIInteractionCallbackResult>;
post(
args: RestArguments<
ProxyRequestMethod.Post,
RESTPostAPIInteractionCallbackJSONBody,
Omit<RESTPostAPIInteractionCallbackQuery, 'with_response'> & { with_response: false }
>,
): Promise<undefined>;
post(
args: RestArguments<
ProxyRequestMethod.Post,

View File

@ -6,6 +6,7 @@ import type {
Command,
CommandContext,
ContextMenuCommand,
EntryPointCommand,
ExtraProps,
MenuCommandContext,
RegisteredMiddlewares,
@ -319,10 +320,12 @@ export class BaseClient {
BaseClient.assertString(applicationId, 'applicationId is not a string');
const commands = this.commands!.values;
const filter = filterSplit(commands, command => !command.guildId);
const filter = filterSplit<
Omit<Command | ContextMenuCommand, 'guildId'> | EntryPointCommand,
MakeRequired<Command | ContextMenuCommand, 'guildId'>
>(commands, command => ('guildId' in command ? !command.guildId : true));
if (this.commands?.entryPoint) {
// @ts-expect-error
filter.expect.push(this.commands.entryPoint);
}
@ -336,7 +339,7 @@ export class BaseClient {
const guilds = new Set<string>();
for (const command of filter.never) {
for (const guild_id of command.guildId!) {
for (const guild_id of command.guildId) {
guilds.add(guild_id);
}
}
@ -349,7 +352,7 @@ export class BaseClient {
.commands.put({
body: filter.never
.filter(
cmd => cmd.guildId?.includes(guildId) && (!('ignore' in cmd) || cmd.ignore !== IgnoreCommand.Slash),
cmd => cmd.guildId.includes(guildId) && (!('ignore' in cmd) || cmd.ignore !== IgnoreCommand.Slash),
)
.map(x => x.toJSON()),
});

View File

@ -79,7 +79,7 @@ export class EntryPointContext<M extends keyof RegisteredMiddlewares = never> ex
channel(mode?: 'rest' | 'flow'): Promise<AllChannels>;
channel(mode?: 'cache'): ReturnCache<AllChannels>;
channel(mode: 'cache' | 'rest' | 'flow' = 'cache') {
if (this.interaction?.channel && mode === 'cache')
if (this.interaction.channel && mode === 'cache')
return this.client.cache.adapter.isAsync ? Promise.resolve(this.interaction.channel) : this.interaction.channel;
return this.client.channels.fetch(this.channelId, mode === 'rest');
}

View File

@ -96,14 +96,17 @@ export function MergeOptions<T>(defaults: any, ...options: any[]): T {
* @param func The predicate function used to test elements of the array.
* @returns An object containing two arrays: one with elements that passed the test and one with elements that did not.
*/
export function filterSplit<Element, Predicate extends (value: Element) => boolean>(arr: Element[], func: Predicate) {
export function filterSplit<Element, Never = Element>(
arr: (Element | Never)[],
func: (value: Element | Never) => boolean,
) {
const expect: Element[] = [];
const never: Element[] = [];
const never: Never[] = [];
for (const element of arr) {
const test = func(element);
if (test) expect.push(element);
else never.push(element);
if (test) expect.push(element as Element);
else never.push(element as Never);
}
return { expect, never };

View File

@ -39,7 +39,6 @@ import {
type APIEntryPointCommandInteraction,
type InteractionCallbackData,
type InteractionCallbackResourceActivity,
type RESTPostAPIInteractionCallbackResult,
} from '../types';
import type { RawFile } from '../api';
@ -509,12 +508,12 @@ export class EntryPointInteraction<FromGuild extends boolean = boolean> extends
files = files ? await resolveFiles(files) : undefined;
body = BaseInteraction.transformBody(rest, files, this.client);
}
const response = (await this.client.proxy
const response = await this.client.proxy
.interactions(this.id)(this.token)
.callback.post({
body,
query: { with_response: true },
})) as RESTPostAPIInteractionCallbackResult;
});
const result: Partial<EntryPointWithResponseResult> = {
interaction: toCamelCase(response.interaction),