fix: defaults of integrationTypes & contexts

This commit is contained in:
MARCROCK22 2024-06-04 00:26:54 +00:00
parent 7e30f4ba78
commit 625e400c20
5 changed files with 265 additions and 260 deletions

View File

@ -91,7 +91,7 @@ export async function onMessageCreate(
const prefixes = (await self.options.commands.prefix(message)).sort((a, b) => b.length - a.length); const prefixes = (await self.options.commands.prefix(message)).sort((a, b) => b.length - a.length);
const prefix = prefixes.find(x => message.content.startsWith(x)); const prefix = prefixes.find(x => message.content.startsWith(x));
if (!(prefix && message.content.startsWith(prefix))) return; if (!(prefix !== undefined && message.content.startsWith(prefix))) return;
const content = message.content.slice(prefix.length).trimStart(); const content = message.content.slice(prefix.length).trimStart();
const { fullCommandName, command, parent } = getCommandFromContent( const { fullCommandName, command, parent } = getCommandFromContent(
@ -105,7 +105,8 @@ export async function onMessageCreate(
if (!command) return; if (!command) return;
if (!command.run) return self.logger.warn(`${fullCommandName} command does not have 'run' callback`); if (!command.run) return self.logger.warn(`${fullCommandName} command does not have 'run' callback`);
if (!(command.contexts?.includes(InteractionContextType.BotDM) || message.guildId)) return; if (!command.contexts.includes(InteractionContextType.BotDM) && !message.guildId) return;
if (!command.contexts.includes(InteractionContextType.Guild) && message.guildId) return;
if (command.guildId && !command.guildId?.includes(message.guildId!)) return; if (command.guildId && !command.guildId?.includes(message.guildId!)) return;
const resolved: MakeRequired<ContextOptionsResolved> = { const resolved: MakeRequired<ContextOptionsResolved> = {

View File

@ -126,8 +126,8 @@ export class BaseCommand {
nsfw?: boolean; nsfw?: boolean;
description!: string; description!: string;
defaultMemberPermissions?: bigint; defaultMemberPermissions?: bigint;
integrationTypes?: ApplicationIntegrationType[]; integrationTypes: ApplicationIntegrationType[] = [];
contexts?: InteractionContextType[]; contexts: InteractionContextType[] = [];
botPermissions?: bigint; botPermissions?: bigint;
name_localizations?: Partial<Record<LocaleString, string>>; name_localizations?: Partial<Record<LocaleString, string>>;
description_localizations?: Partial<Record<LocaleString, string>>; description_localizations?: Partial<Record<LocaleString, string>>;

View File

@ -1,71 +1,71 @@
import type { import type {
ApplicationCommandType, ApplicationCommandType,
ApplicationIntegrationType, ApplicationIntegrationType,
InteractionContextType, InteractionContextType,
LocaleString, LocaleString,
} from 'discord-api-types/v10'; } from 'discord-api-types/v10';
import { magicImport, type PermissionStrings } from '../../common'; import { magicImport, type PermissionStrings } from '../../common';
import type { RegisteredMiddlewares } from '../decorators'; import type { RegisteredMiddlewares } from '../decorators';
import type { MenuCommandContext } from './menucontext'; import type { MenuCommandContext } from './menucontext';
import type { UsingClient } from './shared'; import type { UsingClient } from './shared';
export abstract class ContextMenuCommand { export abstract class ContextMenuCommand {
middlewares: (keyof RegisteredMiddlewares)[] = []; middlewares: (keyof RegisteredMiddlewares)[] = [];
__filePath?: string; __filePath?: string;
__t?: { name: string | undefined; description: string | undefined }; __t?: { name: string | undefined; description: string | undefined };
guildId?: string[]; guildId?: string[];
name!: string; name!: string;
type!: ApplicationCommandType.User | ApplicationCommandType.Message; type!: ApplicationCommandType.User | ApplicationCommandType.Message;
nsfw?: boolean; nsfw?: boolean;
integrationTypes?: ApplicationIntegrationType[]; integrationTypes: ApplicationIntegrationType[] = [];
contexts?: InteractionContextType[]; contexts: InteractionContextType[] = [];
description!: string; description!: string;
defaultMemberPermissions?: bigint; defaultMemberPermissions?: bigint;
botPermissions?: bigint; botPermissions?: bigint;
dm?: boolean; dm?: boolean;
name_localizations?: Partial<Record<LocaleString, string>>; name_localizations?: Partial<Record<LocaleString, string>>;
description_localizations?: Partial<Record<LocaleString, string>>; description_localizations?: Partial<Record<LocaleString, string>>;
toJSON() { toJSON() {
return { return {
name: this.name, name: this.name,
type: this.type, type: this.type,
nsfw: this.nsfw, nsfw: this.nsfw,
description: this.description, description: this.description,
name_localizations: this.name_localizations, name_localizations: this.name_localizations,
description_localizations: this.description_localizations, description_localizations: this.description_localizations,
guild_id: this.guildId, guild_id: this.guildId,
dm_permission: this.dm, dm_permission: this.dm,
default_member_permissions: this.defaultMemberPermissions ? this.defaultMemberPermissions.toString() : undefined, default_member_permissions: this.defaultMemberPermissions ? this.defaultMemberPermissions.toString() : undefined,
contexts: this.contexts, contexts: this.contexts,
integration_types: this.integrationTypes, integration_types: this.integrationTypes,
}; };
} }
async reload() { async reload() {
delete require.cache[this.__filePath!]; delete require.cache[this.__filePath!];
const __tempCommand = await magicImport(this.__filePath!).then(x => x.default ?? x); const __tempCommand = await magicImport(this.__filePath!).then(x => x.default ?? x);
Object.setPrototypeOf(this, __tempCommand.prototype); Object.setPrototypeOf(this, __tempCommand.prototype);
} }
abstract run?(context: MenuCommandContext<any>): any; abstract run?(context: MenuCommandContext<any>): any;
onAfterRun?(context: MenuCommandContext<any>, error: unknown | undefined): any; onAfterRun?(context: MenuCommandContext<any>, error: unknown | undefined): any;
onRunError(context: MenuCommandContext<any, never>, error: unknown): any { onRunError(context: MenuCommandContext<any, never>, error: unknown): any {
context.client.logger.fatal(`${this.name}.<onRunError>`, context.author.id, error); context.client.logger.fatal(`${this.name}.<onRunError>`, context.author.id, error);
} }
onMiddlewaresError(context: MenuCommandContext<any, never>, error: string): any { onMiddlewaresError(context: MenuCommandContext<any, never>, error: string): any {
context.client.logger.fatal(`${this.name}.<onMiddlewaresError>`, context.author.id, error); context.client.logger.fatal(`${this.name}.<onMiddlewaresError>`, context.author.id, error);
} }
onBotPermissionsFail(context: MenuCommandContext<any, never>, permissions: PermissionStrings): any { onBotPermissionsFail(context: MenuCommandContext<any, never>, permissions: PermissionStrings): any {
context.client.logger.fatal(`${this.name}.<onBotPermissionsFail>`, context.author.id, permissions); context.client.logger.fatal(`${this.name}.<onBotPermissionsFail>`, context.author.id, permissions);
} }
onPermissionsFail(context: MenuCommandContext<any, never>, permissions: PermissionStrings): any { onPermissionsFail(context: MenuCommandContext<any, never>, permissions: PermissionStrings): any {
context.client.logger.fatal(`${this.name}.<onPermissionsFail>`, context.author.id, permissions); context.client.logger.fatal(`${this.name}.<onPermissionsFail>`, context.author.id, permissions);
} }
onInternalError(client: UsingClient, error?: unknown): any { onInternalError(client: UsingClient, error?: unknown): any {
client.logger.fatal(error); client.logger.fatal(error);
} }
} }

View File

@ -1,183 +1,187 @@
import { import {
ApplicationCommandType, ApplicationCommandType,
ApplicationIntegrationType, ApplicationIntegrationType,
InteractionContextType, InteractionContextType,
PermissionFlagsBits, PermissionFlagsBits,
type LocaleString, type LocaleString,
} from 'discord-api-types/v10'; } from 'discord-api-types/v10';
import type { FlatObjectKeys, PermissionStrings } from '../common'; import type { FlatObjectKeys, PermissionStrings } from '../common';
import type { CommandOption, OptionsRecord, SubCommand } from './applications/chat'; import type { CommandOption, OptionsRecord, SubCommand } from './applications/chat';
import type { DefaultLocale, IgnoreCommand, MiddlewareContext } from './applications/shared'; import type { DefaultLocale, IgnoreCommand, MiddlewareContext } from './applications/shared';
export interface RegisteredMiddlewares {} export interface RegisteredMiddlewares {}
type DeclareOptions = type DeclareOptions =
| { | {
name: string; name: string;
description: string; description: string;
botPermissions?: PermissionStrings | bigint; botPermissions?: PermissionStrings | bigint;
defaultMemberPermissions?: PermissionStrings | bigint; defaultMemberPermissions?: PermissionStrings | bigint;
guildId?: string[]; guildId?: string[];
nsfw?: boolean; nsfw?: boolean;
integrationTypes?: (keyof typeof ApplicationIntegrationType)[]; integrationTypes?: (keyof typeof ApplicationIntegrationType)[];
contexts?: (keyof typeof InteractionContextType)[]; contexts?: (keyof typeof InteractionContextType)[];
ignore?: IgnoreCommand; ignore?: IgnoreCommand;
aliases?: string[]; aliases?: string[];
} }
| (Omit< | (Omit<
{ {
name: string; name: string;
description: string; description: string;
botPermissions?: PermissionStrings | bigint; botPermissions?: PermissionStrings | bigint;
defaultMemberPermissions?: PermissionStrings | bigint; defaultMemberPermissions?: PermissionStrings | bigint;
guildId?: string[]; guildId?: string[];
nsfw?: boolean; nsfw?: boolean;
integrationTypes?: (keyof typeof ApplicationIntegrationType)[]; integrationTypes?: (keyof typeof ApplicationIntegrationType)[];
contexts?: (keyof typeof InteractionContextType)[]; contexts?: (keyof typeof InteractionContextType)[];
}, },
'type' | 'description' 'type' | 'description'
> & { > & {
type: ApplicationCommandType.User | ApplicationCommandType.Message; type: ApplicationCommandType.User | ApplicationCommandType.Message;
}); });
export function Locales({ export function Locales({
name: names, name: names,
description: descriptions, description: descriptions,
}: { }: {
name?: [language: LocaleString, value: string][]; name?: [language: LocaleString, value: string][];
description?: [language: LocaleString, value: string][]; description?: [language: LocaleString, value: string][];
}) { }) {
return <T extends { new (...args: any[]): {} }>(target: T) => return <T extends { new (...args: any[]): {} }>(target: T) =>
class extends target { class extends target {
name_localizations = names ? Object.fromEntries(names) : undefined; name_localizations = names ? Object.fromEntries(names) : undefined;
description_localizations = descriptions ? Object.fromEntries(descriptions) : undefined; description_localizations = descriptions ? Object.fromEntries(descriptions) : undefined;
}; };
} }
export function LocalesT(name?: FlatObjectKeys<DefaultLocale>, description?: FlatObjectKeys<DefaultLocale>) { export function LocalesT(name?: FlatObjectKeys<DefaultLocale>, description?: FlatObjectKeys<DefaultLocale>) {
return <T extends { new (...args: any[]): {} }>(target: T) => return <T extends { new (...args: any[]): {} }>(target: T) =>
class extends target { class extends target {
__t = { name, description }; __t = { name, description };
}; };
} }
export function GroupsT( export function GroupsT(
groups: Record< groups: Record<
string /* name for group*/, string /* name for group*/,
{ {
name?: FlatObjectKeys<DefaultLocale>; name?: FlatObjectKeys<DefaultLocale>;
description?: FlatObjectKeys<DefaultLocale>; description?: FlatObjectKeys<DefaultLocale>;
defaultDescription: string; defaultDescription: string;
aliases?: 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> = {}; groupsAliases: Record<string, string> = {};
constructor(...args: any[]) { constructor(...args: any[]) {
super(...args); super(...args);
for (const i in groups) { for (const i in groups) {
for (const j of groups[i].aliases ?? []) { for (const j of groups[i].aliases ?? []) {
this.groupsAliases[j] = i; this.groupsAliases[j] = i;
} }
} }
} }
}; };
} }
export function Groups( export function Groups(
groups: Record< groups: Record<
string /* name for group*/, string /* name for group*/,
{ {
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[]; 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> = {}; groupsAliases: Record<string, string> = {};
constructor(...args: any[]) { constructor(...args: any[]) {
super(...args); super(...args);
for (const i in groups) { for (const i in groups) {
for (const j of groups[i].aliases ?? []) { for (const j of groups[i].aliases ?? []) {
this.groupsAliases[j] = i; this.groupsAliases[j] = i;
} }
} }
} }
}; };
} }
export function Group(groupName: string) { export function Group(groupName: string) {
return <T extends { new (...args: any[]): {} }>(target: T) => return <T extends { new (...args: any[]): {} }>(target: T) =>
class extends target { class extends target {
group = groupName; group = groupName;
}; };
} }
export function Options(options: (new () => SubCommand)[] | OptionsRecord) { export function Options(options: (new () => SubCommand)[] | OptionsRecord) {
return <T extends { new (...args: any[]): {} }>(target: T) => return <T extends { new (...args: any[]): {} }>(target: T) =>
class extends target { class extends target {
options: SubCommand[] | CommandOption[] = Array.isArray(options) options: SubCommand[] | CommandOption[] = Array.isArray(options)
? options.map(x => new x()) ? options.map(x => new x())
: Object.entries(options).map(([name, option]) => { : Object.entries(options).map(([name, option]) => {
return { return {
name, name,
...option, ...option,
} as CommandOption; } as CommandOption;
}); });
}; };
} }
export function AutoLoad() { export function AutoLoad() {
return <T extends { new (...args: any[]): {} }>(target: T) => return <T extends { new (...args: any[]): {} }>(target: T) =>
class extends target { class extends target {
__autoload = true; __autoload = true;
}; };
} }
export type ParseMiddlewares<T extends Record<string, MiddlewareContext>> = { export type ParseMiddlewares<T extends Record<string, MiddlewareContext>> = {
[k in keyof T]: T[k]; [k in keyof T]: T[k];
}; };
export function Middlewares(cbs: readonly (keyof RegisteredMiddlewares)[]) { export function Middlewares(cbs: readonly (keyof RegisteredMiddlewares)[]) {
return <T extends { new (...args: any[]): {} }>(target: T) => return <T extends { new (...args: any[]): {} }>(target: T) =>
class extends target { class extends target {
middlewares = cbs; middlewares = cbs;
}; };
} }
export function Declare(declare: DeclareOptions) { export function Declare(declare: DeclareOptions) {
return <T extends { new (...args: any[]): {} }>(target: T) => return <T extends { new (...args: any[]): {} }>(target: T) =>
class extends target { class extends target {
name = declare.name; name = declare.name;
nsfw = declare.nsfw; nsfw = declare.nsfw;
contexts = declare.contexts?.map(i => InteractionContextType[i]); contexts =
integrationTypes = declare.integrationTypes?.map(i => ApplicationIntegrationType[i]); declare.contexts?.map(i => InteractionContextType[i]) ??
defaultMemberPermissions = Array.isArray(declare.defaultMemberPermissions) Object.values(InteractionContextType).filter(x => typeof x === 'number');
? declare.defaultMemberPermissions?.reduce((acc, prev) => acc | PermissionFlagsBits[prev], BigInt(0)) integrationTypes = declare.integrationTypes?.map(i => ApplicationIntegrationType[i]) ?? [
: declare.defaultMemberPermissions; ApplicationIntegrationType.GuildInstall,
botPermissions = Array.isArray(declare.botPermissions) ];
? declare.botPermissions?.reduce((acc, prev) => acc | PermissionFlagsBits[prev], BigInt(0)) defaultMemberPermissions = Array.isArray(declare.defaultMemberPermissions)
: declare.botPermissions; ? declare.defaultMemberPermissions?.reduce((acc, prev) => acc | PermissionFlagsBits[prev], BigInt(0))
description = ''; : declare.defaultMemberPermissions;
type: ApplicationCommandType = ApplicationCommandType.ChatInput; botPermissions = Array.isArray(declare.botPermissions)
guildId?: string[]; ? declare.botPermissions?.reduce((acc, prev) => acc | PermissionFlagsBits[prev], BigInt(0))
ignore?: IgnoreCommand; : declare.botPermissions;
aliases?: string[]; description = '';
constructor(...args: any[]) { type: ApplicationCommandType = ApplicationCommandType.ChatInput;
super(...args); guildId?: string[];
if ('description' in declare) this.description = declare.description; ignore?: IgnoreCommand;
if ('type' in declare) this.type = declare.type; aliases?: string[];
if ('guildId' in declare) this.guildId = declare.guildId; constructor(...args: any[]) {
if ('ignore' in declare) this.ignore = declare.ignore; super(...args);
if ('aliases' in declare) this.aliases = declare.aliases; if ('description' in declare) this.description = declare.description;
// check if all properties are valid if ('type' in declare) this.type = declare.type;
} if ('guildId' in declare) this.guildId = declare.guildId;
}; if ('ignore' in declare) this.ignore = declare.ignore;
} if ('aliases' in declare) this.aliases = declare.aliases;
// check if all properties are valid
}
};
}

View File

@ -143,14 +143,14 @@ export class CommandHandler extends BaseHandler {
//TODO: locales //TODO: locales
if ('contexts' in command && 'contexts' in cached) { if ('contexts' in command && 'contexts' in cached) {
if (command.contexts?.length !== cached.contexts?.length) return true; if (command.contexts.length !== cached.contexts.length) return true;
if (command.contexts && cached.contexts) { if (command.contexts && cached.contexts) {
if (command.contexts.some(ctx => !cached.contexts!.includes(ctx))) return true; if (command.contexts.some(ctx => !cached.contexts!.includes(ctx))) return true;
} }
} }
if ('integration_types' in command && 'integration_types' in cached) { if ('integration_types' in command && 'integration_types' in cached) {
if (command.integration_types?.length !== cached.integration_types?.length) return true; if (command.integration_types.length !== cached.integration_types.length) return true;
if (command.integration_types && cached.integration_types) { if (command.integration_types && cached.integration_types) {
if (command.integration_types.some(ctx => !cached.integration_types!.includes(ctx))) return true; if (command.integration_types.some(ctx => !cached.integration_types!.includes(ctx))) return true;
} }