@pikokr/command.ts 4.0.7 → 5.0.0-dev.44dc47f

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 (97) hide show
  1. package/.github/workflows/codeql-analysis.yml +70 -0
  2. package/.github/workflows/docs.yml +2 -2
  3. package/.github/workflows/publish.stable.yml +1 -1
  4. package/.github/workflows/publish.yml +1 -1
  5. package/.vscode/settings.json +10 -0
  6. package/.vscode/templates/ts.lict +5 -0
  7. package/README.md +2 -0
  8. package/dist/index.d.ts +140 -292
  9. package/dist/index.js +1 -1
  10. package/dist/index.js.map +1 -1
  11. package/docs/index.yml +1 -1
  12. package/package.json +22 -16
  13. package/publish-version.js +20 -0
  14. package/renovate.json +5 -0
  15. package/scripts/docs.ts +8 -4
  16. package/src/applicationCommand/ApplicationCommand.ts +17 -0
  17. package/src/applicationCommand/ApplicationCommandExtension.ts +169 -0
  18. package/src/applicationCommand/ApplicationCommandOption.ts +16 -0
  19. package/src/applicationCommand/index.ts +10 -6
  20. package/src/core/components/BaseComponent.ts +62 -0
  21. package/src/core/components/ComponentArgument.ts +15 -0
  22. package/src/core/components/ComponentArgumentDecorator.ts +25 -0
  23. package/src/core/components/decoratorCreator.ts +81 -0
  24. package/src/core/components/index.ts +13 -0
  25. package/src/core/converter/index.ts +16 -0
  26. package/src/core/extensions/CTSExtension.ts +17 -0
  27. package/src/core/extensions/Extension.ts +62 -0
  28. package/src/core/extensions/index.ts +9 -0
  29. package/src/core/hooks/componentHook.ts +40 -0
  30. package/src/core/hooks/index.ts +11 -0
  31. package/src/core/hooks/moduleHook.ts +39 -0
  32. package/src/core/index.ts +15 -0
  33. package/src/core/listener/index.ts +29 -0
  34. package/src/core/structures/CommandClient.ts +78 -0
  35. package/src/core/structures/Registry.ts +110 -0
  36. package/src/core/structures/index.ts +10 -0
  37. package/src/core/symbols.ts +13 -0
  38. package/src/core/utils/checks.ts +27 -0
  39. package/src/core/utils/errors.ts +9 -0
  40. package/src/core/utils/index.ts +10 -0
  41. package/src/index.ts +11 -16
  42. package/src/textCommand/TextCommand.ts +20 -0
  43. package/src/textCommand/TextCommandExtension.ts +128 -0
  44. package/src/textCommand/index.ts +11 -0
  45. package/src/textCommand/parameters.ts +14 -0
  46. package/test/index.ts +85 -20
  47. package/tsconfig.json +3 -3
  48. package/tsconfig.prod.json +8 -0
  49. package/tsup.config.ts +9 -4
  50. package/src/applicationCommand/AppCommand.ts +0 -32
  51. package/src/applicationCommand/decorator.ts +0 -62
  52. package/src/builtinModules/BuiltInModule.ts +0 -13
  53. package/src/builtinModules/BuiltinApplicationCommandConverters.ts +0 -16
  54. package/src/builtinModules/BuiltinCommandConverters.ts +0 -87
  55. package/src/builtinModules/CommandHandler.ts +0 -363
  56. package/src/builtinModules/index.ts +0 -7
  57. package/src/command/ArgumentConverter.ts +0 -22
  58. package/src/command/Command.ts +0 -35
  59. package/src/command/cooldown/adapter.ts +0 -22
  60. package/src/command/cooldown/decorator.ts +0 -67
  61. package/src/command/cooldown/error.ts +0 -9
  62. package/src/command/cooldown/index.ts +0 -9
  63. package/src/command/cooldown/type.ts +0 -12
  64. package/src/command/decorator.ts +0 -185
  65. package/src/command/index.ts +0 -9
  66. package/src/command/utils.ts +0 -33
  67. package/src/constants.ts +0 -31
  68. package/src/error/ArgumentConverterNotFound.ts +0 -18
  69. package/src/error/ArgumentNotProvided.ts +0 -12
  70. package/src/error/CommandCheckFailed.ts +0 -19
  71. package/src/error/CommandNotFound.ts +0 -11
  72. package/src/error/InvalidTargetError.ts +0 -9
  73. package/src/error/ModuleError.ts +0 -11
  74. package/src/error/PermissionRequired.ts +0 -17
  75. package/src/error/checks/DMOnlyCommand.ts +0 -9
  76. package/src/error/checks/GuildOnlyCommand.ts +0 -9
  77. package/src/error/checks/OwnerOnlyCommand.ts +0 -9
  78. package/src/error/checks/SlashCommandGlobalCheckError.ts +0 -11
  79. package/src/error/checks/index.ts +0 -8
  80. package/src/error/index.ts +0 -12
  81. package/src/interface/index.ts +0 -7
  82. package/src/listener/Listener.ts +0 -7
  83. package/src/listener/decorator.ts +0 -29
  84. package/src/listener/index.ts +0 -6
  85. package/src/messageComponents/base.ts +0 -16
  86. package/src/messageComponents/button.ts +0 -30
  87. package/src/messageComponents/index.ts +0 -6
  88. package/src/messageComponents/selectMenu.ts +0 -30
  89. package/src/structures/CommandClient.ts +0 -103
  90. package/src/structures/Module.ts +0 -54
  91. package/src/structures/Registry.ts +0 -253
  92. package/src/structures/index.ts +0 -7
  93. package/src/typings.ts +0 -35
  94. package/src/utils.ts +0 -10
  95. package/test/config.example.json +0 -3
  96. package/test/modules/dev.ts +0 -44
  97. package/test/modules/test.ts +0 -148
