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 { ActivityGame, ClientActivity } from '../types/presence.ts'
import type { Extension } from '../commands/extension.ts' import type { Extension } from '../commands/extension.ts'
import { SlashClient } from '../interactions/slashClient.ts' import { SlashClient } from '../interactions/slashClient.ts'
import type { Interaction } from '../structures/slash.ts'
import { ShardManager } from './shard.ts' import { ShardManager } from './shard.ts'
import { Application } from '../structures/application.ts' import { Application } from '../structures/application.ts'
import { Invite } from '../structures/invite.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 */ /** Client's presence. Startup one if set before connecting */
presence: ClientPresence = new ClientPresence() 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 _id?: string
@ -175,13 +163,13 @@ export class Client extends HarmonyEventEmitter<ClientEvents> {
this.fetchUncachedReactions = true this.fetchUncachedReactions = true
if ( if (
this._decoratedEvents !== undefined && (this as any)._decoratedEvents !== undefined &&
Object.keys(this._decoratedEvents).length !== 0 Object.keys((this as any)._decoratedEvents).length !== 0
) { ) {
Object.entries(this._decoratedEvents).forEach((entry) => { Object.entries((this as any)._decoratedEvents).forEach((entry) => {
this.on(entry[0] as keyof ClientEvents, entry[1].bind(this)) this.on(entry[0] as keyof ClientEvents, (entry as any)[1].bind(this))
}) })
this._decoratedEvents = undefined ;(this as any)._decoratedEvents = undefined
} }
this.clientProperties = this.clientProperties =
@ -422,19 +410,23 @@ export class Client extends HarmonyEventEmitter<ClientEvents> {
} }
/** Event decorator to create an Event handler from function */ /** 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) { export function event(name?: keyof ClientEvents) {
return function ( return function (
client: Client | Extension, client: Client | Extension,
prop: keyof ClientEvents | string 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 { const listener = ((client as unknown) as {
[name in keyof ClientEvents]: (...args: ClientEvents[name]) => any [name in keyof ClientEvents]: (...args: ClientEvents[name]) => any
})[(prop as unknown) as keyof ClientEvents] })[(prop as unknown) as keyof ClientEvents]
if (typeof listener !== 'function') if (typeof listener !== 'function')
throw new Error('@event decorator requires a 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 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) commands: CommandsManager = new CommandsManager(this)
categories: CategoriesManager = new CategoriesManager(this) categories: CategoriesManager = new CategoriesManager(this)
_decoratedCommands?: { [name: string]: Command }
constructor(options: CommandClientOptions) { constructor(options: CommandClientOptions) {
super(options) super(options)
this.prefix = options.prefix this.prefix = options.prefix
@ -116,11 +114,12 @@ export class CommandClient extends Client implements CommandClientOptions {
this.caseSensitive = this.caseSensitive =
options.caseSensitive === undefined ? false : options.caseSensitive options.caseSensitive === undefined ? false : options.caseSensitive
if (this._decoratedCommands !== undefined) { const self = this as any
Object.values(this._decoratedCommands).forEach((entry) => { if (self._decoratedCommands !== undefined) {
Object.values(self._decoratedCommands).forEach((entry: any) => {
this.commands.add(entry) this.commands.add(entry)
}) })
this._decoratedCommands = undefined self._decoratedCommands = undefined
} }
this.on( this.on(
@ -382,11 +381,11 @@ export class CommandClient extends Client implements CommandClientOptions {
*/ */
export function command(options?: CommandOptions) { export function command(options?: CommandOptions) {
return function (target: CommandClient | Extension, name: string) { 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 { const prop = c[name]
[name: string]: (ctx: CommandContext) => any
})[name]
if (typeof prop !== 'function') if (typeof prop !== 'function')
throw new Error('@command decorator can only be used on class methods') 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 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 registered by this Extension */
events: { [name: string]: (...args: any[]) => {} } = {} events: { [name: string]: (...args: any[]) => {} } = {}
_decoratedCommands?: { [name: string]: Command }
_decoratedEvents?: { [name: string]: (...args: any[]) => any }
constructor(client: CommandClient) { constructor(client: CommandClient) {
this.client = client this.client = client
if (this._decoratedCommands !== undefined) { const self = this as any
Object.entries(this._decoratedCommands).forEach((entry) => { if (self._decoratedCommands !== undefined) {
Object.entries(self._decoratedCommands).forEach((entry: any) => {
entry[1].extension = this entry[1].extension = this
this.commands.add(entry[1]) this.commands.add(entry[1])
}) })
this._decoratedCommands = undefined self._decoratedCommands = undefined
} }
if ( if (
this._decoratedEvents !== undefined && self._decoratedEvents !== undefined &&
Object.keys(this._decoratedEvents).length !== 0 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.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[] = [] modules: SlashModule[] = []
publicKey?: string publicKey?: string
_decoratedSlash?: Array<{
name: string
guild?: string
parent?: string
group?: string
handler: (interaction: Interaction) => any
}>
constructor(options: SlashOptions) { constructor(options: SlashOptions) {
super() super()
let id = options.id let id = options.id
@ -76,17 +68,20 @@ export class SlashClient extends HarmonyEventEmitter<SlashClientEvents> {
this.enabled = options.enabled ?? true this.enabled = options.enabled ?? true
if (this.client?._decoratedSlash !== undefined) { // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
this.client._decoratedSlash.forEach((e) => { const client = this.client as any
if (client?._decoratedSlash !== undefined) {
client._decoratedSlash.forEach((e: any) => {
e.handler = e.handler.bind(this.client) e.handler = e.handler.bind(this.client)
this.handlers.push(e) this.handlers.push(e)
}) })
} }
if (this._decoratedSlash !== undefined) { const self = this as any
this._decoratedSlash.forEach((e) => { if (self._decoratedSlash !== undefined) {
self._decoratedSlash.forEach((e: any) => {
e.handler = e.handler.bind(this.client) 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 */ /** Decorator to create a Slash Command handler */
export function slash(name?: string, guild?: string) { export function slash(name?: string, guild?: string) {
return function (client: Client | SlashClient | SlashModule, prop: 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] const item = (client as { [name: string]: any })[prop]
if (typeof item !== 'function') { if (typeof item !== 'function') {
throw new Error('@slash decorator requires a function') throw new Error('@slash decorator requires a function')
} else } else
client._decoratedSlash.push({ c._decoratedSlash.push({
name: name ?? prop, name: name ?? prop,
guild, guild,
handler: item handler: item
@ -402,12 +399,14 @@ export function slash(name?: string, guild?: string) {
/** Decorator to create a Sub-Slash Command handler */ /** Decorator to create a Sub-Slash Command handler */
export function subslash(parent: string, name?: string, guild?: string) { export function subslash(parent: string, name?: string, guild?: string) {
return function (client: Client | SlashModule | SlashClient, prop: 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] const item = (client as { [name: string]: any })[prop]
if (typeof item !== 'function') { if (typeof item !== 'function') {
throw new Error('@subslash decorator requires a function') throw new Error('@subslash decorator requires a function')
} else } else
client._decoratedSlash.push({ c._decoratedSlash.push({
parent, parent,
name: name ?? prop, name: name ?? prop,
guild, guild,
@ -424,12 +423,14 @@ export function groupslash(
guild?: string guild?: string
) { ) {
return function (client: Client | SlashModule | SlashClient, prop: 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] const item = (client as { [name: string]: any })[prop]
if (typeof item !== 'function') { if (typeof item !== 'function') {
throw new Error('@groupslash decorator requires a function') throw new Error('@groupslash decorator requires a function')
} else } else
client._decoratedSlash.push({ c._decoratedSlash.push({
group, group,
parent, parent,
name: name ?? prop, 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 { CHANNEL } from '../types/endpoint.ts'
import getChannelByType from '../utils/channel.ts' import getChannelByType from '../utils/channel.ts'
import { BaseManager } from './base.ts' import { BaseManager } from './base.ts'
// Deno is bugged
import {} from './_util.ts'
export type AllMessageOptions = MessageOptions | Embed export type AllMessageOptions = MessageOptions | Embed

View File

@ -158,11 +158,7 @@ export enum ChannelTypes {
GUILD_CATEGORY = 4, GUILD_CATEGORY = 4,
GUILD_NEWS = 5, GUILD_NEWS = 5,
GUILD_STORE = 6, GUILD_STORE = 6,
<<<<<<< HEAD
GUILD_STAGE_VOICE = 13,
=======
GUILD_STAGE_VOICE = 13 GUILD_STAGE_VOICE = 13
>>>>>>> origin/main
} }
export interface MessagePayload { export interface MessagePayload {
@ -345,11 +341,7 @@ export enum MessageTypes {
GUILD_DISCOVERY_GRACE_PERIOD_INITIAL_WARNING = 16, GUILD_DISCOVERY_GRACE_PERIOD_INITIAL_WARNING = 16,
GUILD_DISCOVERY_GRACE_PERIOD_FINAL_WARNING = 17, GUILD_DISCOVERY_GRACE_PERIOD_FINAL_WARNING = 17,
REPLY = 19, REPLY = 19,
<<<<<<< HEAD
APPLICATION_COMMAND = 20,
=======
APPLICATION_COMMAND = 20 APPLICATION_COMMAND = 20
>>>>>>> origin/main
} }
export enum MessageActivityTypes { export enum MessageActivityTypes {

View File

@ -34,9 +34,5 @@ export const PermissionFlags: { [key: string]: bigint } = {
MANAGE_EMOJIS: 1n << 30n, MANAGE_EMOJIS: 1n << 30n,
USE_SLASH_COMMANDS: 1n << 31n, USE_SLASH_COMMANDS: 1n << 31n,
// Might be removed (as PR says) // Might be removed (as PR says)
<<<<<<< HEAD
REQUEST_TO_SPEAK: 0x100000000n,
=======
REQUEST_TO_SPEAK: 0x100000000n REQUEST_TO_SPEAK: 0x100000000n
>>>>>>> origin/main
} }

View File

@ -1,48 +1,42 @@
import { import { Client, Intents, event, slash } from '../mod.ts'
Client, import { Interaction } from '../src/structures/slash.ts'
Intents,
event,
slash,
SlashCommandOptionType as Type
} from '../../mod.ts'
import { Interaction } from '../structures/slash.ts'
import { TOKEN } from './config.ts' import { TOKEN } from './config.ts'
export class MyClient extends Client { export class MyClient extends Client {
@event() ready(): void { @event() ready(): void {
console.log(`Logged in as ${this.user?.tag}!`) console.log(`Logged in as ${this.user?.tag}!`)
this.slash.commands.bulkEdit( // this.slash.commands.bulkEdit(
[ // [
{ // {
name: 'test', // name: 'test',
description: 'Test command.', // description: 'Test command.',
options: [ // options: [
{ // {
name: 'user', // name: 'user',
type: Type.USER, // type: Type.USER,
description: 'User' // description: 'User'
}, // },
{ // {
name: 'role', // name: 'role',
type: Type.ROLE, // type: Type.ROLE,
description: 'Role' // description: 'Role'
}, // },
{ // {
name: 'channel', // name: 'channel',
type: Type.CHANNEL, // type: Type.CHANNEL,
description: 'Channel' // description: 'Channel'
}, // },
{ // {
name: 'string', // name: 'string',
type: Type.STRING, // type: Type.STRING,
description: 'String' // description: 'String'
} // }
] // ]
} // }
], // ],
'807935370556866560' // '807935370556866560'
) // )
this.slash.commands.bulkEdit([]) // this.slash.commands.bulkEdit([])
} }
@slash() test(d: Interaction): void { @slash() test(d: Interaction): void {