cordo 2.16.5 → 2.17.0-rc1

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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Cordo is a developer-experience-first Discord App and UI library for TypeScript.
4
4
 
5
- Cordo is built for and used in production with over 700k discord servers at https://freestuffbot.xyz/.
5
+ Cordo is built for and used in production with over 900k discord servers at https://freestuffbot.xyz/.
6
6
 
7
7
 
8
8
  ## Concepts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cordo",
3
- "version": "2.16.5",
3
+ "version": "2.17.0-rc1",
4
4
  "description": "A framework for handling complex discord api interactions",
5
5
  "exports": {
6
6
  ".": "./src/index.ts",
@@ -38,27 +38,27 @@
38
38
  },
39
39
  "homepage": "https://github.com/maanex/cordo#readme",
40
40
  "dependencies": {
41
- "@types/bun": "^1.2.10",
41
+ "@types/bun": "^1.4.3",
42
42
  "axios": "^0.21.4",
43
- "defu": "^6.1.4",
43
+ "defu": "^6.1.7",
44
44
  "discord-interactions": "^2.4.1",
45
- "type-fest": "^4.34.1"
45
+ "type-fest": "^4.41.0"
46
46
  },
47
47
  "devDependencies": {
48
- "@types/axios": "^0.14.0",
49
- "@types/express": "^4.17.13",
50
- "@types/node": "^17.0.8",
51
- "@typescript-eslint/eslint-plugin": "^4.28.3",
52
- "@typescript-eslint/parser": "^4.28.3",
53
- "discord-api-types": "^0.38.53",
54
- "discord.js": "^14.18.0",
55
- "eslint": "^7.30.0",
56
- "eslint-config-maanex": "^1.1.2",
57
- "express": "^4.17.3",
58
- "hono": "^4.7.7",
59
- "reflect-metadata": "^0.1.13",
60
- "typescript": "^5.7.3",
48
+ "@types/axios": "^0.14.4",
49
+ "@types/express": "^4.17.25",
50
+ "@types/node": "^17.0.45",
51
+ "@typescript-eslint/eslint-plugin": "^4.33.0",
52
+ "@typescript-eslint/parser": "^4.33.0",
53
+ "discord-api-types": "^0.38.57",
54
+ "discord.js": "^14.27.0",
55
+ "eslint": "^7.32.0",
56
+ "eslint-config-maanex": "^1.3.3",
57
+ "express": "^4.22.3",
58
+ "hono": "^4.13.13",
59
+ "reflect-metadata": "^0.1.14",
60
+ "typescript": "^5.9.3",
61
61
  "vitepress": "^1.6.4",
62
- "vue": "^3.5.41"
62
+ "vue": "^3.5.44"
63
63
  }
64
64
  }
@@ -0,0 +1,29 @@
1
+ import type { LocalizedString } from "../../lib/localization"
2
+ import type { CordoInteraction } from "../interaction"
3
+
4
+
5
+ const CordoAutocompleteSymbol = Symbol.for('CordoAutocomplete')
6
+
7
+ type AutocompleteHandler<ReturnType extends string | number> = (
8
+ value: ReturnType,
9
+ i: CordoInteraction,
10
+ ) => Array<{
11
+ name: string | LocalizedString
12
+ value: ReturnType
13
+ }>
14
+
15
+ export type CordoAutocomplete<ReturnType extends string | number> = {
16
+ [CordoAutocompleteSymbol]: AutocompleteHandler<ReturnType>
17
+ }
18
+
19
+ export function defineCordoAutocomplete<ReturnType extends string | number>(handler: AutocompleteHandler<ReturnType>): CordoAutocomplete<ReturnType> {
20
+ return {
21
+ [CordoAutocompleteSymbol]: handler
22
+ }
23
+ }
24
+
25
+ export namespace AutocompleteInternals {
26
+ export function isAutocomplete(obj: any): obj is CordoAutocomplete<any> {
27
+ return obj && typeof obj === 'object' && CordoAutocompleteSymbol in obj
28
+ }
29
+ }
@@ -8,8 +8,14 @@ import { CordoMagic } from '../magic'
8
8
  import type { CordoInteraction } from '../interaction'