@@ -0,0 +1,78 @@
1
+ /*
2
+ * File: CommandClient.ts
3
+ *
4
+ * Copyright (c) 2022-2022 pikokr
5
+ *
6
+ * Licensed under MIT License. Please see more defails in LICENSE file.
7
+ */
8
+
9
+ import chalk from 'chalk'
10
+ import { Client, Snowflake, Team, User } from 'discord.js'
11
+ import EventEmitter from 'events'
12
+ import { Logger } from 'tslog'
13
+ import { ApplicationCommandExtension, ApplicationCommandExtensionConfig } from '../../applicationCommand/ApplicationCommandExtension'
14
+ import { TextCommandConfig } from '../../textCommand'
15
+ import { TextCommandExtension } from '../../textCommand/TextCommandExtension'
16
+ import { CommandClientSymbol } from '../symbols'
17
+ import { Registry } from './Registry'
18
+ export class CommandClient extends EventEmitter {
19
+ ctsLogger: Logger
20
+ registry: Registry
21
+
22
+ owners: Set<Snowflake> = new Set()
23
+
24
+ constructor(public discord: Client, public logger: Logger = new Logger({ dateTimeTimezone: Intl.DateTimeFormat().resolvedOptions().timeZone })) {
25
+ super()
26
+
27
+ this.ctsLogger = logger.getChildLogger({ prefix: [chalk.blue('[command.ts]')], displayFilePath: 'hidden', displayFunctionName: false })
28
+
29
+ this.registry = new Registry(this.ctsLogger, this)
30
+
31
+ this.registry.registerEventEmitter('cts', this)
32
+ this.registry.registerEventEmitter('discord', this.discord)
33
+ }
34
+
35
+ async fetchOwners() {
36
+ if (!this.discord.application) throw new Error('The client is not logged in.')
37
+
38
+ this.ctsLogger.info('Fetching owners...')
39
+
40
+ await this.discord.application.fetch()
41
+
42
+ const owner = this.discord.application.owner
43
+
44
+ if (!owner) throw new Error('Cannot find application owner')
45
+
46
+ const owners: string[] = []
47
+
48
+ if (owner instanceof User) {
49
+ this.owners.add(owner.id)
50
+ owners.push(owner.tag)
51
+ } else if (owner instanceof Team) {
52
+ for (const [id, member] of owner.members) {
53
+ this.owners.add(id)
54
+ owners.push(member.user.tag)
55
+ }
56
+ }
57
+
58
+ this.ctsLogger.info(`Fetched ${chalk.green(owners.length)} owners(${owners.map((x) => chalk.blue(x)).join(', ')})`)
59
+ }
60
+
61
+ async enableApplicationCommandsExtension(config: ApplicationCommandExtensionConfig) {
62
+ await this.registry.registerModule(new ApplicationCommandExtension(config))
63
+ this.ctsLogger.info('Application command extension enabled.')
64
+ }
65
+
66
+ async enableTextCommandsExtension(config: TextCommandConfig) {
67
+ await this.registry.registerModule(new TextCommandExtension(config))
68
+ this.ctsLogger.info('Text command extension enabled.')
69
+ }
70
+
71
+ getApplicationCommandsExtension() {
72
+ return this.registry.extensions.find((x) => x.constructor === ApplicationCommandExtension) as ApplicationCommandExtension | undefined
73
+ }
74
+
75
+ static getFromModule(ext: object): CommandClient {
76
+ return Reflect.getMetadata(CommandClientSymbol, ext)
77
+ }
78
+ }
@@ -0,0 +1,110 @@
1
+ /*
2
+ * File: Registry.ts
3
+ *
4
+ * Copyright (c) 2022-2022 pikokr
5
+ *
6
+ * Licensed under MIT License. Please see more defails in LICENSE file.
7
+ */
8
+
9
+ import chalk from 'chalk'
10
+ import { Collection } from 'discord.js'
11
+ import EventEmitter from 'events'
12
+ import _ from 'lodash'
13
+ import { Logger } from 'tslog'
14
+ import { getComponentStore } from '../components'
15
+ import type { BaseComponent } from '../components'
16
+ import { getModuleHookStore } from '../hooks'
17
+ import { ListenerComponent } from '../listener'
18
+ import { CommandClientSymbol } from '../symbols'
19
+ import { CommandClient } from './CommandClient'
20
+
21
+ export class Registry {
22
+ extensions: object[] = []
23
+
24
+ emitters: Collection<string, EventEmitter> = new Collection()
25
+
26
+ logger: Logger
27
+
28
+ constructor(logger: Logger, public client: CommandClient) {
29
+ this.logger = logger.getChildLogger({
30
+ prefix: [chalk.green('[Registry]')],
31
+ })
32
+ }
33
+
34
+ getComponentsWithTypeGlobal<T extends typeof BaseComponent<Config>, Config>(type: T): InstanceType<T>[] {
35
+ const result: InstanceType<T>[] = []
36
+
37
+ for (const ext of this.extensions) {
38
+ result.push(...this.getComponentsWithType(ext, type))
39
+ }
40
+
41
+ return result
42
+ }
43
+
44
+ getComponentsWithType<T extends typeof BaseComponent<Config>, Config>(ext: object, type: T): InstanceType<T>[] {
45
+ const componentStore = getComponentStore(ext)
46
+
47
+ return Array.from(componentStore.filter((x) => (x.constructor as unknown) === type).values() as Iterable<InstanceType<T>>)
48
+ }
49
+
50
+ registerEventListeners(ext: object) {
51
+ const listeners = this.getComponentsWithType(ext, ListenerComponent)
52
+
53
+ for (const listener of listeners) {
54
+ const emitter = this.emitters.get(listener.options.emitter)
55
+
56
+ if (emitter) {
57
+ const bound = listener.method.bind(ext)
58
+
59
+ Reflect.defineMetadata('bound', bound, listener)
60
+
61
+ emitter.addListener(listener.options.event, bound)
62
+ }
63
+ }
64
+ }
65
+
66
+ unregisterEventListeners(ext: object) {
67
+ const listeners = this.getComponentsWithType(ext, ListenerComponent)
68
+
69
+ for (const listener of listeners) {
70
+ const emitter = this.emitters.get(listener.options.emitter)
71
+ const bound = Reflect.getMetadata('bound', listener)
72
+
73
+ if (emitter && bound) {
74
+ emitter.removeListener(listener.options.event, bound)
75
+ }
76
+ }
77
+ }
78
+
79
+ async registerModule(ext: object) {
80
+ Reflect.defineMetadata(CommandClientSymbol, this.client, ext)
81
+
82
+ this.registerEventListeners(ext)
83
+ await this.runModuleHook(ext, 'load')
84
+ this.extensions.push(ext)
85
+ this.logger.info(`Module registered: ${chalk.green(ext.constructor.name)}`)
86
+ }
87
+
88
+ async unregisterModule(ext: object) {
89
+ this.unregisterEventListeners(ext)
90
+ await this.runModuleHook(ext, 'unload')
91
+ _.remove(this.extensions, (x) => x === ext)
92
+ this.logger.info(`Module unregistered: ${chalk.green(ext.constructor.name)}`)
93
+ }
94
+
95
+ runModuleHook(ext: object, hookName: string, ...args: unknown[]) {
96
+ const hooks = getModuleHookStore(ext)
97
+
98
+ const functions = hooks.get(hookName)
99
+
100
+ if (functions) {
101
+ for (const fn of functions) {
102
+ fn.call(ext, ...args)
103
+ }
104
+ }
105
+ }
106
+
107
+ registerEventEmitter(name: string, emitter: EventEmitter) {
108
+ this.emitters.set(name, emitter)
109
+ }
110
+ }
@@ -0,0 +1,10 @@
1
+ /*
2
+ * File: index.ts
3
+ *
4
+ * Copyright (c) 2022-2022 pikokr
5
+ *
6
+ * Licensed under MIT License. Please see more defails in LICENSE file.
7
+ */
8
+
9
+ export * from './Registry'
10
+ export * from './CommandClient'
@@ -0,0 +1,13 @@
1
+ /*
2
+ * File: symbols.ts
3
+ *
4
+ * Copyright (c) 2022-2022 pikokr
5
+ *
6
+ * Licensed under MIT License. Please see more defails in LICENSE file.
7
+ */
8
+
9
+ export const ComponentStoreSymbol = Symbol()
10
+ export const ComponentArgStoreSymbol = Symbol()
11
+ export const ModuleHookStoreSymbol = Symbol()
12
+ export const CommandClientSymbol = Symbol()
13
+ export const ComponentHookSymbol = Symbol()
@@ -0,0 +1,27 @@
1
+ /*
2
+ * File: checks.ts
3
+ *
4
+ * Copyright (c) 2022-2022 pikokr
5
+ *
6
+ * Licensed under MIT License. Please see more defails in LICENSE file.
7
+ */
8
+
9
+ import { BaseInteraction, Interaction, Message } from 'discord.js'
10
+ import { createComponentHook } from '../hooks'
11
+ import { ComponentHookFn } from '../hooks/componentHook'
12
+ import { CommandClient } from '../structures'
13
+ import { OwnerOnlyError } from './errors'
14
+
15
+ export const createCheckDecorator = (fn: ComponentHookFn) => createComponentHook('beforeCall', fn)
16
+
17
+ export const ownerOnly = createCheckDecorator(async (client: CommandClient, i: Interaction | Message) => {
18
+ let isOwner = false
19
+
20
+ if (i instanceof BaseInteraction) {
21
+ isOwner = client.owners.has(i.user.id)
22
+ } else if (i instanceof Message) {
23
+ isOwner = client.owners.has(i.author.id)
24
+ }
25
+
26
+ if (!isOwner) throw new OwnerOnlyError()
27
+ })
@@ -0,0 +1,9 @@
1
+ /*
2
+ * File: errors.ts
3
+ *
4
+ * Copyright (c) 2022-2022 pikokr
5
+ *
6
+ * Licensed under MIT License. Please see more defails in LICENSE file.
7
+ */
8
+
9
+ export class OwnerOnlyError {}
@@ -0,0 +1,10 @@
1
+ /*
2
+ * File: index.ts
3
+ *
4
+ * Copyright (c) 2022-2022 pikokr
5
+ *
6
+ * Licensed under MIT License. Please see more defails in LICENSE file.
7
+ */
8
+
9
+ export * from './checks'
10
+ export * from './errors'
package/src/index.ts CHANGED
@@ -1,16 +1,11 @@
1
- /*
2
- * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
- */
4
-
5
- import 'reflect-metadata'
6
- import './typings'
7
-
8
- export * from './interface'
9
- export * from './structures'
10
- export * from './error'
11
- export * from './command'
12
- export * from './listener'
13
- export * from './builtinModules/BuiltInModule'
14
- export * from './applicationCommand'
15
- export * from './messageComponents'
16
- export * from './error'
1
+ /*
2
+ * File: index.ts
3
+ *
4
+ * Copyright (c) 2022-2022 pikokr
5
+ *
6
+ * Licensed under MIT License. Please see more defails in LICENSE file.
7
+ */
8
+
9
+ export * from './core'
10
+ export * from './applicationCommand'
11
+ export * from './textCommand'
@@ -0,0 +1,20 @@
1
+ /*
2
+ * File: TextCommand.ts
3
+ *
4
+ * Copyright (c) 2022-2022 pikokr
5
+ *
6
+ * Licensed under MIT License. Please see more defails in LICENSE file.
7
+ */
8
+
9
+ import { createComponentDecorator } from '../core/components/decoratorCreator'
10
+ import { BaseComponent } from '../core/components/BaseComponent'
11
+
12
+ type TextCommandOptions = {
13
+ name: string
14
+ aliases?: string
15
+ description?: string
16
+ }
17
+
18
+ export class TextCommandComponent extends BaseComponent<TextCommandOptions> {}
19
+
20
+ export const command = createComponentDecorator(TextCommandComponent)
@@ -0,0 +1,128 @@
1
+ /*
2
+ * File: TextCommandExtension.ts
3
+ *
4
+ * Copyright (c) 2022-2022 pikokr
5
+ *
6
+ * Licensed under MIT License. Please see more defails in LICENSE file.
7
+ */
8
+
9
+ import { listener } from '../core/listener'
10
+ import { Message } from 'discord.js'
11
+ import { CTSExtension } from '../core/extensions/CTSExtension'
12
+ import { TextCommandComponent } from './TextCommand'
13
+ import { TextCommandRestOption } from './parameters'
14
+ import { argConverter } from '../core'
15
+
16
+ export type TextCommandConfig = {
17
+ prefix: string | string[] | ((msg: Message) => Promise<string | string[]> | string | string[])
18
+ }
19
+
20
+ export class TextCommandExtension extends CTSExtension {
21
+ constructor(private config: TextCommandConfig) {
22
+ super()
23
+ }
24
+
25
+ private async processPrefix(msg: Message): Promise<number | null> {
26
+ const content = msg.content
27
+ let prefix = this.config.prefix
28
+
29
+ if (typeof prefix === 'function') {
30
+ prefix = await prefix(msg)
31
+ }
32
+
33
+ if (typeof prefix === 'string') {
34
+ if (content.startsWith(prefix)) return prefix.length
35
+ return null
36
+ }
37
+
38
+ if (prefix instanceof Array) {
39
+ const p = prefix.find((x) => content.startsWith(x))
40
+
41
+ if (p) return p.length
42
+ return null
43
+ }
44
+
45
+ return null
46
+ }
47
+
48
+ @listener({ event: 'messageCreate', emitter: 'discord' })
49
+ private async messageCreate(msg: Message) {
50
+ const startIndex = await this.processPrefix(msg)
51
+
52
+ if (!startIndex) return
53
+
54
+ const content = msg.content.slice(startIndex)
55
+
56
+ const commands: TextCommandComponent[] = []
57
+
58
+ const extensions = new Map<TextCommandComponent, object>()
59
+
60
+ for (const ext of this.commandClient.registry.extensions) {
61
+ for (const cmd of this.commandClient.registry.getComponentsWithType(ext, TextCommandComponent)) {
62
+ commands.push(cmd)
63
+ extensions.set(cmd, ext)
64
+ }
65
+ }
66
+
67
+ let commandNameLength = 0
68
+
69
+ const command = commands.find((x) => {
70
+ const names = [x.options.name]
71
+
72
+ if (x.options.aliases) {
73
+ names.push(...x.options.aliases)
74
+ }
75
+
76
+ for (const name of names) {
77
+ if (content.startsWith(name)) {
78
+ if (content.length === name.length) {
79
+ commandNameLength = name.length
80
+ return true
81
+ }
82
+ commandNameLength = name.length
83
+ return content.startsWith(name + ' ')
84
+ }
85
+ }
86
+
87
+ return false
88
+ })
89
+
90
+ if (!command) return
91
+
92
+ const ext = extensions.get(command)
93
+
94
+ if (!ext) return
95
+
96
+ const args: unknown[] = []
97
+
98
+ let argStrings = content.slice(commandNameLength + 1).split(/ /g)
99
+
100
+ await this.convertArguments(TextCommandComponent, args, command.argTypes, async (arg, i, converter) => {
101
+ if (converter.options.parameterless) return [msg]
102
+
103
+ if (arg.decorators.find((x) => x.constructor === TextCommandRestOption)) {
104
+ const text = argStrings.join(' ')
105
+ argStrings = []
106
+ return [text, msg]
107
+ }
108
+ return [argStrings.shift(), msg]
109
+ })
110
+
111
+ await command.execute(ext, args, [msg])
112
+ }
113
+
114
+ @argConverter({ component: TextCommandComponent, type: Message, parameterless: true })
115
+ async mesage(msg: Message) {
116
+ return msg
117
+ }
118
+
119
+ @argConverter({ component: TextCommandComponent, type: String })
120
+ async str(value: string) {
121
+ return value
122
+ }
123
+
124
+ @argConverter({ component: TextCommandComponent, type: Number })
125
+ async num(value: string) {
126
+ return Number(value)
127
+ }
128
+ }
@@ -0,0 +1,11 @@
1
+ /*
2
+ * File: index.ts
3
+ *
4
+ * Copyright (c) 2022-2022 pikokr
5
+ *
6
+ * Licensed under MIT License. Please see more defails in LICENSE file.
7
+ */
8
+
9
+ export * from './TextCommand'
10
+ export type { TextCommandConfig } from './TextCommandExtension'
11
+ export * from './parameters'
@@ -0,0 +1,14 @@
1
+ /*
2
+ * File: parameters.ts
3
+ *
4
+ * Copyright (c) 2022-2022 pikokr
5
+ *
6
+ * Licensed under MIT License. Please see more defails in LICENSE file.
7
+ */
8
+
9
+ import { ComponentArgumentDecorator } from '../core'
10
+ import { createArgumentDecorator } from '../core'
11
+
12
+ export class TextCommandRestOption extends ComponentArgumentDecorator<void> {}
13
+
14
+ export const rest = createArgumentDecorator(TextCommandRestOption)
package/test/index.ts CHANGED
@@ -1,28 +1,93 @@
1
1
  /*
2
- * Copyright (c) 2022 pikokr. Licensed under the MIT license
2
+ * File: index.ts
3
+ *
4
+ * Copyright (c) 2022-2022 pikokr
5
+ *
6
+ * Licensed under MIT License. Please see more defails in LICENSE file.
3
7
  */
