cordo 2.17.0-rc1 → 2.18.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cordo",
3
- "version": "2.17.0-rc1",
3
+ "version": "2.18.0",
4
4
  "description": "A framework for handling complex discord api interactions",
5
5
  "exports": {
6
6
  ".": "./src/index.ts",
@@ -1,4 +1,4 @@
1
- import crypto from "node:crypto"
1
+ import crypto from 'node:crypto'
2
2
  import type { Permissions } from 'discord-api-types/globals'
3
3
  import { ChannelType, ApplicationCommandOptionType, ApplicationCommandType, type APIApplicationCommandOption } from 'discord-api-types/v10'
4
4
  import type { LocalizedString } from '../../lib/localization'
@@ -6,15 +6,24 @@ import { CordoGateway } from '../gateway'
6
6
  import { RoutingResolve } from '../routing/resolve'
7
7
  import { CordoMagic } from '../magic'
8
8
  import type { CordoInteraction } from '../interaction'
9
- import { LockfileInternals } from "./lockfile"
9
+ import { LockfileInternals } from './lockfile'
10
10
  import type { CordoRoute } from './route'
11
- import type { CordoAutocomplete } from "./command-autocomplete"
12
11
 
13
12
 
13
+ type MaybePromise<T> = T | Promise<T>
14
+
15
+ export type AutocompleteHandler<ReturnType extends string | number> = (
16
+ value: ReturnType,
17
+ i: CordoInteraction,
18
+ ) => MaybePromise<Array<{
19
+ name: string | LocalizedString
20
+ value: ReturnType
21
+ }>>
22
+
14
23
  type CommandChoices<T extends string | number> = Array<{
15
24
  name: string | LocalizedString
16
25
  value: T
17
- }> | CordoAutocomplete<T>
26
+ }> | AutocompleteHandler<T>
18
27
 