9
9
  import { LockfileInternals } from "./lockfile"
10
10
  import type { CordoRoute } from './route'
11
+ import type { CordoAutocomplete } from "./command-autocomplete"
11
12
 
12
13
 
14
+ type CommandChoices<T extends string | number> = Array<{
15
+ name: string | LocalizedString
16
+ value: T
17
+ }> | CordoAutocomplete<T>
18
+
13
19
  type CommandOption = {
14
20
  name: string | LocalizedString
15
21
  description: string | LocalizedString
@@ -18,19 +24,13 @@ type CommandOption = {
18
24
  type: 'string'
19
25
  minLength?: number
20
26
  maxLength?: number
21
- choices?: Array<{
22
- name: string | LocalizedString
23
- value: string
24
- }>
27
+ choices?: CommandChoices<string>
25
28
  } | {
26
29
  type: 'integer'
27
30
  minValue?: number
28
31
  maxValue?: number
29
32
  autocomplete?: boolean
30
- choices?: Array<{
31
- name: string | LocalizedString
32
- value: number
33
- }>
33
+ choices?: CommandChoices<number>
34
34
  } | {
35
35
  type: 'boolean'
36
36
  } | {
@@ -47,10 +47,7 @@ type CommandOption = {
47
47
  minValue?: number
48
48
  maxValue?: number
49
49
  autocomplete?: boolean
50
- choices?: Array<{
51
- name: string | LocalizedString
52
- value: number
53
- }>
50
+ choices?: CommandChoices<number>
54
51
  } | {
55
52
  type: 'attachment'
56
53
  fileTypes?: Array<'image' | 'video' | 'audio' | `.${string}`>
@@ -3,6 +3,7 @@ import { LibIds } from '../../lib/ids'
3
3
  import type { RouteInternals } from './route'
4
4
  import type { ErrorBoundaryInternals } from './error-boundary'
5
5
  import type { CordoCommand } from './command'
6
+ import type { CordoAutocomplete } from './command-autocomplete'
6
7
 
7
8
 
8
9
  export namespace LockfileInternals {
@@ -20,6 +21,7 @@ export namespace LockfileInternals {
20
21
  routeImpls: Map<string, RouteInternals.ParsedRoute>
21
22
  errorBoundaries: Array<ErrorBoundaryInternals.ParsedBoundary>
22
23
  registeredCommands: Map<string, CordoCommand>
24
+ registeredCommandAutocompletes: WeakMap<CordoCommand, Map<string, CordoAutocomplete<any>>>
23
25
  }
24
26
  }
25
27
 
@@ -39,7 +41,8 @@ export namespace LockfileInternals {
39
41
  $runtime: {
40
42
  routeImpls: new Map(),
41
43
  errorBoundaries: [],
42
- registeredCommands: new Map()
44
+ registeredCommands: new Map(),
45
+ registeredCommandAutocompletes: new WeakMap()
43
46
  }
44
47
  }
45
48
  }
@@ -1,4 +1,4 @@
1
- import { ApplicationCommandOptionType, ApplicationCommandType, ComponentType, InteractionResponseType, InteractionType, type APIApplicationCommand, type APIInteraction } from "discord-api-types/v10"
1
+ import { ComponentType, InteractionResponseType, InteractionType, type APIApplicationCommand, type APIInteraction } from "discord-api-types/v10"
2
2
  import type { Method } from "axios"
3
3
  import axios from "axios"
4
4
  import { FunctCompiler } from "../functions/compiler"
@@ -154,44 +154,23 @@ export namespace CordoGateway {
154
154
 
155
155
  if (i.type === InteractionType.ApplicationCommand)
156
156
  return handleCommandInteraction(i)
157
- else if (i.type === InteractionType.MessageComponent)
157
+ else if (i.type === InteractionType.MessageComponent)
158
158
  return handleComponentInteraction(i)
159
- else if (i.type === InteractionType.ModalSubmit)
159
+ else if (i.type === InteractionType.ModalSubmit)
160
160
  return handleModalInteraction(i)
161
+ else if (i.type === InteractionType.ApplicationCommandAutocomplete)
162
+ return handleAutocompleteInteraction(i)
161
163
  }
162
164
 
163
165
  function handleCommandInteraction(i: CordoInteraction & { type: InteractionType.ApplicationCommand }) {
164
- // slash commands: need to check for subcommands
165
- if (i.data.type === ApplicationCommandType.ChatInput) {
166
- let name = i.data.name
167
- let option = i.data.options?.[0]
168
- while (option?.type === ApplicationCommandOptionType.Subcommand || option?.type === ApplicationCommandOptionType.SubcommandGroup) {
169
- name += ` ${option.name}`
170
- option = option.options?.[0]
171
- }
172
-
173
- const { route, path } = RoutingResolve.getRouteForCommand(name, 'slash')
174
-
175
- CordoMagic.setCwd(path)
176
- if (route.routeFilePath && CordoMagic.getLockfile()?.$runtime.registeredCommands.has(route.routeFilePath))
177
- InteractionInternals.get(i).commandEntrypoint = CordoMagic.getLockfile()!.$runtime.registeredCommands.get(route.routeFilePath)!
178
-
179
- return RoutingRespond.callRoute(route.routeId, route.args, i)
180
- }
181
-
182
- // message/user commands: go ahead
183
- if (i.data.type === ApplicationCommandType.Message || i.data.type === ApplicationCommandType.User) {
184
- const { route, path } = RoutingResolve.getRouteForCommand(
185
- i.data.name,
186
- i.data.type === ApplicationCommandType.Message ? 'message' : 'user'
187
- )
188
-
189
- CordoMagic.setCwd(path)
190
- if (route.routeFilePath && CordoMagic.getLockfile()?.$runtime.registeredCommands.has(route.routeFilePath))
191
- InteractionInternals.get(i).commandEntrypoint = CordoMagic.getLockfile()!.$runtime.registeredCommands.get(route.routeFilePath)!
192
-
193
- return RoutingRespond.callRoute(route.routeId, route.args, i)
194
- }
166
+ const res = RoutingResolve.getRouteForCommandObject(i.data)
167
+ if (!res)
168
+ return
169
+ CordoMagic.setCwd(res.path)
170
+ if (res.route.routeFilePath && CordoMagic.getLockfile()?.$runtime.registeredCommands.has(res.route.routeFilePath))
171
+ InteractionInternals.get(i).commandEntrypoint = CordoMagic.getLockfile()!.$runtime.registeredCommands.get(res.route.routeFilePath)!
172
+
173
+ return RoutingRespond.callRoute(res.route.routeId, res.route.args, i)
195
174
  }
196
175
 
197
176
  async function handleComponentInteraction(i: CordoInteraction & { type: InteractionType.MessageComponent }) {
@@ -243,6 +222,29 @@ export namespace CordoGateway {
243
222
  }
244
223
  }
245
224
 
225
+ function handleAutocompleteInteraction(i: CordoInteraction & { type: InteractionType.ApplicationCommandAutocomplete }) {
226
+ const res = RoutingResolve.getRouteForCommandObject(i.data)
227
+ if (!res)
228
+ return
229
+
230
+ const command = CordoMagic.getLockfile()?.$runtime.registeredCommands.get(res.route.routeFilePath ?? res.path)
231
+ if (!command)
232
+ return
233
+
234
+ const commandAutocompletes = CordoMagic.getLockfile()?.$runtime.registeredCommandAutocompletes.get(command)
235
+ if (!commandAutocompletes)
236
+ return
237
+
238
+ // @ts-ignore for now
239
+ const focusedOption = i.data.options?.find(opt => opt.focused)
240
+ if (!focusedOption)
241
+ return
242
+
243
+ const handler = commandAutocompletes.get(focusedOption.name)
244
+ // @ts-ignore for now
245
+ return handler?.(focusedOption.value, i)
246
+ }
247
+
246
248
  //
247
249
 
248
250
  async function parseAndEvokeModalLeafComponents(c: any, i: CordoInteraction, collectedFormItems: Map<string, unknown>): Promise<boolean> {
package/src/core/index.ts CHANGED
@@ -14,6 +14,7 @@ export { type DynamicTypes } from './dynamic-types'
14
14
  export { type CordoConfig, defineCordoConfig } from './files/config'
15
15
  export { type CordoRoute, type RouteRequest, defineCordoRoute, assertCordoRequest } from './files/route'
16
16
  export { type CordoCommand, defineCordoCommand } from './files/command'
17
+ export { type CordoAutocomplete, defineCordoAutocomplete } from './files/command-autocomplete'
17
18
  export { type CordoErrorBoundary, defineCordoErrorBoundary } from './files/error-boundary'
18
19
  export { type CordoInteraction } from './interaction'
19
20
 
@@ -6,6 +6,7 @@ import { LockfileInternals } from "../files/lockfile"
6
6
  import { LibIds } from "../../lib/ids"
7
7
  import { CommandInternals, type CordoCommand } from "../files/command"
8
8
  import type { ParsedCordoConfig } from "../files/config"
9
+ import { AutocompleteInternals, type CordoAutocomplete } from "../files/command-autocomplete"
9
10
 
10
11
 
11
12
  export namespace RoutingFilesystem {
@@ -141,6 +142,17 @@ export namespace RoutingFilesystem {
141
142
  for (const file of files.commands) {
142
143
  const filePath = file.path.join('/')
143
144
  lockfile.$runtime.registeredCommands.set(filePath, file.command)
145
+
146
+ const autocompleteOptions = file.command.options
147
+ ?.filter(opt => 'choices' in opt && AutocompleteInternals.isAutocomplete(opt.choices))
148
+ if (autocompleteOptions?.length) {
149
+ const map = new Map<string, CordoAutocomplete<any>>()
150
+ for (const option of autocompleteOptions) {
151
+ // @ts-ignore we know choices are there and are autocomplete!
152
+ map.set(option.name, option.choices)
153
+ }
154
+ lockfile.$runtime.registeredCommandAutocompletes.set(file.command, map)
155
+ }
144
156
  }
145
157
 
146
158
  return out
@@ -1,3 +1,4 @@
1
+ import { ApplicationCommandOptionType, ApplicationCommandType, InteractionType } from "discord.js"
1
2
  import { MissingContextError } from "../../errors/builtin/missing-context"
2
3
  import { FunctCompiler } from "../../functions/compiler"
3
4
  import { Hooks } from "../hooks"
@@ -136,6 +137,25 @@ export namespace RoutingResolve {
136
137
  }
137
138
  }
138
139
 
140
+ export function getRouteForCommandObject(command: (CordoInteraction & { type: InteractionType.ApplicationCommand | InteractionType.ApplicationCommandAutocomplete })['data']) {
141
+ if (command.type === ApplicationCommandType.ChatInput) {
142
+ let name = command.name
143
+ let option = command.options?.[0]
144
+ while (option?.type === ApplicationCommandOptionType.Subcommand || option?.type === ApplicationCommandOptionType.SubcommandGroup) {
145
+ name += ` ${option.name}`
146
+ option = option.options?.[0]
147
+ }
148
+ return getRouteForCommand(name, 'slash')
149
+ }
150
+ if (command.type === ApplicationCommandType.Message || command.type === ApplicationCommandType.User) {
151
+ return getRouteForCommand(
152
+ command.name,
153
+ command.type === ApplicationCommandType.Message ? 'message' : 'user'
154
+ )
155
+ }
156
+ return null
157
+ }
158
+
139
159
  export function getRouteForCommand(command: string, type: 'slash' | 'message' | 'user') {
140
160
  const fileName = Hooks.isDefined('transformCommandName')
141
161
  ? Hooks.callHook('transformCommandName', command, { type })