4
8
 
5
- import { Client, Intents, IntentsString } from 'discord.js'
6
- import { CommandClient } from '../dist'
9
+ import { ApplicationCommandOptionType, ApplicationCommandType, ChatInputCommandInteraction, Client, Message } from 'discord.js'
10
+ import { applicationCommand, CommandClient, moduleHook, option, ownerOnly, listener, Extension, command, rest } from '../src'
11
+ import 'dotenv/config'
12
+ import { Logger } from 'tslog'
13
+ import chalk from 'chalk'
7
14
 
8
- const config = require('./config.json')
15
+ class Test extends Extension {
16
+ @applicationCommand({
17
+ type: ApplicationCommandType.ChatInput,
18
+ name: 'test',
19
+ description: 'wow this is test',
20
+ })
21
+ async testCommand(i: ChatInputCommandInteraction) {
22
+ i.reply('Wow')
23
+ }
9
24
 
10
- const client = new Client({
11
- intents: ['GUILD_MESSAGES','GUILDS','DIRECT_MESSAGES'],
12
- })
25
+ @ownerOnly
26
+ @applicationCommand({
27
+ type: ApplicationCommandType.ChatInput,
28
+ name: 'test2',
29
+ description: 'wow this is test wow',
30
+ })
31
+ async test2(
32
+ @option({
33
+ name: 'sans',
34
+ description: '와',
35
+ type: ApplicationCommandOptionType.String,
36
+ required: true,
37
+ })
38
+ wa: string,
39
+ i: ChatInputCommandInteraction,
40
+ ) {
41
+ i.reply(wa)
42
+ }
13
43
 
14
- const cts = new CommandClient({
15
- owners: 'auto',
16
- client,
17
- command: {
18
- prefix: '!',
19
- },
20
- applicationCommands: {
21
- autoSync: true,
22
- guild: '969907569592250449',
23
- },
24
- })
44
+ @moduleHook('load')
45
+ load() {
46
+ this.logger.info('Load')
47
+ }
25
48
 
26
- cts.registry.loadModulesIn('modules')
49
+ @moduleHook('unload')
50
+ unload() {
51
+ this.logger.info('Unload')
52
+ }
27
53
 
28
- client.login(config.token)
54
+ @listener({ event: 'ready' })
55
+ async testEvent() {
56
+ this.logger.info(`Login: ${chalk.green(client.user!.tag)}`)
57
+ await this.commandClient.fetchOwners()
58
+ }
59
+
60
+ @command({ name: 'test' })
61
+ async test(msg: Message, @rest() sans: string) {
62
+ console.log(sans)
63
+ await msg.reply('Wow')
64
+ }
65
+ }
66
+
67
+ const ext = new Test()
68
+
69
+ const client = new Client({ intents: ['GuildMessages', 'MessageContent', 'DirectMessages', 'Guilds'] })
70
+
71
+ const logger = new Logger({ dateTimeTimezone: Intl.DateTimeFormat().resolvedOptions().timeZone })
72
+
73
+ const cc = new CommandClient(client, logger)
74
+
75
+ const registry = cc.registry
76
+
77
+ const run = async () => {
78
+ await cc.enableApplicationCommandsExtension({
79
+ guilds: ['832938554438844438'],
80
+ })
81
+
82
+ await cc.enableTextCommandsExtension({
83
+ prefix: '.',
84
+ })
85
+
86
+ await registry.registerModule(ext)
87
+
88
+ await client.login(process.env.TOKEN)
89
+
90
+ await cc.getApplicationCommandsExtension()!.sync()
91
+ }
92
+
93
+ run()
package/tsconfig.json CHANGED
@@ -15,11 +15,10 @@
15
15
  "declaration": true,
