@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.
- package/.prettierrc +2 -1
- package/README.md +3 -1
- package/dist/builtinModules/BuiltInModule.d.ts +4 -0
- package/dist/builtinModules/BuiltInModule.js +12 -0
- package/dist/builtinModules/BuiltinCommandConverters.d.ts +14 -0
- package/dist/builtinModules/BuiltinCommandConverters.js +89 -0
- package/dist/builtinModules/CommandHandler.d.ts +9 -0
- package/dist/builtinModules/CommandHandler.js +134 -0
- package/dist/builtinModules/index.d.ts +2 -0
- package/dist/builtinModules/index.js +14 -0
- package/dist/command/ArgumentConverter.d.ts +9 -0
- package/dist/command/ArgumentConverter.js +14 -0
- package/dist/command/Command.d.ts +14 -5
- package/dist/command/Command.js +9 -4
- package/dist/command/decorator.d.ts +11 -2
- package/dist/command/decorator.js +66 -10
- package/dist/command/index.d.ts +2 -0
- package/dist/command/index.js +2 -0
- package/dist/command/utils.d.ts +2 -0
- package/dist/command/utils.js +17 -0
- package/dist/constants.d.ts +7 -0
- package/dist/constants.js +9 -2
- package/dist/error/ArgumentConverterNotFound.d.ts +7 -0
- package/dist/error/ArgumentConverterNotFound.js +11 -0
- package/dist/error/ArgumentNotProvided.d.ts +8 -0
- package/dist/error/ArgumentNotProvided.js +12 -0
- package/dist/error/CommandCheckFailed.d.ts +7 -0
- package/dist/error/CommandCheckFailed.js +11 -0
- package/dist/error/CommandNotFound.d.ts +4 -0
- package/dist/error/CommandNotFound.js +10 -0
- package/dist/error/PermissionRequired.d.ts +10 -0
- package/dist/error/PermissionRequired.js +18 -0
- package/dist/error/index.d.ts +4 -0
- package/dist/error/index.js +4 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/listener/Listener.d.ts +5 -0
- package/dist/listener/Listener.js +10 -0
- package/dist/listener/decorator.d.ts +2 -0
- package/dist/listener/decorator.js +21 -0
- package/dist/listener/index.d.ts +2 -0
- package/dist/listener/index.js +14 -0
- package/dist/structures/CommandClient.d.ts +0 -1
- package/dist/structures/CommandClient.js +15 -38
- package/dist/structures/Module.d.ts +7 -0
- package/dist/structures/Module.js +13 -0
- package/dist/structures/Registry.d.ts +16 -6
- package/dist/structures/Registry.js +113 -22
- package/dist/structures/index.d.ts +2 -1
- package/dist/structures/index.js +2 -1
- package/dist/typings.d.ts +11 -0
- package/dist/typings.js +2 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +10 -0
- package/package.json +3 -2
- package/src/builtinModules/BuiltInModule.ts +9 -0
- package/src/builtinModules/BuiltinCommandConverters.ts +56 -0
- package/src/builtinModules/CommandHandler.ts +120 -0
- package/src/builtinModules/index.ts +2 -0
- package/src/command/ArgumentConverter.ts +10 -0
- package/src/command/Command.ts +20 -5
- package/src/command/decorator.ts +84 -12
- package/src/command/index.ts +2 -0
- package/src/command/utils.ts +15 -0
- package/src/constants.ts +15 -1
- package/src/error/ArgumentConverterNotFound.ts +8 -0
- package/src/error/ArgumentNotProvided.ts +8 -0
- package/src/error/CommandCheckFailed.ts +8 -0
- package/src/error/CommandNotFound.ts +5 -0
- package/src/error/PermissionRequired.ts +13 -0
- package/src/error/index.ts +4 -0
- package/src/index.ts +3 -0
- package/src/listener/Listener.ts +3 -0
- package/src/listener/decorator.ts +25 -0
- package/src/listener/index.ts +2 -0
- package/src/structures/CommandClient.ts +10 -48
- package/src/structures/Module.ts +21 -0
- package/src/structures/Registry.ts +108 -19
- package/src/structures/index.ts +2 -1
- package/src/typings.ts +12 -0
- package/src/utils.ts +6 -0
- package/test/index.ts +5 -2
- 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 {
|
|
4
|
+
import { KBuiltInModule, KListenerExecuteCache, KModulePath } from '../constants'
|
|
5
5
|
import path from 'path'
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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(
|
|
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
|
-
|
|
22
|
-
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
55
|
-
|
|
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
|
-
|
|
59
|
-
|
|
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
|
}
|
package/src/structures/index.ts
CHANGED
package/src/typings.ts
ADDED
package/src/utils.ts
ADDED
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
|
-
|
|
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
|
+
}
|