@pikokr/command.ts 4.0.5 → 5.0.0-dev.9796a99
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/codeql-analysis.yml +70 -0
- package/.github/workflows/publish.stable.yml +1 -1
- package/.github/workflows/publish.yml +1 -1
- package/.vscode/settings.json +10 -0
- package/.vscode/templates/ts.lict +5 -0
- package/dist/index.d.ts +40 -318
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +11 -8
- package/publish-version.js +10 -0
- package/scripts/docs.ts +8 -4
- package/src/applicationCommand/ApplicationCommand.ts +17 -0
- package/src/applicationCommand/ApplicationCommandOption.ts +9 -0
- package/src/applicationCommand/index.ts +2 -6
- package/src/core/components/BaseComponent.ts +32 -0
- package/src/core/components/ComponentArgument.ts +7 -0
- package/src/core/components/ComponentArgumentDecorator.ts +17 -0
- package/src/core/components/decoratorCreator.ts +68 -0
- package/src/core/components/index.ts +3 -0
- package/src/core/index.ts +1 -0
- package/src/core/symbols.ts +2 -0
- package/src/index.ts +10 -16
- package/test/index.ts +32 -25
- package/tsconfig.json +1 -1
- package/tsconfig.prod.json +4 -0
- package/tsup.config.ts +8 -4
- package/src/applicationCommand/AppCommand.ts +0 -32
- package/src/applicationCommand/decorator.ts +0 -62
- package/src/builtinModules/BuiltInModule.ts +0 -13
- package/src/builtinModules/BuiltinApplicationCommandConverters.ts +0 -16
- package/src/builtinModules/BuiltinCommandConverters.ts +0 -87
- package/src/builtinModules/CommandHandler.ts +0 -363
- package/src/builtinModules/index.ts +0 -7
- package/src/command/ArgumentConverter.ts +0 -22
- package/src/command/Command.ts +0 -35
- package/src/command/cooldown/adapter.ts +0 -22
- package/src/command/cooldown/decorator.ts +0 -67
- package/src/command/cooldown/error.ts +0 -9
- package/src/command/cooldown/index.ts +0 -9
- package/src/command/cooldown/type.ts +0 -12
- package/src/command/decorator.ts +0 -185
- package/src/command/index.ts +0 -9
- package/src/command/utils.ts +0 -33
- package/src/constants.ts +0 -31
- package/src/error/ArgumentConverterNotFound.ts +0 -18
- package/src/error/ArgumentNotProvided.ts +0 -12
- package/src/error/CommandCheckFailed.ts +0 -19
- package/src/error/CommandNotFound.ts +0 -11
- package/src/error/InvalidTargetError.ts +0 -9
- package/src/error/ModuleError.ts +0 -11
- package/src/error/PermissionRequired.ts +0 -17
- package/src/error/checks/DMOnlyCommand.ts +0 -9
- package/src/error/checks/GuildOnlyCommand.ts +0 -9
- package/src/error/checks/OwnerOnlyCommand.ts +0 -9
- package/src/error/checks/SlashCommandGlobalCheckError.ts +0 -11
- package/src/error/checks/index.ts +0 -8
- package/src/error/index.ts +0 -12
- package/src/interface/index.ts +0 -7
- package/src/listener/Listener.ts +0 -7
- package/src/listener/decorator.ts +0 -29
- package/src/listener/index.ts +0 -6
- package/src/messageComponents/base.ts +0 -16
- package/src/messageComponents/button.ts +0 -30
- package/src/messageComponents/index.ts +0 -6
- package/src/messageComponents/selectMenu.ts +0 -30
- package/src/structures/CommandClient.ts +0 -103
- package/src/structures/Module.ts +0 -54
- package/src/structures/Registry.ts +0 -245
- package/src/structures/index.ts +0 -7
- package/src/typings.ts +0 -35
- package/src/utils.ts +0 -10
- package/test/config.example.json +0 -3
- package/test/modules/dev.ts +0 -45
- package/test/modules/test.ts +0 -148
package/src/command/decorator.ts
DELETED
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022 pikokr. Licensed under the MIT license
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { KArgumentConverters, KCommands, KOptionals, KRest, KSlashArgumentConverters } from '../constants'
|
|
6
|
-
import { Command } from './Command'
|
|
7
|
-
import { checkTarget } from '../utils'
|
|
8
|
-
import { ArgumentConverter, ApplicationCommandArgumentConverter } from './ArgumentConverter'
|
|
9
|
-
import { Module } from '../structures'
|
|
10
|
-
import { createCheckDecorator } from './utils'
|
|
11
|
-
import { GuildMember, Message, PermissionResolvable, Permissions, TextChannel } from 'discord.js'
|
|
12
|
-
import { ClientPermissionRequired, DMOnlyCommandError, GuildOnlyCommandError, OwnerOnlyCommandError, UserPermissionRequired } from '../error'
|
|
13
|
-
|
|
14
|
-
type CommandOptions = {
|
|
15
|
-
name: string
|
|
16
|
-
aliases: string[] | ((msg: Message) => string[])
|
|
17
|
-
optionTypes?: any[]
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const command = (options: Partial<CommandOptions> = {}) => {
|
|
21
|
-
return (
|
|
22
|
-
target: Object,
|
|
23
|
-
propertyKey: string,
|
|
24
|
-
// descriptor: TypedPropertyDescriptor<any>,
|
|
25
|
-
) => {
|
|
26
|
-
checkTarget(target)
|
|
27
|
-
|
|
28
|
-
let properties: Command[] = Reflect.getMetadata(KCommands, target)
|
|
29
|
-
|
|
30
|
-
const params: any[] = options.optionTypes ?? Reflect.getMetadata('design:paramtypes', target, propertyKey)
|
|
31
|
-
|
|
32
|
-
const optionals: number = Reflect.getMetadata(KOptionals, target, propertyKey) || -1
|
|
33
|
-
|
|
34
|
-
const rest = Reflect.getMetadata(KRest, target, propertyKey) || -1
|
|
35
|
-
|
|
36
|
-
const command = new Command(
|
|
37
|
-
Reflect.get(target, propertyKey),
|
|
38
|
-
params.map((x, i) => ({
|
|
39
|
-
type: x,
|
|
40
|
-
optional: optionals === -1 ? false : optionals <= i,
|
|
41
|
-
rest: rest === -1 ? false : rest === i,
|
|
42
|
-
})),
|
|
43
|
-
options.name || propertyKey,
|
|
44
|
-
options.aliases || [],
|
|
45
|
-
target as Module,
|
|
46
|
-
propertyKey,
|
|
47
|
-
)
|
|
48
|
-
|
|
49
|
-
if (properties) {
|
|
50
|
-
properties.push(command)
|
|
51
|
-
} else {
|
|
52
|
-
properties = [command]
|
|
53
|
-
Reflect.defineMetadata(KCommands, properties, target)
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export const argumentConverter = (type: object, requireParameter = true) => {
|
|
59
|
-
return (
|
|
60
|
-
target: Object,
|
|
61
|
-
propertyKey: string,
|
|
62
|
-
// descriptor: TypedPropertyDescriptor<any>,
|
|
63
|
-
) => {
|
|
64
|
-
checkTarget(target)
|
|
65
|
-
|
|
66
|
-
let properties: ArgumentConverter[] = Reflect.getMetadata(KArgumentConverters, target)
|
|
67
|
-
|
|
68
|
-
const converter = new ArgumentConverter(type, Reflect.get(target, propertyKey), !requireParameter)
|
|
69
|
-
|
|
70
|
-
if (properties) {
|
|
71
|
-
properties.push(converter)
|
|
72
|
-
} else {
|
|
73
|
-
properties = [converter]
|
|
74
|
-
Reflect.defineMetadata(KArgumentConverters, properties, target)
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export const applicationCommandArgumentConverter = (type: object) => {
|
|
80
|
-
return (
|
|
81
|
-
target: Object,
|
|
82
|
-
propertyKey: string,
|
|
83
|
-
// descriptor: TypedPropertyDescriptor<any>,
|
|
84
|
-
) => {
|
|
85
|
-
checkTarget(target)
|
|
86
|
-
|
|
87
|
-
let properties: ApplicationCommandArgumentConverter[] = Reflect.getMetadata(KSlashArgumentConverters, target)
|
|
88
|
-
|
|
89
|
-
const converter = new ApplicationCommandArgumentConverter(type, Reflect.get(target, propertyKey))
|
|
90
|
-
|
|
91
|
-
if (properties) {
|
|
92
|
-
properties.push(converter)
|
|
93
|
-
} else {
|
|
94
|
-
properties = [converter]
|
|
95
|
-
Reflect.defineMetadata(KSlashArgumentConverters, properties, target)
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export const optional: ParameterDecorator = (target, propertyKey, parameterIndex) => {
|
|
101
|
-
checkTarget(target)
|
|
102
|
-
|
|
103
|
-
Reflect.defineMetadata(KOptionals, parameterIndex, target, propertyKey)
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export const rest: ParameterDecorator = (target, propertyKey, parameterIndex) => {
|
|
107
|
-
checkTarget(target)
|
|
108
|
-
|
|
109
|
-
const params: any[] = Reflect.getMetadata('design:paramtypes', target, propertyKey)
|
|
110
|
-
|
|
111
|
-
if (params.length - 1 !== parameterIndex) throw new Error('Rest decorator must be used at last argument.')
|
|
112
|
-
|
|
113
|
-
if (params[parameterIndex] !== String) throw new Error('Rest argument type must be "String"')
|
|
114
|
-
|
|
115
|
-
Reflect.defineMetadata(KRest, parameterIndex, target, propertyKey)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export const ownerOnly = createCheckDecorator(
|
|
119
|
-
(msg) => {
|
|
120
|
-
if (msg.data.cts.owners.includes(msg.author.id)) return true
|
|
121
|
-
throw new OwnerOnlyCommandError()
|
|
122
|
-
},
|
|
123
|
-
(i) => {
|
|
124
|
-
if (i.data.cts.owners.includes(i.user.id)) return true
|
|
125
|
-
throw new OwnerOnlyCommandError()
|
|
126
|
-
},
|
|
127
|
-
)
|
|
128
|
-
|
|
129
|
-
export const guildOnly = createCheckDecorator(
|
|
130
|
-
(msg) => {
|
|
131
|
-
if (!!msg.guild) return true
|
|
132
|
-
throw new GuildOnlyCommandError()
|
|
133
|
-
},
|
|
134
|
-
(i) => {
|
|
135
|
-
if (!!i.guildId) return true
|
|
136
|
-
throw new GuildOnlyCommandError()
|
|
137
|
-
},
|
|
138
|
-
)
|
|
139
|
-
|
|
140
|
-
export const dmOnly = createCheckDecorator(
|
|
141
|
-
(msg) => {
|
|
142
|
-
if (!msg.guild) return true
|
|
143
|
-
throw new DMOnlyCommandError()
|
|
144
|
-
},
|
|
145
|
-
(i) => {
|
|
146
|
-
if (!i.guildId) return true
|
|
147
|
-
throw new DMOnlyCommandError()
|
|
148
|
-
},
|
|
149
|
-
)
|
|
150
|
-
|
|
151
|
-
export const requireUserPermissions = (permission: PermissionResolvable) =>
|
|
152
|
-
createCheckDecorator(
|
|
153
|
-
(msg) => {
|
|
154
|
-
if (!msg.guild || !msg.member) throw new Error('This command must be used in guild.')
|
|
155
|
-
if (msg.member.permissionsIn(msg.channel as TextChannel).has(permission)) {
|
|
156
|
-
return true
|
|
157
|
-
}
|
|
158
|
-
throw new UserPermissionRequired(msg.member, new Permissions(permission))
|
|
159
|
-
},
|
|
160
|
-
(i) => {
|
|
161
|
-
if (!i.guild || !i.member) throw new Error('This command must be used in guild.')
|
|
162
|
-
if (!(i.member instanceof GuildMember) || i.member.permissionsIn(i.channel as TextChannel).has(permission)) {
|
|
163
|
-
return true
|
|
164
|
-
}
|
|
165
|
-
throw new UserPermissionRequired(i.member, new Permissions(permission))
|
|
166
|
-
},
|
|
167
|
-
)
|
|
168
|
-
|
|
169
|
-
export const requireClientPermissions = (permission: PermissionResolvable) =>
|
|
170
|
-
createCheckDecorator(
|
|
171
|
-
(msg) => {
|
|
172
|
-
if (!msg.guild) throw new Error('This command must be used in guild.')
|
|
173
|
-
if (msg.guild.me!.permissionsIn(msg.channel as TextChannel).has(permission)) {
|
|
174
|
-
return true
|
|
175
|
-
}
|
|
176
|
-
throw new ClientPermissionRequired(new Permissions(permission))
|
|
177
|
-
},
|
|
178
|
-
(i) => {
|
|
179
|
-
if (!i.guild) throw new Error('This command must be used in guild.')
|
|
180
|
-
if (i.guild.me!.permissionsIn(i.channel as TextChannel).has(permission)) {
|
|
181
|
-
return true
|
|
182
|
-
}
|
|
183
|
-
throw new ClientPermissionRequired(new Permissions(permission))
|
|
184
|
-
},
|
|
185
|
-
)
|
package/src/command/index.ts
DELETED
package/src/command/utils.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022 pikokr. Licensed under the MIT license
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { CommandInteraction, ContextMenuInteraction, Message, MessageComponentInteraction } from 'discord.js'
|
|
6
|
-
import type { CheckFunction, ApplicationCommandCheckFunction } from './Command'
|
|
7
|
-
import { KCommandChecks, KApplicationCommandChecks } from '../constants'
|
|
8
|
-
|
|
9
|
-
export const createCheckDecorator = (
|
|
10
|
-
execute: ((msg: Message) => boolean | Promise<boolean>) | null,
|
|
11
|
-
executeApplicationCommand?: (i: CommandInteraction | MessageComponentInteraction | ContextMenuInteraction) => boolean | Promise<boolean>,
|
|
12
|
-
): MethodDecorator => {
|
|
13
|
-
return (target, propertyKey) => {
|
|
14
|
-
if (execute) {
|
|
15
|
-
let properties: CheckFunction[] = Reflect.getMetadata(KCommandChecks, target, propertyKey)
|
|
16
|
-
if (properties) {
|
|
17
|
-
properties.push(execute)
|
|
18
|
-
} else {
|
|
19
|
-
properties = [execute]
|
|
20
|
-
Reflect.defineMetadata(KCommandChecks, properties, target, propertyKey)
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
if (executeApplicationCommand) {
|
|
24
|
-
let properties: ApplicationCommandCheckFunction[] = Reflect.getMetadata(KApplicationCommandChecks, target, propertyKey)
|
|
25
|
-
if (properties) {
|
|
26
|
-
properties.push(executeApplicationCommand)
|
|
27
|
-
} else {
|
|
28
|
-
properties = [executeApplicationCommand]
|
|
29
|
-
Reflect.defineMetadata(KApplicationCommandChecks, properties, target, propertyKey)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
package/src/constants.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022 pikokr. Licensed under the MIT license
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export const KCommands = Symbol('Command.TS Commands')
|
|
6
|
-
|
|
7
|
-
export const KApplicationCommands = Symbol('Command.TS Slash Commands')
|
|
8
|
-
|
|
9
|
-
export const KSlashCommandOptions = Symbol('Command.TS Slash Command Options')
|
|
10
|
-
|
|
11
|
-
export const KListeners = Symbol('Command.TS Listeners')
|
|
12
|
-
|
|
13
|
-
export const KModulePath = Symbol('Command.TS Module Path')
|
|
14
|
-
|
|
15
|
-
export const KListenerExecuteCache = Symbol('Command.TS Module Identifier')
|
|
16
|
-
|
|
17
|
-
export const KBuiltInModule = Symbol('Command.TS Built-In Module')
|
|
18
|
-
|
|
19
|
-
export const KOptionals = Symbol('Command.TS Optional Parameters')
|
|
20
|
-
|
|
21
|
-
export const KRest = Symbol('Command.TS Rest Parameter')
|
|
22
|
-
|
|
23
|
-
export const KArgumentConverters = Symbol('Command.TS Argument Converter')
|
|
24
|
-
|
|
25
|
-
export const KSlashArgumentConverters = Symbol('Command.TS Slash Argument Converter')
|
|
26
|
-
|
|
27
|
-
export const KCommandChecks = Symbol('Command.TS Command Checks')
|
|
28
|
-
|
|
29
|
-
export const KApplicationCommandChecks = Symbol('Command.TS Slash Command Checks')
|
|
30
|
-
|
|
31
|
-
export const KMessageComponentHandlers = Symbol('Command.TS Message Component Handlers')
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022 pikokr. Licensed under the MIT license
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { CommandInteraction, ContextMenuInteraction, Message } from 'discord.js'
|
|
6
|
-
import type { Argument } from '../command'
|
|
7
|
-
import type { AppCommandArgument } from '../applicationCommand'
|
|
8
|
-
|
|
9
|
-
export class ArgumentConverterNotFound extends Error {
|
|
10
|
-
constructor(public type: Argument, public msg: Message) {
|
|
11
|
-
super(`Argument converter ${type.type.name} not found.`)
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
export class ApplicationCommandArgumentConverterNotFound extends Error {
|
|
15
|
-
constructor(public type: AppCommandArgument, public interaction: CommandInteraction | ContextMenuInteraction) {
|
|
16
|
-
super(`Argument converter ${type.type.name} not found.`)
|
|
17
|
-
}
|
|
18
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022 pikokr. Licensed under the MIT license
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { Message } from 'discord.js'
|
|
6
|
-
import { Command } from '../command'
|
|
7
|
-
|
|
8
|
-
export class ArgumentNotProvided extends Error {
|
|
9
|
-
constructor(public index: number, public command: Command, public msg: Message) {
|
|
10
|
-
super(`Required argument #${index} not provided.`)
|
|
11
|
-
}
|
|
12
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022 pikokr. Licensed under the MIT license
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { CommandInteraction, ContextMenuInteraction, Message, MessageComponentInteraction } from 'discord.js'
|
|
6
|
-
import { Command } from '../command'
|
|
7
|
-
import { AppCommand } from '../applicationCommand'
|
|
8
|
-
|
|
9
|
-
export class CommandCheckFailed extends Error {
|
|
10
|
-
constructor(public msg: Message, public command: Command) {
|
|
11
|
-
super()
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export class ApplicationCommandCheckFailed extends Error {
|
|
16
|
-
constructor(public interaction: CommandInteraction | MessageComponentInteraction | ContextMenuInteraction, public command: AppCommand) {
|
|
17
|
-
super()
|
|
18
|
-
}
|
|
19
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022 pikokr. Licensed under the MIT license
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { Message } from 'discord.js'
|
|
6
|
-
|
|
7
|
-
export class CommandNotFound extends Error {
|
|
8
|
-
constructor(public commandName: string, public msg: Message, public args: string[]) {
|
|
9
|
-
super(`Command ${commandName} not found.`)
|
|
10
|
-
}
|
|
11
|
-
}
|
package/src/error/ModuleError.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022 pikokr. Licensed under the MIT license
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export class ModuleLoadError extends Error {
|
|
6
|
-
constructor(file: string, public error: Error) {
|
|
7
|
-
super('Failed to load module ' + file)
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export class InvalidModuleError extends Error {}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022 pikokr. Licensed under the MIT license
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { GuildMember, Permissions } from 'discord.js'
|
|
6
|
-
|
|
7
|
-
export class UserPermissionRequired extends Error {
|
|
8
|
-
constructor(public user: GuildMember, public permissions: Permissions) {
|
|
9
|
-
super()
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export class ClientPermissionRequired extends Error {
|
|
14
|
-
constructor(public permissions: Permissions) {
|
|
15
|
-
super()
|
|
16
|
-
}
|
|
17
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022 pikokr. Licensed under the MIT license
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { CommandInteraction } from 'discord.js'
|
|
6
|
-
|
|
7
|
-
export class SlashCommandGlobalCheckError extends Error {
|
|
8
|
-
constructor(public i: CommandInteraction) {
|
|
9
|
-
super('Slash command before-run check failed.')
|
|
10
|
-
}
|
|
11
|
-
}
|
package/src/error/index.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022 pikokr. Licensed under the MIT license
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export * from './InvalidTargetError'
|
|
6
|
-
export * from './ModuleError'
|
|
7
|
-
export * from './ArgumentNotProvided'
|
|
8
|
-
export * from './ArgumentConverterNotFound'
|
|
9
|
-
export * from './CommandCheckFailed'
|
|
10
|
-
export * from './PermissionRequired'
|
|
11
|
-
export * from './checks'
|
|
12
|
-
export * from './CommandNotFound'
|
package/src/interface/index.ts
DELETED
package/src/listener/Listener.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022 pikokr. Licensed under the MIT license
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { checkTarget } from '../utils'
|
|
6
|
-
import { KListeners } from '../constants'
|
|
7
|
-
import { Listener } from './Listener'
|
|
8
|
-
import { Module } from '../structures'
|
|
9
|
-
|
|
10
|
-
export const listener = (event: string) => {
|
|
11
|
-
return (
|
|
12
|
-
target: Module,
|
|
13
|
-
propertyKey: string,
|
|
14
|
-
// descriptor: TypedPropertyDescriptor<any>,
|
|
15
|
-
) => {
|
|
16
|
-
checkTarget(target)
|
|
17
|
-
|
|
18
|
-
let properties: Listener[] = Reflect.getMetadata(KListeners, target)
|
|
19
|
-
|
|
20
|
-
const listener = new Listener(event, Reflect.get(target, propertyKey))
|
|
21
|
-
|
|
22
|
-
if (properties) {
|
|
23
|
-
properties.push(listener)
|
|
24
|
-
} else {
|
|
25
|
-
properties = [listener]
|
|
26
|
-
Reflect.defineMetadata(KListeners, properties, target)
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
package/src/listener/index.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022 pikokr. Licensed under the MIT license
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { MessageComponentInteraction, MessageComponentType } from 'discord.js'
|
|
6
|
-
import { Module } from '../structures'
|
|
7
|
-
|
|
8
|
-
export type MessageComponentExecutor = (i: MessageComponentInteraction) => void | Promise<void>
|
|
9
|
-
|
|
10
|
-
export class MessageComponentHandler {
|
|
11
|
-
constructor(public componentId: string, public componentType: Exclude<MessageComponentType, 'ACTION_ROW'>, public execute: MessageComponentExecutor) {}
|
|
12
|
-
|
|
13
|
-
run(module: Module, i: MessageComponentInteraction) {
|
|
14
|
-
return this.execute.apply(module, [i])
|
|
15
|
-
}
|
|
16
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022 pikokr. Licensed under the MIT license
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { MessageComponentExecutor, MessageComponentHandler } from './base'
|
|
6
|
-
import { checkTarget } from '../utils'
|
|
7
|
-
import { KMessageComponentHandlers } from '../constants'
|
|
8
|
-
|
|
9
|
-
export class ButtonInteractionHandler extends MessageComponentHandler {
|
|
10
|
-
constructor(id: string, execute: MessageComponentExecutor) {
|
|
11
|
-
super(id, 'BUTTON', execute)
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const messageButton = (id: string): MethodDecorator => {
|
|
16
|
-
return (target, propertyKey) => {
|
|
17
|
-
checkTarget(target)
|
|
18
|
-
|
|
19
|
-
const handler = new ButtonInteractionHandler(id, Reflect.get(target, propertyKey))
|
|
20
|
-
|
|
21
|
-
let properties: MessageComponentHandler[] = Reflect.getMetadata(KMessageComponentHandlers, target)
|
|
22
|
-
|
|
23
|
-
if (properties) {
|
|
24
|
-
properties.push(handler)
|
|
25
|
-
} else {
|
|
26
|
-
properties = [handler]
|
|
27
|
-
Reflect.defineMetadata(KMessageComponentHandlers, properties, target)
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022 pikokr. Licensed under the MIT license
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { MessageComponentExecutor, MessageComponentHandler } from './base'
|
|
6
|
-
import { checkTarget } from '../utils'
|
|
7
|
-
import { KMessageComponentHandlers } from '../constants'
|
|
8
|
-
|
|
9
|
-
export class SelectMenuInteractionHandler extends MessageComponentHandler {
|
|
10
|
-
constructor(id: string, execute: MessageComponentExecutor) {
|
|
11
|
-
super(id, 'SELECT_MENU', execute)
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const messageSelectMenu = (id: string): MethodDecorator => {
|
|
16
|
-
return (target, propertyKey) => {
|
|
17
|
-
checkTarget(target)
|
|
18
|
-
|
|
19
|
-
const handler = new SelectMenuInteractionHandler(id, Reflect.get(target, propertyKey))
|
|
20
|
-
|
|
21
|
-
let properties: MessageComponentHandler[] = Reflect.getMetadata(KMessageComponentHandlers, target)
|
|
22
|
-
|
|
23
|
-
if (properties) {
|
|
24
|
-
properties.push(handler)
|
|
25
|
-
} else {
|
|
26
|
-
properties = [handler]
|
|
27
|
-
Reflect.defineMetadata(KMessageComponentHandlers, properties, target)
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|