mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-02 04:56:07 +00:00
fix: "bro y todo ese codigo repetido"
This commit is contained in:
parent
c3866146e5
commit
92c39b91c4
@ -95,7 +95,7 @@ export class HttpClient extends BaseClient {
|
||||
return this.onPacket(res, req);
|
||||
});
|
||||
this.app.listen(port, () => {
|
||||
this.logger.info(`Listening to port ${port}`);
|
||||
this.logger.info(`Listening to <url>:${port}/interactions`);
|
||||
});
|
||||
} else {
|
||||
this.logger.warn('No UWS installed.');
|
||||
|
@ -1,8 +1,10 @@
|
||||
import { ApplicationCommandType, InteractionType, type APIInteraction } from 'discord-api-types/v10';
|
||||
import {
|
||||
BaseCommand,
|
||||
CommandContext,
|
||||
MenuCommandContext,
|
||||
OptionResolver,
|
||||
type RegisteredMiddlewares,
|
||||
type Command,
|
||||
type ContextMenuCommand,
|
||||
type ContextOptionsResolved,
|
||||
@ -101,7 +103,11 @@ export async function onInteractionCreate(
|
||||
return command.onBotPermissionsFail(context, interaction.appPermissions.keys(permissions));
|
||||
}
|
||||
}
|
||||
const resultRunGlobalMiddlewares = await command.__runGlobalMiddlewares(context);
|
||||
const resultRunGlobalMiddlewares = await BaseCommand.__runMiddlewares(
|
||||
context,
|
||||
(self.options?.globalMiddlewares ?? []) as keyof RegisteredMiddlewares,
|
||||
true,
|
||||
);
|
||||
if (resultRunGlobalMiddlewares.pass) {
|
||||
return;
|
||||
}
|
||||
@ -109,7 +115,11 @@ export async function onInteractionCreate(
|
||||
return command.onMiddlewaresError(context, resultRunGlobalMiddlewares.error ?? 'Unknown error');
|
||||
}
|
||||
|
||||
const resultRunMiddlewares = await command.__runMiddlewares(context);
|
||||
const resultRunMiddlewares = await BaseCommand.__runMiddlewares(
|
||||
context,
|
||||
command.middlewares as keyof RegisteredMiddlewares,
|
||||
false,
|
||||
);
|
||||
if (resultRunMiddlewares.pass) {
|
||||
return;
|
||||
}
|
||||
|
@ -6,7 +6,14 @@ import {
|
||||
type APIApplicationCommandSubcommandGroupOption,
|
||||
type LocaleString,
|
||||
} from 'discord-api-types/v10';
|
||||
import type { PermissionStrings, SeyfertNumberOption, SeyfertStringOption } from '../..';
|
||||
import type {
|
||||
ComponentContext,
|
||||
MenuCommandContext,
|
||||
ModalContext,
|
||||
PermissionStrings,
|
||||
SeyfertNumberOption,
|
||||
SeyfertStringOption,
|
||||
} from '../..';
|
||||
import type { Attachment } from '../../builders';
|
||||
import { magicImport, type FlatObjectKeys } from '../../common';
|
||||
import type { AllChannels, AutocompleteInteraction, GuildRole, InteractionGuildMember, User } from '../../structures';
|
||||
@ -103,7 +110,7 @@ type ContextOptionsAux<T extends OptionsRecord> = {
|
||||
|
||||
export type ContextOptions<T extends OptionsRecord> = ContextOptionsAux<T>;
|
||||
|
||||
class BaseCommand {
|
||||
export class BaseCommand {
|
||||
middlewares: (keyof RegisteredMiddlewares)[] = [];
|
||||
|
||||
__filePath?: string;
|
||||
@ -178,7 +185,7 @@ class BaseCommand {
|
||||
|
||||
/** @internal */
|
||||
static __runMiddlewares(
|
||||
context: CommandContext<{}, never>,
|
||||
context: CommandContext<{}, never> | ComponentContext | MenuCommandContext<any> | ModalContext,
|
||||
middlewares: (keyof RegisteredMiddlewares)[],
|
||||
global: boolean,
|
||||
): Promise<{ error?: string; pass?: boolean }> {
|
||||
|
@ -2,7 +2,7 @@ import type { ApplicationCommandType, LocaleString } from 'discord-api-types/v10
|
||||
import { magicImport, type PermissionStrings } from '../../common';
|
||||
import type { IntegrationTypes, InteractionContextTypes, RegisteredMiddlewares } from '../decorators';
|
||||
import type { MenuCommandContext } from './menucontext';
|
||||
import type { PassFunction, StopFunction, UsingClient } from './shared';
|
||||
import type { UsingClient } from './shared';
|
||||
|
||||
export abstract class ContextMenuCommand {
|
||||
middlewares: (keyof RegisteredMiddlewares)[] = [];
|
||||
@ -23,66 +23,6 @@ export abstract class ContextMenuCommand {
|
||||
name_localizations?: Partial<Record<LocaleString, string>>;
|
||||
description_localizations?: Partial<Record<LocaleString, string>>;
|
||||
|
||||
/** @internal */
|
||||
static __runMiddlewares(
|
||||
context: MenuCommandContext<any>,
|
||||
middlewares: (keyof RegisteredMiddlewares)[],
|
||||
global: boolean,
|
||||
): Promise<{ error?: string; pass?: boolean }> {
|
||||
if (!middlewares.length) {
|
||||
return Promise.resolve({});
|
||||
}
|
||||
let index = 0;
|
||||
|
||||
return new Promise(res => {
|
||||
let running = true;
|
||||
const pass: PassFunction = () => {
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
running = false;
|
||||
return res({ pass: true });
|
||||
};
|
||||
function next(obj: any) {
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
// biome-ignore lint/style/noArguments: yes
|
||||
if (arguments.length) {
|
||||
// @ts-expect-error
|
||||
context[global ? 'globalMetadata' : 'metadata'][middlewares[index]] = obj;
|
||||
}
|
||||
if (++index >= middlewares.length) {
|
||||
running = false;
|
||||
return res({});
|
||||
}
|
||||
context.client.middlewares![middlewares[index]]({ context, next, stop, pass });
|
||||
}
|
||||
const stop: StopFunction = err => {
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
running = false;
|
||||
return res({ error: err });
|
||||
};
|
||||
context.client.middlewares![middlewares[0]]({ context, next, stop, pass });
|
||||
});
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
__runMiddlewares(context: MenuCommandContext<any, never>) {
|
||||
return ContextMenuCommand.__runMiddlewares(context, this.middlewares as (keyof RegisteredMiddlewares)[], false);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
__runGlobalMiddlewares(context: MenuCommandContext<any, never>) {
|
||||
return ContextMenuCommand.__runMiddlewares(
|
||||
context,
|
||||
(context.client.options?.globalMiddlewares ?? []) as (keyof RegisteredMiddlewares)[],
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
name: this.name,
|
||||
|
@ -38,4 +38,28 @@ export class BaseContext {
|
||||
isModal(): this is ModalContext {
|
||||
return false;
|
||||
}
|
||||
|
||||
isButton(): this is ComponentContext<'Button'> {
|
||||
return false;
|
||||
}
|
||||
|
||||
isChannelSelectMenu(): this is ComponentContext<'ChannelSelect'> {
|
||||
return false;
|
||||
}
|
||||
|
||||
isRoleSelectMenu(): this is ComponentContext<'RoleSelect'> {
|
||||
return false;
|
||||
}
|
||||
|
||||
isMentionableSelectMenu(): this is ComponentContext<'MentionableSelect'> {
|
||||
return false;
|
||||
}
|
||||
|
||||
isUserSelectMenu(): this is ComponentContext<'UserSelect'> {
|
||||
return false;
|
||||
}
|
||||
|
||||
isStringSelectMenu(): this is ComponentContext<'StringSelect'> {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { ComponentType } from 'discord-api-types/v10';
|
||||
import type { ContextComponentCommandInteractionMap, ComponentContext } from './componentcontext';
|
||||
import type { PassFunction, RegisteredMiddlewares, StopFunction, UsingClient } from '../commands';
|
||||
import type { RegisteredMiddlewares, UsingClient } from '../commands';
|
||||
|
||||
export const InteractionCommandType = {
|
||||
COMPONENT: 0,
|
||||
@ -33,63 +33,4 @@ export abstract class ComponentCommand {
|
||||
}
|
||||
|
||||
middlewares: (keyof RegisteredMiddlewares)[] = [];
|
||||
/** @internal */
|
||||
static __runMiddlewares(
|
||||
context: ComponentContext,
|
||||
middlewares: (keyof RegisteredMiddlewares)[],
|
||||
global: boolean,
|
||||
): Promise<{ error?: string; pass?: boolean }> {
|
||||
if (!middlewares.length) {
|
||||
return Promise.resolve({});
|
||||
}
|
||||
let index = 0;
|
||||
|
||||
return new Promise(res => {
|
||||
let running = true;
|
||||
const pass: PassFunction = () => {
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
running = false;
|
||||
return res({ pass: true });
|
||||
};
|
||||
function next(obj: any) {
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
// biome-ignore lint/style/noArguments: yes
|
||||
if (arguments.length) {
|
||||
// @ts-expect-error
|
||||
context[global ? 'globalMetadata' : 'metadata'][middlewares[index]] = obj;
|
||||
}
|
||||
if (++index >= middlewares.length) {
|
||||
running = false;
|
||||
return res({});
|
||||
}
|
||||
context.client.middlewares![middlewares[index]]({ context, next, stop, pass });
|
||||
}
|
||||
const stop: StopFunction = err => {
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
running = false;
|
||||
return res({ error: err });
|
||||
};
|
||||
context.client.middlewares![middlewares[0]]({ context, next, stop, pass });
|
||||
});
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
__runMiddlewares(context: ComponentContext) {
|
||||
return ComponentCommand.__runMiddlewares(context, this.middlewares as (keyof RegisteredMiddlewares)[], false);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
__runGlobalMiddlewares(context: ComponentContext) {
|
||||
return ComponentCommand.__runMiddlewares(
|
||||
context,
|
||||
(context.client.options?.globalMiddlewares ?? []) as (keyof RegisteredMiddlewares)[],
|
||||
true,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import type { ComponentCallback, ListenerOptions, ModalSubmitCallback } from '../builders/types';
|
||||
import { LimitedCollection } from '../collection';
|
||||
import type { UsingClient } from '../commands';
|
||||
import { BaseCommand, type RegisteredMiddlewares, type UsingClient } from '../commands';
|
||||
import { BaseHandler, magicImport, type Logger, type OnFailCallback } from '../common';
|
||||
import type { ComponentInteraction, ModalSubmitInteraction, StringSelectMenuInteraction } from '../structures';
|
||||
import { ComponentCommand, InteractionCommandType } from './componentcommand';
|
||||
@ -160,7 +160,7 @@ export class ComponentHandler extends BaseHandler {
|
||||
component = this.callback(paths[i].file);
|
||||
if (!component) continue;
|
||||
} catch (e) {
|
||||
if (e instanceof Error && e.message === 'paths[i].file is not a constructor') {
|
||||
if (e instanceof Error && e.message.includes('is not a constructor')) {
|
||||
this.logger.warn(
|
||||
`${paths[i].path
|
||||
.split(process.cwd())
|
||||
@ -215,7 +215,11 @@ export class ComponentHandler extends BaseHandler {
|
||||
Object.assign(context, extended);
|
||||
if (!(await i.filter(context))) continue;
|
||||
try {
|
||||
const resultRunGlobalMiddlewares = await i.__runGlobalMiddlewares(context);
|
||||
const resultRunGlobalMiddlewares = await BaseCommand.__runMiddlewares(
|
||||
context,
|
||||
(context.client.options?.globalMiddlewares ?? []) as keyof RegisteredMiddlewares,
|
||||
true,
|
||||
);
|
||||
if (resultRunGlobalMiddlewares.pass) {
|
||||
return;
|
||||
}
|
||||
@ -223,7 +227,7 @@ export class ComponentHandler extends BaseHandler {
|
||||
return i.onMiddlewaresError(context, resultRunGlobalMiddlewares.error ?? 'Unknown error');
|
||||
}
|
||||
|
||||
const resultRunMiddlewares = await i.__runMiddlewares(context);
|
||||
const resultRunMiddlewares = await BaseCommand.__runMiddlewares(context, i.middlewares, false);
|
||||
if (resultRunMiddlewares.pass) {
|
||||
return;
|
||||
}
|
||||
@ -258,7 +262,11 @@ export class ComponentHandler extends BaseHandler {
|
||||
try {
|
||||
if (i.type === InteractionCommandType.MODAL && (await i.filter(context))) {
|
||||
try {
|
||||
const resultRunGlobalMiddlewares = await i.__runGlobalMiddlewares(context);
|
||||
const resultRunGlobalMiddlewares = await BaseCommand.__runMiddlewares(
|
||||
context,
|
||||
(context.client.options?.globalMiddlewares ?? []) as keyof RegisteredMiddlewares,
|
||||
true,
|
||||
);
|
||||
if (resultRunGlobalMiddlewares.pass) {
|
||||
return;
|
||||
}
|
||||
@ -266,7 +274,7 @@ export class ComponentHandler extends BaseHandler {
|
||||
return i.onMiddlewaresError(context, resultRunGlobalMiddlewares.error ?? 'Unknown error');
|
||||
}
|
||||
|
||||
const resultRunMiddlewares = await i.__runMiddlewares(context);
|
||||
const resultRunMiddlewares = await BaseCommand.__runMiddlewares(context, i.middlewares, false);
|
||||
if (resultRunMiddlewares.pass) {
|
||||
return;
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import type { RegisteredMiddlewares, PassFunction, StopFunction, UsingClient } from '../commands';
|
||||
import type { RegisteredMiddlewares, UsingClient } from '../commands';
|
||||
import { InteractionCommandType } from './componentcommand';
|
||||
import type { ModalContext } from './modalcontext';
|
||||
|
||||
@ -23,64 +23,4 @@ export abstract class ModalCommand {
|
||||
onInternalError(client: UsingClient, error?: unknown): any {
|
||||
client.logger.fatal(error);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
static __runMiddlewares(
|
||||
context: ModalContext,
|
||||
middlewares: (keyof RegisteredMiddlewares)[],
|
||||
global: boolean,
|
||||
): Promise<{ error?: string; pass?: boolean }> {
|
||||
if (!middlewares.length) {
|
||||
return Promise.resolve({});
|
||||
}
|
||||
let index = 0;
|
||||
|
||||
return new Promise(res => {
|
||||
let running = true;
|
||||
const pass: PassFunction = () => {
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
running = false;
|
||||
return res({ pass: true });
|
||||
};
|
||||
function next(obj: any) {
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
// biome-ignore lint/style/noArguments: yes
|
||||
if (arguments.length) {
|
||||
// @ts-expect-error
|
||||
context[global ? 'globalMetadata' : 'metadata'][middlewares[index]] = obj;
|
||||
}
|
||||
if (++index >= middlewares.length) {
|
||||
running = false;
|
||||
return res({});
|
||||
}
|
||||
context.client.middlewares![middlewares[index]]({ context, next, stop, pass });
|
||||
}
|
||||
const stop: StopFunction = err => {
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
running = false;
|
||||
return res({ error: err });
|
||||
};
|
||||
context.client.middlewares![middlewares[0]]({ context, next, stop, pass });
|
||||
});
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
__runMiddlewares(context: ModalContext) {
|
||||
return ModalCommand.__runMiddlewares(context, this.middlewares as (keyof RegisteredMiddlewares)[], false);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
__runGlobalMiddlewares(context: ModalContext) {
|
||||
return ModalCommand.__runMiddlewares(
|
||||
context,
|
||||
(context.client.options?.globalMiddlewares ?? []) as (keyof RegisteredMiddlewares)[],
|
||||
true,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -32,6 +32,10 @@ export class ModalContext<M extends keyof RegisteredMiddlewares = never> extends
|
||||
metadata: CommandMetadata<UnionToTuple<M>> = {} as never;
|
||||
globalMetadata: GlobalMetadata = {};
|
||||
|
||||
get customId() {
|
||||
return this.interaction.customId;
|
||||
}
|
||||
|
||||
get components() {
|
||||
return this.interaction.components;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user