@pikokr/command.ts 3.2.4 → 4.0.1

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.
Files changed (182) hide show
  1. package/dist/index.d.ts +310 -9
  2. package/dist/index.js +1 -21
  3. package/dist/index.js.map +1 -1
  4. package/package.json +6 -5
  5. package/publish-version.js +7 -3
  6. package/src/applicationCommand/AppCommand.ts +32 -0
  7. package/src/{slashCommand → applicationCommand}/decorator.ts +14 -10
  8. package/src/applicationCommand/index.ts +6 -0
  9. package/src/builtinModules/BuiltInModule.ts +4 -0
  10. package/src/builtinModules/BuiltinApplicationCommandConverters.ts +16 -0
  11. package/src/builtinModules/BuiltinCommandConverters.ts +4 -0
  12. package/src/builtinModules/CommandHandler.ts +178 -22
  13. package/src/builtinModules/index.ts +5 -1
  14. package/src/command/ArgumentConverter.ts +7 -3
  15. package/src/command/Command.ts +6 -2
  16. package/src/command/cooldown/adapter.ts +4 -0
  17. package/src/command/cooldown/decorator.ts +4 -0
  18. package/src/command/cooldown/error.ts +4 -0
  19. package/src/command/cooldown/index.ts +4 -0
  20. package/src/command/cooldown/type.ts +4 -0
  21. package/src/command/decorator.ts +10 -5
  22. package/src/command/index.ts +4 -0
  23. package/src/command/utils.ts +13 -9
  24. package/src/constants.ts +8 -2
  25. package/src/error/ArgumentConverterNotFound.ts +8 -4
  26. package/src/error/ArgumentNotProvided.ts +4 -0
  27. package/src/error/CommandCheckFailed.ts +8 -4
  28. package/src/error/CommandNotFound.ts +4 -0
  29. package/src/error/InvalidTargetError.ts +4 -0
  30. package/src/error/ModuleError.ts +4 -0
  31. package/src/error/PermissionRequired.ts +4 -0
  32. package/src/error/checks/DMOnlyCommand.ts +4 -0
  33. package/src/error/checks/GuildOnlyCommand.ts +4 -0
  34. package/src/error/checks/OwnerOnlyCommand.ts +4 -0
  35. package/src/error/checks/SlashCommandGlobalCheckError.ts +11 -0
  36. package/src/error/checks/index.ts +4 -0
  37. package/src/error/index.ts +4 -0
  38. package/src/index.ts +6 -1
  39. package/src/interface/index.ts +4 -0
  40. package/src/listener/Listener.ts +4 -0
  41. package/src/listener/decorator.ts +4 -0
  42. package/src/listener/index.ts +4 -0
  43. package/src/messageComponents/base.ts +16 -0
  44. package/src/messageComponents/button.ts +30 -0
  45. package/src/messageComponents/index.ts +6 -0
  46. package/src/messageComponents/selectMenu.ts +30 -0
  47. package/src/structures/CommandClient.ts +22 -10
  48. package/src/structures/Module.ts +15 -6
  49. package/src/structures/Registry.ts +59 -28
  50. package/src/structures/index.ts +4 -0
  51. package/src/typings.ts +18 -2
  52. package/src/utils.ts +4 -0
  53. package/test/index.ts +6 -2
  54. package/test/modules/dev.ts +13 -5
  55. package/test/modules/test.ts +92 -15
  56. package/tsup.config.ts +14 -0
  57. package/dist/builtinModules/BuiltInModule.d.ts +0 -4
  58. package/dist/builtinModules/BuiltInModule.js +0 -13
  59. package/dist/builtinModules/BuiltInModule.js.map +0 -1
  60. package/dist/builtinModules/BuiltinCommandConverters.d.ts +0 -16
  61. package/dist/builtinModules/BuiltinCommandConverters.js +0 -127
  62. package/dist/builtinModules/BuiltinCommandConverters.js.map +0 -1
  63. package/dist/builtinModules/BuiltinSlashCommandConverters.d.ts +0 -10
  64. package/dist/builtinModules/BuiltinSlashCommandConverters.js +0 -42
  65. package/dist/builtinModules/BuiltinSlashCommandConverters.js.map +0 -1
  66. package/dist/builtinModules/CommandHandler.d.ts +0 -11
  67. package/dist/builtinModules/CommandHandler.js +0 -230
  68. package/dist/builtinModules/CommandHandler.js.map +0 -1
  69. package/dist/builtinModules/index.d.ts +0 -3
  70. package/dist/builtinModules/index.js +0 -16
  71. package/dist/builtinModules/index.js.map +0 -1
  72. package/dist/command/ArgumentConverter.d.ts +0 -15
  73. package/dist/command/ArgumentConverter.js +0 -25
  74. package/dist/command/ArgumentConverter.js.map +0 -1
  75. package/dist/command/Command.d.ts +0 -20
  76. package/dist/command/Command.js +0 -22
  77. package/dist/command/Command.js.map +0 -1
  78. package/dist/command/cooldown/adapter.d.ts +0 -10
  79. package/dist/command/cooldown/adapter.js +0 -31
  80. package/dist/command/cooldown/adapter.js.map +0 -1
  81. package/dist/command/cooldown/decorator.d.ts +0 -2
  82. package/dist/command/cooldown/decorator.js +0 -73
  83. package/dist/command/cooldown/decorator.js.map +0 -1
  84. package/dist/command/cooldown/error.d.ts +0 -4
  85. package/dist/command/cooldown/error.js +0 -11
  86. package/dist/command/cooldown/error.js.map +0 -1
  87. package/dist/command/cooldown/index.d.ts +0 -5
  88. package/dist/command/cooldown/index.js +0 -18
  89. package/dist/command/cooldown/index.js.map +0 -1
  90. package/dist/command/cooldown/type.d.ts +0 -8
  91. package/dist/command/cooldown/type.js +0 -13
  92. package/dist/command/cooldown/type.js.map +0 -1
  93. package/dist/command/decorator.d.ts +0 -16
  94. package/dist/command/decorator.js +0 -137
  95. package/dist/command/decorator.js.map +0 -1
  96. package/dist/command/index.d.ts +0 -5
  97. package/dist/command/index.js +0 -18
  98. package/dist/command/index.js.map +0 -1
  99. package/dist/command/utils.d.ts +0 -2
  100. package/dist/command/utils.js +0 -30
  101. package/dist/command/utils.js.map +0 -1
  102. package/dist/constants.d.ts +0 -13
  103. package/dist/constants.js +0 -17
  104. package/dist/constants.js.map +0 -1
  105. package/dist/error/ArgumentConverterNotFound.d.ts +0 -13
  106. package/dist/error/ArgumentConverterNotFound.js +0 -20
  107. package/dist/error/ArgumentConverterNotFound.js.map +0 -1
  108. package/dist/error/ArgumentNotProvided.d.ts +0 -8
  109. package/dist/error/ArgumentNotProvided.js +0 -13
  110. package/dist/error/ArgumentNotProvided.js.map +0 -1
  111. package/dist/error/CommandCheckFailed.d.ts +0 -13
  112. package/dist/error/CommandCheckFailed.js +0 -20
  113. package/dist/error/CommandCheckFailed.js.map +0 -1
  114. package/dist/error/CommandNotFound.d.ts +0 -7
  115. package/dist/error/CommandNotFound.js +0 -13
  116. package/dist/error/CommandNotFound.js.map +0 -1
  117. package/dist/error/InvalidTargetError.d.ts +0 -3
  118. package/dist/error/InvalidTargetError.js +0 -10
  119. package/dist/error/InvalidTargetError.js.map +0 -1
  120. package/dist/error/ModuleError.d.ts +0 -6
  121. package/dist/error/ModuleError.js +0 -14
  122. package/dist/error/ModuleError.js.map +0 -1
  123. package/dist/error/PermissionRequired.d.ts +0 -10
  124. package/dist/error/PermissionRequired.js +0 -19
  125. package/dist/error/PermissionRequired.js.map +0 -1
  126. package/dist/error/checks/DMOnlyCommand.d.ts +0 -3
  127. package/dist/error/checks/DMOnlyCommand.js +0 -10
  128. package/dist/error/checks/DMOnlyCommand.js.map +0 -1
  129. package/dist/error/checks/GuildOnlyCommand.d.ts +0 -3
  130. package/dist/error/checks/GuildOnlyCommand.js +0 -10
  131. package/dist/error/checks/GuildOnlyCommand.js.map +0 -1
  132. package/dist/error/checks/OwnerOnlyCommand.d.ts +0 -3
  133. package/dist/error/checks/OwnerOnlyCommand.js +0 -10
  134. package/dist/error/checks/OwnerOnlyCommand.js.map +0 -1
  135. package/dist/error/checks/index.d.ts +0 -3
  136. package/dist/error/checks/index.js +0 -16
  137. package/dist/error/checks/index.js.map +0 -1
  138. package/dist/error/index.d.ts +0 -7
  139. package/dist/error/index.js +0 -20
  140. package/dist/error/index.js.map +0 -1
  141. package/dist/interface/index.d.ts +0 -1
  142. package/dist/interface/index.js +0 -4
  143. package/dist/interface/index.js.map +0 -1
  144. package/dist/listener/Listener.d.ts +0 -5
  145. package/dist/listener/Listener.js +0 -11
  146. package/dist/listener/Listener.js.map +0 -1
  147. package/dist/listener/decorator.d.ts +0 -2
  148. package/dist/listener/decorator.js +0 -22
  149. package/dist/listener/decorator.js.map +0 -1
  150. package/dist/listener/index.d.ts +0 -2
  151. package/dist/listener/index.js +0 -15
  152. package/dist/listener/index.js.map +0 -1
  153. package/dist/slashCommand/SlashCommand.d.ts +0 -19
  154. package/dist/slashCommand/SlashCommand.js +0 -22
  155. package/dist/slashCommand/SlashCommand.js.map +0 -1
  156. package/dist/slashCommand/decorator.d.ts +0 -10
  157. package/dist/slashCommand/decorator.js +0 -38
  158. package/dist/slashCommand/decorator.js.map +0 -1
  159. package/dist/slashCommand/index.d.ts +0 -2
  160. package/dist/slashCommand/index.js +0 -15
  161. package/dist/slashCommand/index.js.map +0 -1
  162. package/dist/structures/CommandClient.d.ts +0 -40
  163. package/dist/structures/CommandClient.js +0 -94
  164. package/dist/structures/CommandClient.js.map +0 -1
  165. package/dist/structures/Module.d.ts +0 -19
  166. package/dist/structures/Module.js +0 -35
  167. package/dist/structures/Module.js.map +0 -1
  168. package/dist/structures/Registry.d.ts +0 -28
  169. package/dist/structures/Registry.js +0 -223
  170. package/dist/structures/Registry.js.map +0 -1
  171. package/dist/structures/index.d.ts +0 -3
  172. package/dist/structures/index.js +0 -16
  173. package/dist/structures/index.js.map +0 -1
  174. package/dist/typings.d.ts +0 -18
  175. package/dist/typings.js +0 -3
  176. package/dist/typings.js.map +0 -1
  177. package/dist/utils.d.ts +0 -1
  178. package/dist/utils.js +0 -11
  179. package/dist/utils.js.map +0 -1
  180. package/src/builtinModules/BuiltinSlashCommandConverters.ts +0 -23
  181. package/src/slashCommand/SlashCommand.ts +0 -29
  182. package/src/slashCommand/index.ts +0 -2
