cordo 2.17.0 → 2.18.1

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",
3
+ "version": "2.18.1",
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 { AutocompleteInternals, 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
@@ -145,7 +154,7 @@ export namespace CommandInternals {
145
154
  return { name, name_localizations, value: c.value }
146
155
  })
147
156
  : undefined,
148
- autocomplete: opt.choices && AutocompleteInternals.isAutocomplete(opt.choices)
157
+ autocomplete: opt.choices && (typeof opt.choices === 'function')
149
158
  }
150
159
 
151
160
  return res
@@ -164,7 +173,7 @@ export namespace CommandInternals {
164
173
 
165
174
  const options: any[] = []
166
175
  if (isGroup) {
167
- for (const [ subName, subTree ] of tree.subcommands.entries())
176
+ for (const [ subName, subTree ] of tree.subcommands.entries())
168
177
  options.push(buildApiOption(subName, subTree))
169
178
  } else if (tree.command?.options) {
170
179
  options.push(...tree.command.options.map(mapCommandOptionToApiOption))
@@ -204,11 +213,11 @@ export namespace CommandInternals {
204
213
 
205
214
  for (let i = 0; i < parts.length; i++) {
206
215
  const part = parts[i]
207
- if (!currentLevel.has(part))
216
+ if (!currentLevel.has(part))
208
217
  currentLevel.set(part, { subcommands: new Map() })
209
218
 
210
219
  const node = currentLevel.get(part)!
211
- if (i === parts.length - 1)
220
+ if (i === parts.length - 1)
212
221
  node.command = command
213
222
 
214
223
  currentLevel = node.subcommands
@@ -233,7 +242,7 @@ export namespace CommandInternals {
233
242
  }
234
243
 
235
244
  const { value: description, localizations: descriptionLocalizations } = resolveLocalizedText(
236
- tree.command?.description,
245
+ tree.command?.description,
237
246
  tree.subcommands.size > 0 ? 'Command group' : 'No description provided'
238
247
  )
239
248
 
@@ -241,9 +250,9 @@ export namespace CommandInternals {
241
250
 
242
251
  const apiOptions: any[] = []
243
252
  if (tree.subcommands.size > 0) {
244
- for (const [ subName, subTree ] of tree.subcommands.entries())
253
+ for (const [ subName, subTree ] of tree.subcommands.entries())
245
254
  apiOptions.push(buildApiOption(subName, subTree))
246
-
255
+
247
256
  } else if (tree.command?.options) {
248
257
  apiOptions.push(...tree.command.options.map(mapCommandOptionToApiOption))
249
258
  }
@@ -273,9 +282,9 @@ export namespace CommandInternals {
273
282
  const maxRetries = options?.maxRetries ?? 3
274
283
 
275
284
  const hash = crypto.createHash('sha256').update(JSON.stringify(apiCommands)).digest('hex')
276
- if (CordoMagic.globalLockfile && CordoMagic.globalLockfile.reg.commandSyncHash === hash)
285
+ if (CordoMagic.globalLockfile && CordoMagic.globalLockfile.reg.commandSyncHash === hash)
277
286
  return
278
-
287
+
279
288
  console.log(`[Cordo.syncCommands] Syncing ${apiCommands.length} command(s) to Discord...`)
280
289
 
281
290
  let retries = 0
@@ -300,7 +309,7 @@ export namespace CommandInternals {
300
309
  await new Promise(resolve => setTimeout(resolve, backoffTime))
301
310
  continue
302
311
  } else {
303
- console.error(`Max retries reached for bulk command sync. Skipping.`)
312
+ console.error('Max retries reached for bulk command sync. Skipping.')
304
313
  break
305
314
  }
306
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,17 +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"
14
- import { AutocompleteInternals } from "./files/command-autocomplete"
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'
15
14
 
16
15
 
17
16
  export namespace CordoGateway {
@@ -123,7 +122,7 @@ export namespace CordoGateway {
123
122
  if (!config)
124
123
  throw new MissingContextError('Could not respond to interaction, no config found in context.')
125
124
  if (!config.client.id) {
126
- 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.')
127
126
  return null
128
127
  }
129
128
 
@@ -153,11 +152,11 @@ export namespace CordoGateway {
153
152
  }, deferAfter)
154
153
  }
155
154
 
156
- if (i.type === InteractionType.ApplicationCommand)
155
+ if (i.type === InteractionType.ApplicationCommand)
157
156
  return handleCommandInteraction(i)
158
- else if (i.type === InteractionType.MessageComponent)
157
+ else if (i.type === InteractionType.MessageComponent)
159
158
  return handleComponentInteraction(i)
160
- else if (i.type === InteractionType.ModalSubmit)
159
+ else if (i.type === InteractionType.ModalSubmit)
161
160
  return handleModalInteraction(i)
162
161
  else if (i.type === InteractionType.ApplicationCommandAutocomplete)
163
162
  return handleAutocompleteInteraction(i)
@@ -227,7 +226,7 @@ export namespace CordoGateway {
227
226
  const res = RoutingResolve.getRouteForCommandObject(i.data)
228
227
  if (!res)
229
228
  return
230
-
229
+
231
230
  const command = CordoMagic.getLockfile()?.$runtime.registeredCommands.get(res.route.routeFilePath ?? res.path)
232
231
  if (!command)
233
232
  return
@@ -240,10 +239,10 @@ export namespace CordoGateway {
240
239
  const focusedOption = i.data.options?.find(opt => opt.focused)
241
240
  if (!focusedOption)
242
241
  return
243
-
242
+
244
243
  const handler = commandAutocompletes.get(focusedOption.name)
245
244
  // @ts-ignore for now
246
- const result = await AutocompleteInternals.callHandler(handler, focusedOption.value, i)
245
+ const result = await handler?.(focusedOption.value, i)
247
246
  return respondTo(i, {
248
247
  type: InteractionResponseType.ApplicationCommandAutocompleteResult,
249
248
  data: {
@@ -266,7 +265,7 @@ export namespace CordoGateway {
266
265
  }
267
266
  return true
268
267
  }
269
-
268
+
270
269
  if (c.type === ComponentType.Label) {
271
270
  const success = await parseAndEvokeModalLeafComponents(c.component, i, collectedFormItems)
272
271
  return success
@@ -278,7 +277,7 @@ export namespace CordoGateway {
278
277
 
279
278
  const formRef = parsedCustomId.values[0]
280
279
 
281
- //
280
+ //
282
281
  if ('values' in c && c.values.length > 0 && typeof c.values[0] === 'string') {
283
282
  if (executeAllowedComponentTypes.includes(c.type)) {
284
283
  const success = await parseAndEvokeComponentWithValues(c, i)
@@ -331,7 +330,7 @@ export namespace CordoGateway {
331
330
  ): Promise<boolean> {
332
331
  if (!executeAllowedComponentTypes.includes('type' in data ? data.type : data.component_type)) // just to be sure
333
332
  return true
334
-
333
+
335
334
  const option = FunctCompiler.parseCustomId(data.value)
336
335
  data.value = option.values[0]
337
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,37 +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 MaybePromise<T> = T | Promise<T>
8
-
9
- type AutocompleteHandler<ReturnType extends string | number> = (
10
- value: ReturnType,
11
- i: CordoInteraction,
12
- ) => MaybePromise<Array<{
13
- name: string | LocalizedString
14
- value: ReturnType
15
- }>>
16
-
17
- export type CordoAutocomplete<ReturnType extends string | number> = {
18
- [CordoAutocompleteSymbol]: AutocompleteHandler<ReturnType>
19
- }
20
-
21
- export function defineCordoAutocomplete<ReturnType extends string | number>(handler: AutocompleteHandler<ReturnType>): CordoAutocomplete<ReturnType> {
22
- return {
23
- [CordoAutocompleteSymbol]: handler
24
- }
25
- }
26
-
27
- export namespace AutocompleteInternals {
28
- export function isAutocomplete(obj: any): obj is CordoAutocomplete<any> {
29
- return obj && typeof obj === 'object' && CordoAutocompleteSymbol in obj
30
- }
31
-
32
- export function callHandler(obj: CordoAutocomplete<any>, value: string | number, i: CordoInteraction) {
33
- if (!isAutocomplete(obj))
34
- return Promise.resolve([])
35
- return obj[CordoAutocompleteSymbol](value, i)
36
- }
37
- }