@pikokr/command.ts 3.0.0-dev.7170769 → 3.0.0-dev.85a29c3

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 (83) hide show
  1. package/.prettierrc +2 -1
  2. package/README.md +3 -1
  3. package/dist/builtinModules/BuiltInModule.d.ts +4 -0
  4. package/dist/builtinModules/BuiltInModule.js +12 -0
  5. package/dist/builtinModules/BuiltinCommandConverters.d.ts +14 -0
  6. package/dist/builtinModules/BuiltinCommandConverters.js +89 -0
  7. package/dist/builtinModules/CommandHandler.d.ts +9 -0
  8. package/dist/builtinModules/CommandHandler.js +134 -0
  9. package/dist/builtinModules/index.d.ts +2 -0
  10. package/dist/builtinModules/index.js +14 -0
  11. package/dist/command/ArgumentConverter.d.ts +9 -0
  12. package/dist/command/ArgumentConverter.js +14 -0
  13. package/dist/command/Command.d.ts +14 -5
  14. package/dist/command/Command.js +9 -4
  15. package/dist/command/decorator.d.ts +11 -2
  16. package/dist/command/decorator.js +66 -10
  17. package/dist/command/index.d.ts +2 -0
  18. package/dist/command/index.js +2 -0
  19. package/dist/command/utils.d.ts +2 -0
  20. package/dist/command/utils.js +17 -0
  21. package/dist/constants.d.ts +7 -0
  22. package/dist/constants.js +9 -2
  23. package/dist/error/ArgumentConverterNotFound.d.ts +7 -0
  24. package/dist/error/ArgumentConverterNotFound.js +11 -0
  25. package/dist/error/ArgumentNotProvided.d.ts +8 -0
  26. package/dist/error/ArgumentNotProvided.js +12 -0
  27. package/dist/error/CommandCheckFailed.d.ts +7 -0
  28. package/dist/error/CommandCheckFailed.js +11 -0
  29. package/dist/error/CommandNotFound.d.ts +4 -0
  30. package/dist/error/CommandNotFound.js +10 -0
  31. package/dist/error/PermissionRequired.d.ts +10 -0
  32. package/dist/error/PermissionRequired.js +18 -0
  33. package/dist/error/index.d.ts +4 -0
  34. package/dist/error/index.js +4 -0
  35. package/dist/index.d.ts +3 -0
  36. package/dist/index.js +3 -0
  37. package/dist/listener/Listener.d.ts +5 -0
  38. package/dist/listener/Listener.js +10 -0
  39. package/dist/listener/decorator.d.ts +2 -0
  40. package/dist/listener/decorator.js +21 -0
  41. package/dist/listener/index.d.ts +2 -0
  42. package/dist/listener/index.js +14 -0
  43. package/dist/structures/CommandClient.d.ts +0 -1
  44. package/dist/structures/CommandClient.js +15 -38
  45. package/dist/structures/Module.d.ts +7 -0
  46. package/dist/structures/Module.js +13 -0
  47. package/dist/structures/Registry.d.ts +16 -6
  48. package/dist/structures/Registry.js +113 -22
  49. package/dist/structures/index.d.ts +2 -1
  50. package/dist/structures/index.js +2 -1
  51. package/dist/typings.d.ts +11 -0
  52. package/dist/typings.js +2 -0
  53. package/dist/utils.d.ts +1 -0
  54. package/dist/utils.js +10 -0
  55. package/package.json +3 -2
  56. package/src/builtinModules/BuiltInModule.ts +9 -0
  57. package/src/builtinModules/BuiltinCommandConverters.ts +56 -0
  58. package/src/builtinModules/CommandHandler.ts +120 -0
  59. package/src/builtinModules/index.ts +2 -0
  60. package/src/command/ArgumentConverter.ts +10 -0
  61. package/src/command/Command.ts +20 -5
  62. package/src/command/decorator.ts +84 -12
  63. package/src/command/index.ts +2 -0
  64. package/src/command/utils.ts +15 -0
  65. package/src/constants.ts +15 -1
  66. package/src/error/ArgumentConverterNotFound.ts +8 -0
  67. package/src/error/ArgumentNotProvided.ts +8 -0
  68. package/src/error/CommandCheckFailed.ts +8 -0
  69. package/src/error/CommandNotFound.ts +5 -0
  70. package/src/error/PermissionRequired.ts +13 -0
  71. package/src/error/index.ts +4 -0
  72. package/src/index.ts +3 -0
  73. package/src/listener/Listener.ts +3 -0
  74. package/src/listener/decorator.ts +25 -0
  75. package/src/listener/index.ts +2 -0
  76. package/src/structures/CommandClient.ts +10 -48
  77. package/src/structures/Module.ts +21 -0
  78. package/src/structures/Registry.ts +108 -19
  79. package/src/structures/index.ts +2 -1
  80. package/src/typings.ts +12 -0
  81. package/src/utils.ts +6 -0
  82. package/test/index.ts +5 -2
  83. package/test/modules/test.ts +44 -0