@@ -1,6 +1,10 @@
1
- import { CommandInteraction, Message } from 'discord.js'
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
5
+ import { CommandInteraction, ContextMenuInteraction, Message, MessageComponentInteraction } from 'discord.js'
2
6
  import { Command } from '../command'
3
- import { SlashCommand } from '../slashCommand'
7
+ import { AppCommand } from '../applicationCommand'
4
8
 
5
9
  export class CommandCheckFailed extends Error {
6
10
  constructor(public msg: Message, public command: Command) {
@@ -8,8 +12,8 @@ export class CommandCheckFailed extends Error {
8
12
  }
9
13
  }
10
14
 
11
- export class SlashCommandCheckFailed extends Error {
12
- constructor(public interaction: CommandInteraction, public command: SlashCommand) {
15
+ export class ApplicationCommandCheckFailed extends Error {
16
+ constructor(public interaction: CommandInteraction | MessageComponentInteraction | ContextMenuInteraction, public command: AppCommand) {
13
17
  super()
14
18
  }
15
19
  }
@@ -1,3 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  import { Message } from 'discord.js'
2
6
 
3
7
  export class CommandNotFound extends Error {
@@ -1,3 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  export class InvalidTargetError extends Error {
2
6
  constructor() {
3
7
  super('Class does not extend "Module" class.')
@@ -1,3 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  export class ModuleLoadError extends Error {
2
6
  constructor(file: string, public error: Error) {
3
7
  super('Failed to load module ' + file)
@@ -1,3 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  import { GuildMember, Permissions } from 'discord.js'
2
6
 
3
7
  export class UserPermissionRequired extends Error {
@@ -1,3 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  export class DMOnlyCommandError extends Error {
2
6
  constructor() {
3
7
  super()
@@ -1,3 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  export class GuildOnlyCommandError extends Error {
2
6
  constructor() {
3
7
  super()
@@ -1,3 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  export class OwnerOnlyCommandError extends Error {
2
6
  constructor() {
3
7
  super()
@@ -0,0 +1,11 @@
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
+ }
@@ -1,3 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  export * from './OwnerOnlyCommand'
2
6
  export * from './GuildOnlyCommand'
3
7
  export * from './DMOnlyCommand'
@@ -1,3 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  export * from './InvalidTargetError'
2
6
  export * from './ModuleError'
3
7
  export * from './ArgumentNotProvided'
package/src/index.ts CHANGED
@@ -1,3 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  import 'reflect-metadata'
2
6
  import './typings'
3
7
 
@@ -7,4 +11,5 @@ export * from './error'
7
11
  export * from './command'
8
12
  export * from './listener'
9
13
  export * from './builtinModules/BuiltInModule'
10
- export * from './slashCommand'
14
+ export * from './applicationCommand'
15
+ export * from './messageComponents'
@@ -1,3 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  // export type { HandlerAdapter } from './HandlerAdapter'
2
6
 
3
7
  export {}
@@ -1,3 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  export class Listener {
2
6
  constructor(public name: string, public execute: Function) {}
3
7
  }
@@ -1,3 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  import { checkTarget } from '../utils'
2
6
  import { KListeners } from '../constants'
3
7
  import { Listener } from './Listener'
@@ -1,2 +1,6 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  export * from './Listener'
2
6
  export * from './decorator'
@@ -0,0 +1,16 @@
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
+ }
@@ -0,0 +1,30 @@
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
+ }
@@ -0,0 +1,6 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
5
+ export * from './button'
6
+ export * from './selectMenu'
@@ -0,0 +1,30 @@
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
+ }
@@ -1,31 +1,42 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  import _ from 'lodash'
2
6
  import { Registry } from './Registry'
3
- import { Client, Message, Snowflake, User } from 'discord.js'
4
- import { BuiltinCommandConverters, BuiltinSlashCommandConverters, CommandHandler } from '../builtinModules'
7
+ import { Client, CommandInteraction, Interaction, Message, Snowflake, User } from 'discord.js'
8
+ import { BuiltinCommandConverters, BuiltinApplicationCommandConverters, CommandHandler } from '../builtinModules'
5
9
  import { CoolDownAdapter, DefaultCoolDownAdapter } from '../command'
6
- import { REST } from '@discordjs/rest'
7
10
  import { Logger } from 'tslog'
8
11
 
9
12
  export interface CommandOptions {
10
13
  prefix: string | ((msg: any) => string | Promise<string | string[]> | string[]) | string[]
11
14
  check: (msg: Message) => boolean | Promise<boolean>
15
+ globalAliases: (cmd: string, msg: Message) => string[] | Promise<string[]>
12
16
  }
13
17
 
14
18
  export interface SlashCommandOptions {
19
+ check: (i: CommandInteraction) => boolean | Promise<boolean>
20
+ }
21
+
22
+ export interface ApplicationCommandOptions {
15
23
  guild?: Snowflake | Snowflake[]
16
24
  autoSync: boolean
25
+ beforeRunCheck: (i: Interaction) => void | Promise<void>
17
26
  }
18
27
 
19
28
  export interface CommandClientOptions {
20
29
  command: CommandOptions
21
30
  owners: 'auto' | Snowflake[]
22
31
  slashCommands: SlashCommandOptions
32
+ applicationCommands: ApplicationCommandOptions
23
33
  }
24
34
 
25
35
  export interface CommandClientOptionsParam {
26
36
  command: Partial<CommandOptions>
27
37
  owners: 'auto' | string[]
28
38
  slashCommands: Partial<SlashCommandOptions>
39
+ applicationCommands: Partial<ApplicationCommandOptions>
29
40
  }
30
41
 
31
42
  export class CommandClient {
@@ -34,9 +45,6 @@ export class CommandClient {
34
45
  registry = new Registry(this)
35
46
  client: Client
36
47
  coolDownAdapter: CoolDownAdapter
37
- rest = new REST({
38
- version: '9',
39
- })
40
48
  logger: Logger
41
49
 
42
50
  private _isReady = false
@@ -51,13 +59,12 @@ export class CommandClient {
51
59
 
52
60
  async ready() {
53
61
  if (this._isReady) return
54
- this.rest.setToken(this.client.token!)
55
62
  this._isReady = true
56
63
  if (this.options.owners === 'auto') {
57
64
  const owners = await this.fetchOwners()
58
65
  this.owners.push(...owners)
59
66
  }
60
- if (this.options.slashCommands.autoSync) {
67
+ if (this.options.applicationCommands.autoSync) {
61
68
  await this.registry.syncCommands()
62
69
  }
63
70
  }
@@ -70,10 +77,15 @@ export class CommandClient {
70
77
  command: {
71
78
  prefix: '!',
72
79
  check: () => true,
80
+ globalAliases: () => [],
73
81
  },
74
82
  owners: 'auto',
75
83
  slashCommands: {
76
- autoSync: true,
84
+ check: () => true,
85
+ },
86
+ applicationCommands: {
87
+ autoSync: false,
88
+ beforeRunCheck: () => {},
77
89
  },
78
90
  },
79
91
  options,
@@ -88,6 +100,6 @@ export class CommandClient {
88
100
  this.client.once('ready', () => this.ready())
89
101
  this.registry.registerModule(new CommandHandler(this.registry))
90
102
  this.registry.registerModule(new BuiltinCommandConverters(this))
91
- this.registry.registerModule(new BuiltinSlashCommandConverters(this))
103
+ this.registry.registerModule(new BuiltinApplicationCommandConverters(this))
92
104
  }
93
105
  }
@@ -1,9 +1,14 @@
1
- import { KArgumentConverters, KCommands, KListeners, KModulePath, KSlashArgumentConverters, KSlashCommands } from '../constants'
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
5
+ import { KArgumentConverters, KCommands, KListeners, KMessageComponentHandlers, KModulePath, KSlashArgumentConverters, KApplicationCommands } from '../constants'
2
6
  import type { Command } from '../command'
3
7
  import { Listener } from '../listener'
4
- import { ArgumentConverter, SlashArgumentConverter } from '../command'
5
- import { SlashCommand } from '../slashCommand'
8
+ import { ArgumentConverter, ApplicationCommandArgumentConverter } from '../command'
9
+ import { AppCommand } from '../applicationCommand'
6
10
  import { CommandClient } from './CommandClient'
11
+ import { MessageComponentHandler } from '../messageComponents/base'
7
12
 
8
13
  export abstract class Module {
9
14
  commandClient!: CommandClient
@@ -26,12 +31,16 @@ export abstract class Module {
26
31
  return Reflect.getMetadata(KArgumentConverters, this) || []
27
32
  }
28
33
 
29
- get slashArgumentConverters(): SlashArgumentConverter[] {
34
+ get applicationCommandArgumentConverters(): ApplicationCommandArgumentConverter[] {
30
35
  return Reflect.getMetadata(KSlashArgumentConverters, this) || []
31
36
  }
32
37
 
33
- get slashCommands(): SlashCommand[] {
34
- return Reflect.getMetadata(KSlashCommands, this) || []
38
+ get applicationCommands(): AppCommand[] {
39
+ return Reflect.getMetadata(KApplicationCommands, this) || []
40
+ }
41
+
42
+ get messageComponentHandlers(): MessageComponentHandler[] {
43
+ return Reflect.getMetadata(KMessageComponentHandlers, this) || []
35
44
  }
36
45
 
37
46
  get path(): string | undefined {
@@ -1,15 +1,19 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  import { CommandClient } from './CommandClient'
2
6
  import { Module } from './Module'
3
- import { Command, SlashArgumentConverter } from '../command'
7
+ import { Command, ApplicationCommandArgumentConverter } from '../command'
4
8
  import { KBuiltInModule, KListenerExecuteCache, KModulePath } from '../constants'
5
9
  import path from 'path'
6
10
  import { InvalidModuleError, InvalidTargetError, ModuleLoadError } from '../error'
7
11
  import { Collection, Guild } from 'discord.js'
8
12
  import walkSync from 'walk-sync'
9
13
  import { ArgumentConverter } from '../command'
10
- import { SlashCommand } from '../slashCommand'
11
- import { Routes } from 'discord-api-types/v9'
14
+ import { AppCommand } from '../applicationCommand'
12
15
  import * as fs from 'fs'
16
+ import { MessageComponentHandler } from '../messageComponents/base'
13
17
 
14
18
  type ListenerExecutor = {
15
19
  event: string
@@ -47,21 +51,31 @@ export class Registry {
47
51
  return result
48
52
  }
49
53
 
50
- get slashArgumentConverters(): SlashArgumentConverter[] {
51
- const result: SlashArgumentConverter[] = []
54
+ get applicationCommandArgumentConverters(): ApplicationCommandArgumentConverter[] {
55
+ const result: ApplicationCommandArgumentConverter[] = []
52
56
 
53
57
  for (const [, module] of this.modules) {
54
- result.push(...module.slashArgumentConverters)
58
+ result.push(...module.applicationCommandArgumentConverters)
55
59
  }
56
60
 
57
61
  return result
58
62
  }
59
63
 
60
- get slashCommands(): SlashCommand[] {
61
- const result: SlashCommand[] = []
64
+ get applicationCommands(): AppCommand[] {
65
+ const result: AppCommand[] = []
62
66
 
63
67
  for (const [, module] of this.modules) {
64
- result.push(...module.slashCommands)
68
+ result.push(...module.applicationCommands)
69
+ }
70
+
71
+ return result
72
+ }
73
+
74
+ get messageComponentHandlers(): MessageComponentHandler[] {
75
+ const result: MessageComponentHandler[] = []
76
+
77
+ for (const [, module] of this.modules) {
78
+ result.push(...module.messageComponentHandlers)
65
79
  }
66
80
 
67
81
  return result
@@ -128,34 +142,51 @@ export class Registry {
128
142
 
129
143
  async syncCommands() {
130
144
  this.logger.debug(`Syncing commands...`)
131
- const commands = this.slashCommands.filter((x) => !x.guild)
132
- const guild = this.client.options.slashCommands.guild
145
+ const commands = this.applicationCommands.filter((x) => !x.guild)
146
+ const guild = this.client.options.applicationCommands.guild
147
+ const syncForGuild = async (g: Guild, commands: AppCommand[]) => {
148
+ this.logger.debug(`Syncing for guild ${g.name}(${g.id})`)
149
+ const commandsToRegister = [
150
+ ...commands.map((x) => x.command),
151
+ ...this.applicationCommands.filter((y) => y.guild === g.id || y.guild?.includes(g.id) || false).map((x) => x.command),
152
+ ]
153
+ this.logger.debug(`Command List: ${commandsToRegister.map((x) => x.name).join(', ')}`)
154
+ await g.commands.set(commandsToRegister)
155
+ }
133
156
  if (guild) {
134
- const syncForGuild = async (g: Guild) => {
135
- this.logger.debug(`Syncing for guild ${g.name}(${g.id})`)
136
- const commandsToRegister = [
137
- ...commands.map((x) => x.commandBuilder),
138
- ...this.slashCommands.filter((y) => y.guild === g.id || y.guild?.includes(g.id) || false).map((x) => x.commandBuilder),
139
- ]
140
- this.logger.debug(`Command List: ${commandsToRegister.map((x) => x.name).join(', ')}`)
141
- await this.client.rest.put(Routes.applicationGuildCommands(this.client.client.application!.id, g.id) as any, {
142
- body: commandsToRegister.map((x) => x.toJSON()),
143
- })
144
- }
145
-
146
157
  if (typeof guild === 'string') {
147
- await syncForGuild(await this.client.client.guilds.fetch(guild))
158
+ await syncForGuild(await this.client.client.guilds.fetch(guild), commands)
148
159
  } else {
149
160
  for (const g of guild) {
150
- await syncForGuild(await this.client.client.guilds.fetch(g))
161
+ await syncForGuild(await this.client.client.guilds.fetch(g), commands)
151
162
  }
152
163
  }
153
164
  } else {
154
165
  this.logger.debug('Syncing global...')
155
- await this.client.rest.put(Routes.applicationCommands(this.client.client.application!.id) as any, {
156
- body: commands.map((x) => x.commandBuilder.toJSON()),
157
- })
166
+ await this.client.client.application?.commands.set(commands.map((x) => x.command))
167
+ }
168
+ const commandsWithGuild = this.applicationCommands.filter((x) => x.guild)
169
+
170
+ const guilds = new Set<string>()
171
+
172
+ for (const command of commandsWithGuild) {
173
+ if (!command.guild) continue
174
+ if (typeof command.guild === 'string') {
175
+ guilds.add(command.guild)
176
+ } else {
177
+ for (const guild of command.guild) {
178
+ guilds.add(guild)
179
+ }
180
+ }
158
181
  }
182
+
183
+ for (const guild of guilds) {
184
+ await syncForGuild(
185
+ await this.client.client.guilds.fetch(guild),
186
+ commandsWithGuild.filter((x) => x.guild && typeof (x.guild === 'string' ? guild === x.guild : x.guild.includes(guild))),
187
+ )
188
+ }
189
+
159
190
  this.logger.debug('Syncing ended.')
160
191
  }
161
192
 
@@ -1,3 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  export * from './Module'
2
6
  export * from './CommandClient'
3
7
  export * from './Registry'
package/src/typings.ts CHANGED
@@ -1,6 +1,10 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  import type { Command } from './command'
2
6
  import { CommandClient } from './structures'
3
- import { SlashCommand } from './slashCommand'
7
+ import { AppCommand } from './applicationCommand'
4
8
 
5
9
  declare module 'discord.js' {
6
10
  interface Message {
@@ -12,7 +16,19 @@ declare module 'discord.js' {
12
16
  }
13
17
  interface CommandInteraction {
14
18
  data: {
15
- command: SlashCommand
19
+ command: AppCommand
20
+ cts: CommandClient
21
+ }
22
+ }
23
+ interface MessageComponentInteraction {
24
+ data: {
25
+ command: AppCommand
26
+ cts: CommandClient
27
+ }
28
+ }
29
+ interface ContextMenuInteraction {
30
+ data: {
31
+ command: AppCommand
16
32
  cts: CommandClient
17
33
  }
18
34
  }
package/src/utils.ts CHANGED
@@ -1,3 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  import { Module } from './structures'
2
6
  import { InvalidTargetError } from './error'
3
7
 
package/test/index.ts CHANGED
@@ -1,3 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
1
5
  import { Client, Intents, IntentsString } from 'discord.js'
2
6
  import { CommandClient } from '../src'
3
7
 
@@ -8,12 +12,12 @@ const client = new Client({
8
12
  })
9
13
 
10
14
  const cts = new CommandClient({
11
- owners: [],
15
+ owners: 'auto',
12
16
  client,
13
17
  command: {
14
18
  prefix: '!',
15
19
  },
16
- slashCommands: {
20
+ applicationCommands: {
17
21
  autoSync: true,
18
22
  guild: '832938554438844438',
19
23
  },
@@ -1,6 +1,9 @@
1
- import { BuiltInModule, CommandClient, ownerOnly, slashCommand, listener, SlashCommandCheckFailed } from '../../src'
1
+ /*
2
+ * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
+ */
4
+
5
+ import { BuiltInModule, CommandClient, ownerOnly, applicationCommand, listener, ApplicationCommandCheckFailed } from '../../src'
2
6
  import { CommandInteraction } from 'discord.js'
3
- import { SlashCommandBuilder } from '@discordjs/builders'
4
7
 
5
8
  export class Dev extends BuiltInModule {
6
9
  constructor(private cts: CommandClient) {
@@ -9,7 +12,7 @@ export class Dev extends BuiltInModule {
9
12
 
10
13
  @listener('slashCommandError')
11
14
  slashError(e: Error, i: CommandInteraction) {
12
- if (e instanceof SlashCommandCheckFailed) {
15
+ if (e instanceof ApplicationCommandCheckFailed) {
13
16
  return i.reply({
14
17
  content: 'Command before-run check failed',
15
18
  ephemeral: true,
@@ -18,8 +21,13 @@ export class Dev extends BuiltInModule {
18
21
  console.error(e.message)
19
22
  }
20
23
 
21
- @slashCommand({
22
- command: new SlashCommandBuilder().setName('reload').setDescription('리로드 커맨드'),
24
+ // new SlashCommandBuilder().setName('reload').setDescription('리로드 커맨드')
25
+ @applicationCommand({
26
+ command: {
27
+ name: 'reload',
28
+ type: 'CHAT_INPUT',
29
+ description: '리로드 커맨드',
30
+ },
23
31
  })
24
32
  @ownerOnly
25
33
  async reload(i: CommandInteraction) {