@pikokr/command.ts 3.0.0-dev.a94f324 → 3.0.0-dev.ca01eb5

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 (90) hide show
  1. package/.prettierrc +2 -1
  2. package/README.md +2 -2
  3. package/dist/builtinModules/BuiltinCommandConverters.d.ts +9 -1
  4. package/dist/builtinModules/BuiltinCommandConverters.js +53 -0
  5. package/dist/builtinModules/BuiltinSlashCommandConverters.d.ts +9 -0
  6. package/dist/builtinModules/BuiltinSlashCommandConverters.js +32 -0
  7. package/dist/builtinModules/CommandHandler.d.ts +4 -2
  8. package/dist/builtinModules/CommandHandler.js +152 -53
  9. package/dist/builtinModules/index.d.ts +1 -0
  10. package/dist/builtinModules/index.js +1 -0
  11. package/dist/command/ArgumentConverter.d.ts +7 -1
  12. package/dist/command/ArgumentConverter.js +11 -1
  13. package/dist/command/Command.d.ts +8 -2
  14. package/dist/command/Command.js +7 -1
  15. package/dist/command/cooldown/adapter.d.ts +10 -0
  16. package/dist/command/cooldown/adapter.js +30 -0
  17. package/dist/command/cooldown/decorator.d.ts +2 -0
  18. package/dist/command/cooldown/decorator.js +72 -0
  19. package/dist/command/cooldown/error.d.ts +4 -0
  20. package/dist/command/cooldown/error.js +10 -0
  21. package/dist/command/cooldown/index.d.ts +5 -0
  22. package/dist/command/cooldown/index.js +17 -0
  23. package/dist/command/cooldown/type.d.ts +8 -0
  24. package/dist/command/cooldown/type.js +12 -0
  25. package/dist/command/decorator.d.ts +9 -1
  26. package/dist/command/decorator.js +70 -12
  27. package/dist/command/index.d.ts +2 -0
  28. package/dist/command/index.js +2 -0
  29. package/dist/command/utils.d.ts +2 -0
  30. package/dist/command/utils.js +29 -0
  31. package/dist/constants.d.ts +6 -0
  32. package/dist/constants.js +7 -1
  33. package/dist/error/ArgumentConverterNotFound.d.ts +7 -1
  34. package/dist/error/ArgumentConverterNotFound.js +9 -1
  35. package/dist/error/ArgumentNotProvided.d.ts +3 -1
  36. package/dist/error/ArgumentNotProvided.js +2 -1
  37. package/dist/error/CommandCheckFailed.d.ts +13 -0
  38. package/dist/error/CommandCheckFailed.js +19 -0
  39. package/dist/error/PermissionRequired.d.ts +10 -0
  40. package/dist/error/PermissionRequired.js +18 -0
  41. package/dist/error/index.d.ts +2 -0
  42. package/dist/error/index.js +2 -0
  43. package/dist/index.d.ts +3 -0
  44. package/dist/index.js +3 -0
  45. package/dist/slashCommand/SlashCommand.d.ts +19 -0
  46. package/dist/slashCommand/SlashCommand.js +21 -0
  47. package/dist/slashCommand/decorator.d.ts +10 -0
  48. package/dist/slashCommand/decorator.js +37 -0
  49. package/dist/slashCommand/index.d.ts +2 -0
  50. package/dist/slashCommand/index.js +14 -0
  51. package/dist/structures/CommandClient.d.ts +14 -3
  52. package/dist/structures/CommandClient.js +27 -12
  53. package/dist/structures/Module.d.ts +5 -1
  54. package/dist/structures/Module.js +9 -0
  55. package/dist/structures/Registry.d.ts +16 -6
  56. package/dist/structures/Registry.js +130 -40
  57. package/dist/typings.d.ts +18 -0
  58. package/dist/typings.js +2 -0
  59. package/package.json +4 -1
  60. package/src/builtinModules/BuiltinCommandConverters.ts +42 -1
  61. package/src/builtinModules/BuiltinSlashCommandConverters.ts +18 -0
  62. package/src/builtinModules/CommandHandler.ts +155 -63
  63. package/src/builtinModules/index.ts +1 -0
  64. package/src/command/ArgumentConverter.ts +10 -6
  65. package/src/command/Command.ts +12 -1
  66. package/src/command/cooldown/adapter.ts +18 -0
  67. package/src/command/cooldown/decorator.ts +62 -0
  68. package/src/command/cooldown/error.ts +5 -0
  69. package/src/command/cooldown/index.ts +5 -0
  70. package/src/command/cooldown/type.ts +8 -0
  71. package/src/command/decorator.ts +102 -36
  72. package/src/command/index.ts +2 -0
  73. package/src/command/utils.ts +29 -0
  74. package/src/constants.ts +12 -0
  75. package/src/error/ArgumentConverterNotFound.ts +7 -1
  76. package/src/error/ArgumentNotProvided.ts +2 -1
  77. package/src/error/CommandCheckFailed.ts +15 -0
  78. package/src/error/PermissionRequired.ts +13 -0
  79. package/src/error/index.ts +2 -0
  80. package/src/index.ts +3 -0
  81. package/src/slashCommand/SlashCommand.ts +29 -0
  82. package/src/slashCommand/decorator.ts +58 -0
  83. package/src/slashCommand/index.ts +2 -0
  84. package/src/structures/CommandClient.ts +31 -19
  85. package/src/structures/Module.ts +15 -2
  86. package/src/structures/Registry.ts +96 -29
  87. package/src/typings.ts +19 -0
  88. package/test/index.ts +4 -0
  89. package/test/modules/test.ts +32 -5
  90. package/pagesconfig.json +0 -13
