mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-03 05:26: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);
|
return this.onPacket(res, req);
|
||||||
});
|
});
|
||||||
this.app.listen(port, () => {
|
this.app.listen(port, () => {
|
||||||
this.logger.info(`Listening to port ${port}`);
|
this.logger.info(`Listening to <url>:${port}/interactions`);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
this.logger.warn('No UWS installed.');
|
this.logger.warn('No UWS installed.');
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
import { ApplicationCommandType, InteractionType, type APIInteraction } from 'discord-api-types/v10';
|
import { ApplicationCommandType, InteractionType, type APIInteraction } from 'discord-api-types/v10';
|
||||||
import {
|
import {
|
||||||
|
BaseCommand,
|
||||||
CommandContext,
|
CommandContext,
|
||||||
MenuCommandContext,
|
MenuCommandContext,
|
||||||
OptionResolver,
|
OptionResolver,
|
||||||
|
type RegisteredMiddlewares,
|
||||||
type Command,
|
type Command,
|
||||||
type ContextMenuCommand,
|
type ContextMenuCommand,
|
||||||
type ContextOptionsResolved,
|
type ContextOptionsResolved,
|
||||||
@ -101,7 +103,11 @@ export async function onInteractionCreate(
|
|||||||
return command.onBotPermissionsFail(context, interaction.appPermissions.keys(permissions));
|
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) {
|
if (resultRunGlobalMiddlewares.pass) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -109,7 +115,11 @@ export async function onInteractionCreate(
|
|||||||
return command.onMiddlewaresError(context, resultRunGlobalMiddlewares.error ?? 'Unknown error');
|
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) {
|
if (resultRunMiddlewares.pass) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,14 @@ import {
|
|||||||
type APIApplicationCommandSubcommandGroupOption,
|
type APIApplicationCommandSubcommandGroupOption,
|
||||||
type LocaleString,
|
type LocaleString,
|
||||||
} from 'discord-api-types/v10';
|
} 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 type { Attachment } from '../../builders';
|
||||||
import { magicImport, type FlatObjectKeys } from '../../common';
|
import { magicImport, type FlatObjectKeys } from '../../common';
|
||||||
import type { AllChannels, AutocompleteInteraction, GuildRole, InteractionGuildMember, User } from '../../structures';
|
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>;
|
export type ContextOptions<T extends OptionsRecord> = ContextOptionsAux<T>;
|
||||||
|
|
||||||
class BaseCommand {
|
export class BaseCommand {
|
||||||
middlewares: (keyof RegisteredMiddlewares)[] = [];
|
middlewares: (keyof RegisteredMiddlewares)[] = [];
|
||||||
|
|
||||||
__filePath?: string;
|
__filePath?: string;
|
||||||
@ -178,7 +185,7 @@ class BaseCommand {
|
|||||||
|
|
||||||
/** @internal */
|
/** @internal */
|
||||||
static __runMiddlewares(
|
static __runMiddlewares(
|
||||||
context: CommandContext<{}, never>,
|
context: CommandContext<{}, never> | ComponentContext | MenuCommandContext<any> | ModalContext,
|
||||||
middlewares: (keyof RegisteredMiddlewares)[],
|
middlewares: (keyof RegisteredMiddlewares)[],
|
||||||
global: boolean,
|
global: boolean,
|
||||||
): Promise<{ error?: string; pass?: 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 { magicImport, type PermissionStrings } from '../../common';
|
||||||
import type { IntegrationTypes, InteractionContextTypes, RegisteredMiddlewares } from '../decorators';
|
import type { IntegrationTypes, InteractionContextTypes, RegisteredMiddlewares } from '../decorators';
|
||||||
import type { MenuCommandContext } from './menucontext';
|
import type { MenuCommandContext } from './menucontext';
|
||||||
import type { PassFunction, StopFunction, UsingClient } from './shared';
|
import type { UsingClient } from './shared';
|
||||||
|
|
||||||
export abstract class ContextMenuCommand {
|
export abstract class ContextMenuCommand {
|
||||||
middlewares: (keyof RegisteredMiddlewares)[] = [];
|
middlewares: (keyof RegisteredMiddlewares)[] = [];
|
||||||
@ -23,66 +23,6 @@ export abstract class ContextMenuCommand {
|
|||||||
name_localizations?: Partial<Record<LocaleString, string>>;
|
name_localizations?: Partial<Record<LocaleString, string>>;
|
||||||
description_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() {
|
toJSON() {
|
||||||
return {
|
return {
|
||||||
name: this.name,
|
name: this.name,
|
||||||
|
@ -38,4 +38,28 @@ export class BaseContext {
|
|||||||
isModal(): this is ModalContext {
|
isModal(): this is ModalContext {
|
||||||
return false;
|
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 { ComponentType } from 'discord-api-types/v10';
|
||||||
import type { ContextComponentCommandInteractionMap, ComponentContext } from './componentcontext';
|
import type { ContextComponentCommandInteractionMap, ComponentContext } from './componentcontext';
|
||||||
import type { PassFunction, RegisteredMiddlewares, StopFunction, UsingClient } from '../commands';
|
import type { RegisteredMiddlewares, UsingClient } from '../commands';
|
||||||
|
|
||||||
export const InteractionCommandType = {
|
export const InteractionCommandType = {
|
||||||
COMPONENT: 0,
|
COMPONENT: 0,
|
||||||
@ -33,63 +33,4 @@ export abstract class ComponentCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
middlewares: (keyof RegisteredMiddlewares)[] = [];
|
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 type { ComponentCallback, ListenerOptions, ModalSubmitCallback } from '../builders/types';
|
||||||
import { LimitedCollection } from '../collection';
|
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 { BaseHandler, magicImport, type Logger, type OnFailCallback } from '../common';
|
||||||
import type { ComponentInteraction, ModalSubmitInteraction, StringSelectMenuInteraction } from '../structures';
|
import type { ComponentInteraction, ModalSubmitInteraction, StringSelectMenuInteraction } from '../structures';
|
||||||
import { ComponentCommand, InteractionCommandType } from './componentcommand';
|
import { ComponentCommand, InteractionCommandType } from './componentcommand';
|
||||||
@ -160,7 +160,7 @@ export class ComponentHandler extends BaseHandler {
|
|||||||
component = this.callback(paths[i].file);
|
component = this.callback(paths[i].file);
|
||||||
if (!component) continue;
|
if (!component) continue;
|
||||||
} catch (e) {
|
} 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(
|
this.logger.warn(
|
||||||
`${paths[i].path
|
`${paths[i].path
|
||||||
.split(process.cwd())
|
.split(process.cwd())
|
||||||
@ -215,7 +215,11 @@ export class ComponentHandler extends BaseHandler {
|
|||||||
Object.assign(context, extended);
|
Object.assign(context, extended);
|
||||||
if (!(await i.filter(context))) continue;
|
if (!(await i.filter(context))) continue;
|
||||||
try {
|
try {
|
||||||
const resultRunGlobalMiddlewares = await i.__runGlobalMiddlewares(context);
|
const resultRunGlobalMiddlewares = await BaseCommand.__runMiddlewares(
|
||||||
|
context,
|
||||||
|
(context.client.options?.globalMiddlewares ?? []) as keyof RegisteredMiddlewares,
|
||||||
|
true,
|
||||||
|
);
|
||||||
if (resultRunGlobalMiddlewares.pass) {
|
if (resultRunGlobalMiddlewares.pass) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -223,7 +227,7 @@ export class ComponentHandler extends BaseHandler {
|
|||||||
return i.onMiddlewaresError(context, resultRunGlobalMiddlewares.error ?? 'Unknown error');
|
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) {
|
if (resultRunMiddlewares.pass) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -258,7 +262,11 @@ export class ComponentHandler extends BaseHandler {
|
|||||||
try {
|
try {
|
||||||
if (i.type === InteractionCommandType.MODAL && (await i.filter(context))) {
|
if (i.type === InteractionCommandType.MODAL && (await i.filter(context))) {
|
||||||
try {
|
try {
|
||||||
const resultRunGlobalMiddlewares = await i.__runGlobalMiddlewares(context);
|
const resultRunGlobalMiddlewares = await BaseCommand.__runMiddlewares(
|
||||||
|
context,
|
||||||
|
(context.client.options?.globalMiddlewares ?? []) as keyof RegisteredMiddlewares,
|
||||||
|
true,
|
||||||
|
);
|
||||||
if (resultRunGlobalMiddlewares.pass) {
|
if (resultRunGlobalMiddlewares.pass) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -266,7 +274,7 @@ export class ComponentHandler extends BaseHandler {
|
|||||||
return i.onMiddlewaresError(context, resultRunGlobalMiddlewares.error ?? 'Unknown error');
|
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) {
|
if (resultRunMiddlewares.pass) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import type { RegisteredMiddlewares, PassFunction, StopFunction, UsingClient } from '../commands';
|
import type { RegisteredMiddlewares, UsingClient } from '../commands';
|
||||||
import { InteractionCommandType } from './componentcommand';
|
import { InteractionCommandType } from './componentcommand';
|
||||||
import type { ModalContext } from './modalcontext';
|
import type { ModalContext } from './modalcontext';
|
||||||
|
|
||||||
@ -23,64 +23,4 @@ export abstract class ModalCommand {
|
|||||||
onInternalError(client: UsingClient, error?: unknown): any {
|
onInternalError(client: UsingClient, error?: unknown): any {
|
||||||
client.logger.fatal(error);
|
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;
|
metadata: CommandMetadata<UnionToTuple<M>> = {} as never;
|
||||||
globalMetadata: GlobalMetadata = {};
|
globalMetadata: GlobalMetadata = {};
|
||||||
|
|
||||||
|
get customId() {
|
||||||
|
return this.interaction.customId;
|
||||||
|
}
|
||||||
|
|
||||||
get components() {
|
get components() {
|
||||||
return this.interaction.components;
|
return this.interaction.components;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user