cordo 2.16.3 → 2.16.4
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 +1 -1
- package/src/core/files/command.ts +37 -32
- package/src/core/files/lockfile.ts +4 -0
- package/src/core/gateway.ts +11 -0
- package/src/core/index.ts +5 -4
- package/src/core/magic.ts +2 -1
- package/src/errors/builtin/headless-mode.ts +9 -0
- package/src/errors/builtin/invalid-id.ts +9 -0
- package/src/errors/builtin/not-mounted.ts +9 -0
- package/src/errors/builtin/plugin-configuration.ts +9 -0
- package/src/errors/index.ts +5 -0
- package/src/lib/ids.ts +2 -1
- package/src/plugin/djs.ts +2 -2
- package/src/plugin/express.ts +2 -1
- package/src/plugin/hono.ts +2 -1
package/package.json
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import crypto from "node:crypto"
|
|
1
2
|
import type { Permissions } from 'discord-api-types/globals'
|
|
2
3
|
import { ChannelType, ApplicationCommandOptionType, ApplicationCommandType, type APIApplicationCommandOption } from 'discord-api-types/v10'
|
|
3
4
|
import type { LocalizedString } from '../../lib/localization'
|
|
@@ -5,6 +6,7 @@ import { CordoGateway } from '../gateway'
|
|
|
5
6
|
import { RoutingResolve } from '../routing/resolve'
|
|
6
7
|
import { CordoMagic } from '../magic'
|
|
7
8
|
import type { CordoInteraction } from '../interaction'
|
|
9
|
+
import { LockfileInternals } from "./lockfile"
|
|
8
10
|
import type { CordoRoute } from './route'
|
|
9
11
|
|
|
10
12
|
|
|
@@ -186,7 +188,7 @@ export namespace CommandInternals {
|
|
|
186
188
|
console.warn('No commands to sync. Is cordo initialized and commands defined?')
|
|
187
189
|
return
|
|
188
190
|
}
|
|
189
|
-
|
|
191
|
+
|
|
190
192
|
const rootCommands = new Map<string, CommandTree>()
|
|
191
193
|
|
|
192
194
|
const config = CordoMagic.getConfig()
|
|
@@ -271,44 +273,47 @@ export namespace CommandInternals {
|
|
|
271
273
|
|
|
272
274
|
const maxRetries = options?.maxRetries ?? 3
|
|
273
275
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
+
const hash = crypto.createHash('sha256').update(JSON.stringify(apiCommands)).digest('hex')
|
|
277
|
+
if (CordoMagic.globalLockfile && CordoMagic.globalLockfile.reg.commandSyncHash === hash)
|
|
278
|
+
return
|
|
279
|
+
|
|
280
|
+
console.log(`[Cordo.syncCommands] Syncing ${apiCommands.length} command(s) to Discord...`)
|
|
276
281
|
|
|
277
|
-
|
|
278
|
-
const res = await CordoGateway.upsertCommand(apiCmd, guild)
|
|
282
|
+
let retries = 0
|
|
279
283
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
const waitTime = retryAfter ? parseFloat(retryAfter) * 1000 : 5000
|
|
283
|
-
console.warn(`Rate limited (429) when syncing command '${apiCmd.name}'. Waiting ${waitTime}ms...`)
|
|
284
|
-
await new Promise(resolve => setTimeout(resolve, waitTime))
|
|
285
|
-
continue
|
|
286
|
-
}
|
|
284
|
+
while (true) {
|
|
285
|
+
const res = await CordoGateway.bulkUpsertCommands(apiCommands, guild)
|
|
287
286
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
continue
|
|
296
|
-
} else {
|
|
297
|
-
console.error(`Max retries reached for command '${apiCmd.name}'. Skipping.`)
|
|
298
|
-
break
|
|
299
|
-
}
|
|
300
|
-
}
|
|
287
|
+
if (res.status === 429) {
|
|
288
|
+
const retryAfter = res.headers['retry-after'] || res.data?.retry_after
|
|
289
|
+
const waitTime = retryAfter ? parseFloat(retryAfter) * 1000 : 5000
|
|
290
|
+
console.warn(`Rate limited (429) when bulk syncing commands. Waiting ${waitTime}ms...`)
|
|
291
|
+
await new Promise(resolve => setTimeout(resolve, waitTime))
|
|
292
|
+
continue
|
|
293
|
+
}
|
|
301
294
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
if (
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
295
|
+
if (res.status >= 400) {
|
|
296
|
+
console.error(`Error bulk syncing commands [HTTP ${res.status}]: ${JSON.stringify(res.data)}`)
|
|
297
|
+
if (retries < maxRetries) {
|
|
298
|
+
retries++
|
|
299
|
+
const backoffTime = 1000 * retries
|
|
300
|
+
console.log(`Retrying (${retries}/${maxRetries}) in ${backoffTime}ms...`)
|
|
301
|
+
await new Promise(resolve => setTimeout(resolve, backoffTime))
|
|
302
|
+
continue
|
|
303
|
+
} else {
|
|
304
|
+
console.error(`Max retries reached for bulk command sync. Skipping.`)
|
|
305
|
+
break
|
|
308
306
|
}
|
|
307
|
+
}
|
|
309
308
|
|
|
310
|
-
|
|
309
|
+
if (CordoMagic.globalLockfile && CordoMagic.globalConfig && !CordoMagic.globalConfig.headless) {
|
|
310
|
+
CordoMagic.globalLockfile.reg.commandSyncHash = hash
|
|
311
|
+
await LockfileInternals.writeLockfile(CordoMagic.globalConfig.paths.lockfile, CordoMagic.globalLockfile, CordoMagic.globalConfig.paths.types)
|
|
311
312
|
}
|
|
313
|
+
|
|
314
|
+
console.log(`[Cordo.syncCommands] Successfully synced ${apiCommands.length} command(s) to Discord.`)
|
|
315
|
+
break
|
|
312
316
|
}
|
|
313
317
|
}
|
|
314
318
|
}
|
|
319
|
+
|
|
@@ -12,6 +12,7 @@ export namespace LockfileInternals {
|
|
|
12
12
|
reg: {
|
|
13
13
|
idCounter: number
|
|
14
14
|
lutCounter: number
|
|
15
|
+
commandSyncHash?: string
|
|
15
16
|
}
|
|
16
17
|
routes: Array<Omit<RouteInternals.ParsedRoute, 'impl'>>
|
|
17
18
|
lut: Array<string>
|
|
@@ -88,6 +89,8 @@ export namespace LockfileInternals {
|
|
|
88
89
|
out.reg.idCounter = parseInt(value) ?? 0
|
|
89
90
|
else if (key === 'lutc')
|
|
90
91
|
out.reg.lutCounter = parseInt(value) ?? 0
|
|
92
|
+
else if (key === 'csync')
|
|
93
|
+
out.reg.commandSyncHash = value
|
|
91
94
|
}
|
|
92
95
|
|
|
93
96
|
if (block === 'routes') {
|
|
@@ -119,6 +122,7 @@ export namespace LockfileInternals {
|
|
|
119
122
|
'[reg]',
|
|
120
123
|
`idc ${data.reg.idCounter}`,
|
|
121
124
|
`lutc ${data.reg.lutCounter}`,
|
|
125
|
+
...(data.reg.commandSyncHash ? [ `csync ${data.reg.commandSyncHash}` ] : []),
|
|
122
126
|
'',
|
|
123
127
|
'[routes]',
|
|
124
128
|
...data.routes.map(route => `${route.name} ${route.filePath}`),
|
package/src/core/gateway.ts
CHANGED
|
@@ -345,4 +345,15 @@ export namespace CordoGateway {
|
|
|
345
345
|
return apiRequest('post', endpoint, command)
|
|
346
346
|
}
|
|
347
347
|
|
|
348
|
+
export function bulkUpsertCommands(commands: APIApplicationCommand[], guild?: string) {
|
|
349
|
+
const config = CordoMagic.getConfig()
|
|
350
|
+
if (!config?.client.id)
|
|
351
|
+
throw new MissingContextError('Could not bulk upsert commands, no client id found in config context.')
|
|
352
|
+
if (!config?.client.token)
|
|
353
|
+
throw new MissingContextError('Could not bulk upsert commands, no bot token found in config context (client.token).')
|
|
354
|
+
|
|
355
|
+
const endpoint = guild ? `/applications/${config.client.id}/guilds/${guild}/commands` : `/applications/${config.client.id}/commands`
|
|
356
|
+
return apiRequest('put', endpoint, commands)
|
|
357
|
+
}
|
|
358
|
+
|
|
348
359
|
}
|
package/src/core/index.ts
CHANGED
|
@@ -1,6 +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
5
|
import { ConfigInternals, type CordoConfig, type ParsedCordoConfig } from './files/config'
|
|
5
6
|
import { LockfileInternals } from './files/lockfile'
|
|
6
7
|
import { CordoGateway } from './gateway'
|
|
@@ -37,9 +38,9 @@ function triggerInteraction(interaction: APIInteraction, opts: {
|
|
|
37
38
|
} = {}) {
|
|
38
39
|
if (!CordoMagic.globalLockfile || !CordoMagic.globalConfig) {
|
|
39
40
|
if (CordoMagic.globalConfig?.headless)
|
|
40
|
-
throw new
|
|
41
|
+
throw new HeadlessModeError('[Cordo.triggerInteraction] Cordo is mounted in headless mode and cannot process interactions')
|
|
41
42
|
else
|
|
42
|
-
throw new
|
|
43
|
+
throw new NotMountedError('[Cordo.triggerInteraction] Cordo is not mounted')
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
CordoGateway.triggerInteraction({
|
|
@@ -58,9 +59,9 @@ function triggerInteraction(interaction: APIInteraction, opts: {
|
|
|
58
59
|
function registerConstants(constants: readonly string[]) {
|
|
59
60
|
if (!CordoMagic.globalLockfile || !CordoMagic.globalConfig) {
|
|
60
61
|
if (CordoMagic.globalConfig?.headless)
|
|
61
|
-
throw new
|
|
62
|
+
throw new HeadlessModeError('[Cordo.registerConstants] Cordo is mounted in headless mode and cannot register constants')
|
|
62
63
|
else
|
|
63
|
-
throw new
|
|
64
|
+
throw new NotMountedError('[Cordo.registerConstants] Cordo is not mounted')
|
|
64
65
|
}
|
|
65
66
|
|
|
66
67
|
let changesMade = false
|
package/src/core/magic.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'async_hooks'
|
|
2
|
+
import { MissingContextError } from "../errors"
|
|
2
3
|
import { LibIds } from '../lib/ids'
|
|
3
4
|
import type { CordoInteraction } from './interaction'
|
|
4
5
|
import type { LockfileInternals } from './files/lockfile'
|
|
@@ -77,7 +78,7 @@ export namespace CordoMagic {
|
|
|
77
78
|
/** request a new id, if in a context */
|
|
78
79
|
export function requestNewId<T extends boolean>(asString: T): T extends true ? string : number {
|
|
79
80
|
if (!Internals.getCtx())
|
|
80
|
-
throw new
|
|
81
|
+
throw new MissingContextError('Not in a Cordo context')
|
|
81
82
|
|
|
82
83
|
const id = ++Internals.getCtx().idCounter
|
|
83
84
|
if (!asString)
|
package/src/errors/index.ts
CHANGED
|
@@ -4,3 +4,8 @@ export { CordoError } from './cordo-error'
|
|
|
4
4
|
|
|
5
5
|
export { RouteNotFoundError } from './builtin/route-not-found'
|
|
6
6
|
export { RouteAssumptionFailedError } from './builtin/route-assumption-failed'
|
|
7
|
+
export { MissingContextError } from './builtin/missing-context'
|
|
8
|
+
export { NotMountedError } from './builtin/not-mounted'
|
|
9
|
+
export { HeadlessModeError } from './builtin/headless-mode'
|
|
10
|
+
export { InvalidIdError } from './builtin/invalid-id'
|
|
11
|
+
export { PluginConfigurationError } from './builtin/plugin-configuration'
|
package/src/lib/ids.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { InvalidIdError } from "../errors"
|
|
1
2
|
|
|
2
3
|
|
|
3
4
|
export namespace LibIds {
|
|
@@ -17,7 +18,7 @@ export namespace LibIds {
|
|
|
17
18
|
|
|
18
19
|
export function stringify(id: number, length: number) {
|
|
19
20
|
if (id > maxId)
|
|
20
|
-
throw new
|
|
21
|
+
throw new InvalidIdError(`Id ${id} is larger than 31 bits`)
|
|
21
22
|
let out = ''
|
|
22
23
|
for (let i = 0; i < length; i++)
|
|
23
24
|
out += bitSet[(id >> (size*i)) & bitMap]
|
package/src/plugin/djs.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { Client, Events } from 'discord.js'
|
|
2
2
|
import { Cordo } from '../core'
|
|
3
|
-
|
|
3
|
+
import { PluginConfigurationError } from '../errors'
|
|
4
4
|
|
|
5
5
|
export function useWithDiscordJs(client: Client) {
|
|
6
6
|
if (!client)
|
|
7
|
-
throw new
|
|
7
|
+
throw new PluginConfigurationError('You must provide a discord.js client object');
|
|
8
8
|
|
|
9
9
|
client.on(Events.Raw, (event) => {
|
|
10
10
|
if (event.t === 'INTERACTION_CREATE')
|
package/src/plugin/express.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { verifyKeyMiddleware } from "discord-interactions"
|
|
2
2
|
import { Cordo } from "../core"
|
|
3
|
+
import { PluginConfigurationError } from "../errors"
|
|
3
4
|
|
|
4
5
|
|
|
5
6
|
type ExpressRequest = {
|
|
@@ -14,7 +15,7 @@ type ExpressResponse = {
|
|
|
14
15
|
|
|
15
16
|
export function useWithExpress(clientPublicKey: string) {
|
|
16
17
|
if (!clientPublicKey)
|
|
17
|
-
throw new
|
|
18
|
+
throw new PluginConfigurationError('You must specify a Discord client public key');
|
|
18
19
|
|
|
19
20
|
const checkKey = verifyKeyMiddleware(clientPublicKey)
|
|
20
21
|
|
package/src/plugin/hono.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { verifyKey } from "discord-interactions"
|
|
2
2
|
import { Hono } from "hono"
|
|
3
3
|
import { Cordo } from "../core"
|
|
4
|
+
import { PluginConfigurationError } from "../errors"
|
|
4
5
|
|
|
5
6
|
|
|
6
7
|
export function useWithHono(clientPublicKey: string) {
|
|
7
8
|
if (!clientPublicKey)
|
|
8
|
-
throw new
|
|
9
|
+
throw new PluginConfigurationError('You must specify a Discord client public key');
|
|
9
10
|
|
|
10
11
|
const app = new Hono()
|
|
11
12
|
|