cordo 2.3.4 → 2.3.6

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.3.4",
3
+ "version": "2.3.6",
4
4
  "description": "A framework for handling complex discord api interactions",
5
5
  "exports": {
6
6
  ".": "./src/index.ts",
@@ -1,5 +1,6 @@
1
1
  import { ComponentType, createComponent } from "../component"
2
2
  import { Hooks } from "../../core/hooks"
3
+ import { FunctCompiler } from "../../functions/compiler"
3
4
 
4
5
 
5
6
  export function textInput() {
@@ -24,7 +25,7 @@ export function textInput() {
24
25
 
25
26
  function getLabel() {
26
27
  if (!labelVal)
27
- return 'Your response'
28
+ return 'Your Response'
28
29
  return Hooks.callHook(
29
30
  'transformUserFacingText',
30
31
  labelVal,
@@ -40,9 +41,9 @@ export function textInput() {
40
41
  min_length: minLength,
41
42
  max_length: maxLength,
42
43
  required: requiredVal,
43
- size: sizeVal ?? 1,
44
+ style: sizeVal ?? 1,
44
45
  value: currentVal,
45
- custom_id: ref
46
+ custom_id: ref ?? FunctCompiler.toCustomId([]) // get a noop if no ref
46
47
  })),
47
48
 
48
49
  as: (id: string) => {
@@ -83,6 +83,12 @@ type RouteRequestFromSelect = {
83
83
  selected: string[]
84
84
  }
85
85
 
86
+ type RouteRequestFromModal = {
87
+ source: 'modal'
88
+ command: null
89
+ selected: Map<string, string>
90
+ }
91
+
86
92
  export type RouteRequest = {
87
93
  params: Record<string, string>
88
94
  /** route path is the path to this route you are currently in */
@@ -111,7 +117,7 @@ export type RouteRequest = {
111
117
  run: (...params: Parameters<typeof run>) => Promise<boolean | null>
112
118
  /** let a different route handle this. Returns whether successful, null if not run */
113
119
  goto: (...params: Parameters<typeof goto>) => Promise<boolean | null>
114
- } & (RouteRequestInGuild | RouteRequestInDM) & (RouteRequestFromCommand | RouteRequestFromButton | RouteRequestFromSelect)
120
+ } & (RouteRequestInGuild | RouteRequestInDM) & (RouteRequestFromCommand | RouteRequestFromButton | RouteRequestFromSelect | RouteRequestFromModal)
115
121
 
116
122
  export type AsyncInteractionHandler = (i: RouteRequest) => RouteResponse | Promise<RouteResponse> | void | Promise<void> | boolean | Promise<boolean>
117
123
 
@@ -144,7 +150,9 @@ export function assertCordoRequest<
144
150
  ? RouteRequestFromCommand
145
151
  : Source extends 'button'
146
152
  ? RouteRequestFromButton
147
- : RouteRequestFromSelect
153
+ : Source extends 'select'
154
+ ? RouteRequestFromSelect
155
+ : RouteRequestFromModal
148
156
  ) {
149
157
  if (assumptions.location && request.location !== assumptions.location)
150
158
  throw new RouteAssumptionFailedError(request, assumptions)
@@ -120,7 +120,8 @@ export namespace CordoGateway {
120
120
  return handleCommandInteraction(i)
121
121
  else if (i.type === InteractionType.MessageComponent)
122
122
  return handleComponentInteraction(i)
123
-
123
+ else if (i.type === InteractionType.ModalSubmit)
124
+ return handleModalInteraction(i)
124
125
  }
125
126
 
126
127
  function handleCommandInteraction(i: CordoInteraction & { type: InteractionType.ApplicationCommand }) {
@@ -161,5 +162,21 @@ export namespace CordoGateway {
161
162
  if (!success) return
162
163
  }
163
164
  }
165
+
166
+ async function handleModalInteraction(i: CordoInteraction & { type: InteractionType.ModalSubmit }) {
167
+ const id = i.data.custom_id
168
+ const parsedCustomId = FunctCompiler.parseCustomId(id)
169
+
170
+ if (parsedCustomId.cwd)
171
+ CordoMagic.setCwd(parsedCustomId.cwd)
172
+
173
+ if (!parsedCustomId.functs.length && !InteractionInternals.get(i).answered)
174
+ await respondTo(i, null)
175
+
176
+ for (const action of parsedCustomId.functs) {
177
+ const success = await FunctInternals.evalFunct(action, i)
178
+ if (!success) return
179
+ }
180
+ }
164
181
 
165
182
  }
@@ -8,6 +8,7 @@ import { CordoGateway } from "../gateway"
8
8
  import { FunctInternals } from "../../functions/funct"
9
9
  import { goto, run } from "../../functions"
10
10
  import type { CordoModifier } from "../../components/modifier"
11
+ import { FunctCompiler } from "../../functions/compiler"
11
12
  import { RoutingResolve } from "./resolve"
12
13
 
13
14
 
@@ -77,6 +78,19 @@ export namespace RoutingRespond {
77
78
  }
78
79
  }
79
80
 
81
+ function parseModalResponse(interaction: CordoInteraction & { type: InteractionType.ModalSubmit }) {
82
+ const out = new Map<string, string>()
83
+ for (const row of interaction.data.components) {
84
+ for (const item of row.components) {
85
+ if (item.custom_id.startsWith(FunctCompiler.NoopIndicator))
86
+ continue
87
+ if (item.type === ComponentType.TextInput)
88
+ out.set(item.custom_id, item.value)
89
+ }
90
+ }
91
+ return out
92
+ }
93
+
80
94
  export function buildRouteRequest(
81
95
  route: RouteInternals.ParsedRoute,
82
96
  args: string[],
@@ -102,13 +116,15 @@ export namespace RoutingRespond {
102
116
 
103
117
  const source: RouteRequest['source'] | null = (interaction.type === InteractionType.ApplicationCommand)
104
118
  ? 'command'
105
- : (interaction.type === InteractionType.MessageComponent)
106
- ? (interaction.data.component_type === ComponentType.Button)
107
- ? 'button'
108
- : (interaction.data.component_type === ComponentType.StringSelect)
109
- ? 'select'
110
- : null
111
- : null
119
+ : (interaction.type === InteractionType.ModalSubmit)
120
+ ? 'modal'
121
+ : (interaction.type === InteractionType.MessageComponent)
122
+ ? (interaction.data.component_type === ComponentType.Button)
123
+ ? 'button'
124
+ : (interaction.data.component_type === ComponentType.StringSelect)
125
+ ? 'select'
126
+ : null
127
+ : null
112
128
 
113
129
  if (!source)
114
130
  return null
@@ -203,7 +219,9 @@ export namespace RoutingRespond {
203
219
  selected: (source === 'select')
204
220
  // @ts-ignore
205
221
  ? interaction.data.values
206
- : null
222
+ : (source === 'modal')
223
+ ? parseModalResponse(interaction as CordoInteraction & { type: InteractionType.ModalSubmit })
224
+ : null
207
225
  }
208
226
  }
209
227
 
@@ -11,7 +11,7 @@ export namespace FunctCompiler {
11
11
  /** the current version of custom ids. pick another non base64 character if you make breaking changes */
12
12
  const FunctVersion = '$'
13
13
  /** customids starting with this will be ignored silently */
14
- const NoopIndicator = '!'
14
+ export const NoopIndicator = '!'
15
15
  /** the following argument should be treated as is */
16
16
  const PlainArgumentIndicator = '/'
17
17
  /** the following argument should be infered from the lookup table */