cordo 2.17.0-rc1 → 2.17.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.17.0",
4
4
  "description": "A framework for handling complex discord api interactions",
5
5
  "exports": {
6
6
  ".": "./src/index.ts",
@@ -4,13 +4,15 @@ import type { CordoInteraction } from "../interaction"
4
4
 
5
5
  const CordoAutocompleteSymbol = Symbol.for('CordoAutocomplete')
6
6
 
7
+ type MaybePromise<T> = T | Promise<T>
8
+
7
9
  type AutocompleteHandler<ReturnType extends string | number> = (
8
10
  value: ReturnType,
9
11
  i: CordoInteraction,
10
- ) => Array<{
12
+ ) => MaybePromise<Array<{
11
13
  name: string | LocalizedString
12
14
  value: ReturnType
13
- }>
15
+ }>>
14
16
 
15
17
  export type CordoAutocomplete<ReturnType extends string | number> = {
16
18
  [CordoAutocompleteSymbol]: AutocompleteHandler<ReturnType>
@@ -26,4 +28,10 @@ export namespace AutocompleteInternals {
26
28
  export function isAutocomplete(obj: any): obj is CordoAutocomplete<any> {
27
29
  return obj && typeof obj === 'object' && CordoAutocompleteSymbol in obj
28
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
+ }
29
37
  }
@@ -8,7 +8,7 @@ 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
+ import { AutocompleteInternals, type CordoAutocomplete } from "./command-autocomplete"
12
12
 
13
13
 
14
14
  type CommandChoices<T extends string | number> = Array<{
@@ -133,17 +133,19 @@ export namespace CommandInternals {
133
133
  description,
134
134
  description_localizations,
135
135
  required: opt.required,
136
- autocomplete: opt.autocomplete,
137
136
  min_value: opt.minValue,
138
137
  max_value: opt.maxValue,
139
138
  min_length: opt.minLength,
140
139
  max_length: opt.maxLength,
141
140
  file_types: opt.fileTypes,
142
141
  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
- })
142
+ choices: opt.choices && Array.isArray(opt.choices)
143
+ ? opt.choices.map((c: any) => {
144
+ const { value: name, localizations: name_localizations } = resolveLocalizedText(c.name)
145
+ return { name, name_localizations, value: c.value }
146
+ })
147
+ : undefined,
148
+ autocomplete: opt.choices && AutocompleteInternals.isAutocomplete(opt.choices)
147
149
  }
148
150
 
149
151
  return res
@@ -11,6 +11,7 @@ import type { ParsedCordoConfig } from "./files/config"
11
11
  import { Hooks } from "./hooks"
12
12
  import { RoutingResolve } from "./routing/resolve"
13
13
  import { RoutingRespond } from "./routing/respond"
14
+ import { AutocompleteInternals } from "./files/command-autocomplete"
14
15
 
15
16
 
16
17
  export namespace CordoGateway {
@@ -222,7 +223,7 @@ export namespace CordoGateway {
222
223
  }
223
224
  }
224
225
 
225
- function handleAutocompleteInteraction(i: CordoInteraction & { type: InteractionType.ApplicationCommandAutocomplete }) {
226
+ async function handleAutocompleteInteraction(i: CordoInteraction & { type: InteractionType.ApplicationCommandAutocomplete }) {
226
227
  const res = RoutingResolve.getRouteForCommandObject(i.data)
227
228
  if (!res)
228
229
  return
@@ -239,10 +240,19 @@ export namespace CordoGateway {
239
240
  const focusedOption = i.data.options?.find(opt => opt.focused)
240
241
  if (!focusedOption)
241
242
  return
242
-
243
+
243
244
  const handler = commandAutocompletes.get(focusedOption.name)
244
245
  // @ts-ignore for now
245
- return handler?.(focusedOption.value, i)
246
+ const result = await AutocompleteInternals.callHandler(handler, focusedOption.value, i)
247
+ return respondTo(i, {
248
+ type: InteractionResponseType.ApplicationCommandAutocompleteResult,
249
+ data: {
250
+ choices: result?.map(r => ({
251
+ name: typeof r.name === 'string' ? r.name : r.name[i.locale] ?? r.name['en-US'] ?? Object.values(r.name)[0],
252
+ value: r.value
253
+ })) ?? []
254
+ }
255
+ })
246
256
  }
247
257
 
248
258
  //