16
16
  /* Generates corresponding '.d.ts' file. */
17
17
  // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
18
- "sourceMap": true, /* Generates corresponding '.map' file. */
18
+ "sourceMap": true /* Generates corresponding '.map' file. */,
19
19
  // "outFile": "./", /* Concatenate and emit output to single file. */
20
20
  "outDir": "./dist",
21
21
  /* Redirect output structure to the directory. */
22
- "rootDir": "./src",
23
22
  /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
24
23
  // "composite": true, /* Enable project compilation */
25
24
  // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
@@ -31,6 +30,7 @@
31
30
 
32
31
  /* Strict Type-Checking Options */
33
32
  "strict": true,
33
+
34
34
  /* Enable all strict type-checking options. */
35
35
  // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
36
36
  // "strictNullChecks": true, /* Enable strict null checks. */
@@ -79,5 +79,5 @@
79
79
  "forceConsistentCasingInFileNames": true
80
80
  /* Disallow inconsistently-cased references to the same file. */
81
81
  },
82
- "exclude": ["test", "node_modules", "dist", "tsup.config.ts", "scripts"]
82
+ "exclude": ["node_modules"]
83
83
  }
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "exclude": ["node_modules", "scripts", "test", "tsup.config.ts"],
4
+ "include": ["src"],
5
+ "compilerOptions": {
6
+ "rootDir": "./src",
7
+ }
8
+ }
package/tsup.config.ts CHANGED
@@ -1,7 +1,11 @@
1
- /*
2
- * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
- */
4
-
1
+ /*
2
+ * File: tsup.config.ts
3
+ *
4
+ * Copyright (c) 2022-2022 pikokr
5
+ *
6
+ * Licensed under MIT License. Please see more defails in LICENSE file.
7
+ */
8
+
5
9
  import { defineConfig } from 'tsup'
