cordo 2.16.5 → 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/README.md +1 -1
- package/package.json +18 -18
- package/src/core/files/command-autocomplete.ts +37 -0
- package/src/core/files/command.ts +16 -17
- package/src/core/files/lockfile.ts +4 -1
- package/src/core/gateway.ts +46 -34
- package/src/core/index.ts +1 -0
- package/src/core/routing/filesystem.ts +12 -0
- package/src/core/routing/resolve.ts +20 -0
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
|
|
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.
|
|
3
|
+
"version": "2.17.0",
|
|
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.
|
|
41
|
+
"@types/bun": "^1.4.3",
|
|
42
42
|
"axios": "^0.21.4",
|
|
43
|
-
"defu": "^6.1.
|
|
43
|
+
"defu": "^6.1.7",
|
|
44
44
|
"discord-interactions": "^2.4.1",
|
|
45
|
-
"type-fest": "^4.
|
|
45
|
+
"type-fest": "^4.41.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@types/axios": "^0.14.
|
|
49
|
-
"@types/express": "^4.17.
|
|
50
|
-
"@types/node": "^17.0.
|
|
51
|
-
"@typescript-eslint/eslint-plugin": "^4.
|
|
52
|
-
"@typescript-eslint/parser": "^4.
|
|
53
|
-
"discord-api-types": "^0.38.
|
|
54
|
-
"discord.js": "^14.
|
|
55
|
-
"eslint": "^7.
|
|
56
|
-
"eslint-config-maanex": "^1.
|
|
57
|
-
"express": "^4.
|
|
58
|
-
"hono": "^4.
|
|
59
|
-
"reflect-metadata": "^0.1.
|
|
60
|
-
"typescript": "^5.
|
|
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.
|
|
62
|
+
"vue": "^3.5.44"
|
|
63
63
|
}
|
|
64
64
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
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
|
+
}
|
|
@@ -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 { AutocompleteInternals, 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?:
|
|
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?:
|
|
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?:
|
|
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}`>
|
|
@@ -136,17 +133,19 @@ export namespace CommandInternals {
|
|
|
136
133
|
description,
|
|
137
134
|
description_localizations,
|
|
138
135
|
required: opt.required,
|
|
139
|
-
autocomplete: opt.autocomplete,
|
|
140
136
|
min_value: opt.minValue,
|
|
141
137
|
max_value: opt.maxValue,
|
|
142
138
|
min_length: opt.minLength,
|
|
143
139
|
max_length: opt.maxLength,
|
|
144
140
|
file_types: opt.fileTypes,
|
|
145
141
|
channel_types: opt.channelTypes,
|
|
146
|
-
choices: opt.choices
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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)
|
|
150
149
|
}
|
|
151
150
|
|
|
152
151
|
return res
|
|
@@ -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
|
}
|
package/src/core/gateway.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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"
|
|
@@ -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 {
|
|
@@ -154,44 +155,23 @@ export namespace CordoGateway {
|
|
|
154
155
|
|
|
155
156
|
if (i.type === InteractionType.ApplicationCommand)
|
|
156
157
|
return handleCommandInteraction(i)
|
|
157
|
-
|
|
158
|
+
else if (i.type === InteractionType.MessageComponent)
|
|
158
159
|
return handleComponentInteraction(i)
|
|
159
|
-
|
|
160
|
+
else if (i.type === InteractionType.ModalSubmit)
|
|
160
161
|
return handleModalInteraction(i)
|
|
162
|
+
else if (i.type === InteractionType.ApplicationCommandAutocomplete)
|
|
163
|
+
return handleAutocompleteInteraction(i)
|
|
161
164
|
}
|
|
162
165
|
|
|
163
166
|
function handleCommandInteraction(i: CordoInteraction & { type: InteractionType.ApplicationCommand }) {
|
|
164
|
-
|
|
165
|
-
if (
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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
|
-
}
|
|
167
|
+
const res = RoutingResolve.getRouteForCommandObject(i.data)
|
|
168
|
+
if (!res)
|
|
169
|
+
return
|
|
170
|
+
CordoMagic.setCwd(res.path)
|
|
171
|
+
if (res.route.routeFilePath && CordoMagic.getLockfile()?.$runtime.registeredCommands.has(res.route.routeFilePath))
|
|
172
|
+
InteractionInternals.get(i).commandEntrypoint = CordoMagic.getLockfile()!.$runtime.registeredCommands.get(res.route.routeFilePath)!
|
|
173
|
+
|
|
174
|
+
return RoutingRespond.callRoute(res.route.routeId, res.route.args, i)
|
|
195
175
|
}
|
|
196
176
|
|
|
197
177
|
async function handleComponentInteraction(i: CordoInteraction & { type: InteractionType.MessageComponent }) {
|
|
@@ -243,6 +223,38 @@ export namespace CordoGateway {
|
|
|
243
223
|
}
|
|
244
224
|
}
|
|
245
225
|
|
|
226
|
+
async function handleAutocompleteInteraction(i: CordoInteraction & { type: InteractionType.ApplicationCommandAutocomplete }) {
|
|
227
|
+
const res = RoutingResolve.getRouteForCommandObject(i.data)
|
|
228
|
+
if (!res)
|
|
229
|
+
return
|
|
230
|
+
|
|
231
|
+
const command = CordoMagic.getLockfile()?.$runtime.registeredCommands.get(res.route.routeFilePath ?? res.path)
|
|
232
|
+
if (!command)
|
|
233
|
+
return
|
|
234
|
+
|
|
235
|
+
const commandAutocompletes = CordoMagic.getLockfile()?.$runtime.registeredCommandAutocompletes.get(command)
|
|
236
|
+
if (!commandAutocompletes)
|
|
237
|
+
return
|
|
238
|
+
|
|
239
|
+
// @ts-ignore for now
|
|
240
|
+
const focusedOption = i.data.options?.find(opt => opt.focused)
|
|
241
|
+
if (!focusedOption)
|
|
242
|
+
return
|
|
243
|
+
|
|
244
|
+
const handler = commandAutocompletes.get(focusedOption.name)
|
|
245
|
+
// @ts-ignore for now
|
|
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
|
+
})
|
|
256
|
+
}
|
|
257
|
+
|
|
246
258
|
//
|
|
247
259
|
|
|
248
260
|
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 })
|