Skip to content

Commit

Permalink
feat: more types and command
Browse files Browse the repository at this point in the history
  • Loading branch information
socram03 committed Aug 26, 2024
1 parent 45ce2f1 commit e0218d1
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 34 deletions.
6 changes: 4 additions & 2 deletions src/api/Routes/interactions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { RESTPostAPIInteractionCallbackJSONBody } from '../../types';
import type { RESTPostAPIInteractionCallbackJSONBody, RESTPostAPIInteractionCallbackResult } from '../../types';
import type { ProxyRequestMethod } from '../Router';
import type { RestArguments } from '../api';

export interface InteractionRoutes {
interactions: (id: string) => (token: string) => {
callback: {
post(args: RestArguments<ProxyRequestMethod.Post, RESTPostAPIInteractionCallbackJSONBody>): Promise<never>;
post(
args: RestArguments<ProxyRequestMethod.Post, RESTPostAPIInteractionCallbackJSONBody>,
): Promise<RESTPostAPIInteractionCallbackResult>;
};
};
}
71 changes: 71 additions & 0 deletions src/commands/applications/entryPoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { magicImport, type PermissionStrings } from '../../common';
import {
ApplicationCommandType,
type ApplicationIntegrationType,
type InteractionContextType,
type LocaleString,
} from '../../types';
import type { RegisteredMiddlewares } from '../decorators';
import type { ContextMenuCommand } from './menu';
import type { MenuCommandContext } from './menucontext';
import type { ExtraProps, UsingClient } from './shared';

export abstract class EntryPointCommand {
middlewares: (keyof RegisteredMiddlewares)[] = [];

__filePath?: string;
__t?: { name: string | undefined; description: string | undefined };

guildId?: string[];
name!: string;
type = ApplicationCommandType.PrimaryEntryPoint;
nsfw?: boolean;
integrationTypes: ApplicationIntegrationType[] = [];
contexts: InteractionContextType[] = [];
description!: string;
defaultMemberPermissions?: bigint;
botPermissions?: bigint;
dm?: boolean;
name_localizations?: Partial<Record<LocaleString, string>>;
description_localizations?: Partial<Record<LocaleString, string>>;

props: ExtraProps = {};

toJSON() {
return {
name: this.name,
type: this.type,
nsfw: this.nsfw,
description: this.description,
name_localizations: this.name_localizations,
description_localizations: this.description_localizations,
guild_id: this.guildId,
dm_permission: this.dm,
default_member_permissions: this.defaultMemberPermissions ? this.defaultMemberPermissions.toString() : undefined,
contexts: this.contexts,
integration_types: this.integrationTypes,
};
}

async reload() {
delete require.cache[this.__filePath!];
const __tempCommand = await magicImport(this.__filePath!).then(x => x.default ?? x);

Object.setPrototypeOf(this, __tempCommand.prototype);
}

abstract run?(context: MenuCommandContext<any>): any;
onAfterRun?(context: MenuCommandContext<any>, error: unknown | undefined): any;
onRunError(context: MenuCommandContext<any, never>, error: unknown): any {
context.client.logger.fatal(`${this.name}.<onRunError>`, context.author.id, error);
}
onMiddlewaresError(context: MenuCommandContext<any, never>, error: string): any {
context.client.logger.fatal(`${this.name}.<onMiddlewaresError>`, context.author.id, error);
}
onBotPermissionsFail(context: MenuCommandContext<any, never>, permissions: PermissionStrings): any {
context.client.logger.fatal(`${this.name}.<onBotPermissionsFail>`, context.author.id, permissions);
}
onInternalError(client: UsingClient, command: ContextMenuCommand, error?: unknown): any {
client.logger.fatal(command.name, error);
}
}
48 changes: 18 additions & 30 deletions src/commands/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,24 @@ import type { DefaultLocale, ExtraProps, IgnoreCommand, MiddlewareContext } from

export interface RegisteredMiddlewares {}

