fix decorators

This commit is contained in:
DjDeveloperr 2021-04-23 11:18:59 +05:30
parent a1ead7e15e
commit 945bcc6f40
9 changed files with 85 additions and 110 deletions

View file

@ -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<ClientEvents> {
/** 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<ClientEvents> {
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<ClientEvents> {
}
/** 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
}
}

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -55,14 +55,6 @@ export class SlashClient extends HarmonyEventEmitter<SlashClientEvents> {
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<SlashClientEvents> {
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<SlashClientEvents> {
/** 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,

1
src/managers/_util.ts Normal file
View file

@ -0,0 +1 @@
export {}

View file

@ -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

View file

@ -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 {

View file

@ -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
}

View file

@ -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 {