@pikokr/command.ts 4.0.5 → 5.0.0-dev.645f87d

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/.github/workflows/codeql-analysis.yml +70 -0
  2. package/.github/workflows/docs.yml +1 -1
  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 +63 -310
  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 +14 -11
  13. package/publish-version.js +10 -0
  14. package/scripts/docs.ts +5 -1
  15. package/src/applicationCommand/ApplicationCommand.ts +17 -0
  16. package/src/applicationCommand/ApplicationCommandOption.ts +9 -0
  17. package/src/applicationCommand/index.ts +2 -6
  18. package/src/core/components/BaseComponent.ts +34 -0
  19. package/src/core/components/ComponentArgument.ts +7 -0
  20. package/src/core/components/ComponentArgumentDecorator.ts +17 -0
  21. package/src/core/components/decoratorCreator.ts +67 -0
  22. package/src/core/components/index.ts +3 -0
  23. package/src/core/hooks/index.ts +1 -0
  24. package/src/core/hooks/moduleHook.ts +31 -0
  25. package/src/core/index.ts +3 -0
  26. package/src/core/listener/index.ts +9 -0
  27. package/src/core/structures/CommandClient.ts +14 -0
  28. package/src/core/structures/Registry.ts +86 -0
  29. package/src/core/structures/index.ts +2 -0
  30. package/src/core/symbols.ts +4 -0
  31. package/src/index.ts +6 -12
  32. package/test/index.ts +62 -23
  33. package/tsconfig.json +3 -2
  34. package/tsconfig.prod.json +4 -0
  35. package/tsup.config.ts +6 -1
  36. package/src/applicationCommand/AppCommand.ts +0 -32
  37. package/src/applicationCommand/decorator.ts +0 -62
  38. package/src/builtinModules/BuiltInModule.ts +0 -13
  39. package/src/builtinModules/BuiltinApplicationCommandConverters.ts +0 -16
  40. package/src/builtinModules/BuiltinCommandConverters.ts +0 -87
  41. package/src/builtinModules/CommandHandler.ts +0 -363
  42. package/src/builtinModules/index.ts +0 -7
  43. package/src/command/ArgumentConverter.ts +0 -22
  44. package/src/command/Command.ts +0 -35
  45. package/src/command/cooldown/adapter.ts +0 -22
  46. package/src/command/cooldown/decorator.ts +0 -67
  47. package/src/command/cooldown/error.ts +0 -9
  48. package/src/command/cooldown/index.ts +0 -9
  49. package/src/command/cooldown/type.ts +0 -12
  50. package/src/command/decorator.ts +0 -185
  51. package/src/command/index.ts +0 -9
  52. package/src/command/utils.ts +0 -33
  53. package/src/constants.ts +0 -31
  54. package/src/error/ArgumentConverterNotFound.ts +0 -18
  55. package/src/error/ArgumentNotProvided.ts +0 -12
  56. package/src/error/CommandCheckFailed.ts +0 -19
  57. package/src/error/CommandNotFound.ts +0 -11
  58. package/src/error/InvalidTargetError.ts +0 -9
  59. package/src/error/ModuleError.ts +0 -11
  60. package/src/error/PermissionRequired.ts +0 -17
  61. package/src/error/checks/DMOnlyCommand.ts +0 -9
  62. package/src/error/checks/GuildOnlyCommand.ts +0 -9
  63. package/src/error/checks/OwnerOnlyCommand.ts +0 -9
  64. package/src/error/checks/SlashCommandGlobalCheckError.ts +0 -11
  65. package/src/error/checks/index.ts +0 -8
  66. package/src/error/index.ts +0 -12
  67. package/src/interface/index.ts +0 -7
  68. package/src/listener/Listener.ts +0 -7
  69. package/src/listener/decorator.ts +0 -29
  70. package/src/listener/index.ts +0 -6
  71. package/src/messageComponents/base.ts +0 -16
  72. package/src/messageComponents/button.ts +0 -30
  73. package/src/messageComponents/index.ts +0 -6
  74. package/src/messageComponents/selectMenu.ts +0 -30
  75. package/src/structures/CommandClient.ts +0 -103
  76. package/src/structures/Module.ts +0 -54
  77. package/src/structures/Registry.ts +0 -245
  78. package/src/structures/index.ts +0 -7
  79. package/src/typings.ts +0 -35
  80. package/src/utils.ts +0 -10
  81. package/test/config.example.json +0 -3
  82. package/test/modules/dev.ts +0 -45
  83. package/test/modules/test.ts +0 -148
@@ -0,0 +1,2 @@
1
+ export * from './Registry'
2
+ export * from './CommandClient'
@@ -0,0 +1,4 @@
1
+ export const ComponentStoreSymbol = Symbol()
2
+ export const ComponentArgStoreSymbol = Symbol()
3
+ export const ModuleHookStoreSymbol = Symbol()
4
+ export const ListenersSymbol = Symbol()
package/src/index.ts CHANGED
@@ -1,16 +1,10 @@
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 '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'
9
+ export * from './core'
14
10
  export * from './applicationCommand'
