From 945bcc6f4074be1c40bac9844c4648598d8492d6 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Fri, 23 Apr 2021 11:18:59 +0530 Subject: [PATCH] fix decorators --- src/client/client.ts | 30 +++++-------- src/commands/client.ts | 19 ++++----- src/commands/extension.ts | 18 ++++---- src/interactions/slashClient.ts | 39 ++++++++--------- src/managers/_util.ts | 1 + src/managers/channels.ts | 2 + src/types/channel.ts | 8 ---- src/types/permissionFlags.ts | 4 -- test/slash.ts | 74 +++++++++++++++------------------ 9 files changed, 85 insertions(+), 110 deletions(-) create mode 100644 src/managers/_util.ts diff --git a/src/client/client.ts b/src/client/client.ts index 457d77b..ea3650d 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -12,7 +12,6 @@ import { EmojisManager } from '../managers/emojis.ts' import { ActivityGame, ClientActivity } from '../types/presence.ts' import type { Extension } from '../commands/extension.ts' import { SlashClient } from '../interactions/slashClient.ts' -import type { Interaction } from '../structures/slash.ts' import { ShardManager } from './shard.ts' import { Application } from '../structures/application.ts' import { Invite } from '../structures/invite.ts' @@ -113,17 +112,6 @@ export class Client extends HarmonyEventEmitter { /** Client's presence. Startup one if set before connecting */ presence: ClientPresence = new ClientPresence() - _decoratedEvents?: { - [name: string]: (...args: any[]) => void - } - - _decoratedSlash?: Array<{ - name: string - guild?: string - parent?: string - group?: string - handler: (interaction: Interaction) => any - }> _id?: string @@ -175,13 +163,13 @@ export class Client extends HarmonyEventEmitter { this.fetchUncachedReactions = true if ( - this._decoratedEvents !== undefined && - Object.keys(this._decoratedEvents).length !== 0 + (this as any)._decoratedEvents !== undefined && + Object.keys((this as any)._decoratedEvents).length !== 0 ) { - Object.entries(this._decoratedEvents).forEach((entry) => { - this.on(entry[0] as keyof ClientEvents, entry[1].bind(this)) + Object.entries((this as any)._decoratedEvents).forEach((entry) => { + this.on(entry[0] as keyof ClientEvents, (entry as any)[1].bind(this)) }) - this._decoratedEvents = undefined + ;(this as any)._decoratedEvents = undefined } this.clientProperties = @@ -422,19 +410,23 @@ export class Client extends HarmonyEventEmitter { } /** Event decorator to create an Event handler from function */ +// eslint-disable-next-line @typescript-eslint/explicit-function-return-type export function event(name?: keyof ClientEvents) { return function ( client: Client | Extension, prop: keyof ClientEvents | string ) { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion + const c = client as any const listener = ((client as unknown) as { [name in keyof ClientEvents]: (...args: ClientEvents[name]) => any })[(prop as unknown) as keyof ClientEvents] if (typeof listener !== 'function') throw new Error('@event decorator requires a function') - if (client._decoratedEvents === undefined) client._decoratedEvents = {} + + if (c._decoratedEvents === undefined) c._decoratedEvents = {} const key = name === undefined ? prop : name - client._decoratedEvents[key] = listener + c._decoratedEvents[key] = listener } } diff --git a/src/commands/client.ts b/src/commands/client.ts index b2dd693..22b0117 100644 --- a/src/commands/client.ts +++ b/src/commands/client.ts @@ -70,8 +70,6 @@ export class CommandClient extends Client implements CommandClientOptions { commands: CommandsManager = new CommandsManager(this) categories: CategoriesManager = new CategoriesManager(this) - _decoratedCommands?: { [name: string]: Command } - constructor(options: CommandClientOptions) { super(options) this.prefix = options.prefix @@ -116,11 +114,12 @@ export class CommandClient extends Client implements CommandClientOptions { this.caseSensitive = options.caseSensitive === undefined ? false : options.caseSensitive - if (this._decoratedCommands !== undefined) { - Object.values(this._decoratedCommands).forEach((entry) => { + const self = this as any + if (self._decoratedCommands !== undefined) { + Object.values(self._decoratedCommands).forEach((entry: any) => { this.commands.add(entry) }) - this._decoratedCommands = undefined + self._decoratedCommands = undefined } this.on( @@ -382,11 +381,11 @@ export class CommandClient extends Client implements CommandClientOptions { */ export function command(options?: CommandOptions) { return function (target: CommandClient | Extension, name: string) { - if (target._decoratedCommands === undefined) target._decoratedCommands = {} + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion + const c = target as any + if (c._decoratedCommands === undefined) c._decoratedCommands = {} - const prop = ((target as unknown) as { - [name: string]: (ctx: CommandContext) => any - })[name] + const prop = c[name] if (typeof prop !== 'function') throw new Error('@command decorator can only be used on class methods') @@ -400,6 +399,6 @@ export function command(options?: CommandOptions) { if (target instanceof Extension) command.extension = target - target._decoratedCommands[command.name] = command + c._decoratedCommands[command.name] = command } } diff --git a/src/commands/extension.ts b/src/commands/extension.ts index 80f4bc1..1a29be1 100644 --- a/src/commands/extension.ts +++ b/src/commands/extension.ts @@ -73,27 +73,25 @@ export class Extension { /** Events registered by this Extension */ events: { [name: string]: (...args: any[]) => {} } = {} - _decoratedCommands?: { [name: string]: Command } - _decoratedEvents?: { [name: string]: (...args: any[]) => any } - constructor(client: CommandClient) { this.client = client - if (this._decoratedCommands !== undefined) { - Object.entries(this._decoratedCommands).forEach((entry) => { + const self = this as any + if (self._decoratedCommands !== undefined) { + Object.entries(self._decoratedCommands).forEach((entry: any) => { entry[1].extension = this this.commands.add(entry[1]) }) - this._decoratedCommands = undefined + self._decoratedCommands = undefined } if ( - this._decoratedEvents !== undefined && - Object.keys(this._decoratedEvents).length !== 0 + self._decoratedEvents !== undefined && + Object.keys(self._decoratedEvents).length !== 0 ) { - Object.entries(this._decoratedEvents).forEach((entry) => { + Object.entries(self._decoratedEvents).forEach((entry: any) => { this.listen(entry[0] as keyof ClientEvents, entry[1].bind(this)) }) - this._decoratedEvents = undefined + self._decoratedEvents = undefined } } diff --git a/src/interactions/slashClient.ts b/src/interactions/slashClient.ts index 29e808c..ac76107 100644 --- a/src/interactions/slashClient.ts +++ b/src/interactions/slashClient.ts @@ -55,14 +55,6 @@ export class SlashClient extends HarmonyEventEmitter { modules: SlashModule[] = [] publicKey?: string - _decoratedSlash?: Array<{ - name: string - guild?: string - parent?: string - group?: string - handler: (interaction: Interaction) => any - }> - constructor(options: SlashOptions) { super() let id = options.id @@ -76,17 +68,20 @@ export class SlashClient extends HarmonyEventEmitter { this.enabled = options.enabled ?? true - if (this.client?._decoratedSlash !== undefined) { - this.client._decoratedSlash.forEach((e) => { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion + const client = this.client as any + if (client?._decoratedSlash !== undefined) { + client._decoratedSlash.forEach((e: any) => { e.handler = e.handler.bind(this.client) this.handlers.push(e) }) } - if (this._decoratedSlash !== undefined) { - this._decoratedSlash.forEach((e) => { + const self = this as any + if (self._decoratedSlash !== undefined) { + self._decoratedSlash.forEach((e: any) => { e.handler = e.handler.bind(this.client) - this.handlers.push(e) + self.handlers.push(e) }) } @@ -386,12 +381,14 @@ export class SlashClient extends HarmonyEventEmitter { /** Decorator to create a Slash Command handler */ export function slash(name?: string, guild?: string) { return function (client: Client | SlashClient | SlashModule, prop: string) { - if (client._decoratedSlash === undefined) client._decoratedSlash = [] + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion + const c = client as any + if (c._decoratedSlash === undefined) c._decoratedSlash = [] const item = (client as { [name: string]: any })[prop] if (typeof item !== 'function') { throw new Error('@slash decorator requires a function') } else - client._decoratedSlash.push({ + c._decoratedSlash.push({ name: name ?? prop, guild, handler: item @@ -402,12 +399,14 @@ export function slash(name?: string, guild?: string) { /** Decorator to create a Sub-Slash Command handler */ export function subslash(parent: string, name?: string, guild?: string) { return function (client: Client | SlashModule | SlashClient, prop: string) { - if (client._decoratedSlash === undefined) client._decoratedSlash = [] + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion + const c = client as any + if (c._decoratedSlash === undefined) c._decoratedSlash = [] const item = (client as { [name: string]: any })[prop] if (typeof item !== 'function') { throw new Error('@subslash decorator requires a function') } else - client._decoratedSlash.push({ + c._decoratedSlash.push({ parent, name: name ?? prop, guild, @@ -424,12 +423,14 @@ export function groupslash( guild?: string ) { return function (client: Client | SlashModule | SlashClient, prop: string) { - if (client._decoratedSlash === undefined) client._decoratedSlash = [] + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion + const c = client as any + if (c._decoratedSlash === undefined) c._decoratedSlash = [] const item = (client as { [name: string]: any })[prop] if (typeof item !== 'function') { throw new Error('@groupslash decorator requires a function') } else - client._decoratedSlash.push({ + c._decoratedSlash.push({ group, parent, name: name ?? prop, diff --git a/src/managers/_util.ts b/src/managers/_util.ts new file mode 100644 index 0000000..e7f0809 --- /dev/null +++ b/src/managers/_util.ts @@ -0,0 +1 @@ +export {} diff --git a/src/managers/channels.ts b/src/managers/channels.ts index 69efe6c..a8c4b30 100644 --- a/src/managers/channels.ts +++ b/src/managers/channels.ts @@ -11,6 +11,8 @@ import type { import { CHANNEL } from '../types/endpoint.ts' import getChannelByType from '../utils/channel.ts' import { BaseManager } from './base.ts' +// Deno is bugged +import {} from './_util.ts' export type AllMessageOptions = MessageOptions | Embed diff --git a/src/types/channel.ts b/src/types/channel.ts index 8c5fd41..8db4e5e 100644 --- a/src/types/channel.ts +++ b/src/types/channel.ts @@ -158,11 +158,7 @@ export enum ChannelTypes { GUILD_CATEGORY = 4, GUILD_NEWS = 5, GUILD_STORE = 6, -<<<<<<< HEAD - GUILD_STAGE_VOICE = 13, -======= GUILD_STAGE_VOICE = 13 ->>>>>>> origin/main } export interface MessagePayload { @@ -345,11 +341,7 @@ export enum MessageTypes { GUILD_DISCOVERY_GRACE_PERIOD_INITIAL_WARNING = 16, GUILD_DISCOVERY_GRACE_PERIOD_FINAL_WARNING = 17, REPLY = 19, -<<<<<<< HEAD - APPLICATION_COMMAND = 20, -======= APPLICATION_COMMAND = 20 ->>>>>>> origin/main } export enum MessageActivityTypes { diff --git a/src/types/permissionFlags.ts b/src/types/permissionFlags.ts index 41039e5..7f9a542 100644 --- a/src/types/permissionFlags.ts +++ b/src/types/permissionFlags.ts @@ -34,9 +34,5 @@ export const PermissionFlags: { [key: string]: bigint } = { MANAGE_EMOJIS: 1n << 30n, USE_SLASH_COMMANDS: 1n << 31n, // Might be removed (as PR says) -<<<<<<< HEAD - REQUEST_TO_SPEAK: 0x100000000n, -======= REQUEST_TO_SPEAK: 0x100000000n ->>>>>>> origin/main } diff --git a/test/slash.ts b/test/slash.ts index 215cad0..ead5400 100644 --- a/test/slash.ts +++ b/test/slash.ts @@ -1,48 +1,42 @@ -import { - Client, - Intents, - event, - slash, - SlashCommandOptionType as Type -} from '../../mod.ts' -import { Interaction } from '../structures/slash.ts' +import { Client, Intents, event, slash } from '../mod.ts' +import { Interaction } from '../src/structures/slash.ts' import { TOKEN } from './config.ts' export class MyClient extends Client { @event() ready(): void { console.log(`Logged in as ${this.user?.tag}!`) - this.slash.commands.bulkEdit( - [ - { - name: 'test', - description: 'Test command.', - options: [ - { - name: 'user', - type: Type.USER, - description: 'User' - }, - { - name: 'role', - type: Type.ROLE, - description: 'Role' - }, - { - name: 'channel', - type: Type.CHANNEL, - description: 'Channel' - }, - { - name: 'string', - type: Type.STRING, - description: 'String' - } - ] - } - ], - '807935370556866560' - ) - this.slash.commands.bulkEdit([]) + // this.slash.commands.bulkEdit( + // [ + // { + // name: 'test', + // description: 'Test command.', + // options: [ + // { + // name: 'user', + // type: Type.USER, + // description: 'User' + // }, + // { + // name: 'role', + // type: Type.ROLE, + // description: 'Role' + // }, + // { + // name: 'channel', + // type: Type.CHANNEL, + // description: 'Channel' + // }, + // { + // name: 'string', + // type: Type.STRING, + // description: 'String' + // } + // ] + // } + // ], + // '807935370556866560' + // ) + // this.slash.commands.bulkEdit([]) } @slash() test(d: Interaction): void {