@@ -1,20 +1,14 @@
1
1
  import { CommandClient } from './CommandClient'
2
2
  import { Module } from './Module'
3
- import { Command } from '../command'
4
- import {
5
- KBuiltInModule,
6
- KListenerExecuteCache,
7
- KModulePath,
8
- } from '../constants'
3
+ import { Command, SlashArgumentConverter } from '../command'
4
+ import { KBuiltInModule, KListenerExecuteCache, KModulePath } from '../constants'
9
5
  import path from 'path'
10
- import {
11
- InvalidModuleError,
12
- InvalidTargetError,
13
- ModuleLoadError,
14
- } from '../error'
15
- import { Collection } from 'discord.js'
6
+ import { InvalidModuleError, InvalidTargetError, ModuleLoadError } from '../error'
7
+ import { Collection, Guild } from 'discord.js'
16
8
  import walkSync from 'walk-sync'
17
- import { ArgumentConverter } from '../command/ArgumentConverter'
9
+ import { ArgumentConverter } from '../command'
10
+ import { SlashCommand } from '../slashCommand'
11
+ import { Routes } from 'discord-api-types/v9'
18
12
 
19
13
  type ListenerExecutor = {
20
14
  event: string
@@ -46,6 +40,26 @@ export class Registry {
46
40
  return result
47
41
  }
48
42
 
43
+ get slashArgumentConverters(): SlashArgumentConverter[] {
44
+ const result: SlashArgumentConverter[] = []
45
+
46
+ for (const [, module] of this.modules) {
47
+ result.push(...module.slashArgumentConverters)
48
+ }
49
+
50
+ return result
51
+ }
52
+
53
+ get slashCommands(): SlashCommand[] {
54
+ const result: SlashCommand[] = []
55
+
56
+ for (const [, module] of this.modules) {
57
+ result.push(...module.slashCommands)
58
+ }
59
+
60
+ return result
61
+ }
62
+
49
63
  registerModule(module: Module) {
50
64
  this.modules.set(Symbol(module.constructor.name), module)
51
65
 
@@ -62,15 +76,15 @@ export class Registry {
62
76
  return module
63
77
  }
64
78
 
65
- loadModulesIn(dir: string, absolute = false) {
79
+ async loadModulesIn(dir: string, absolute = false) {
66
80
  let p = absolute ? dir : path.join(require.main!.path, dir)
67
81
 
68
82
  for (const i of walkSync(p)) {
69
- this.loadModule(path.join(p, i), true)
83
+ await this.loadModule(path.join(p, i), true)
70
84
  }
71
85
  }
72
86
 
73
- loadModule(file: string, absolute: boolean = false) {
87
+ async loadModule(file: string, absolute: boolean = false) {
74
88
  let p = absolute ? file : path.join(require.main!.path, file)
75
89
 
76
90
  let m
@@ -93,23 +107,43 @@ export class Registry {
93
107
 
94
108
  this.registerModule(mod)
95
109
 
96
- mod.load()
110
+ await mod.load()
97
111
 
98
112
  m.loaded = true
99
113
 
100
114
  return mod
101
115
  }
102
116
 
103
- unregisterModule(module: Module) {
104
- if (Reflect.getMetadata(KBuiltInModule, module))
105
- throw new Error('Built-in modules cannot be unloaded')
117
+ async syncCommands() {
118
+ const commands = this.slashCommands.filter((x) => !x.guild)
119
+ const guild = this.client.options.slashCommands.guild
120
+ if (guild) {
121
+ const syncForGuild = async (g: Guild) => {
122
+ await this.client.rest.put(Routes.applicationGuildCommands(this.client.client.application!.id, g.id) as any, {
123
+ body: commands.map((x) => x.commandBuilder.toJSON()),
124
+ })
125
+ }
126
+
127
+ if (typeof guild === 'string') {
128
+ await syncForGuild(await this.client.client.guilds.fetch(guild))
129
+ } else {
130
+ for (const g of guild) {
131
+ await syncForGuild(await this.client.client.guilds.fetch(g))
132
+ }
133
+ }
134
+ } else {
135
+ await this.client.rest.put(Routes.applicationCommands(this.client.client.application!.id) as any, {
136
+ body: commands.map((x) => x.commandBuilder.toJSON()),
137
+ })
138
+ }
139
+ }
140
+
141
+ async unregisterModule(module: Module) {
142
+ if (Reflect.getMetadata(KBuiltInModule, module)) throw new Error('Built-in modules cannot be unloaded')
106
143
  const symbol = this.modules.findKey((x) => x === module)
107
144
  if (!symbol) return module
108
- module.unload()
109
- const list: ListenerExecutor[] = Reflect.getMetadata(
110
- KListenerExecuteCache,
111
- module,
112
- )
145
+ await module.unload()
146
+ const list: ListenerExecutor[] = Reflect.getMetadata(KListenerExecuteCache, module)
113
147
  for (const listener of list) {
114
148
  this.client.client.removeListener(listener.event, listener.execute)
115
149
  }
@@ -117,13 +151,46 @@ export class Registry {
117
151
  return module
118
152
  }
119
153
 
120
- unloadModule(module: Module) {
154
+ async unloadModule(module: Module) {
121
155
  const p = Reflect.getMetadata(KModulePath, module)
122
156
 
123
- if (!p)
124
- throw new InvalidModuleError('This module is not loaded by loadModule.')
157
+ if (!p) throw new InvalidModuleError('This module is not loaded by loadModule.')
125
158
 
126
- this.unregisterModule(module)
159
+ await this.unregisterModule(module)
127
160
  delete require.cache[p]
128
161
  }
162
+
163
+ async reloadModule(module: Module) {
164
+ await module.beforeReload()
165
+ const p = Reflect.getMetadata(KModulePath, module)
166
+ await this.unloadModule(module)
167
+ const mod = await this.loadModule(p, true)
168
+ await mod.afterReload()
169
+ return true
170
+ }
171
+
172
+ async reloadAll() {
173
+ const results: {
174
+ path: string
175
+ success: boolean
176
+ error?: Error
177
+ }[] = []
178
+
179
+ for (const [, module] of this.modules.filter((x) => !!x.path)) {
180
+ try {
181
+ await this.reloadModule(module)
182
+ results.push({
183
+ path: module.path!,
184
+ success: false,
185
+ })
186
+ } catch (e: any) {
187
+ results.push({
188
+ error: e,
189
+ path: module.path!,
190
+ success: false,
191
+ })
192
+ }
193
+ }
194
+ return results
195
+ }
129
196
  }
package/src/typings.ts ADDED
@@ -0,0 +1,19 @@
1
+ import type { Command } from './command'
2
+ import { CommandClient } from './structures'
3
+ import { SlashCommand } from './slashCommand'
4
+
5
+ declare module 'discord.js' {
6
+ interface Message {
7
+ data: {
8
+ command: Command
9
+ prefix: string
10
+ cts: CommandClient
11
+ }
12
+ }
13
+ interface CommandInteraction {
14
+ data: {
15
+ command: SlashCommand
16
+ cts: CommandClient
17
+ }
18
+ }
19
+ }
package/test/index.ts CHANGED
@@ -13,6 +13,10 @@ const cts = new CommandClient({
13
13
  command: {
14
14
  prefix: '!',
15
15
  },
16
+ slashCommands: {
17
+ autoSync: true,
18
+ guild: '832938554438844438',
19
+ },
16
20
  })
17
21
 
18
22
  require('./modules/test')
@@ -1,5 +1,6 @@
1
- import { command, CommandClient, Module, listener, optional } from '../../src'
2
- import { Message } from 'discord.js'
1
+ import { command, CommandClient, coolDown, CoolDownError, CoolDownType, listener, Module, option, rest, slashCommand } from '../../src'
2
+ import { CommandInteraction, Message } from 'discord.js'
3
+ import { SlashCommandBuilder } from '@discordjs/builders'
3
4
 
4
5
  class Test extends Module {
5
6
  constructor(private client: CommandClient) {
@@ -28,15 +29,41 @@ class Test extends Module {
28
29
  }
29
30
 
30
31
  @listener('commandError')
31
- error(err: Error) {
32
+ error(err: Error, msg: Message) {
33
+ if (err instanceof CoolDownError) {
34
+ return msg.reply(`쿨다운: <t:${(err.endsAt.getTime() / 1000).toFixed(0)}:R>`)
35
+ }
36
+ console.error(err)
37
+ }
38
+ @listener('slashCommandError')
39
+ slashCommandError(err: Error, msg: CommandInteraction) {
40
+ if (err instanceof CoolDownError) {
41
+ return msg.reply({
42
+ content: `쿨다운: <t:${(err.endsAt.getTime() / 1000).toFixed(0)}:R>`,
43
+ ephemeral: true,
44
+ })
45
+ }
32
46
  console.error(err)
33
47
  }
34
48
 
35
49
  @command()
36
- test(msg: Message, @optional asdf: string = 'wa sans') {
37
- console.log(asdf)
50
+ @coolDown(CoolDownType.USER, 10)
51
+ test(msg: Message, @rest asdf: string = 'wa sans') {
38
52
  msg.reply(asdf)
39
53
  }
54
+
55
+ @slashCommand({
56
+ command: new SlashCommandBuilder()
57
+ .setName('test')
58
+ .setDescription('test command')
59
+ .addStringOption((builder) => builder.setName('test').setDescription('test option').setRequired(false)),
60
+ })
61
+ @coolDown(CoolDownType.USER, 10)
62
+ async testSlash(i: CommandInteraction, @option('test') test: string = 'wa sans') {
63
+ return i.reply({
64
+ content: test,
65
+ })
66
+ }
40
67
  }
41
68
 
42
69
  export function install(client: CommandClient) {
package/pagesconfig.json DELETED
@@ -1,13 +0,0 @@
1
- {
2
- "groups": [
3
- {
4
- "title": "DOCS",
5
- "pages": [
6
- {
7
- "title": "Intro",
8
- "source": "./docs/README.md"
9
- }
10
- ]
11
- }
12
- ]
13
- }