15
- export * from './messageComponents'
16
- export * from './error'
package/test/index.ts CHANGED
@@ -1,30 +1,69 @@
1
- /*
2
- * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
- */
1
+ import { ApplicationCommandOptionType, Client } from 'discord.js'
2
+ import { applicationCommand, ApplicationCommandComponent, CommandClient, moduleHook, option, Registry } from '../src'
3
+ import { listener } from '../src/core/listener'
4
4
 
5
- import { Client, Intents, IntentsString } from 'discord.js'
6
- import { CommandClient } from '../src'
5
+ class Test {
6
+ @applicationCommand({
7
+ name: 'test',
8
+ description: 'wow this is test',
9
+ })
10
+ async testCommand(
11
+ @option({
12
+ name: 'hello',
13
+ description: '와아',
14
+ type: ApplicationCommandOptionType.String,
15
+ })
16
+ hello: string,
17
+ world: string,
18
+ ) {}
7
19
 
8
- const config = require('./config.json')
20
+ @applicationCommand({
21
+ name: 'test2',
22
+ description: 'wow this is test wow',
23
+ })
24
+ async test2(
25
+ @option({
26
+ name: 'sans',
27
+ description: '와',
28
+ type: ApplicationCommandOptionType.String,
29
+ })
30
+ wa: string,
31
+ ) {}
9
32
 
10
- const client = new Client({
11
- intents: Object.keys(Intents.FLAGS) as IntentsString[],
12
- })
33
+ @moduleHook('load')
34
+ load() {
35
+ console.log('load')
36
+ }
13
37
 
14
- const cts = new CommandClient({
15
- owners: 'auto',
16
- client,
17
- command: {
18
- prefix: '!',
19
- },
20
- applicationCommands: {
21
- autoSync: true,
22
- guild: '832938554438844438',
23
- },
24
- })
38
+ @moduleHook('unload')
39
+ unload() {
40
+ console.log('unload')
41
+ }
25
42
 
26
- require('./modules/test')
43
+ @listener({ event: 'test', emitter: 'discord' })
44
+ testEvent() {
45
+ console.log('test')
46
+ }
47
+ }
27
48
 
28
- cts.registry.loadModulesIn('modules')
49
+ const ext = new Test()
29
50
 
30
- client.login(config.token)
51
+ const client = new Client({ intents: [] })
52
+
53
+ const cc = new CommandClient(client)
54
+
55
+ const registry = cc.registry
56
+
57
+ const run = async () => {
58
+ await registry.registerModule(ext)
59
+
60
+ // listener test
61
+ client.emit('test')
62
+
63
+ await registry.unregisterModule(ext)
64
+
65
+ // shold not work
66
+ client.emit('test')
67
+ }
68
+
69
+ run()
package/tsconfig.json CHANGED
@@ -15,7 +15,7 @@
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. */
@@ -31,6 +31,7 @@
31
31
 
32
32
  /* Strict Type-Checking Options */
33
33
  "strict": true,
34
+
34
35
  /* Enable all strict type-checking options. */
35
36
  // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
36
37
  // "strictNullChecks": true, /* Enable strict null checks. */
@@ -79,5 +80,5 @@
79
80
  "forceConsistentCasingInFileNames": true
80
81
  /* Disallow inconsistently-cased references to the same file. */
81
82
  },
82
- "exclude": ["test", "node_modules", "dist", "tsup.config.ts", "scripts"]
83
+ "exclude": ["node_modules"]
83
84
  }
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "exclude": ["node_modules", "scripts", "test", "tsup.config.ts"]
4
+ }
package/tsup.config.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  /*
2
- * Copyright (c) 2022 pikokr. Licensed under the MIT license
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.
3
7
  */
4
8
 
5
9
  import { defineConfig } from 'tsup'
