feat: message commands aliases

This commit is contained in:
MARCROCK22 2024-04-08 22:18:28 -04:00
parent 34482e4fd6
commit 33165faf95
4 changed files with 57 additions and 20 deletions

View File

@ -1,4 +1,4 @@
import { type GatewayDispatchPayload, GatewayIntentBits, type GatewayPresenceUpdateData } from 'discord-api-types/v10'; import { GatewayIntentBits, type GatewayDispatchPayload, type GatewayPresenceUpdateData } from 'discord-api-types/v10';
import { parentPort, workerData } from 'node:worker_threads'; import { parentPort, workerData } from 'node:worker_threads';
import type { ClientEvent, Command, CommandContext, EventHandlerLike, Message, SubCommand } from '..'; import type { ClientEvent, Command, CommandContext, EventHandlerLike, Message, SubCommand } from '..';
import type { DeepPartial, If, WatcherPayload, WatcherSendToShard } from '../common'; import type { DeepPartial, If, WatcherPayload, WatcherSendToShard } from '../common';

View File

@ -32,31 +32,41 @@ function getCommandFromContent(
parent?: Command; parent?: Command;
fullCommandName: string; fullCommandName: string;
} { } {
const parentName = commandRaw[0]; const rawParentName = commandRaw[0];
const groupName = commandRaw.length === 3 ? commandRaw[1] : undefined; const rawGroupName = commandRaw.length === 3 ? commandRaw[1] : undefined;
const subcommandName = groupName ? commandRaw[2] : commandRaw[1]; const rawSubcommandName = rawGroupName ? commandRaw[2] : commandRaw[1];
const parent = self.commands!.values.find( const parent = self.commands!.values.find(
x => (!('ignore' in x) || x.ignore !== IgnoreCommand.Message) && x.name === parentName, x =>
(!('ignore' in x) || x.ignore !== IgnoreCommand.Message) &&
(x.name === rawParentName || ('aliases' in x ? x.aliases?.includes(rawParentName) : false)),
); );
const fullCommandName = `${parentName}${ const fullCommandName = `${rawParentName}${
groupName ? ` ${groupName} ${subcommandName}` : `${subcommandName ? ` ${subcommandName}` : ''}` rawGroupName ? ` ${rawGroupName} ${rawSubcommandName}` : `${rawSubcommandName ? ` ${rawSubcommandName}` : ''}`
}`; }`;
if (!(parent instanceof Command)) return { fullCommandName }; if (!(parent instanceof Command)) return { fullCommandName };
if (groupName && !parent.groups?.[groupName!]) return getCommandFromContent([parentName, groupName], self); if (rawGroupName && !parent.groups?.[rawGroupName] && !parent.groupsAliases?.[rawGroupName])
if (subcommandName && !parent.options?.some(x => x instanceof SubCommand && x.name === subcommandName)) return getCommandFromContent([rawParentName, rawGroupName], self);
return getCommandFromContent([parentName], self); if (
rawSubcommandName &&
!parent.options?.some(
x => x instanceof SubCommand && (x.name === rawSubcommandName || x.aliases?.includes(rawSubcommandName)),
)
)
return getCommandFromContent([rawParentName], self);
const groupName = rawGroupName ? parent.groupsAliases?.[rawGroupName] || rawGroupName : undefined;
const command = const command =
groupName || subcommandName groupName || rawSubcommandName
? (parent.options?.find(opt => { ? (parent.options?.find(opt => {
if (opt instanceof SubCommand) { if (opt instanceof SubCommand) {
if (groupName) { if (groupName) {
if (opt.group !== groupName) return false; if (opt.group !== groupName) return false;
} }
if (opt.group && !groupName) return false; if (opt.group && !groupName) return false;
return subcommandName === opt.name; return rawSubcommandName === opt.name || opt.aliases?.includes(rawSubcommandName);
} }
return false; return false;
}) as SubCommand) }) as SubCommand)

View File

@ -109,14 +109,6 @@ class BaseCommand {
__filePath?: string; __filePath?: string;
__t?: { name: string | undefined; description: string | undefined }; __t?: { name: string | undefined; description: string | undefined };
__autoload?: true; __autoload?: true;
__tGroups?: Record<
string /* name for group*/,
{
name: string | undefined;
description: string | undefined;
defaultDescription: string;
}
>;
guildId?: string[]; guildId?: string[];
name!: string; name!: string;
@ -134,6 +126,8 @@ class BaseCommand {
ignore?: IgnoreCommand; ignore?: IgnoreCommand;
aliases?: string[];
/** @internal */ /** @internal */
async __runOptions( async __runOptions(
ctx: CommandContext<{}, never>, ctx: CommandContext<{}, never>,
@ -296,6 +290,16 @@ export class Command extends BaseCommand {
type = ApplicationCommandType.ChatInput; type = ApplicationCommandType.ChatInput;
groups?: Parameters<typeof Groups>[0]; groups?: Parameters<typeof Groups>[0];
groupsAliases?: Record<string, string>;
__tGroups?: Record<
string /* name for group*/,
{
name: string | undefined;
description: string | undefined;
defaultDescription: string;
}
>;
toJSON() { toJSON() {
const options: APIApplicationCommandOption[] = []; const options: APIApplicationCommandOption[] = [];

View File

@ -26,6 +26,7 @@ type DeclareOptions =
integrationTypes?: (keyof typeof IntegrationTypes)[]; integrationTypes?: (keyof typeof IntegrationTypes)[];
contexts?: (keyof typeof InteractionContextTypes)[]; contexts?: (keyof typeof InteractionContextTypes)[];
ignore?: IgnoreCommand; ignore?: IgnoreCommand;
aliases?: string[];
} }
| (Omit< | (Omit<
{ {
@ -71,12 +72,22 @@ export function GroupsT(
name?: FlatObjectKeys<DefaultLocale>; name?: FlatObjectKeys<DefaultLocale>;
description?: FlatObjectKeys<DefaultLocale>; description?: FlatObjectKeys<DefaultLocale>;
defaultDescription: string; defaultDescription: string;
aliases?: string[];
} }
>, >,
) { ) {
return <T extends { new (...args: any[]): {} }>(target: T) => return <T extends { new (...args: any[]): {} }>(target: T) =>
class extends target { class extends target {
__tGroups = groups; __tGroups = groups;
groupsAliases: Record<string, string> = {};
constructor(...args: any[]) {
super(...args);
for (const i in groups) {
for (const j of groups[i].aliases ?? []) {
this.groupsAliases[j] = i;
}
}
}
}; };
} }
@ -87,12 +98,22 @@ export function Groups(
name?: [language: LocaleString, value: string][]; name?: [language: LocaleString, value: string][];
description?: [language: LocaleString, value: string][]; description?: [language: LocaleString, value: string][];
defaultDescription: string; defaultDescription: string;
aliases?: string[];
} }
>, >,
) { ) {
return <T extends { new (...args: any[]): {} }>(target: T) => return <T extends { new (...args: any[]): {} }>(target: T) =>
class extends target { class extends target {
groups = groups; groups = groups;
groupsAliases: Record<string, string> = {};
constructor(...args: any[]) {
super(...args);
for (const i in groups) {
for (const j of groups[i].aliases ?? []) {
this.groupsAliases[j] = i;
}
}
}
}; };
} }
@ -152,12 +173,14 @@ export function Declare(declare: DeclareOptions) {
type: ApplicationCommandType = ApplicationCommandType.ChatInput; type: ApplicationCommandType = ApplicationCommandType.ChatInput;
guildId?: string[]; guildId?: string[];
ignore?: IgnoreCommand; ignore?: IgnoreCommand;
aliases?: string[];
constructor(...args: any[]) { constructor(...args: any[]) {
super(...args); super(...args);
if ('description' in declare) this.description = declare.description; if ('description' in declare) this.description = declare.description;
if ('type' in declare) this.type = declare.type; if ('type' in declare) this.type = declare.type;
if ('guildId' in declare) this.guildId = declare.guildId; if ('guildId' in declare) this.guildId = declare.guildId;
if ('ignore' in declare) this.ignore = declare.ignore; if ('ignore' in declare) this.ignore = declare.ignore;
if ('aliases' in declare) this.aliases = declare.aliases;
// check if all properties are valid // check if all properties are valid
} }
}; };