19
28
  type CommandOption = {
20
29
  name: string | LocalizedString
@@ -133,17 +142,19 @@ export namespace CommandInternals {
133
142
  description,
134
143
  description_localizations,
135
144
  required: opt.required,
136
- autocomplete: opt.autocomplete,
137
145
  min_value: opt.minValue,
138
146
  max_value: opt.maxValue,
139
147
  min_length: opt.minLength,
140
148
  max_length: opt.maxLength,
141
149
  file_types: opt.fileTypes,
142
150
  channel_types: opt.channelTypes,
143
- choices: opt.choices?.map((c: any) => {
144
- const { value: name, localizations: name_localizations } = resolveLocalizedText(c.name)
145
- return { name, name_localizations, value: c.value }
146
- })
151
+ choices: opt.choices && Array.isArray(opt.choices)
152
+ ? opt.choices.map((c: any) => {
153
+ const { value: name, localizations: name_localizations } = resolveLocalizedText(c.name)
154
+ return { name, name_localizations, value: c.value }
155
+ })
156
+ : undefined,
157
+ autocomplete: opt.choices && (typeof opt.choices === 'function')
147
158
  }
148
159
 
149
160
  return res
@@ -162,7 +173,7 @@ export namespace CommandInternals {
162
173
 
163
174
  const options: any[] = []
164
175
  if (isGroup) {
165
- for (const [ subName, subTree ] of tree.subcommands.entries())
176
+ for (const [ subName, subTree ] of tree.subcommands.entries())
166
177
  options.push(buildApiOption(subName, subTree))
167
178
  } else if (tree.command?.options) {
168
179
  options.push(...tree.command.options.map(mapCommandOptionToApiOption))
@@ -202,11 +213,11 @@ export namespace CommandInternals {
202
213
 
203
214
  for (let i = 0; i < parts.length; i++) {
204
215
  const part = parts[i]
205
- if (!currentLevel.has(part))
216
+ if (!currentLevel.has(part))
206
217
  currentLevel.set(part, { subcommands: new Map() })
207
218
 
208
219
  const node = currentLevel.get(part)!
209
- if (i === parts.length - 1)
220
+ if (i === parts.length - 1)
210
221
  node.command = command
211
222
 
212
223
  currentLevel = node.subcommands
@@ -231,7 +242,7 @@ export namespace CommandInternals {
231
242
  }
232
243
 
233
244
  const { value: description, localizations: descriptionLocalizations } = resolveLocalizedText(
234
- tree.command?.description,
245
+ tree.command?.description,
235
246
  tree.subcommands.size > 0 ? 'Command group' : 'No description provided'
236
247
  )
237
248
 
@@ -239,9 +250,9 @@ export namespace CommandInternals {
239
250
 
240
251
  const apiOptions: any[] = []
241
252
  if (tree.subcommands.size > 0) {
242
- for (const [ subName, subTree ] of tree.subcommands.entries())
253
+ for (const [ subName, subTree ] of tree.subcommands.entries())
243
254
  apiOptions.push(buildApiOption(subName, subTree))
244
-
255
+
245
256
  } else if (tree.command?.options) {
246
257
  apiOptions.push(...tree.command.options.map(mapCommandOptionToApiOption))
247
258
  }
@@ -271,9 +282,9 @@ export namespace CommandInternals {
271
282
  const maxRetries = options?.maxRetries ?? 3
272
283
 
273
284
  const hash = crypto.createHash('sha256').update(JSON.stringify(apiCommands)).digest('hex')
274
- if (CordoMagic.globalLockfile && CordoMagic.globalLockfile.reg.commandSyncHash === hash)
285
+ if (CordoMagic.globalLockfile && CordoMagic.globalLockfile.reg.commandSyncHash === hash)
275
286
  return
276
-
287
+
277
288
  console.log(`[Cordo.syncCommands] Syncing ${apiCommands.length} command(s) to Discord...`)
278
289
 
279
290
  let retries = 0
@@ -298,7 +309,7 @@ export namespace CommandInternals {
298
309
  await new Promise(resolve => setTimeout(resolve, backoffTime))
299
310
  continue
300
311
  } else {
301
- console.error(`Max retries reached for bulk command sync. Skipping.`)
312
+ console.error('Max retries reached for bulk command sync. Skipping.')
302
313
  break
303
314
  }
304
315
  }
@@ -2,8 +2,7 @@ import { promises as fs } from 'node:fs'
2
2
  import { LibIds } from '../../lib/ids'
3
3
  import type { RouteInternals } from './route'
4
4
  import type { ErrorBoundaryInternals } from './error-boundary'
5
- import type { CordoCommand } from './command'
6
- import type { CordoAutocomplete } from './command-autocomplete'
5
+ import type { AutocompleteHandler, CordoCommand } from './command'
7
6
 
8
7
 
9
8
  export namespace LockfileInternals {
@@ -21,7 +20,7 @@ export namespace LockfileInternals {
21
20
  routeImpls: Map<string, RouteInternals.ParsedRoute>
22
21
  errorBoundaries: Array<ErrorBoundaryInternals.ParsedBoundary>
23
22
  registeredCommands: Map<string, CordoCommand>
24
- registeredCommandAutocompletes: WeakMap<CordoCommand, Map<string, CordoAutocomplete<any>>>
23
+ registeredCommandAutocompletes: WeakMap<CordoCommand, Map<string, AutocompleteHandler<string | number>>>
25
24
  }
26
25
  }
27
26
 
@@ -146,7 +145,7 @@ export namespace LockfileInternals {
146
145
 
147
146
  const types = [
148
147
  'declare module "cordo" {',
149
- ` export type DynamicTypes = {`,
148
+ ' export type DynamicTypes = {',
150
149
  ` Route: ${routes.map(route => `\`${route}\``).join(' | ')}`,
151
150
  ' }',
152
151
  '}',
@@ -1,9 +1,9 @@
1
- import type { APIEntitlement, APIInteractionGuildMember, APIMessage, APIPartialChannel, APIPartialGuild, APIUser } from "discord-api-types/v10"
2
- import type { CordoInteraction } from "../interaction"
3
- import type { goto, run } from "../../functions"
4
- import type { CordoComponent } from "../../components/component"
5
- import type { CordoModifier } from "../../components/modifier"
6
- import { RouteAssumptionFailedError } from "../../errors"
1
+ import type { APIEntitlement, APIInteractionGuildMember, APIMessage, APIPartialChannel, APIPartialGuild, APIUser } from 'discord-api-types/v10'
2
+ import type { CordoInteraction } from '../interaction'
3
+ import type { goto, run } from '../../functions'
4
+ import type { CordoComponent } from '../../components/component'
5
+ import type { CordoModifier } from '../../components/modifier'
6
+ import { RouteAssumptionFailedError } from '../../errors'
7
7
 
8
8
 
9
9
  const CordoRouteSymbol = Symbol.for('CordoRoute')
@@ -1,16 +1,16 @@
1
- import { ComponentType, InteractionResponseType, InteractionType, type APIApplicationCommand, type APIInteraction } from "discord-api-types/v10"
2
- import type { Method } from "axios"
3
- import axios from "axios"
4
- import { FunctCompiler } from "../functions/compiler"
5
- import { FunctInternals } from "../functions/funct"
6
- import { MissingContextError } from "../errors/builtin/missing-context"
7
- import { InteractionInternals, type CordoInteraction } from "./interaction"
8
- import { CordoMagic } from "./magic"
9
- import type { LockfileInternals } from "./files/lockfile"
10
- import type { ParsedCordoConfig } from "./files/config"
11
- import { Hooks } from "./hooks"
12
- import { RoutingResolve } from "./routing/resolve"
13
- import { RoutingRespond } from "./routing/respond"
1
+ import { ComponentType, InteractionResponseType, InteractionType, type APIApplicationCommand, type APIInteraction } from 'discord-api-types/v10'
2
+ import type { Method } from 'axios'
3
+ import axios from 'axios'
4
+ import { FunctCompiler } from '../functions/compiler'
5
+ import { FunctInternals } from '../functions/funct'
6
+ import { MissingContextError } from '../errors/builtin/missing-context'
7
+ import { InteractionInternals, type CordoInteraction } from './interaction'
8
+ import { CordoMagic } from './magic'
9
+ import type { LockfileInternals } from './files/lockfile'
10
+ import type { ParsedCordoConfig } from './files/config'
11
+ import { Hooks } from './hooks'
12
+ import { RoutingResolve } from './routing/resolve'
13
+ import { RoutingRespond } from './routing/respond'
14
14
 
15
15
 
16
16
  export namespace CordoGateway {
@@ -122,7 +122,7 @@ export namespace CordoGateway {
122
122
  if (!config)
123
123
  throw new MissingContextError('Could not respond to interaction, no config found in context.')
124
124
  if (!config.client.id) {
125
- console.warn(`No client id provided in config. Cannot respond to interactions.`)
125
+ console.warn('No client id provided in config. Cannot respond to interactions.')
126
126
  return null
127
127
  }
128
128
 
@@ -152,11 +152,11 @@ export namespace CordoGateway {
152
152
  }, deferAfter)
153
153
  }
154
154
 
155
- if (i.type === InteractionType.ApplicationCommand)
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
161
  else if (i.type === InteractionType.ApplicationCommandAutocomplete)
162
162
  return handleAutocompleteInteraction(i)
@@ -222,11 +222,11 @@ export namespace CordoGateway {
222
222
  }
223
223
  }
224
224
 
225
- function handleAutocompleteInteraction(i: CordoInteraction & { type: InteractionType.ApplicationCommandAutocomplete }) {
225
+ async function handleAutocompleteInteraction(i: CordoInteraction & { type: InteractionType.ApplicationCommandAutocomplete }) {
226
226
  const res = RoutingResolve.getRouteForCommandObject(i.data)
227
227
  if (!res)
228
228
  return
229
-
229
+
230
230
  const command = CordoMagic.getLockfile()?.$runtime.registeredCommands.get(res.route.routeFilePath ?? res.path)
231
231
  if (!command)
232
232
  return
@@ -242,7 +242,16 @@ export namespace CordoGateway {
242
242
 
243
243
  const handler = commandAutocompletes.get(focusedOption.name)
244
244
  // @ts-ignore for now
245
- return handler?.(focusedOption.value, i)
245
+ const result = await handler?.(focusedOption.value, i)
246
+ return respondTo(i, {
247
+ type: InteractionResponseType.ApplicationCommandAutocompleteResult,
248
+ data: {
249
+ choices: result?.map(r => ({
250
+ name: typeof r.name === 'string' ? r.name : r.name[i.locale] ?? r.name['en-US'] ?? Object.values(r.name)[0],
251
+ value: r.value
252
+ })) ?? []
253
+ }
254
+ })
246
255
  }
247
256
 
248
257
  //
@@ -256,7 +265,7 @@ export namespace CordoGateway {
256
265
  }
257
266
  return true
258
267
  }
259
-
268
+
260
269
  if (c.type === ComponentType.Label) {
261
270
  const success = await parseAndEvokeModalLeafComponents(c.component, i, collectedFormItems)
262
271
  return success
@@ -268,7 +277,7 @@ export namespace CordoGateway {
268
277
 
269
278
  const formRef = parsedCustomId.values[0]
270
279
 
271
- //
280
+ //
272
281
  if ('values' in c && c.values.length > 0 && typeof c.values[0] === 'string') {
273
282
  if (executeAllowedComponentTypes.includes(c.type)) {
274
283
  const success = await parseAndEvokeComponentWithValues(c, i)
@@ -321,7 +330,7 @@ export namespace CordoGateway {
321
330
  ): Promise<boolean> {
322
331
  if (!executeAllowedComponentTypes.includes('type' in data ? data.type : data.component_type)) // just to be sure
323
332
  return true
324
-
333
+
325
334
  const option = FunctCompiler.parseCustomId(data.value)
326
335
  data.value = option.values[0]
327
336
 
package/src/core/hooks.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { CordoConfig } from "./files/config"
2
- import { CordoMagic } from "./magic"
1
+ import type { CordoConfig } from './files/config'
2
+ import { CordoMagic } from './magic'
3
3
 
4
4
 
5
5
  export namespace Hooks {
package/src/core/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { APIInteraction } from 'discord-api-types/v10'
2
2
  import defu from 'defu'
3
3
  import type { PartialDeep } from 'type-fest'
4
- import { HeadlessModeError, NotMountedError } from "../errors"
4
+ import { HeadlessModeError, NotMountedError } from '../errors'
5
5
  import { ConfigInternals, type CordoConfig, type ParsedCordoConfig } from './files/config'
6
6
  import { LockfileInternals } from './files/lockfile'
7
7
  import { CordoGateway } from './gateway'
@@ -14,7 +14,6 @@ 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'
18
17
  export { type CordoErrorBoundary, defineCordoErrorBoundary } from './files/error-boundary'
19
18
  export { type CordoInteraction } from './interaction'
20
19
 
@@ -29,7 +28,7 @@ async function mountCordo(configOverrides?: PartialDeep<CordoConfig>) {
29
28
  if (!CordoMagic.globalConfig.headless) {
30
29
  CordoMagic.globalLockfile = await LockfileInternals.readOrCreateLockfile(CordoMagic.globalConfig!.paths.lockfile)
31
30
  await RoutingFilesystem.readFsTreeAndSyncLockfile(CordoMagic.globalConfig.paths.routes, CordoMagic.globalLockfile, CordoMagic.globalConfig)
32
-
31
+
33
32
  LockfileInternals.writeLockfile(CordoMagic.globalConfig.paths.lockfile, CordoMagic.globalLockfile, CordoMagic.globalConfig.paths.types)
34
33
  }
35
34
  }
@@ -1,5 +1,5 @@
1
- import type { APIInteraction } from "discord-api-types/v10"
2
- import type { CordoCommand } from "./files/command"
1
+ import type { APIInteraction } from 'discord-api-types/v10'
2
+ import type { CordoCommand } from './files/command'
3
3
 
4
4
 
5
5
  const CordoInteractionSymbol = Symbol.for('CordoInteraction')
package/src/core/magic.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AsyncLocalStorage } from 'async_hooks'
2
- import { MissingContextError } from "../errors"
2
+ import { MissingContextError } from '../errors'
3
3
  import { LibIds } from '../lib/ids'
4
4
  import type { CordoInteraction } from './interaction'
5
5
  import type { LockfileInternals } from './files/lockfile'
@@ -1,12 +1,11 @@
1
- import { promises as fs } from "node:fs"
2
- import { join } from "node:path"
3
- import { ErrorBoundaryInternals, type CordoErrorBoundary } from "../files/error-boundary"
4
- import { RouteInternals, type CordoRoute } from "../files/route"
5
- import { LockfileInternals } from "../files/lockfile"
6
- import { LibIds } from "../../lib/ids"
7
- import { CommandInternals, type CordoCommand } from "../files/command"
8
- import type { ParsedCordoConfig } from "../files/config"
9
- import { AutocompleteInternals, type CordoAutocomplete } from "../files/command-autocomplete"
1
+ import { promises as fs } from 'node:fs'
2
+ import { join } from 'node:path'
3
+ import { ErrorBoundaryInternals, type CordoErrorBoundary } from '../files/error-boundary'
4
+ import { RouteInternals, type CordoRoute } from '../files/route'
5
+ import { LockfileInternals } from '../files/lockfile'
6
+ import { LibIds } from '../../lib/ids'
7
+ import { CommandInternals, type AutocompleteHandler, type CordoCommand } from '../files/command'
8
+ import type { ParsedCordoConfig } from '../files/config'
10
9
 
11
10
 
12
11
  export namespace RoutingFilesystem {
@@ -144,9 +143,9 @@ export namespace RoutingFilesystem {
144
143
  lockfile.$runtime.registeredCommands.set(filePath, file.command)
145
144
 
146
145
  const autocompleteOptions = file.command.options
147
- ?.filter(opt => 'choices' in opt && AutocompleteInternals.isAutocomplete(opt.choices))
146
+ ?.filter(opt => 'choices' in opt && (typeof opt.choices === 'function'))
148
147
  if (autocompleteOptions?.length) {
149
- const map = new Map<string, CordoAutocomplete<any>>()
148
+ const map = new Map<string, AutocompleteHandler<any>>()
150
149
  for (const option of autocompleteOptions) {
151
150
  // @ts-ignore we know choices are there and are autocomplete!
152
151
  map.set(option.name, option.choices)
@@ -1,9 +1,9 @@
1
- import { ApplicationCommandOptionType, ApplicationCommandType, InteractionType } from "discord.js"
2
- import { MissingContextError } from "../../errors/builtin/missing-context"
3
- import { FunctCompiler } from "../../functions/compiler"
4
- import { Hooks } from "../hooks"
5
- import type { CordoInteraction } from "../interaction"
6
- import { CordoMagic } from "../magic"
1
+ import { ApplicationCommandOptionType, ApplicationCommandType, InteractionType } from 'discord.js'
2
+ import { MissingContextError } from '../../errors/builtin/missing-context'
3
+ import { FunctCompiler } from '../../functions/compiler'
4
+ import { Hooks } from '../hooks'
5
+ import type { CordoInteraction } from '../interaction'
6
+ import { CordoMagic } from '../magic'
7
7
 
8
8
  const DefaultFileName = 'index'
9
9
 
@@ -18,7 +18,7 @@ export namespace RoutingResolve {
18
18
 
19
19
  const cwd = startingPath.split('/')
20
20
  if (cwd.at(-1) === DefaultFileName) cwd.pop()
21
- while (inputPath.startsWith('.')) {
21
+ while (inputPath.startsWith('.')) {
22
22
  if (inputPath.startsWith('..')) {
23
23
  if (cwd.length <= 0)
24
24
  return null
@@ -1,14 +1,14 @@
1
- import { ApplicationCommandOptionType, ApplicationCommandType, InteractionContextType, InteractionResponseType, InteractionType, MessageFlags, type APIUser } from "discord-api-types/v10"
2
- import { disableAllComponents } from "../../components"
3
- import { ComponentType, renderComponent, renderComponentList, type CordoComponent } from "../../components/component"
4
- import type { RouteInternals, RouteRequest, RouteResponse } from "../files/route"
5
- import { InteractionInternals, type CordoInteraction } from "../interaction"
6
- import { CordoMagic } from "../magic"
7
- import { CordoGateway } from "../gateway"
8
- import { FunctInternals } from "../../functions/funct"
9
- import { goto, run } from "../../functions"
10
- import { isModifier, readModifier } from "../../components/modifier"
11
- import { RoutingResolve } from "./resolve"
1
+ import { ApplicationCommandOptionType, ApplicationCommandType, InteractionContextType, InteractionResponseType, InteractionType, MessageFlags, type APIUser } from 'discord-api-types/v10'
2
+ import { disableAllComponents } from '../../components'
3
+ import { ComponentType, renderComponent, renderComponentList, type CordoComponent } from '../../components/component'
4
+ import type { RouteInternals, RouteRequest, RouteResponse } from '../files/route'
5
+ import { InteractionInternals, type CordoInteraction } from '../interaction'
6
+ import { CordoMagic } from '../magic'
7
+ import { CordoGateway } from '../gateway'
8
+ import { FunctInternals } from '../../functions/funct'
9
+ import { goto, run } from '../../functions'
10
+ import { isModifier, readModifier } from '../../components/modifier'
11
+ import { RoutingResolve } from './resolve'
12
12
 
13
13
 
14
14
  export namespace RoutingRespond {
@@ -87,7 +87,7 @@ export namespace RoutingRespond {
87
87
  if (options[0].type === ApplicationCommandOptionType.Subcommand || options[0].type === ApplicationCommandOptionType.SubcommandGroup)
88
88
  return parseCommandOptions(options[0].options)
89
89
  }
90
-
90
+
91
91
  return (options as Record<string, any>[]).reduce((out, opt) => ({ [opt.name]: opt.value, ...out }), {})
92
92
  }
93
93
 
@@ -96,7 +96,7 @@ export namespace RoutingRespond {
96
96
  args: string[],
97
97
  interaction: CordoInteraction,
98
98
  opts: RouteOpts = {}
99
- ): RouteRequest | null {
99
+ ): RouteRequest | null {
100
100
  const params = route.path
101
101
  .split('/')
102
102
  .filter(p => /^\[\w+\]$/.test(p))
@@ -1,29 +0,0 @@
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
- }