@@ -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
- }
@@ -1,62 +0,0 @@
1
- /*
2
- * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
- */
4
-
5
- import { ApplicationCommandDataResolvable, Collection, Snowflake } from 'discord.js'
6
- import { checkTarget } from '../utils'
7
- import { KSlashCommandOptions, KApplicationCommands } from '../constants'
8
- import { Module } from '../structures'
9
- import { AppCommand } from './AppCommand'
10
-
11
- type ApplicationCommandOptions = {
12
- guild: Snowflake | Snowflake[]
13
- optionTypes?: any[]
14
- }
15
-
16
- export const applicationCommand = (opt: Partial<ApplicationCommandOptions> & { command: ApplicationCommandDataResolvable }) => {
17
- return (
18
- target: Object,
19
- propertyKey: string,
20
- // descriptor: TypedPropertyDescriptor<any>,
21
- ) => {
22
- checkTarget(target)
23
-
24
- let properties: AppCommand[] = Reflect.getMetadata(KApplicationCommands, target)
25
-
26
- const params: any[] = opt.optionTypes ?? Reflect.getMetadata('design:paramtypes', target, propertyKey)
27
-
28
- const options: Collection<number, string> = Reflect.getMetadata(KSlashCommandOptions, target, propertyKey) || new Collection<number, string>()
29
-
30
- const command = new AppCommand(
31
- opt.command,
32
- Reflect.get(target, propertyKey),
33
- target as Module,
34
- params.map((x, i) => ({
35
- type: x,
36
- name: options.get(i),
37
- })),
38
- opt.guild,
39
- propertyKey,
40
- )
41
-
42
- if (properties) {
43
- properties.push(command)
44
- } else {
45
- properties = [command]
46
- Reflect.defineMetadata(KApplicationCommands, properties, target)
47
- }
48
- }
49
- }
50
-
51
- export const option = (key: string): ParameterDecorator => (target, propertyKey, parameterIndex) => {
52
- checkTarget(target)
53
-
54
- let properties: Collection<number, string> = Reflect.getMetadata(KSlashCommandOptions, target, propertyKey)
55
-
56
- if (!properties) {
57
- properties = new Collection<number, string>()
58
- Reflect.defineMetadata(KSlashCommandOptions, properties, target, propertyKey)
59
- }
60
-
61
- properties.set(parameterIndex, key)
62
- }
@@ -1,13 +0,0 @@
1
- /*
2
- * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
- */
4
-
5
- import { Module } from '../structures'
6
- import { KBuiltInModule } from '../constants'
7
-
8
- export class BuiltInModule extends Module {
9
- constructor() {
10
- super()
11
- Reflect.defineMetadata(KBuiltInModule, true, this)
12
- }
13
- }
@@ -1,16 +0,0 @@
1
- /*
2
- * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
- */
4
-
5
- import { BuiltInModule } from './BuiltInModule'
6
- import { Client } from 'discord.js'
7
- import { CommandClient } from '../structures'
8
-
9
- export class BuiltinApplicationCommandConverters extends BuiltInModule {
10
- client: Client
11
-
12
- constructor(private cts: CommandClient) {
13
- super()
14
- this.client = cts.client
15
- }
16
- }
@@ -1,87 +0,0 @@
1
- /*
2
- * Copyright (c) 2022 pikokr. Licensed under the MIT license
3
- */
4
-
5
- import { BuiltInModule } from './BuiltInModule'
6
- import { argumentConverter } from '../command'
7
- import { Client, GuildMember, Message, User, Role } from 'discord.js'
8
- import { CommandClient } from '../structures'
9
-
10
- export class BuiltinCommandConverters extends BuiltInModule {
11
- client: Client
12
-
13
- constructor(private cts: CommandClient) {
14
- super()
15
- this.client = cts.client
16
- }
17
-
18
- @argumentConverter(Message, false)
19
- message(msg: Message) {
20
- return msg
21
- }
22
-
23
- @argumentConverter(String)
24
- string(msg: Message, arg: string) {
25
- return arg
26
- }
27
-
28
- getUserIDByMention(mention: string): `${bigint}` | undefined {
29
- if (!mention) return
30
- if (mention.startsWith('<@') && mention.endsWith('>')) {
31
- mention = mention.slice(2, -1)
32
- if (mention.startsWith('!')) {
33
- mention = mention.slice(1)
34
- }
35
- return mention as `${bigint}`
36
- }
37
- }
38
-
39
- @argumentConverter(User)
40
- user(msg: Message, value: string): User | null {
41
- let user = this.client.users.cache.get(value)
42
- if (user) return user
43
- user = this.client.users.cache.find((x) => x.tag === value || x.username === value)
44
- if (user) return user
45
- const id = this.getUserIDByMention(value)
46
- if (!id) return null
47
- user = this.client.users.cache.get(id)
48
- return user || null
49
- }
50
-
51
- @argumentConverter(GuildMember)
52
- member(msg: Message, value: string): GuildMember | undefined {
53
- let user = msg.guild?.members.cache.get(value)
54
- if (!user) return user
55
- user = msg.guild?.members.cache.find((x) => x.user.tag === value)
56
- if (user) return user
57
- const id = this.getUserIDByMention(value)
58
- if (!id) return
59
- user = msg.guild?.members.cache.get(id)
60
- return user || undefined
61
- }
62
-
63
- @argumentConverter(Number)
64
- number(msg: Message, value: string) {
65
- const n = Number(value)
66
- return isNaN(n) ? undefined : n
67
- }
68
-
69
- getRoleIDByMention(mention: string): `${bigint}` | undefined {
70
- if (!mention) return
71
- if (mention.startsWith('<@') && mention.endsWith('>')) {
72
- mention = mention.slice(2, -1)
73
- if (mention.startsWith('&')) {
74
- mention = mention.slice(1)
75
- }
76
- return mention as `${bigint}`
77
- }
78
- }
79
-
80
- @argumentConverter(Role)
81
- role(msg: Message, value: string): Role | undefined {
82
- const id = this.getRoleIDByMention(value)
83
- if (!id) return
84
- const role = msg.guild?.roles.cache.get(id)
85
- return role || undefined
86
- }
87
- }