6
10
 
7
11
  export default defineConfig({
@@ -11,4 +15,5 @@ export default defineConfig({
11
15
  clean: true,
12
16
  dts: true,
13
17
  minify: true,
18
+ tsconfig: 'tsconfig.prod.json',
14
19
  })
@@ -1,32 +0,0 @@
1
- /*
2
- * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
- */
4
-
5
- import { Module } from '../structures'
6
- import { ApplicationCommandDataResolvable, Snowflake } from 'discord.js'
7
- import { KApplicationCommandChecks } from '../constants'
8
- import { ApplicationCommandCheckFunction } from '../command'
9
-
10
- export type AppCommandArgument = {
11
- type: any
12
- name?: string
13
- }
14
-
15
- export class AppCommand {
16
- get checks(): ApplicationCommandCheckFunction[] {
17
- return Reflect.getMetadata(KApplicationCommandChecks, this.module, this.key) || []
18
- }
19
-
20
- execute(module: Module, args: any[]) {
21
- return this.run.apply(module, args)
22
- }
23
-
24
- constructor(
25
- public command: ApplicationCommandDataResolvable,
26
- private run: Function,
27
- public module: Module,
28
- public params: AppCommandArgument[],
29
- public guild: Snowflake | Snowflake[] | undefined,
30
- private key: string | symbol,
31
- ) {}
32
- }