type DeclareOptions =
| {
name: string;
description: string;
botPermissions?: PermissionStrings | bigint;
defaultMemberPermissions?: PermissionStrings | bigint;
guildId?: string[];
nsfw?: boolean;
integrationTypes?: (keyof typeof ApplicationIntegrationType)[];
contexts?: (keyof typeof InteractionContextType)[];
ignore?: IgnoreCommand;
aliases?: string[];
props?: ExtraProps;
}
| (Omit<
{
name: string;
description: string;
botPermissions?: PermissionStrings | bigint;
defaultMemberPermissions?: PermissionStrings | bigint;
guildId?: string[];
nsfw?: boolean;
integrationTypes?: (keyof typeof ApplicationIntegrationType)[];
contexts?: (keyof typeof InteractionContextType)[];
props?: ExtraProps;
},
'type' | 'description'
> & {
type: ApplicationCommandType.User | ApplicationCommandType.Message;
export type CommandDeclareOptions =
| DecoratorDeclareOptions
| (Omit<DecoratorDeclareOptions, 'type' | 'description'> & {
type: ApplicationCommandType.User | ApplicationCommandType.Message | ApplicationCommandType.PrimaryEntryPoint;
});
export interface DecoratorDeclareOptions {
name: string;
description: string;
botPermissions?: PermissionStrings | bigint;
defaultMemberPermissions?: PermissionStrings | bigint;
guildId?: string[];
nsfw?: boolean;
integrationTypes?: (keyof typeof ApplicationIntegrationType)[];
contexts?: (keyof typeof InteractionContextType)[];
ignore?: IgnoreCommand;
aliases?: string[];
props?: ExtraProps;
}

export function Locales({
name: names,
Expand Down Expand Up @@ -154,7 +142,7 @@ export function Middlewares(cbs: readonly (keyof RegisteredMiddlewares)[]) {
};
}

export function Declare(declare: DeclareOptions) {
export function Declare(declare: CommandDeclareOptions) {
return <T extends { new (...args: any[]): {} }>(target: T) =>
class extends target {
name = declare.name;
Expand Down
69 changes: 69 additions & 0 deletions src/types/payloads/_interactions/responses.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { MakeRequired } from '../../../common';
import type { RESTPostAPIWebhookWithTokenJSONBody } from '../../index';
import type { APIActionRowComponent, APIModalActionRowComponent } from '../channel';
import type { MessageFlags } from '../index';
Expand Down Expand Up @@ -137,3 +138,71 @@ export interface APIModalInteractionResponseCallbackData {
*/
components: APIActionRowComponent<APIModalActionRowComponent>[];
}

/**
* https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback-interaction-callback-object
*/
export interface InteractionCallbackData<T extends InteractionType = InteractionType> {
id: string;
type: T;
/**
* Instance ID of the Activity if one was launched or joined
*/
activity_instance_id?: string;
/**
* ID of the message that was created by the interaction
*/
response_message_id?: string;
/**
* Whether or not the message is in a loading state
*/
response_message_loading?: boolean;
/**
* Whether or not the response message was ephemeral
*/
response_message_ephemeral?: boolean;
}

/**
* https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback-interaction-callback-resource-object
*/
export interface InteractionCallbackResourceActivity {
/**
* Instance ID of the Activity if one was launched or joined.
*/
id: string;
}

/**
* https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback-interaction-callback-activity-instance-resource
*/
export interface InteractionCallbackResource<T extends InteractionResponseType = InteractionResponseType> {
type: T;
/**
* Represents the Activity launched by this interaction.
*/
activity_instance?: InteractionCallbackResourceActivity;
/**
* Message created by the interaction.
*/
message?: Omit<RESTPostAPIWebhookWithTokenJSONBody, 'avatar_url' | 'username'> & { flags?: MessageFlags };
}

/**
* https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback-interaction-callback-response-object
*/
export interface InteractionCallbackResponse {
interaction: InteractionCallbackData;
resource?: InteractionCallbackResource;
}

/**
* https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback-interaction-callback-response-object
*/
export type APIInteractionCallbackLaunchActivity = InteractionCallbackResponse & {
resource?: Omit<MakeRequired<InteractionCallbackResource, 'activity_instance'>, 'message'>;
};

export type APIInteractionCallbackMessage = InteractionCallbackResponse & {
resource?: Omit<MakeRequired<InteractionCallbackResource, 'message'>, 'activity_instance'>;
};
12 changes: 10 additions & 2 deletions src/types/rest/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type {
APIApplicationCommand,
APIApplicationCommandPermission,
APIGuildApplicationCommandPermissions,
APIInteractionCallbackLaunchActivity,
APIInteractionCallbackMessage,
APIInteractionResponse,
APIInteractionResponseCallbackData,
ApplicationCommandType,
Expand Down Expand Up @@ -83,15 +85,16 @@ export interface RESTPostAPIContextMenuApplicationCommandsJSONBody extends RESTP
export interface RESTPostAPIEntryPointApplicationCommandsJSONBody extends RESTPostAPIBaseApplicationCommandsJSONBody {
type: ApplicationCommandType.PrimaryEntryPoint;
description: string;
handle: EntryPointCommandHandlerType;
handler: EntryPointCommandHandlerType;
}

/**
* https://discord.com/developers/docs/interactions/application-commands#create-global-application-command
*/
export type RESTPostAPIApplicationCommandsJSONBody =
| RESTPostAPIChatInputApplicationCommandsJSONBody
| RESTPostAPIContextMenuApplicationCommandsJSONBody;
| RESTPostAPIContextMenuApplicationCommandsJSONBody
| RESTPostAPIEntryPointApplicationCommandsJSONBody;

/**
* https://discord.com/developers/docs/interactions/application-commands#create-global-application-command
Expand Down Expand Up @@ -180,6 +183,11 @@ export type RESTPutAPIApplicationGuildCommandsResult = RESTAPIApplicationGuildCo
*/
export type RESTPostAPIInteractionCallbackJSONBody = APIInteractionResponse;

/**
* https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback
*/
export type RESTPostAPIInteractionCallbackResult = APIInteractionCallbackLaunchActivity | APIInteractionCallbackMessage;

/**
* https://discord.com/developers/docs/interactions/receiving-and-responding#create-interaction-response
*/
Expand Down

0 comments on commit e0218d1

Please sign in to comment.