cordo 2.13.2 → 2.15.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.13.2",
3
+ "version": "2.15.0",
4
4
  "description": "A framework for handling complex discord api interactions",
5
5
  "exports": {
6
6
  ".": "./src/index.ts",
@@ -17,6 +17,9 @@
17
17
  "tsconfig.json"
18
18
  ],
19
19
  "scripts": {
20
+ "docs:dev": "vitepress dev docs",
21
+ "docs:build": "vitepress build docs",
22
+ "docs:preview": "vitepress preview docs",
20
23
  "build": "tsc",
21
24
  "lint": "eslint --ext .ts --ignore-path .gitignore src/**/*.ts",
22
25
  "tunnel": "cloudflared tunnel --config ~/.cloudflared/config.yml run fsb",
@@ -54,6 +57,8 @@
54
57
  "express": "^4.17.3",
55
58
  "hono": "^4.7.7",
56
59
  "reflect-metadata": "^0.1.13",
57
- "typescript": "^5.7.3"
60
+ "typescript": "^5.7.3",
61
+ "vitepress": "^1.6.4",
62
+ "vue": "^3.5.41"
58
63
  }
59
64
  }
@@ -3,8 +3,10 @@ import { ChannelType, ApplicationCommandOptionType, ApplicationCommandType, type
3
3
  import type { LocalizedString } from '../../lib/localization'
4
4
  import { CordoGateway } from '../gateway'
5
5
  import { RoutingResolve } from '../routing/resolve'
6
+ import type { CordoInteraction } from '../interaction'
6
7
  import type { CordoRoute } from './route'
7
8
 
9
+
8
10
  type CommandOption = {
9
11
  name: string | LocalizedString
10
12
  description: string | LocalizedString
@@ -61,10 +63,9 @@ type CommandInput = {
61
63
  nsfw?: boolean
62
64
  limitInstallTypes?: Array<'guild' | 'user'>
63
65
  limitContexts?: Array<'guild' | 'dm' | 'group'>
64
-
65
- options: CommandOption[]
66
-
66
+ options?: CommandOption[]
67
67
  route: CordoRoute | 'launch_activity'
68
+ private?: boolean | ((r: CordoInteraction) => boolean)
68
69
  }
69
70
 
70
71
  export type CordoCommand = {
@@ -117,7 +117,7 @@ export type RouteRequest = {
117
117
  rawEntitlements: APIEntitlement[]
118
118
 
119
119
  /** acknowledge the request but only provide a reply later */
120
- ack: (opts?: { private?: boolean }) => void
120
+ ack: () => void
121
121
  /** render the provided components */
122
122
  render: (...response: RouteResponse) => void
123
123
  /** open a modal with the provided components */
@@ -72,9 +72,15 @@ export namespace CordoGateway {
72
72
  }
73
73
 
74
74
  /** if the payload is null the interaction will be defered if not done already */
75
- export async function respondTo(i: CordoInteraction, payload: Record<string, any> | null, opts?: { ephemeral?: boolean }) {
75
+ export async function respondTo(i: CordoInteraction, payload: Record<string, any> | null, opts: { ephemeral?: boolean } = {}) {
76
76
  payload = await Hooks.callHook('onBeforeRespond', payload, { interaction: i })
77
77
 
78
+ // if not specifically set, check if it's triggered by a command and if so -> is that command private?
79
+ if (opts?.ephemeral === undefined) {
80
+ const commandPrivate = InteractionInternals.get(i).commandEntrypoint?.private
81
+ opts.ephemeral = typeof commandPrivate === 'function' ? commandPrivate(i) : Boolean(commandPrivate)
82
+ }
83
+
78
84
  const internals = InteractionInternals.get(i)
79
85
  if (internals.httpCallback && !internals.answered) {
80
86
  internals.answered = true
@@ -92,6 +98,8 @@ export namespace CordoGateway {
92
98
  }
93
99
 
94
100
  if (!internals.answered) {
101
+ internals.answered = true
102
+
95
103
  if (payload)
96
104
  return apiRequest('post', `/interactions/${i.id}/${i.token}/callback`, payload)
97
105
 
@@ -162,7 +170,11 @@ export namespace CordoGateway {
162
170
  }
163
171
 
164
172
  const { route, path } = RoutingResolve.getRouteForCommand(name, 'slash')
173
+
165
174
  CordoMagic.setCwd(path)
175
+ if (route.routeFilePath && CordoMagic.getLockfile()?.$runtime.registeredCommands.has(route.routeFilePath))
176
+ InteractionInternals.get(i).commandEntrypoint = CordoMagic.getLockfile()!.$runtime.registeredCommands.get(route.routeFilePath)!
177
+
166
178
  return RoutingRespond.callRoute(route.routeId, route.args, i)
167
179
  }
168
180
 
@@ -172,7 +184,11 @@ export namespace CordoGateway {
172
184
  i.data.name,
173
185
  i.data.type === ApplicationCommandType.Message ? 'message' : 'user'
174
186
  )
187
+
175
188
  CordoMagic.setCwd(path)
189
+ if (route.routeFilePath && CordoMagic.getLockfile()?.$runtime.registeredCommands.has(route.routeFilePath))
190
+ InteractionInternals.get(i).commandEntrypoint = CordoMagic.getLockfile()!.$runtime.registeredCommands.get(route.routeFilePath)!
191
+
176
192
  return RoutingRespond.callRoute(route.routeId, route.args, i)
177
193
  }
178
194
  }
@@ -1,4 +1,5 @@
1
1
  import type { APIInteraction } from "discord-api-types/v10"
2
+ import type { CordoCommand } from "./files/command"
2
3
 
3
4
 
4
5
  const CordoInteractionSymbol = Symbol.for('CordoInteraction')
@@ -7,6 +8,8 @@ export type CordoInteraction = {
7
8
  [CordoInteractionSymbol]: {
8
9
  answered: boolean
9
10
  httpCallback: ((payload: any) => any) | null
11
+ // will be set to the command linked to this interaction if it was triggered by a command, otherwise null
12
+ commandEntrypoint: CordoCommand | null
10
13
  }
11
14
  locals: Record<string, any>
12
15
  } & APIInteraction
@@ -29,7 +32,8 @@ export namespace InteractionInternals {
29
32
  locals: {},
30
33
  [CordoInteractionSymbol]: {
31
34
  answered: false,
32
- httpCallback: null
35
+ httpCallback: null,
36
+ commandEntrypoint: null
33
37
  }
34
38
  }
35
39
  }
@@ -44,13 +44,19 @@ export namespace RoutingFilesystem {
44
44
  boundary: child.boundary
45
45
  })
46
46
  }
47
+ for (const child of subTree.commands) {
48
+ out.commands.push({
49
+ path: [ item.name, ...child.path ],
50
+ command: child.command
51
+ })
52
+ }
47
53
  } else if (item.isFile()) {
48
54
  if (!supportedExtensions.some(ext => item.name.endsWith(`.${ext}`)))
49
55
  continue
50
56
 
51
57
  const command = await CommandInternals.readCommand(join(treeRoot, item.name))
52
58
  if (command) {
53
- if (!treeRoot.startsWith(config.defaults.commandRoutePrefix ?? 'command'))
59
+ if (!treeRoot.includes(config.defaults.commandRoutePrefix ?? 'command'))
54
60
  console.warn(`File ${join(treeRoot, item.name)} defined a command but is not in the command route prefix folder (${config.defaults.commandRoutePrefix ?? 'command'}). The command will probably not trigger the configured route when run.`)
55
61
 
56
62
  out.commands.push({
@@ -132,15 +138,10 @@ export namespace RoutingFilesystem {
132
138
  })
133
139
  }
134
140
 
135
- const commandRoutePrefix = config?.defaults.commandRoutePrefix ?? 'command'
136
141
  for (const file of files.commands) {
137
- const filePath = file.path.join('/').startsWith(`${commandRoutePrefix}/`)
138
- ? file.path.join('/').slice(commandRoutePrefix.length + 1)
139
- : file.path.join('/')
140
-
142
+ const filePath = file.path.join('/')
141
143
  lockfile.$runtime.registeredCommands.set(filePath, file.command)
142
144
  }
143
-
144
145
 
145
146
  return out
146
147
  }
@@ -34,6 +34,11 @@ export namespace RoutingRespond {
34
34
  if (opts.disableComponents)
35
35
  response.push(disableAllComponents())
36
36
 
37
+ if (opts.isPrivate === undefined) {
38
+ const commandPrivate = InteractionInternals.get(i).commandEntrypoint?.private
39
+ opts.isPrivate = typeof commandPrivate === 'function' ? commandPrivate(i) : Boolean(commandPrivate)
40
+ }
41
+
37
42
  let type: InteractionResponseType = InteractionResponseType.Pong
38
43
  if (i.type === InteractionType.ApplicationCommand) {
39
44
  type = (InteractionInternals.get(i).answered && !opts.asReply)
@@ -145,8 +150,8 @@ export namespace RoutingRespond {
145
150
  has: (key: string) => key in interaction.locals
146
151
  },
147
152
 
148
- ack: opts.disableRendering ? noOp : (ackOpts?: { private?: boolean }) => {
149
- CordoGateway.respondTo(interaction, null, { ephemeral: ackOpts?.private })
153
+ ack: opts.disableRendering ? noOp : () => {
154
+ CordoGateway.respondTo(interaction, null)
150
155
  },
151
156
  render: opts.disableRendering ? noOp : (...response) => {
152
157
  const rendered = renderRouteResponse(response, interaction, opts)