@@ -1,35 +1,68 @@
1
1
  import { CommandClient } from './CommandClient'
2
2
  import { Module } from './Module'
3
3
  import { Command } from '../command'
4
- import { KCommands, KModulePath } from '../constants'
4
+ import { KBuiltInModule, KListenerExecuteCache, KModulePath } from '../constants'
5
5
  import path from 'path'
6
- import {
7
- InvalidModuleError,
8
- InvalidTargetError,
9
- ModuleLoadError,
10
- } from '../error'
6
+ import { InvalidModuleError, InvalidTargetError, ModuleLoadError } from '../error'
7
+ import { Collection } from 'discord.js'
8
+ import walkSync from 'walk-sync'
9
+ import { ArgumentConverter } from '../command'
10
+
11
+ type ListenerExecutor = {
12
+ event: string
13
+ execute: any
14
+ }
11
15
 
12
16
  export class Registry {
13
- constructor(private client: CommandClient) {}
17
+ constructor(public client: CommandClient) {}
14
18
 
15
- modules: Module[] = []
19
+ modules: Collection<symbol, Module> = new Collection()
16
20
 
17
21
  get commands(): Command[] {
18
22
  const result: Command[] = []
19
23
 
20
- for (const module of this.modules) {
21
- const commands = Reflect.getMetadata(KCommands, module)
22
- result.push(...commands)
24
+ for (const [, module] of this.modules) {
25
+ result.push(...module.commands)
26
+ }
27
+
28
+ return result
29
+ }
30
+
31
+ get argumentConverters(): ArgumentConverter[] {
32
+ const result: ArgumentConverter[] = []
33
+
34
+ for (const [, module] of this.modules) {
35
+ result.push(...module.argumentConverters)
23
36
  }
24
37
 
25
38
  return result
26
39
  }
27
40
 
28
41
  registerModule(module: Module) {
29
- // TODO
42
+ this.modules.set(Symbol(module.constructor.name), module)
43
+
44
+ const list: ListenerExecutor[] = []
45
+
46
+ for (const listener of module.listeners) {
47
+ const bound = listener.execute.bind(module)
48
+ list.push({ event: listener.name, execute: bound })
49
+ this.client.client.on(listener.name, bound)
50
+ }
51
+
52
+ Reflect.defineMetadata(KListenerExecuteCache, list, module)
53
+
54
+ return module
55
+ }
56
+
57
+ async loadModulesIn(dir: string, absolute = false) {
58
+ let p = absolute ? dir : path.join(require.main!.path, dir)
59
+
60
+ for (const i of walkSync(p)) {
61
+ await this.loadModule(path.join(p, i), true)
62
+ }
30
63
  }
31
64
 
32
- loadModule(file: string, absolute: boolean = false) {
65
+ async loadModule(file: string, absolute: boolean = false) {
33
66
  let p = absolute ? file : path.join(require.main!.path, file)
34
67
 
35
68
  let m
@@ -40,22 +73,78 @@ export class Registry {
40
73
  throw new ModuleLoadError(p)
41
74
  }
42
75
 
76
+ if (m.loaded) throw new Error('MODULE_ALREADY_LOADED')
77
+
43
78
  if (!m.install) throw new InvalidModuleError('Install function not found.')
44
79
 
45
- const mod = m.install()
80
+ const mod = m.install(this.client)
46
81
 
47
82
  if (!(mod instanceof Module)) throw new InvalidTargetError()
48
83
 
49
84
  Reflect.defineMetadata(KModulePath, require.resolve(p), mod)
50
85
 
51
- this.registerModule(m)
86
+ this.registerModule(mod)
87
+
88
+ await mod.load()
89
+
90
+ m.loaded = true
91
+
92
+ return mod
93
+ }
94
+
95
+ async unregisterModule(module: Module) {
96
+ if (Reflect.getMetadata(KBuiltInModule, module)) throw new Error('Built-in modules cannot be unloaded')
97
+ const symbol = this.modules.findKey((x) => x === module)
98
+ if (!symbol) return module
99
+ await module.unload()
100
+ const list: ListenerExecutor[] = Reflect.getMetadata(KListenerExecuteCache, module)
101
+ for (const listener of list) {
102
+ this.client.client.removeListener(listener.event, listener.execute)
103
+ }
104
+ this.modules.delete(symbol)
105
+ return module
52
106
  }
53
107
 
54
- unregisterModule(module: Module) {
55
- // TODO
108
+ async unloadModule(module: Module) {
109
+ const p = Reflect.getMetadata(KModulePath, module)
110
+
111
+ if (!p) throw new InvalidModuleError('This module is not loaded by loadModule.')
112
+
113
+ await this.unregisterModule(module)
114
+ delete require.cache[p]
56
115
  }
57
116
 
58
- unloadModule(module: Module) {
59
- // TODO
117
+ async reloadModule(module: Module) {
118
+ await module.beforeReload()
119
+ const p = Reflect.getMetadata(KModulePath, module)
120
+ await this.unloadModule(module)
121
+ const mod = await this.loadModule(p, true)
122
+ await mod.afterReload()
123
+ return true
124
+ }
125
+
126
+ async reloadAll() {
127
+ const results: {
128
+ path: string
129
+ success: boolean
130
+ error?: Error
131
+ }[] = []
132
+
133
+ for (const [, module] of this.modules.filter((x) => !!x.path)) {
134
+ try {
135
+ await this.reloadModule(module)
136
+ results.push({
137
+ path: module.path!,
138
+ success: false,
139
+ })
140
+ } catch (e: any) {
141
+ results.push({
142
+ error: e,
143
+ path: module.path!,
144
+ success: false,
145
+ })
146
+ }
147
+ }
148
+ return results
60
149
  }
61
150
  }
@@ -1,2 +1,3 @@
1
- export * from './CommandClient'
2
1
  export * from './Module'
2
+ export * from './CommandClient'
3
+ export * from './Registry'
package/src/typings.ts ADDED
@@ -0,0 +1,12 @@
1
+ import type { Command } from './command'
2
+ import { CommandClient } from './structures'
3
+
4
+ declare module 'discord.js' {
5
+ interface Message {
6
+ data: {
7
+ command: Command
8
+ prefix: string
9
+ cts: CommandClient
10
+ }
11
+ }
12
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { Module } from './structures'
2
+ import { InvalidTargetError } from './error'
3
+
4
+ export const checkTarget = (target: Object) => {
5
+ if (!(target instanceof Module)) throw new InvalidTargetError()
6
+ }
package/test/index.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { Client, Intents, IntentsString } from 'discord.js'
2
2
  import { CommandClient } from '../src'
3
- import { DJSAdapter } from '../src/adapters/DJSAdapter'
4
3
 
5
4
  const config = require('./config.json')
6
5
 
@@ -10,10 +9,14 @@ const client = new Client({
10
9
 
11
10
  const cts = new CommandClient({
12
11
  owners: 'auto',
13
- adapter: new DJSAdapter(client),
12
+ client,
14
13
  command: {
15
14
  prefix: '!',
16
15
  },
17
16
  })
18
17
 
18
+ require('./modules/test')
19
+
20
+ cts.registry.loadModulesIn('modules')
21
+
19
22
  client.login(config.token)
@@ -0,0 +1,44 @@
1
+ import { command, CommandClient, Module, listener, rest } from '../../src'
2
+ import { Message } from 'discord.js'
3
+
4
+ class Test extends Module {
5
+ constructor(private client: CommandClient) {
6
+ super()
7
+ }
8
+
9
+ load() {
10
+ console.log('load')
11
+ }
12
+
13
+ unload() {
14
+ console.log('unload')
15
+ }
16
+
17
+ beforeReload() {
18
+ console.log('before reload')
19
+ }
20
+
21
+ afterReload() {
22
+ console.log('after reload')
23
+ }
24
+
25
+ @listener('ready')
26
+ ready() {
27
+ console.log(`Logged in as ${this.client.client.user!.tag}`)
28
+ }
29
+
30
+ @listener('commandError')
31
+ error(err: Error) {
32
+ console.error(err)
33
+ }
34
+
35
+ @command()
36
+ test(msg: Message, @rest asdf: string = 'wa sans') {
37
+ console.log(asdf)
38
+ msg.reply(asdf)
39
+ }
40
+ }
41
+
42
+ export function install(client: CommandClient) {
43
+ return new Test(client)
44
+ }