@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.
- package/.prettierrc +2 -1
- package/README.md +2 -2
- package/dist/builtinModules/BuiltinCommandConverters.d.ts +9 -1
- package/dist/builtinModules/BuiltinCommandConverters.js +53 -0
- package/dist/builtinModules/BuiltinSlashCommandConverters.d.ts +9 -0
- package/dist/builtinModules/BuiltinSlashCommandConverters.js +32 -0
- package/dist/builtinModules/CommandHandler.d.ts +4 -2
- package/dist/builtinModules/CommandHandler.js +152 -53
- package/dist/builtinModules/index.d.ts +1 -0
- package/dist/builtinModules/index.js +1 -0
- package/dist/command/ArgumentConverter.d.ts +7 -1
- package/dist/command/ArgumentConverter.js +11 -1
- package/dist/command/Command.d.ts +8 -2
- package/dist/command/Command.js +7 -1
- package/dist/command/cooldown/adapter.d.ts +10 -0
- package/dist/command/cooldown/adapter.js +30 -0
- package/dist/command/cooldown/decorator.d.ts +2 -0
- package/dist/command/cooldown/decorator.js +72 -0
- package/dist/command/cooldown/error.d.ts +4 -0
- package/dist/command/cooldown/error.js +10 -0
- package/dist/command/cooldown/index.d.ts +5 -0
- package/dist/command/cooldown/index.js +17 -0
- package/dist/command/cooldown/type.d.ts +8 -0
- package/dist/command/cooldown/type.js +12 -0
- package/dist/command/decorator.d.ts +9 -1
- package/dist/command/decorator.js +70 -12
- 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 +29 -0
- package/dist/constants.d.ts +6 -0
- package/dist/constants.js +7 -1
- package/dist/error/ArgumentConverterNotFound.d.ts +7 -1
- package/dist/error/ArgumentConverterNotFound.js +9 -1
- package/dist/error/ArgumentNotProvided.d.ts +3 -1
- package/dist/error/ArgumentNotProvided.js +2 -1
- package/dist/error/CommandCheckFailed.d.ts +13 -0
- package/dist/error/CommandCheckFailed.js +19 -0
- package/dist/error/PermissionRequired.d.ts +10 -0
- package/dist/error/PermissionRequired.js +18 -0
- package/dist/error/index.d.ts +2 -0
- package/dist/error/index.js +2 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/slashCommand/SlashCommand.d.ts +19 -0
- package/dist/slashCommand/SlashCommand.js +21 -0
- package/dist/slashCommand/decorator.d.ts +10 -0
- package/dist/slashCommand/decorator.js +37 -0
- package/dist/slashCommand/index.d.ts +2 -0
- package/dist/slashCommand/index.js +14 -0
- package/dist/structures/CommandClient.d.ts +14 -3
- package/dist/structures/CommandClient.js +27 -12
- package/dist/structures/Module.d.ts +5 -1
- package/dist/structures/Module.js +9 -0
- package/dist/structures/Registry.d.ts +16 -6
- package/dist/structures/Registry.js +130 -40
- package/dist/typings.d.ts +18 -0
- package/dist/typings.js +2 -0
- package/package.json +4 -1
- package/src/builtinModules/BuiltinCommandConverters.ts +42 -1
- package/src/builtinModules/BuiltinSlashCommandConverters.ts +18 -0
- package/src/builtinModules/CommandHandler.ts +155 -63
- package/src/builtinModules/index.ts +1 -0
- package/src/command/ArgumentConverter.ts +10 -6
- package/src/command/Command.ts +12 -1
- package/src/command/cooldown/adapter.ts +18 -0
- package/src/command/cooldown/decorator.ts +62 -0
- package/src/command/cooldown/error.ts +5 -0
- package/src/command/cooldown/index.ts +5 -0
- package/src/command/cooldown/type.ts +8 -0
- package/src/command/decorator.ts +102 -36
- package/src/command/index.ts +2 -0
- package/src/command/utils.ts +29 -0
- package/src/constants.ts +12 -0
- package/src/error/ArgumentConverterNotFound.ts +7 -1
- package/src/error/ArgumentNotProvided.ts +2 -1
- package/src/error/CommandCheckFailed.ts +15 -0
- package/src/error/PermissionRequired.ts +13 -0
- package/src/error/index.ts +2 -0
- package/src/index.ts +3 -0
- package/src/slashCommand/SlashCommand.ts +29 -0
- package/src/slashCommand/decorator.ts +58 -0
- package/src/slashCommand/index.ts +2 -0
- package/src/structures/CommandClient.ts +31 -19
- package/src/structures/Module.ts +15 -2
- package/src/structures/Registry.ts +96 -29
- package/src/typings.ts +19 -0
- package/test/index.ts +4 -0
- package/test/modules/test.ts +32 -5
- 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
|
-
|
|
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
|
|
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
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
package/test/modules/test.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { command, CommandClient,
|
|
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
|
-
|
|
37
|
-
|
|
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) {
|