@pikokr/command.ts 3.2.4-dev.434c22e → 3.2.4-dev.535de3c
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/dist/applicationCommand/AppCommand.d.ts +18 -0
- package/dist/{slashCommand/SlashCommand.js → applicationCommand/AppCommand.js} +7 -7
- package/dist/applicationCommand/AppCommand.js.map +1 -0
- package/dist/applicationCommand/decorator.d.ts +10 -0
- package/dist/{slashCommand → applicationCommand}/decorator.js +9 -8
- package/dist/applicationCommand/decorator.js.map +1 -0
- package/dist/applicationCommand/index.d.ts +2 -0
- package/dist/{slashCommand → applicationCommand}/index.js +1 -1
- package/dist/applicationCommand/index.js.map +1 -0
- package/dist/builtinModules/CommandHandler.d.ts +4 -1
- package/dist/builtinModules/CommandHandler.js +107 -15
- package/dist/builtinModules/CommandHandler.js.map +1 -1
- package/dist/command/Command.d.ts +2 -2
- package/dist/command/decorator.d.ts +1 -0
- package/dist/command/decorator.js +2 -1
- package/dist/command/decorator.js.map +1 -1
- package/dist/command/utils.d.ts +2 -2
- package/dist/command/utils.js +6 -6
- package/dist/command/utils.js.map +1 -1
- package/dist/constants.d.ts +3 -2
- package/dist/constants.js +4 -3
- package/dist/constants.js.map +1 -1
- package/dist/error/ArgumentConverterNotFound.d.ts +1 -1
- package/dist/error/CommandCheckFailed.d.ts +6 -6
- package/dist/error/CommandCheckFailed.js +3 -3
- package/dist/error/CommandCheckFailed.js.map +1 -1
- package/dist/error/checks/SlashCommandGlobalCheckError.d.ts +5 -0
- package/dist/error/checks/SlashCommandGlobalCheckError.js +11 -0
- package/dist/error/checks/SlashCommandGlobalCheckError.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/messageComponents/base.d.ts +10 -0
- package/dist/messageComponents/base.js +15 -0
- package/dist/messageComponents/base.js.map +1 -0
- package/dist/messageComponents/button.d.ts +5 -0
- package/dist/messageComponents/button.js +28 -0
- package/dist/messageComponents/button.js.map +1 -0
- package/dist/messageComponents/index.d.ts +2 -0
- package/dist/messageComponents/index.js +15 -0
- package/dist/messageComponents/index.js.map +1 -0
- package/dist/messageComponents/selectMenu.d.ts +5 -0
- package/dist/messageComponents/selectMenu.js +28 -0
- package/dist/messageComponents/selectMenu.js.map +1 -0
- package/dist/structures/CommandClient.d.ts +7 -3
- package/dist/structures/CommandClient.js +6 -7
- package/dist/structures/CommandClient.js.map +1 -1
- package/dist/structures/Module.d.ts +4 -2
- package/dist/structures/Module.js +5 -2
- package/dist/structures/Module.js.map +1 -1
- package/dist/structures/Registry.d.ts +4 -2
- package/dist/structures/Registry.js +16 -13
- package/dist/structures/Registry.js.map +1 -1
- package/dist/typings.d.ts +14 -2
- package/package.json +1 -3
- package/src/applicationCommand/AppCommand.ts +28 -0
- package/src/{slashCommand → applicationCommand}/decorator.ts +10 -10
- package/src/applicationCommand/index.ts +2 -0
- package/src/builtinModules/CommandHandler.ts +123 -18
- package/src/command/Command.ts +2 -2
- package/src/command/decorator.ts +2 -1
- package/src/command/utils.ts +9 -9
- package/src/constants.ts +4 -2
- package/src/error/ArgumentConverterNotFound.ts +1 -1
- package/src/error/CommandCheckFailed.ts +4 -4
- package/src/error/checks/SlashCommandGlobalCheckError.ts +7 -0
- package/src/index.ts +2 -1
- package/src/messageComponents/base.ts +12 -0
- package/src/messageComponents/button.ts +26 -0
- package/src/messageComponents/index.ts +2 -0
- package/src/messageComponents/selectMenu.ts +26 -0
- package/src/structures/CommandClient.ts +14 -8
- package/src/structures/Module.ts +9 -4
- package/src/structures/Registry.ts +21 -15
- package/src/typings.ts +14 -2
- package/test/index.ts +2 -2
- package/test/modules/dev.ts +9 -5
- package/test/modules/test.ts +88 -15
- package/dist/slashCommand/SlashCommand.d.ts +0 -19
- package/dist/slashCommand/SlashCommand.js.map +0 -1
- package/dist/slashCommand/decorator.d.ts +0 -10
- package/dist/slashCommand/decorator.js.map +0 -1
- package/dist/slashCommand/index.d.ts +0 -2
- package/dist/slashCommand/index.js.map +0 -1
- package/src/slashCommand/SlashCommand.ts +0 -29
- package/src/slashCommand/index.ts +0 -2
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
import { BuiltInModule } from './BuiltInModule'
|
|
2
2
|
import { Registry } from '../structures'
|
|
3
3
|
import { listener } from '../listener'
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
CommandInteraction,
|
|
6
|
+
GuildMember,
|
|
7
|
+
Interaction,
|
|
8
|
+
Message,
|
|
9
|
+
MessageComponentInteraction,
|
|
10
|
+
MessageContextMenuInteraction,
|
|
11
|
+
Role,
|
|
12
|
+
User,
|
|
13
|
+
UserContextMenuInteraction,
|
|
14
|
+
} from 'discord.js'
|
|
5
15
|
import { CommandClient } from '../structures'
|
|
6
16
|
import { Command } from '../command'
|
|
7
|
-
import { ArgumentConverterNotFound, ArgumentNotProvided, CommandCheckFailed, SlashArgumentConverterNotFound,
|
|
17
|
+
import { ArgumentConverterNotFound, ArgumentNotProvided, CommandCheckFailed, SlashArgumentConverterNotFound, ApplicationCommandCheckFailed } from '../error'
|
|
8
18
|
import { CommandNotFound } from '../error/CommandNotFound'
|
|
19
|
+
import { SlashCommandGlobalCheckError } from '../error/checks/SlashCommandGlobalCheckError'
|
|
20
|
+
import { MessageComponentHandler } from '../messageComponents/base'
|
|
9
21
|
|
|
10
22
|
export class CommandHandler extends BuiltInModule {
|
|
11
23
|
private readonly client: CommandClient
|
|
@@ -15,6 +27,7 @@ export class CommandHandler extends BuiltInModule {
|
|
|
15
27
|
this.client = registry.client
|
|
16
28
|
}
|
|
17
29
|
|
|
30
|
+
// region message command handler
|
|
18
31
|
@listener('messageCreate')
|
|
19
32
|
async message(msg: Message) {
|
|
20
33
|
const error = (error: Error) => this.client.client.emit('commandError', error, msg)
|
|
@@ -129,31 +142,32 @@ export class CommandHandler extends BuiltInModule {
|
|
|
129
142
|
return error(e)
|
|
130
143
|
}
|
|
131
144
|
}
|
|
145
|
+
// endregion
|
|
132
146
|
|
|
147
|
+
// region slash command handler
|
|
133
148
|
private async command(i: CommandInteraction) {
|
|
134
|
-
const error = (error: Error) => this.client.client.emit('
|
|
149
|
+
const error = (error: Error) => this.client.client.emit('applicationCommandError', error, i)
|
|
135
150
|
try {
|
|
136
|
-
const cmd = this.registry.
|
|
151
|
+
const cmd = this.registry.applicationCommands.find((x) => x.command.type === 'CHAT_INPUT' && x.command.name === i.commandName)
|
|
137
152
|
|
|
138
|
-
|
|
153
|
+
if (!cmd) return
|
|
154
|
+
|
|
155
|
+
const module = this.registry.modules.find((x) => x.applicationCommands.includes(cmd))
|
|
139
156
|
|
|
140
157
|
if (!module) return
|
|
141
158
|
|
|
142
159
|
const argList: any[] = []
|
|
143
160
|
|
|
144
|
-
if (!cmd)
|
|
145
|
-
return i.reply({
|
|
146
|
-
content: 'Unknown command.',
|
|
147
|
-
ephemeral: true,
|
|
148
|
-
})
|
|
149
|
-
|
|
150
161
|
i.data = {
|
|
151
162
|
cts: this.client,
|
|
152
163
|
command: cmd,
|
|
153
164
|
}
|
|
154
165
|
|
|
166
|
+
if (!(await this.client.options.slashCommands.check(i))) {
|
|
167
|
+
return error(new SlashCommandGlobalCheckError(i))
|
|
168
|
+
}
|
|
155
169
|
for (const check of cmd.checks) {
|
|
156
|
-
if (!(await check(i))) return error(new
|
|
170
|
+
if (!(await check(i))) return error(new ApplicationCommandCheckFailed(i, cmd))
|
|
157
171
|
}
|
|
158
172
|
|
|
159
173
|
for (let j = 0; j < cmd.params.length; j++) {
|
|
@@ -200,11 +214,82 @@ export class CommandHandler extends BuiltInModule {
|
|
|
200
214
|
argList.push(await converter.execute(module, i))
|
|
201
215
|
}
|
|
202
216
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
217
|
+
await cmd.execute(module, argList)
|
|
218
|
+
} catch (e) {
|
|
219
|
+
return error(e)
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
// endregion
|
|
223
|
+
|
|
224
|
+
private async messageComponent(i: MessageComponentInteraction) {
|
|
225
|
+
const error = (e: any) => this.client.client.emit('messageComponentError', e)
|
|
226
|
+
|
|
227
|
+
try {
|
|
228
|
+
const handlers: MessageComponentHandler[] = []
|
|
229
|
+
|
|
230
|
+
for (const handler of this.registry.messageComponentHandlers) {
|
|
231
|
+
if (handler.componentId === handler.componentId && handler.componentType === i.componentType) {
|
|
232
|
+
handlers.push(handler)
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
for (const handler of handlers) {
|
|
237
|
+
const module = this.registry.modules.find((x) => x.messageComponentHandlers.includes(handler))
|
|
238
|
+
if (!module) continue
|
|
239
|
+
await handler.run(module, i)
|
|
207
240
|
}
|
|
241
|
+
} catch (e) {
|
|
242
|
+
error(e)
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
private async userContextMenu(i: UserContextMenuInteraction) {
|
|
247
|
+
const error = (error: Error) => this.client.client.emit('applicationCommandError', error, i)
|
|
248
|
+
try {
|
|
249
|
+
const cmd = this.registry.applicationCommands.find((x) => x.command.type === 'USER' && x.command.name === i.commandName)
|
|
250
|
+
|
|
251
|
+
if (!cmd) return
|
|
252
|
+
|
|
253
|
+
const module = this.registry.modules.find((x) => x.applicationCommands.includes(cmd))
|
|
254
|
+
|
|
255
|
+
if (!module) return
|
|
256
|
+
|
|
257
|
+
i.data = {
|
|
258
|
+
cts: this.client,
|
|
259
|
+
command: cmd,
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
for (const check of cmd.checks) {
|
|
263
|
+
if (!(await check(i))) return error(new ApplicationCommandCheckFailed(i, cmd))
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
await cmd.execute(module, [i])
|
|
267
|
+
} catch (e) {
|
|
268
|
+
return error(e)
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
private async messageContextMenu(i: MessageContextMenuInteraction) {
|
|
273
|
+
const error = (error: Error) => this.client.client.emit('applicationCommandError', error, i)
|
|
274
|
+
try {
|
|
275
|
+
const cmd = this.registry.applicationCommands.find((x) => x.command.type === 'MESSAGE' && x.command.name === i.commandName)
|
|
276
|
+
|
|
277
|
+
if (!cmd) return
|
|
278
|
+
|
|
279
|
+
const module = this.registry.modules.find((x) => x.applicationCommands.includes(cmd))
|
|
280
|
+
|
|
281
|
+
if (!module) return
|
|
282
|
+
|
|
283
|
+
i.data = {
|
|
284
|
+
cts: this.client,
|
|
285
|
+
command: cmd,
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
for (const check of cmd.checks) {
|
|
289
|
+
if (!(await check(i))) return error(new ApplicationCommandCheckFailed(i, cmd))
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
await cmd.execute(module, [i])
|
|
208
293
|
} catch (e) {
|
|
209
294
|
return error(e)
|
|
210
295
|
}
|
|
@@ -212,8 +297,28 @@ export class CommandHandler extends BuiltInModule {
|
|
|
212
297
|
|
|
213
298
|
@listener('interactionCreate')
|
|
214
299
|
async interaction(i: Interaction) {
|
|
215
|
-
|
|
216
|
-
|
|
300
|
+
const error = (e: any) => this.client.client.emit('interactionError', e, i)
|
|
301
|
+
|
|
302
|
+
try {
|
|
303
|
+
await this.client.options.applicationCommands.beforeRunCheck(i)
|
|
304
|
+
if (i.isCommand()) {
|
|
305
|
+
await this.command(i)
|
|
306
|
+
return
|
|
307
|
+
}
|
|
308
|
+
if (i.isMessageComponent()) {
|
|
309
|
+
await this.messageComponent(i)
|
|
310
|
+
return
|
|
311
|
+
}
|
|
312
|
+
if (i.isMessageContextMenu()) {
|
|
313
|
+
await this.messageContextMenu(i)
|
|
314
|
+
return
|
|
315
|
+
}
|
|
316
|
+
if (i.isUserContextMenu()) {
|
|
317
|
+
await this.userContextMenu(i)
|
|
318
|
+
return
|
|
319
|
+
}
|
|
320
|
+
} catch (e) {
|
|
321
|
+
return error(e)
|
|
217
322
|
}
|
|
218
323
|
}
|
|
219
324
|
}
|
package/src/command/Command.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Module } from '../structures'
|
|
2
|
-
import { CommandInteraction, Message } from 'discord.js'
|
|
2
|
+
import { CommandInteraction, ContextMenuInteraction, Message, MessageComponentInteraction } from 'discord.js'
|
|
3
3
|
import { KCommandChecks } from '../constants'
|
|
4
4
|
|
|
5
5
|
export type Argument = {
|
|
@@ -9,7 +9,7 @@ export type Argument = {
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
export type CheckFunction = (msg: Message) => boolean | Promise<boolean>
|
|
12
|
-
export type
|
|
12
|
+
export type ApplicationCommandCheckFunction = (i: CommandInteraction | MessageComponentInteraction | ContextMenuInteraction) => boolean | Promise<boolean>
|
|
13
13
|
|
|
14
14
|
export class Command {
|
|
15
15
|
execute(module: Module, args: any[]) {
|
package/src/command/decorator.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { ClientPermissionRequired, DMOnlyCommandError, GuildOnlyCommandError, Ow
|
|
|
10
10
|
type CommandOptions = {
|
|
11
11
|
name: string
|
|
12
12
|
aliases: string[] | ((msg: Message) => string[])
|
|
13
|
+
optionTypes?: any[]
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
export const command = (options: Partial<CommandOptions> = {}) => {
|
|
@@ -22,7 +23,7 @@ export const command = (options: Partial<CommandOptions> = {}) => {
|
|
|
22
23
|
|
|
23
24
|
let properties: Command[] = Reflect.getMetadata(KCommands, target)
|
|
24
25
|
|
|
25
|
-
const params: any[] = Reflect.getMetadata('design:paramtypes', target, propertyKey)
|
|
26
|
+
const params: any[] = options.optionTypes ?? Reflect.getMetadata('design:paramtypes', target, propertyKey)
|
|
26
27
|
|
|
27
28
|
const optionals: number = Reflect.getMetadata(KOptionals, target, propertyKey) || -1
|
|
28
29
|
|
package/src/command/utils.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { CommandInteraction, Message } from 'discord.js'
|
|
2
|
-
import type { CheckFunction,
|
|
3
|
-
import { KCommandChecks,
|
|
1
|
+
import { CommandInteraction, ContextMenuInteraction, Message, MessageComponentInteraction } from 'discord.js'
|
|
2
|
+
import type { CheckFunction, ApplicationCommandCheckFunction } from './Command'
|
|
3
|
+
import { KCommandChecks, KApplicationCommandChecks } from '../constants'
|
|
4
4
|
|
|
5
5
|
export const createCheckDecorator = (
|
|
6
6
|
execute: ((msg: Message) => boolean | Promise<boolean>) | null,
|
|
7
|
-
|
|
7
|
+
executeApplicationCommand?: (i: CommandInteraction | MessageComponentInteraction | ContextMenuInteraction) => boolean | Promise<boolean>,
|
|
8
8
|
): MethodDecorator => {
|
|
9
9
|
return (target, propertyKey) => {
|
|
10
10
|
if (execute) {
|
|
@@ -16,13 +16,13 @@ export const createCheckDecorator = (
|
|
|
16
16
|
Reflect.defineMetadata(KCommandChecks, properties, target, propertyKey)
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
-
if (
|
|
20
|
-
let properties:
|
|
19
|
+
if (executeApplicationCommand) {
|
|
20
|
+
let properties: ApplicationCommandCheckFunction[] = Reflect.getMetadata(KApplicationCommandChecks, target, propertyKey)
|
|
21
21
|
if (properties) {
|
|
22
|
-
properties.push(
|
|
22
|
+
properties.push(executeApplicationCommand)
|
|
23
23
|
} else {
|
|
24
|
-
properties = [
|
|
25
|
-
Reflect.defineMetadata(
|
|
24
|
+
properties = [executeApplicationCommand]
|
|
25
|
+
Reflect.defineMetadata(KApplicationCommandChecks, properties, target, propertyKey)
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
}
|
package/src/constants.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export const KCommands = Symbol('Command.TS Commands')
|
|
2
2
|
|
|
3
|
-
export const
|
|
3
|
+
export const KApplicationCommands = Symbol('Command.TS Slash Commands')
|
|
4
4
|
|
|
5
5
|
export const KSlashCommandOptions = Symbol('Command.TS Slash Command Options')
|
|
6
6
|
|
|
@@ -22,4 +22,6 @@ export const KSlashArgumentConverters = Symbol('Command.TS Slash Argument Conver
|
|
|
22
22
|
|
|
23
23
|
export const KCommandChecks = Symbol('Command.TS Command Checks')
|
|
24
24
|
|
|
25
|
-
export const
|
|
25
|
+
export const KApplicationCommandChecks = Symbol('Command.TS Slash Command Checks')
|
|
26
|
+
|
|
27
|
+
export const KMessageComponentHandlers = Symbol('Command.TS Message Component Handlers')
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CommandInteraction, Message } from 'discord.js'
|
|
2
2
|
import type { Argument } from '../command'
|
|
3
|
-
import type { SlashArgument } from '../
|
|
3
|
+
import type { SlashArgument } from '../applicationCommand'
|
|
4
4
|
|
|
5
5
|
export class ArgumentConverterNotFound extends Error {
|
|
6
6
|
constructor(public type: Argument, public msg: Message) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { CommandInteraction, Message } from 'discord.js'
|
|
1
|
+
import { CommandInteraction, ContextMenuInteraction, Message, MessageComponentInteraction } from 'discord.js'
|
|
2
2
|
import { Command } from '../command'
|
|
3
|
-
import {
|
|
3
|
+
import { AppCommand } from '../applicationCommand'
|
|
4
4
|
|
|
5
5
|
export class CommandCheckFailed extends Error {
|
|
6
6
|
constructor(public msg: Message, public command: Command) {
|
|
@@ -8,8 +8,8 @@ export class CommandCheckFailed extends Error {
|
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export class
|
|
12
|
-
constructor(public interaction: CommandInteraction, public command:
|
|
11
|
+
export class ApplicationCommandCheckFailed extends Error {
|
|
12
|
+
constructor(public interaction: CommandInteraction | MessageComponentInteraction | ContextMenuInteraction, public command: AppCommand) {
|
|
13
13
|
super()
|
|
14
14
|
}
|
|
15
15
|
}
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { MessageComponentInteraction, MessageComponentType } from 'discord.js'
|
|
2
|
+
import { Module } from '../structures'
|
|
3
|
+
|
|
4
|
+
export type MessageComponentExecutor = (i: MessageComponentInteraction) => void | Promise<void>
|
|
5
|
+
|
|
6
|
+
export class MessageComponentHandler {
|
|
7
|
+
constructor(public componentId: string, public componentType: Exclude<MessageComponentType, 'ACTION_ROW'>, public execute: MessageComponentExecutor) {}
|
|
8
|
+
|
|
9
|
+
run(module: Module, i: MessageComponentInteraction) {
|
|
10
|
+
return this.execute.apply(module, [i])
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { MessageComponentExecutor, MessageComponentHandler } from './base'
|
|
2
|
+
import { checkTarget } from '../utils'
|
|
3
|
+
import { KMessageComponentHandlers } from '../constants'
|
|
4
|
+
|
|
5
|
+
export class ButtonInteractionHandler extends MessageComponentHandler {
|
|
6
|
+
constructor(id: string, execute: MessageComponentExecutor) {
|
|
7
|
+
super(id, 'BUTTON', execute)
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const messageButton = (id: string): MethodDecorator => {
|
|
12
|
+
return (target, propertyKey) => {
|
|
13
|
+
checkTarget(target)
|
|
14
|
+
|
|
15
|
+
const handler = new ButtonInteractionHandler(id, Reflect.get(target, propertyKey))
|
|
16
|
+
|
|
17
|
+
let properties: MessageComponentHandler[] = Reflect.getMetadata(KMessageComponentHandlers, target)
|
|
18
|
+
|
|
19
|
+
if (properties) {
|
|
20
|
+
properties.push(handler)
|
|
21
|
+
} else {
|
|
22
|
+
properties = [handler]
|
|
23
|
+
Reflect.defineMetadata(KMessageComponentHandlers, properties, target)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { MessageComponentExecutor, MessageComponentHandler } from './base'
|
|
2
|
+
import { checkTarget } from '../utils'
|
|
3
|
+
import { KMessageComponentHandlers } from '../constants'
|
|
4
|
+
|
|
5
|
+
export class SelectMenuInteractionHandler extends MessageComponentHandler {
|
|
6
|
+
constructor(id: string, execute: MessageComponentExecutor) {
|
|
7
|
+
super(id, 'SELECT_MENU', execute)
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const messageSelectMenu = (id: string): MethodDecorator => {
|
|
12
|
+
return (target, propertyKey) => {
|
|
13
|
+
checkTarget(target)
|
|
14
|
+
|
|
15
|
+
const handler = new SelectMenuInteractionHandler(id, Reflect.get(target, propertyKey))
|
|
16
|
+
|
|
17
|
+
let properties: MessageComponentHandler[] = Reflect.getMetadata(KMessageComponentHandlers, target)
|
|
18
|
+
|
|
19
|
+
if (properties) {
|
|
20
|
+
properties.push(handler)
|
|
21
|
+
} else {
|
|
22
|
+
properties = [handler]
|
|
23
|
+
Reflect.defineMetadata(KMessageComponentHandlers, properties, target)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import _ from 'lodash'
|
|
2
2
|
import { Registry } from './Registry'
|
|
3
|
-
import { Client, Message, Snowflake, User } from 'discord.js'
|
|
3
|
+
import { Client, CommandInteraction, Interaction, Message, Snowflake, User } from 'discord.js'
|
|
4
4
|
import { BuiltinCommandConverters, BuiltinSlashCommandConverters, CommandHandler } from '../builtinModules'
|
|
5
5
|
import { CoolDownAdapter, DefaultCoolDownAdapter } from '../command'
|
|
6
|
-
import { REST } from '@discordjs/rest'
|
|
7
6
|
import { Logger } from 'tslog'
|
|
8
7
|
|
|
9
8
|
export interface CommandOptions {
|
|
@@ -13,20 +12,27 @@ export interface CommandOptions {
|
|
|
13
12
|
}
|
|
14
13
|
|
|
15
14
|
export interface SlashCommandOptions {
|
|
15
|
+
check: (i: CommandInteraction) => boolean | Promise<boolean>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ApplicationCommandOptions {
|
|
16
19
|
guild?: Snowflake | Snowflake[]
|
|
17
20
|
autoSync: boolean
|
|
21
|
+
beforeRunCheck: (i: Interaction) => void | Promise<void>
|
|
18
22
|
}
|
|
19
23
|
|
|
20
24
|
export interface CommandClientOptions {
|
|
21
25
|
command: CommandOptions
|
|
22
26
|
owners: 'auto' | Snowflake[]
|
|
23
27
|
slashCommands: SlashCommandOptions
|
|
28
|
+
applicationCommands: ApplicationCommandOptions
|
|
24
29
|
}
|
|
25
30
|
|
|
26
31
|
export interface CommandClientOptionsParam {
|
|
27
32
|
command: Partial<CommandOptions>
|
|
28
33
|
owners: 'auto' | string[]
|
|
29
34
|
slashCommands: Partial<SlashCommandOptions>
|
|
35
|
+
applicationCommands: Partial<ApplicationCommandOptions>
|
|
30
36
|
}
|
|
31
37
|
|
|
32
38
|
export class CommandClient {
|
|
@@ -35,9 +41,6 @@ export class CommandClient {
|
|
|
35
41
|
registry = new Registry(this)
|
|
36
42
|
client: Client
|
|
37
43
|
coolDownAdapter: CoolDownAdapter
|
|
38
|
-
rest = new REST({
|
|
39
|
-
version: '9',
|
|
40
|
-
})
|
|
41
44
|
logger: Logger
|
|
42
45
|
|
|
43
46
|
private _isReady = false
|
|
@@ -52,13 +55,12 @@ export class CommandClient {
|
|
|
52
55
|
|
|
53
56
|
async ready() {
|
|
54
57
|
if (this._isReady) return
|
|
55
|
-
this.rest.setToken(this.client.token!)
|
|
56
58
|
this._isReady = true
|
|
57
59
|
if (this.options.owners === 'auto') {
|
|
58
60
|
const owners = await this.fetchOwners()
|
|
59
61
|
this.owners.push(...owners)
|
|
60
62
|
}
|
|
61
|
-
if (this.options.
|
|
63
|
+
if (this.options.applicationCommands.autoSync) {
|
|
62
64
|
await this.registry.syncCommands()
|
|
63
65
|
}
|
|
64
66
|
}
|
|
@@ -75,7 +77,11 @@ export class CommandClient {
|
|
|
75
77
|
},
|
|
76
78
|
owners: 'auto',
|
|
77
79
|
slashCommands: {
|
|
78
|
-
|
|
80
|
+
check: () => true,
|
|
81
|
+
},
|
|
82
|
+
applicationCommands: {
|
|
83
|
+
autoSync: false,
|
|
84
|
+
beforeRunCheck: () => {},
|
|
79
85
|
},
|
|
80
86
|
},
|
|
81
87
|
options,
|
package/src/structures/Module.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { KArgumentConverters, KCommands, KListeners, KModulePath, KSlashArgumentConverters,
|
|
1
|
+
import { KArgumentConverters, KCommands, KListeners, KMessageComponentHandlers, KModulePath, KSlashArgumentConverters, KApplicationCommands } from '../constants'
|
|
2
2
|
import type { Command } from '../command'
|
|
3
3
|
import { Listener } from '../listener'
|
|
4
4
|
import { ArgumentConverter, SlashArgumentConverter } from '../command'
|
|
5
|
-
import {
|
|
5
|
+
import { AppCommand } from '../applicationCommand'
|
|
6
6
|
import { CommandClient } from './CommandClient'
|
|
7
|
+
import { MessageComponentHandler } from '../messageComponents/base'
|
|
7
8
|
|
|
8
9
|
export abstract class Module {
|
|
9
10
|
commandClient!: CommandClient
|
|
@@ -30,8 +31,12 @@ export abstract class Module {
|
|
|
30
31
|
return Reflect.getMetadata(KSlashArgumentConverters, this) || []
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
get
|
|
34
|
-
return Reflect.getMetadata(
|
|
34
|
+
get applicationCommands(): AppCommand[] {
|
|
35
|
+
return Reflect.getMetadata(KApplicationCommands, this) || []
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get messageComponentHandlers(): MessageComponentHandler[] {
|
|
39
|
+
return Reflect.getMetadata(KMessageComponentHandlers, this) || []
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
get path(): string | undefined {
|
|
@@ -7,9 +7,9 @@ import { InvalidModuleError, InvalidTargetError, ModuleLoadError } from '../erro
|
|
|
7
7
|
import { Collection, Guild } from 'discord.js'
|
|
8
8
|
import walkSync from 'walk-sync'
|
|
9
9
|
import { ArgumentConverter } from '../command'
|
|
10
|
-
import {
|
|
11
|
-
import { Routes } from 'discord-api-types/v9'
|
|
10
|
+
import { AppCommand } from '../applicationCommand'
|
|
12
11
|
import * as fs from 'fs'
|
|
12
|
+
import { MessageComponentHandler } from '../messageComponents/base'
|
|
13
13
|
|
|
14
14
|
type ListenerExecutor = {
|
|
15
15
|
event: string
|
|
@@ -57,11 +57,21 @@ export class Registry {
|
|
|
57
57
|
return result
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
get
|
|
61
|
-
const result:
|
|
60
|
+
get applicationCommands(): AppCommand[] {
|
|
61
|
+
const result: AppCommand[] = []
|
|
62
62
|
|
|
63
63
|
for (const [, module] of this.modules) {
|
|
64
|
-
result.push(...module.
|
|
64
|
+
result.push(...module.applicationCommands)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return result
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
get messageComponentHandlers(): MessageComponentHandler[] {
|
|
71
|
+
const result: MessageComponentHandler[] = []
|
|
72
|
+
|
|
73
|
+
for (const [, module] of this.modules) {
|
|
74
|
+
result.push(...module.messageComponentHandlers)
|
|
65
75
|
}
|
|
66
76
|
|
|
67
77
|
return result
|
|
@@ -128,19 +138,17 @@ export class Registry {
|
|
|
128
138
|
|
|
129
139
|
async syncCommands() {
|
|
130
140
|
this.logger.debug(`Syncing commands...`)
|
|
131
|
-
const commands = this.
|
|
132
|
-
const guild = this.client.options.
|
|
141
|
+
const commands = this.applicationCommands.filter((x) => !x.guild)
|
|
142
|
+
const guild = this.client.options.applicationCommands.guild
|
|
133
143
|
if (guild) {
|
|
134
144
|
const syncForGuild = async (g: Guild) => {
|
|
135
145
|
this.logger.debug(`Syncing for guild ${g.name}(${g.id})`)
|
|
136
146
|
const commandsToRegister = [
|
|
137
|
-
...commands.map((x) => x.
|
|
138
|
-
...this.
|
|
147
|
+
...commands.map((x) => x.command),
|
|
148
|
+
...this.applicationCommands.filter((y) => y.guild === g.id || y.guild?.includes(g.id) || false).map((x) => x.command),
|
|
139
149
|
]
|
|
140
150
|
this.logger.debug(`Command List: ${commandsToRegister.map((x) => x.name).join(', ')}`)
|
|
141
|
-
await
|
|
142
|
-
body: commandsToRegister.map((x) => x.toJSON()),
|
|
143
|
-
})
|
|
151
|
+
await g.commands.set(commandsToRegister)
|
|
144
152
|
}
|
|
145
153
|
|
|
146
154
|
if (typeof guild === 'string') {
|
|
@@ -152,9 +160,7 @@ export class Registry {
|
|
|
152
160
|
}
|
|
153
161
|
} else {
|
|
154
162
|
this.logger.debug('Syncing global...')
|
|
155
|
-
await this.client.
|
|
156
|
-
body: commands.map((x) => x.commandBuilder.toJSON()),
|
|
157
|
-
})
|
|
163
|
+
await this.client.client.application?.commands.set(commands.map((x) => x.command))
|
|
158
164
|
}
|
|
159
165
|
this.logger.debug('Syncing ended.')
|
|
160
166
|
}
|
package/src/typings.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Command } from './command'
|
|
2
2
|
import { CommandClient } from './structures'
|
|
3
|
-
import {
|
|
3
|
+
import { AppCommand } from './applicationCommand'
|
|
4
4
|
|
|
5
5
|
declare module 'discord.js' {
|
|
6
6
|
interface Message {
|
|
@@ -12,7 +12,19 @@ declare module 'discord.js' {
|
|
|
12
12
|
}
|
|
13
13
|
interface CommandInteraction {
|
|
14
14
|
data: {
|
|
15
|
-
command:
|
|
15
|
+
command: AppCommand
|
|
16
|
+
cts: CommandClient
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
interface MessageComponentInteraction {
|
|
20
|
+
data: {
|
|
21
|
+
command: AppCommand
|
|
22
|
+
cts: CommandClient
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
interface ContextMenuInteraction {
|
|
26
|
+
data: {
|
|
27
|
+
command: AppCommand
|
|
16
28
|
cts: CommandClient
|
|
17
29
|
}
|
|
18
30
|
}
|
package/test/index.ts
CHANGED
|
@@ -8,12 +8,12 @@ const client = new Client({
|
|
|
8
8
|
})
|
|
9
9
|
|
|
10
10
|
const cts = new CommandClient({
|
|
11
|
-
owners:
|
|
11
|
+
owners: 'auto',
|
|
12
12
|
client,
|
|
13
13
|
command: {
|
|
14
14
|
prefix: '!',
|
|
15
15
|
},
|
|
16
|
-
|
|
16
|
+
applicationCommands: {
|
|
17
17
|
autoSync: true,
|
|
18
18
|
guild: '832938554438844438',
|
|
19
19
|
},
|
package/test/modules/dev.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { BuiltInModule, CommandClient, ownerOnly,
|
|
1
|
+
import { BuiltInModule, CommandClient, ownerOnly, applicationCommand, listener, ApplicationCommandCheckFailed } from '../../src'
|
|
2
2
|
import { CommandInteraction } from 'discord.js'
|
|
3
|
-
import { SlashCommandBuilder } from '@discordjs/builders'
|
|
4
3
|
|
|
5
4
|
export class Dev extends BuiltInModule {
|
|
6
5
|
constructor(private cts: CommandClient) {
|
|
@@ -9,7 +8,7 @@ export class Dev extends BuiltInModule {
|
|
|
9
8
|
|
|
10
9
|
@listener('slashCommandError')
|
|
11
10
|
slashError(e: Error, i: CommandInteraction) {
|
|
12
|
-
if (e instanceof
|
|
11
|
+
if (e instanceof ApplicationCommandCheckFailed) {
|
|
13
12
|
return i.reply({
|
|
14
13
|
content: 'Command before-run check failed',
|
|
15
14
|
ephemeral: true,
|
|
@@ -18,8 +17,13 @@ export class Dev extends BuiltInModule {
|
|
|
18
17
|
console.error(e.message)
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
// new SlashCommandBuilder().setName('reload').setDescription('리로드 커맨드')
|
|
21
|
+
@applicationCommand({
|
|
22
|
+
command: {
|
|
23
|
+
name: 'reload',
|
|
24
|
+
type: 'CHAT_INPUT',
|
|
25
|
+
description: '리로드 커맨드',
|
|
26
|
+
},
|
|
23
27
|
})
|
|
24
28
|
@ownerOnly
|
|
25
29
|
async reload(i: CommandInteraction) {
|