@abyss-project/runtime 3.7.0 → 3.8.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.
Files changed (55) hide show
  1. package/.eslintrc.js +32 -32
  2. package/.prettierrc +5 -5
  3. package/README.md +88 -88
  4. package/dist/engine/engine-source.js +1 -1
  5. package/dist/engine/run-in-isolate.js +32 -2
  6. package/dist/engine/run-in-isolate.js.map +1 -1
  7. package/dist/runner-protocol/execution-protocol.types.d.ts +2 -0
  8. package/dist/runner-protocol/execution-protocol.types.js +2 -1
  9. package/dist/runner-protocol/execution-protocol.types.js.map +1 -1
  10. package/jest.config.ts +11 -11
  11. package/package.json +10 -15
  12. package/scripts/build-engine.cjs +70 -70
  13. package/src/__tests__/engine-bundle.test.ts +121 -121
  14. package/src/__tests__/surface.test.ts +12 -1
  15. package/src/access/__tests__/host-match.test.ts +24 -24
  16. package/src/access/access.types.ts +25 -25
  17. package/src/access/host-match.ts +13 -13
  18. package/src/cost/__tests__/execution-cost.test.ts +196 -196
  19. package/src/cost/execution-cost.ts +186 -186
  20. package/src/definition/__tests__/validate-definition.test.ts +354 -354
  21. package/src/definition/definition.limits.ts +9 -9
  22. package/src/definition/definition.schema.ts +172 -172
  23. package/src/definition/definition.types.ts +171 -171
  24. package/src/definition/validate-definition.ts +206 -206
  25. package/src/definition/walk-steps.ts +44 -44
  26. package/src/discord/__tests__/discord-event-input.test.ts +89 -89
  27. package/src/discord/discord.types.ts +639 -639
  28. package/src/engine/__tests__/run-in-isolate.test.ts +56 -5
  29. package/src/engine/engine-entry.ts +3 -3
  30. package/src/engine/engine-global.ts +2 -2
  31. package/src/engine/run-in-isolate.ts +47 -4
  32. package/src/errors/runtime-error-code.enum.ts +37 -37
  33. package/src/index.ts +32 -32
  34. package/src/interpreter/__tests__/interpreter.test.ts +854 -854
  35. package/src/interpreter/__tests__/log-respond-resume.test.ts +271 -271
  36. package/src/interpreter/interpreter-error.ts +12 -12
  37. package/src/interpreter/interpreter.ts +483 -483
  38. package/src/interpreter/interpreter.types.ts +204 -204
  39. package/src/interpreter/sequence-path.ts +67 -67
  40. package/src/interpreter/test-host.ts +85 -85
  41. package/src/runner-protocol/evaluation.types.ts +21 -21
  42. package/src/runner-protocol/execution-protocol.types.ts +20 -3
  43. package/src/schedule/schedule.types.ts +100 -100
  44. package/src/server/__tests__/code-runner-session.test.ts +311 -311
  45. package/src/server/code-runner-error.ts +12 -12
  46. package/src/server/code-runner-session.ts +310 -310
  47. package/src/server/index.ts +11 -11
  48. package/src/steps/__tests__/global-references.test.ts +133 -133
  49. package/src/steps/execute-step.ts +186 -186
  50. package/src/steps/global-references.ts +48 -48
  51. package/src/steps/step-access.ts +56 -56
  52. package/src/steps/step-fields.ts +87 -87
  53. package/src/steps/value-source.ts +69 -69
  54. package/tsconfig.check.json +15 -15
  55. package/tsconfig.json +35 -35
@@ -1,639 +1,639 @@
1
- import type { JSONValue } from '../runner-protocol/execution-protocol.types';
2
-
3
- /**
4
- * Host capabilities of a Discord bot Service. The host declares them in the execution's
5
- * capabilities; client code reaches them through `abyss.discord.<name>(...)`; every helper exists,
6
- * and one whose capability is not granted rejects with an error naming the action to declare. The argument shapes below are the `args` of the `call`.
7
- */
8
- export enum DiscordCapability {
9
- /** Answers the interaction. Arguments: a `DiscordMessage`. */
10
- REPLY = 'discord.reply',
11
- /** Edits the message a component interaction came from. Arguments: a `DiscordMessage`. */
12
- UPDATE = 'discord.update',
13
- /** Opens a modal, before the interaction is acknowledged. Arguments: `DiscordShowModalArgs`. */
14
- SHOW_MODAL = 'discord.showModal',
15
- /** Sends an additional message for the interaction. Arguments: a `DiscordMessage`. */
16
- FOLLOW_UP = 'discord.followUp',
17
- /** Posts a message in a channel as the bot. Arguments: `DiscordSendMessageArgs`. */
18
- SEND_MESSAGE = 'discord.sendMessage',
19
- /** Edits a message of the bot. Arguments: `DiscordEditMessageArgs`. */
20
- EDIT_MESSAGE = 'discord.editMessage',
21
- /** Deletes a message. Arguments: `DiscordDeleteMessageArgs`. */
22
- DELETE_MESSAGE = 'discord.deleteMessage',
23
- /** Gives a role to a member. Arguments: `DiscordRoleArgs`. */
24
- ADD_ROLE = 'discord.addRole',
25
- /** Takes a role from a member. Arguments: `DiscordRoleArgs`. */
26
- REMOVE_ROLE = 'discord.removeRole',
27
- /** Sends a direct message to a user. Arguments: `DiscordSendDirectMessageArgs`. */
28
- SEND_DIRECT_MESSAGE = 'discord.sendDirectMessage',
29
- /** Reads a member of a server. Arguments: `DiscordGetMemberArgs`; result: `DiscordMember | null`. */
30
- GET_MEMBER = 'discord.getMember',
31
- /** Reads the roles of a server. Arguments: `DiscordGetGuildRolesArgs`; result: `DiscordRole[]`. */
32
- GET_GUILD_ROLES = 'discord.getGuildRoles',
33
- /** Reads a channel. Arguments: `DiscordGetChannelArgs`; result: `DiscordChannel`. */
34
- GET_CHANNEL = 'discord.getChannel',
35
- /** Reads a server. Arguments: `DiscordGetGuildArgs`; result: `DiscordGuild`. */
36
- GET_GUILD = 'discord.getGuild',
37
- /** Removes a member from a server. Arguments: `DiscordKickMemberArgs`. */
38
- KICK_MEMBER = 'discord.kickMember',
39
- /** Bans a user from a server. Arguments: `DiscordBanMemberArgs`. */
40
- BAN_MEMBER = 'discord.banMember',
41
- /** Lifts the ban of a user. Arguments: `DiscordUnbanMemberArgs`. */
42
- UNBAN_MEMBER = 'discord.unbanMember',
43
- /** Times a member out, or lifts the timeout. Arguments: `DiscordTimeoutMemberArgs`. */
44
- TIMEOUT_MEMBER = 'discord.timeoutMember',
45
- /** Deletes 2 to 100 messages of a channel in one call. Arguments: `DiscordBulkDeleteMessagesArgs`. */
46
- BULK_DELETE_MESSAGES = 'discord.bulkDeleteMessages',
47
- /** Adds the bot's reaction to a message. Arguments: `DiscordReactionArgs`. */
48
- ADD_REACTION = 'discord.addReaction',
49
- /** Removes the bot's reaction, or a user's. Arguments: `DiscordRemoveReactionArgs`. */
50
- REMOVE_REACTION = 'discord.removeReaction',
51
- /** Creates a channel in a server. Arguments: `DiscordCreateChannelArgs`; result: `DiscordChannel`. */
52
- CREATE_CHANNEL = 'discord.createChannel',
53
- /** Deletes a channel or a thread. Arguments: `DiscordDeleteChannelArgs`. */
54
- DELETE_CHANNEL = 'discord.deleteChannel',
55
- /** Sets what a role or a member may do in a channel. Arguments: `DiscordSetChannelPermissionArgs`. */
56
- SET_CHANNEL_PERMISSION = 'discord.setChannelPermission',
57
- /** Removes what a channel sets for a role or a member. Arguments: `DiscordRemoveChannelPermissionArgs`. */
58
- REMOVE_CHANNEL_PERMISSION = 'discord.removeChannelPermission',
59
- /** Creates a role in a server. Arguments: `DiscordCreateRoleArgs`; result: `DiscordRole`. */
60
- CREATE_ROLE = 'discord.createRole',
61
- /** Deletes a role of a server. Arguments: `DiscordDeleteRoleArgs`. */
62
- DELETE_ROLE = 'discord.deleteRole',
63
- /** Starts a thread, from a message or not. Arguments: `DiscordCreateThreadArgs`; result: `DiscordChannel`. */
64
- CREATE_THREAD = 'discord.createThread',
65
- /** Ends a poll of the bot before its time. Arguments: `DiscordEndPollArgs`. */
66
- END_POLL = 'discord.endPoll',
67
- }
68
-
69
- /**
70
- * Bounds the host applies to the arguments of `abyss.discord.*` before any call to Discord: a call
71
- * outside them fails with an argument error, without any call to Discord (it is still billed as one call).
72
- */
73
- export const DISCORD_CAPABILITY_LIMITS = {
74
- /** `reason` of an action, sent as Discord's audit log reason. */
75
- auditReasonMaxLength: 512,
76
- /** Longest timeout Discord allows, from now. */
77
- timeoutMaxDays: 28,
78
- /** Messages a ban may delete, in seconds back from now (7 days). */
79
- banDeleteMessageSecondsMax: 604_800,
80
- bulkDeleteMin: 2,
81
- bulkDeleteMax: 100,
82
- channelNameMaxLength: 100,
83
- channelTopicMaxLength: 1024,
84
- permissionOverwritesMax: 100,
85
- roleNameMaxLength: 100,
86
- roleColorMax: 0xffffff,
87
- threadNameMaxLength: 100,
88
- /** Auto-archive durations Discord accepts, in minutes. */
89
- threadAutoArchiveMinutes: [60, 1440, 4320, 10080],
90
- pollQuestionMaxLength: 300,
91
- pollAnswersMax: 10,
92
- pollAnswerTextMaxLength: 55,
93
- /** 32 days. */
94
- pollDurationHoursMax: 768,
95
- pollDurationHoursDefault: 24,
96
- } as const;
97
-
98
- /**
99
- * Codes of the errors `abyss.discord.*` throws. The host fails the call with the message
100
- * `<code>: <detail>` (or the bare code); the helper rethrows an `Error` named `DiscordError`
101
- * whose `code` is the code and whose `message` is the detail.
102
- */
103
- export enum DiscordErrorCode {
104
- /** `showModal` after the interaction was acknowledged (more than 2.5 s after it arrived). */
105
- MODAL_TOO_LATE = 'MODAL_TOO_LATE',
106
- /** The bot's token is refused (circuit breaker open): no call was made to Discord. */
107
- DISCORD_TOKEN_INVALID = 'DISCORD_TOKEN_INVALID',
108
- /** Discord answered 403. */
109
- DISCORD_FORBIDDEN = 'DISCORD_FORBIDDEN',
110
- /** Discord answered 404. */
111
- DISCORD_NOT_FOUND = 'DISCORD_NOT_FOUND',
112
- /** Discord answered 429. */
113
- DISCORD_RATE_LIMITED = 'DISCORD_RATE_LIMITED',
114
- /**
115
- * `reply`, `update`, `showModal` or `followUp` in an execution no interaction started (a task,
116
- * a delayed run): there is no interaction to answer.
117
- */
118
- NO_INTERACTION = 'NO_INTERACTION',
119
- }
120
-
121
- /** Embed and component payloads are Discord's own JSON, relayed as is. */
122
- export type DiscordJsonObject = { [key: string]: JSONValue };
123
-
124
- /**
125
- * What a message may notify. `users` and `roles` let the mentions of every user or role ping;
126
- * `everyone` lets `@everyone` and `@here` notify the whole server.
127
- */
128
- export enum DiscordMentionType {
129
- USERS = 'users',
130
- ROLES = 'roles',
131
- EVERYONE = 'everyone',
132
- }
133
-
134
- /**
135
- * Who a message may notify: Discord's `allowed_mentions` in camelCase. Without it a message pings
136
- * the users it mentions and nobody else: `@everyone`, `@here` and roles stay plain text. An object
137
- * replaces that default as a whole, so what it leaves out notifies nobody. `parse` allows every
138
- * mention of a type; `users` and `roles` allow those ids only (at most 100 each). A type is given
139
- * by `parse` or by its list, never both. `repliedUser` is Discord's reply ping.
140
- */
141
- export interface DiscordAllowedMentions {
142
- parse?: DiscordMentionType[];
143
- users?: string[];
144
- roles?: string[];
145
- repliedUser?: boolean;
146
- }
147
-
148
- /** An answer of a poll. `emoji` is a unicode emoji or a server emoji `name:id`. */
149
- export interface DiscordPollAnswer {
150
- text: string;
151
- emoji?: string;
152
- }
153
-
154
- /**
155
- * A native Discord poll attached to a new message. Discord never edits a poll: `editMessage` and
156
- * `update` refuse a message that carries one.
157
- */
158
- export interface DiscordPoll {
159
- /** 1 to 300 characters. */
160
- question: string;
161
- /** 1 to 10 answers. */
162
- answers: DiscordPollAnswer[];
163
- /** 1 to 768 hours (32 days), 24 by default. */
164
- durationHours?: number;
165
- allowMultiselect?: boolean;
166
- }
167
-
168
- export interface DiscordMessage {
169
- content?: string;
170
- embeds?: DiscordJsonObject[];
171
- components?: DiscordJsonObject[];
172
- /** Only the reply to an interaction can be ephemeral. */
173
- ephemeral?: boolean;
174
- /** Who the message may notify; by default the mentioned users only (see `DiscordAllowedMentions`). */
175
- allowedMentions?: DiscordAllowedMentions;
176
- /** A poll; only on a new message (`sendMessage`, `sendDirectMessage`, `reply`, `followUp`). */
177
- poll?: DiscordPoll;
178
- }
179
-
180
- /** Text input style of a modal field. */
181
- export enum DiscordModalFieldStyle {
182
- SHORT = 'SHORT',
183
- PARAGRAPH = 'PARAGRAPH',
184
- }
185
-
186
- export interface DiscordModalField {
187
- customId: string;
188
- label: string;
189
- style?: DiscordModalFieldStyle;
190
- required?: boolean;
191
- placeholder?: string;
192
- value?: string;
193
- minLength?: number;
194
- maxLength?: number;
195
- }
196
-
197
- export interface DiscordShowModalArgs {
198
- customId: string;
199
- title: string;
200
- fields: DiscordModalField[];
201
- }
202
-
203
- export interface DiscordSendMessageArgs {
204
- channelId: string;
205
- message: DiscordMessage;
206
- }
207
-
208
- export interface DiscordEditMessageArgs {
209
- channelId: string;
210
- messageId: string;
211
- message: DiscordMessage;
212
- }
213
-
214
- /**
215
- * The audit log reason an action may carry: 1 to 512 characters, shown in the server's audit log
216
- * (Discord's `X-Audit-Log-Reason`).
217
- */
218
- export interface DiscordAuditReason {
219
- reason?: string;
220
- }
221
-
222
- export interface DiscordDeleteMessageArgs extends DiscordAuditReason {
223
- channelId: string;
224
- messageId: string;
225
- }
226
-
227
- export interface DiscordRoleArgs extends DiscordAuditReason {
228
- guildId: string;
229
- userId: string;
230
- roleId: string;
231
- }
232
-
233
- export interface DiscordKickMemberArgs extends DiscordAuditReason {
234
- guildId: string;
235
- userId: string;
236
- }
237
-
238
- export interface DiscordBanMemberArgs extends DiscordAuditReason {
239
- guildId: string;
240
- userId: string;
241
- /** Deletes the user's messages of that many seconds back, 0 to 604 800 (7 days); 0 by default. */
242
- deleteMessageSeconds?: number;
243
- }
244
-
245
- export interface DiscordUnbanMemberArgs extends DiscordAuditReason {
246
- guildId: string;
247
- userId: string;
248
- }
249
-
250
- export interface DiscordTimeoutMemberArgs extends DiscordAuditReason {
251
- guildId: string;
252
- userId: string;
253
- /** End of the timeout, ISO 8601, in the future and at most 28 days away; null lifts it. */
254
- until: string | null;
255
- }
256
-
257
- export interface DiscordBulkDeleteMessagesArgs extends DiscordAuditReason {
258
- channelId: string;
259
- /** 2 to 100 distinct ids; Discord refuses a message older than 14 days. */
260
- messageIds: string[];
261
- }
262
-
263
- /** `emoji` is a unicode emoji or a server emoji `name:id`. */
264
- export interface DiscordReactionArgs {
265
- channelId: string;
266
- messageId: string;
267
- emoji: string;
268
- }
269
-
270
- export interface DiscordRemoveReactionArgs extends DiscordReactionArgs {
271
- /** Whose reaction to remove; the bot's own without it. */
272
- userId?: string;
273
- }
274
-
275
- /** Discord's channel types a bot can create (`type` of `DiscordChannel` holds Discord's number). */
276
- export enum DiscordChannelType {
277
- TEXT = 0,
278
- VOICE = 2,
279
- CATEGORY = 4,
280
- ANNOUNCEMENT = 5,
281
- FORUM = 15,
282
- }
283
-
284
- /** What a channel permission applies to. */
285
- export enum DiscordPermissionTargetType {
286
- ROLE = 'ROLE',
287
- MEMBER = 'MEMBER',
288
- }
289
-
290
- /** Permissions a channel allows or denies to one role or member; bits as decimal strings. */
291
- export interface DiscordPermissionOverwrite {
292
- /** The role or member id. */
293
- id: string;
294
- type: DiscordPermissionTargetType;
295
- allow?: string;
296
- deny?: string;
297
- }
298
-
299
- export interface DiscordCreateChannelArgs extends DiscordAuditReason {
300
- guildId: string;
301
- /** 1 to 100 characters. */
302
- name: string;
303
- type: DiscordChannelType;
304
- /** The category the channel goes in. */
305
- parentId?: string;
306
- /** Up to 1024 characters. */
307
- topic?: string;
308
- nsfw?: boolean;
309
- /** 100 at most. */
310
- permissionOverwrites?: DiscordPermissionOverwrite[];
311
- }
312
-
313
- export interface DiscordDeleteChannelArgs extends DiscordAuditReason {
314
- channelId: string;
315
- }
316
-
317
- export interface DiscordSetChannelPermissionArgs extends DiscordAuditReason {
318
- channelId: string;
319
- targetId: string;
320
- targetType: DiscordPermissionTargetType;
321
- /** Permission bits as a decimal string; none by default. */
322
- allow?: string;
323
- deny?: string;
324
- }
325
-
326
- export interface DiscordRemoveChannelPermissionArgs extends DiscordAuditReason {
327
- channelId: string;
328
- targetId: string;
329
- }
330
-
331
- export interface DiscordCreateRoleArgs extends DiscordAuditReason {
332
- guildId: string;
333
- /** 1 to 100 characters. */
334
- name: string;
335
- /** Permission bits as a decimal string; Discord's default without it. */
336
- permissions?: string;
337
- /** RGB color as an integer, 0 to 0xFFFFFF. */
338
- color?: number;
339
- hoist?: boolean;
340
- mentionable?: boolean;
341
- }
342
-
343
- export interface DiscordDeleteRoleArgs extends DiscordAuditReason {
344
- guildId: string;
345
- roleId: string;
346
- }
347
-
348
- export interface DiscordCreateThreadArgs extends DiscordAuditReason {
349
- channelId: string;
350
- /** 1 to 100 characters. */
351
- name: string;
352
- /** Starts the thread from this message; without it the thread stands alone. */
353
- messageId?: string;
354
- /** A private thread; only without `messageId`. */
355
- private?: boolean;
356
- /** 60, 1440, 4320 or 10080. */
357
- autoArchiveMinutes?: number;
358
- }
359
-
360
- export interface DiscordEndPollArgs {
361
- channelId: string;
362
- messageId: string;
363
- }
364
-
365
- export interface DiscordSendDirectMessageArgs {
366
- userId: string;
367
- message: DiscordMessage;
368
- }
369
-
370
- export interface DiscordGetMemberArgs {
371
- guildId: string;
372
- userId: string;
373
- }
374
-
375
- export interface DiscordGetGuildRolesArgs {
376
- guildId: string;
377
- }
378
-
379
- export interface DiscordGetChannelArgs {
380
- channelId: string;
381
- }
382
-
383
- export interface DiscordGetGuildArgs {
384
- guildId: string;
385
- }
386
-
387
- /** A Discord user, as the reads return it. */
388
- export interface DiscordUser {
389
- id: string;
390
- username: string;
391
- globalName: string | null;
392
- bot: boolean;
393
- }
394
-
395
- /**
396
- * A member of a server (`discord.getMember`): Discord's guild member in camelCase. The read gives
397
- * `null` instead when the user is not in the server. Dates are ISO 8601.
398
- */
399
- export interface DiscordMember {
400
- userId: string;
401
- nick: string | null;
402
- /** Role ids. */
403
- roles: string[];
404
- joinedAt: string | null;
405
- premiumSince: string | null;
406
- communicationDisabledUntil: string | null;
407
- /** True while the member has not passed the server's membership screening. */
408
- pending: boolean;
409
- /** Avatar hash specific to the server. */
410
- avatar: string | null;
411
- user: DiscordUser;
412
- }
413
-
414
- /**
415
- * A role of a server (`discord.getGuildRoles`, `DiscordGuild.roles`). `permissions` is Discord's
416
- * permission bit set as a decimal string: it exceeds 2^53.
417
- */
418
- export interface DiscordRole {
419
- id: string;
420
- name: string;
421
- /** RGB color as an integer, 0 for none. */
422
- color: number;
423
- position: number;
424
- permissions: string;
425
- managed: boolean;
426
- mentionable: boolean;
427
- hoist: boolean;
428
- }
429
-
430
- /** A channel (`discord.getChannel`). `type` is Discord's channel type number. */
431
- export interface DiscordChannel {
432
- id: string;
433
- type: number;
434
- name: string | null;
435
- guildId: string | null;
436
- parentId: string | null;
437
- topic: string | null;
438
- nsfw: boolean;
439
- position: number | null;
440
- }
441
-
442
- /** A server (`discord.getGuild`). The approximate counts are null when Discord does not give them. */
443
- export interface DiscordGuild {
444
- id: string;
445
- name: string;
446
- /** Icon hash. */
447
- icon: string | null;
448
- ownerId: string;
449
- preferredLocale: string;
450
- roles: DiscordRole[];
451
- approximateMemberCount: number | null;
452
- approximatePresenceCount: number | null;
453
- }
454
-
455
- /** The kind of interaction a function was started by. */
456
- export enum DiscordInteractionKind {
457
- COMMAND = 'COMMAND',
458
- COMPONENT = 'COMPONENT',
459
- MODAL = 'MODAL',
460
- }
461
-
462
- /** Discord's interaction types (`type` of the payload) a bot's edge serves. */
463
- export enum DiscordInteractionType {
464
- PING = 1,
465
- APPLICATION_COMMAND = 2,
466
- MESSAGE_COMPONENT = 3,
467
- MODAL_SUBMIT = 5,
468
- }
469
-
470
- /** Discord's interaction callback types the edge answers with. */
471
- export enum DiscordCallbackType {
472
- PONG = 1,
473
- CHANNEL_MESSAGE_WITH_SOURCE = 4,
474
- DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE = 5,
475
- DEFERRED_UPDATE_MESSAGE = 6,
476
- UPDATE_MESSAGE = 7,
477
- MODAL = 9,
478
- }
479
-
480
- /** Discord's message flag that makes a message visible to its recipient only. */
481
- export const EPHEMERAL_FLAG = 64;
482
-
483
- /** A member as an interaction resolves it: partial, without its user (see `DiscordResolved.users`). */
484
- export interface DiscordResolvedMember {
485
- nick: string | null;
486
- /** Role ids. */
487
- roles: string[];
488
- joinedAt: string | null;
489
- /** The member's permissions in the channel, decimal string; null when Discord does not give them. */
490
- permissions: string | null;
491
- }
492
-
493
- /** A file a user attached to an ATTACHMENT option. */
494
- export interface DiscordAttachment {
495
- id: string;
496
- filename: string;
497
- /** Bytes. */
498
- size: number;
499
- url: string;
500
- contentType: string | null;
501
- }
502
-
503
- /**
504
- * The users, members, roles, channels and files an interaction names, by id, as Discord resolves
505
- * them for its options and typed select menus. A key is absent when Discord resolved none of its kind.
506
- */
507
- export interface DiscordResolved {
508
- users?: Record<string, DiscordUser>;
509
- members?: Record<string, DiscordResolvedMember>;
510
- roles?: Record<string, DiscordRole>;
511
- /** Partial channels: what Discord gives in an interaction. */
512
- channels?: Record<string, DiscordChannel>;
513
- attachments?: Record<string, DiscordAttachment>;
514
- }
515
-
516
- /**
517
- * The `input` of a function triggered by a Discord interaction. The interaction token never
518
- * appears here: it stays in the edge.
519
- */
520
- export interface DiscordInteractionInput {
521
- kind: DiscordInteractionKind;
522
- /** Resolved option values, by option name; `resolved` details the ids they hold, when there are some. */
523
- command?: {
524
- name: string;
525
- options: Record<string, string | number | boolean>;
526
- resolved?: DiscordResolved;
527
- };
528
- /** `values` of a select menu; `resolved` details them for a user, role, channel or mentionable menu. */
529
- component?: { customId: string; values?: string[]; resolved?: DiscordResolved };
530
- modal?: { customId: string; fields: Record<string, string> };
531
- user: { id: string; username: string; globalName: string | null };
532
- member?: { roles: string[]; permissions: string };
533
- guildId?: string;
534
- channelId?: string;
535
- locale: string;
536
- /** Discord's payload, without the interaction token. */
537
- interaction: { id: string; applicationId: string; type: number; data: JSONValue };
538
- }
539
-
540
- /** The Gateway events a bot's function can be triggered by. */
541
- export enum DiscordEventType {
542
- GUILD_MEMBER_ADD = 'GUILD_MEMBER_ADD',
543
- GUILD_MEMBER_REMOVE = 'GUILD_MEMBER_REMOVE',
544
- MESSAGE_CREATE = 'MESSAGE_CREATE',
545
- MESSAGE_REACTION_ADD = 'MESSAGE_REACTION_ADD',
546
- }
547
-
548
- /** `data` of a `GUILD_MEMBER_ADD` event. */
549
- export interface DiscordMemberAddData {
550
- user: {
551
- id: string;
552
- username: string;
553
- globalName: string | null;
554
- bot: boolean;
555
- avatar: string | null;
556
- };
557
- /** Role ids. */
558
- roles: string[];
559
- joinedAt: string | null;
560
- /** True while the member has not passed the server's membership screening. */
561
- pending: boolean;
562
- }
563
-
564
- /** `data` of a `GUILD_MEMBER_REMOVE` event. */
565
- export interface DiscordMemberRemoveData {
566
- user: { id: string; username: string; globalName: string | null; bot: boolean };
567
- }
568
-
569
- /** `data` of a `MESSAGE_CREATE` event. */
570
- export interface DiscordMessageCreateData {
571
- id: string;
572
- channelId: string;
573
- author: { id: string; username: string; globalName: string | null; bot: boolean };
574
- member: { roles: string[]; nick: string | null } | null;
575
- /** Ids of the mentioned users. */
576
- mentions: string[];
577
- referencedMessageId: string | null;
578
- attachmentCount: number;
579
- /** ISO 8601. */
580
- createdAt: string;
581
- /** Present only when the trigger asks for the content (`includeContent`). */
582
- content?: string;
583
- }
584
-
585
- /** `data` of a `MESSAGE_REACTION_ADD` event. */
586
- export interface DiscordReactionAddData {
587
- userId: string;
588
- channelId: string;
589
- messageId: string;
590
- messageAuthorId: string | null;
591
- member: { roles: string[] } | null;
592
- emoji: { id: string | null; name: string | null; animated: boolean };
593
- }
594
-
595
- /** The `kind` of a `DiscordEventInput`. */
596
- export const DISCORD_EVENT_INPUT_KIND = 'DISCORD_EVENT';
597
-
598
- /** The `input` of a function triggered by a Discord Gateway event. */
599
- export interface DiscordEventInput {
600
- kind: typeof DISCORD_EVENT_INPUT_KIND;
601
- event: { id: string; type: DiscordEventType; trigger: { id: string; name: string } };
602
- guildId: string;
603
- /** When the Gateway received it, ISO 8601. */
604
- receivedAt: string;
605
- firedAt: string;
606
- attempt: number;
607
- data:
608
- | DiscordMemberAddData
609
- | DiscordMemberRemoveData
610
- | DiscordMessageCreateData
611
- | DiscordReactionAddData;
612
- }
613
-
614
- const isRecord = (value: unknown): value is Record<string, unknown> =>
615
- typeof value === 'object' && value !== null && !Array.isArray(value);
616
-
617
- /**
618
- * Tells a `DiscordEventInput` from the other inputs of a function (interaction, scheduled run). It
619
- * checks the envelope (`kind`, event, guild, dates, attempt, `data` object), not the shape of `data`.
620
- */
621
- export const isDiscordEventInput = (value: unknown): value is DiscordEventInput => {
622
- if (!isRecord(value) || value.kind !== DISCORD_EVENT_INPUT_KIND) {
623
- return false;
624
- }
625
- const { event } = value;
626
- return (
627
- isRecord(event) &&
628
- typeof event.id === 'string' &&
629
- Object.values(DiscordEventType).includes(event.type as DiscordEventType) &&
630
- isRecord(event.trigger) &&
631
- typeof event.trigger.id === 'string' &&
632
- typeof event.trigger.name === 'string' &&
633
- typeof value.guildId === 'string' &&
634
- typeof value.receivedAt === 'string' &&
635
- typeof value.firedAt === 'string' &&
636
- typeof value.attempt === 'number' &&
637
- isRecord(value.data)
638
- );
639
- };
1
+ import type { JSONValue } from '../runner-protocol/execution-protocol.types';
2
+
3
+ /**
4
+ * Host capabilities of a Discord bot Service. The host declares them in the execution's
5
+ * capabilities; client code reaches them through `abyss.discord.<name>(...)`; every helper exists,
6
+ * and one whose capability is not granted rejects with an error naming the action to declare. The argument shapes below are the `args` of the `call`.
7
+ */
8
+ export enum DiscordCapability {
9
+ /** Answers the interaction. Arguments: a `DiscordMessage`. */
10
+ REPLY = 'discord.reply',
11
+ /** Edits the message a component interaction came from. Arguments: a `DiscordMessage`. */
12
+ UPDATE = 'discord.update',
13
+ /** Opens a modal, before the interaction is acknowledged. Arguments: `DiscordShowModalArgs`. */
14
+ SHOW_MODAL = 'discord.showModal',
15
+ /** Sends an additional message for the interaction. Arguments: a `DiscordMessage`. */
16
+ FOLLOW_UP = 'discord.followUp',
17
+ /** Posts a message in a channel as the bot. Arguments: `DiscordSendMessageArgs`. */
18
+ SEND_MESSAGE = 'discord.sendMessage',
19
+ /** Edits a message of the bot. Arguments: `DiscordEditMessageArgs`. */
20
+ EDIT_MESSAGE = 'discord.editMessage',
21
+ /** Deletes a message. Arguments: `DiscordDeleteMessageArgs`. */
22
+ DELETE_MESSAGE = 'discord.deleteMessage',
23
+ /** Gives a role to a member. Arguments: `DiscordRoleArgs`. */
24
+ ADD_ROLE = 'discord.addRole',
25
+ /** Takes a role from a member. Arguments: `DiscordRoleArgs`. */
26
+ REMOVE_ROLE = 'discord.removeRole',
27
+ /** Sends a direct message to a user. Arguments: `DiscordSendDirectMessageArgs`. */
28
+ SEND_DIRECT_MESSAGE = 'discord.sendDirectMessage',
29
+ /** Reads a member of a server. Arguments: `DiscordGetMemberArgs`; result: `DiscordMember | null`. */
30
+ GET_MEMBER = 'discord.getMember',
31
+ /** Reads the roles of a server. Arguments: `DiscordGetGuildRolesArgs`; result: `DiscordRole[]`. */
32
+ GET_GUILD_ROLES = 'discord.getGuildRoles',
33
+ /** Reads a channel. Arguments: `DiscordGetChannelArgs`; result: `DiscordChannel`. */
34
+ GET_CHANNEL = 'discord.getChannel',
35
+ /** Reads a server. Arguments: `DiscordGetGuildArgs`; result: `DiscordGuild`. */
36
+ GET_GUILD = 'discord.getGuild',
37
+ /** Removes a member from a server. Arguments: `DiscordKickMemberArgs`. */
38
+ KICK_MEMBER = 'discord.kickMember',
39
+ /** Bans a user from a server. Arguments: `DiscordBanMemberArgs`. */
40
+ BAN_MEMBER = 'discord.banMember',
41
+ /** Lifts the ban of a user. Arguments: `DiscordUnbanMemberArgs`. */
42
+ UNBAN_MEMBER = 'discord.unbanMember',
43
+ /** Times a member out, or lifts the timeout. Arguments: `DiscordTimeoutMemberArgs`. */
44
+ TIMEOUT_MEMBER = 'discord.timeoutMember',
45
+ /** Deletes 2 to 100 messages of a channel in one call. Arguments: `DiscordBulkDeleteMessagesArgs`. */
46
+ BULK_DELETE_MESSAGES = 'discord.bulkDeleteMessages',
47
+ /** Adds the bot's reaction to a message. Arguments: `DiscordReactionArgs`. */
48
+ ADD_REACTION = 'discord.addReaction',
49
+ /** Removes the bot's reaction, or a user's. Arguments: `DiscordRemoveReactionArgs`. */
50
+ REMOVE_REACTION = 'discord.removeReaction',
51
+ /** Creates a channel in a server. Arguments: `DiscordCreateChannelArgs`; result: `DiscordChannel`. */
52
+ CREATE_CHANNEL = 'discord.createChannel',
53
+ /** Deletes a channel or a thread. Arguments: `DiscordDeleteChannelArgs`. */
54
+ DELETE_CHANNEL = 'discord.deleteChannel',
55
+ /** Sets what a role or a member may do in a channel. Arguments: `DiscordSetChannelPermissionArgs`. */
56
+ SET_CHANNEL_PERMISSION = 'discord.setChannelPermission',
57
+ /** Removes what a channel sets for a role or a member. Arguments: `DiscordRemoveChannelPermissionArgs`. */
58
+ REMOVE_CHANNEL_PERMISSION = 'discord.removeChannelPermission',
59
+ /** Creates a role in a server. Arguments: `DiscordCreateRoleArgs`; result: `DiscordRole`. */
60
+ CREATE_ROLE = 'discord.createRole',
61
+ /** Deletes a role of a server. Arguments: `DiscordDeleteRoleArgs`. */
62
+ DELETE_ROLE = 'discord.deleteRole',
63
+ /** Starts a thread, from a message or not. Arguments: `DiscordCreateThreadArgs`; result: `DiscordChannel`. */
64
+ CREATE_THREAD = 'discord.createThread',
65
+ /** Ends a poll of the bot before its time. Arguments: `DiscordEndPollArgs`. */
66
+ END_POLL = 'discord.endPoll',
67
+ }
68
+
69
+ /**
70
+ * Bounds the host applies to the arguments of `abyss.discord.*` before any call to Discord: a call
71
+ * outside them fails with an argument error, without any call to Discord (it is still billed as one call).
72
+ */
73
+ export const DISCORD_CAPABILITY_LIMITS = {
74
+ /** `reason` of an action, sent as Discord's audit log reason. */
75
+ auditReasonMaxLength: 512,
76
+ /** Longest timeout Discord allows, from now. */
77
+ timeoutMaxDays: 28,
78
+ /** Messages a ban may delete, in seconds back from now (7 days). */
79
+ banDeleteMessageSecondsMax: 604_800,
80
+ bulkDeleteMin: 2,
81
+ bulkDeleteMax: 100,
82
+ channelNameMaxLength: 100,
83
+ channelTopicMaxLength: 1024,
84
+ permissionOverwritesMax: 100,
85
+ roleNameMaxLength: 100,
86
+ roleColorMax: 0xffffff,
87
+ threadNameMaxLength: 100,
88
+ /** Auto-archive durations Discord accepts, in minutes. */
89
+ threadAutoArchiveMinutes: [60, 1440, 4320, 10080],
90
+ pollQuestionMaxLength: 300,
91
+ pollAnswersMax: 10,
92
+ pollAnswerTextMaxLength: 55,
93
+ /** 32 days. */
94
+ pollDurationHoursMax: 768,
95
+ pollDurationHoursDefault: 24,
96
+ } as const;
97
+
98
+ /**
99
+ * Codes of the errors `abyss.discord.*` throws. The host fails the call with the message
100
+ * `<code>: <detail>` (or the bare code); the helper rethrows an `Error` named `DiscordError`
101
+ * whose `code` is the code and whose `message` is the detail.
102
+ */
103
+ export enum DiscordErrorCode {
104
+ /** `showModal` after the interaction was acknowledged (more than 2.5 s after it arrived). */
105
+ MODAL_TOO_LATE = 'MODAL_TOO_LATE',
106
+ /** The bot's token is refused (circuit breaker open): no call was made to Discord. */
107
+ DISCORD_TOKEN_INVALID = 'DISCORD_TOKEN_INVALID',
108
+ /** Discord answered 403. */
109
+ DISCORD_FORBIDDEN = 'DISCORD_FORBIDDEN',
110
+ /** Discord answered 404. */
111
+ DISCORD_NOT_FOUND = 'DISCORD_NOT_FOUND',
112
+ /** Discord answered 429. */
113
+ DISCORD_RATE_LIMITED = 'DISCORD_RATE_LIMITED',
114
+ /**
115
+ * `reply`, `update`, `showModal` or `followUp` in an execution no interaction started (a task,
116
+ * a delayed run): there is no interaction to answer.
117
+ */
118
+ NO_INTERACTION = 'NO_INTERACTION',
119
+ }
120
+
121
+ /** Embed and component payloads are Discord's own JSON, relayed as is. */
122
+ export type DiscordJsonObject = { [key: string]: JSONValue };
123
+
124
+ /**
125
+ * What a message may notify. `users` and `roles` let the mentions of every user or role ping;
126
+ * `everyone` lets `@everyone` and `@here` notify the whole server.
127
+ */
128
+ export enum DiscordMentionType {
129
+ USERS = 'users',
130
+ ROLES = 'roles',
131
+ EVERYONE = 'everyone',
132
+ }
133
+
134
+ /**
135
+ * Who a message may notify: Discord's `allowed_mentions` in camelCase. Without it a message pings
136
+ * the users it mentions and nobody else: `@everyone`, `@here` and roles stay plain text. An object
137
+ * replaces that default as a whole, so what it leaves out notifies nobody. `parse` allows every
138
+ * mention of a type; `users` and `roles` allow those ids only (at most 100 each). A type is given
139
+ * by `parse` or by its list, never both. `repliedUser` is Discord's reply ping.
140
+ */
141
+ export interface DiscordAllowedMentions {
142
+ parse?: DiscordMentionType[];
143
+ users?: string[];
144
+ roles?: string[];
145
+ repliedUser?: boolean;
146
+ }
147
+
148
+ /** An answer of a poll. `emoji` is a unicode emoji or a server emoji `name:id`. */
149
+ export interface DiscordPollAnswer {
150
+ text: string;
151
+ emoji?: string;
152
+ }
153
+
154
+ /**
155
+ * A native Discord poll attached to a new message. Discord never edits a poll: `editMessage` and
156
+ * `update` refuse a message that carries one.
157
+ */
158
+ export interface DiscordPoll {
159
+ /** 1 to 300 characters. */
160
+ question: string;
161
+ /** 1 to 10 answers. */
162
+ answers: DiscordPollAnswer[];
163
+ /** 1 to 768 hours (32 days), 24 by default. */
164
+ durationHours?: number;
165
+ allowMultiselect?: boolean;
166
+ }
167
+
168
+ export interface DiscordMessage {
169
+ content?: string;
170
+ embeds?: DiscordJsonObject[];
171
+ components?: DiscordJsonObject[];
172
+ /** Only the reply to an interaction can be ephemeral. */
173
+ ephemeral?: boolean;
174
+ /** Who the message may notify; by default the mentioned users only (see `DiscordAllowedMentions`). */
175
+ allowedMentions?: DiscordAllowedMentions;
176
+ /** A poll; only on a new message (`sendMessage`, `sendDirectMessage`, `reply`, `followUp`). */
177
+ poll?: DiscordPoll;
178
+ }
179
+
180
+ /** Text input style of a modal field. */
181
+ export enum DiscordModalFieldStyle {
182
+ SHORT = 'SHORT',
183
+ PARAGRAPH = 'PARAGRAPH',
184
+ }
185
+
186
+ export interface DiscordModalField {
187
+ customId: string;
188
+ label: string;
189
+ style?: DiscordModalFieldStyle;
190
+ required?: boolean;
191
+ placeholder?: string;
192
+ value?: string;
193
+ minLength?: number;
194
+ maxLength?: number;
195
+ }
196
+
197
+ export interface DiscordShowModalArgs {
198
+ customId: string;
199
+ title: string;
200
+ fields: DiscordModalField[];
201
+ }
202
+
203
+ export interface DiscordSendMessageArgs {
204
+ channelId: string;
205
+ message: DiscordMessage;
206
+ }
207
+
208
+ export interface DiscordEditMessageArgs {
209
+ channelId: string;
210
+ messageId: string;
211
+ message: DiscordMessage;
212
+ }
213
+
214
+ /**
215
+ * The audit log reason an action may carry: 1 to 512 characters, shown in the server's audit log
216
+ * (Discord's `X-Audit-Log-Reason`).
217
+ */
218
+ export interface DiscordAuditReason {
219
+ reason?: string;
220
+ }
221
+
222
+ export interface DiscordDeleteMessageArgs extends DiscordAuditReason {
223
+ channelId: string;
224
+ messageId: string;
225
+ }
226
+
227
+ export interface DiscordRoleArgs extends DiscordAuditReason {
228
+ guildId: string;
229
+ userId: string;
230
+ roleId: string;
231
+ }
232
+
233
+ export interface DiscordKickMemberArgs extends DiscordAuditReason {
234
+ guildId: string;
235
+ userId: string;
236
+ }
237
+
238
+ export interface DiscordBanMemberArgs extends DiscordAuditReason {
239
+ guildId: string;
240
+ userId: string;
241
+ /** Deletes the user's messages of that many seconds back, 0 to 604 800 (7 days); 0 by default. */
242
+ deleteMessageSeconds?: number;
243
+ }
244
+
245
+ export interface DiscordUnbanMemberArgs extends DiscordAuditReason {
246
+ guildId: string;
247
+ userId: string;
248
+ }
249
+
250
+ export interface DiscordTimeoutMemberArgs extends DiscordAuditReason {
251
+ guildId: string;
252
+ userId: string;
253
+ /** End of the timeout, ISO 8601, in the future and at most 28 days away; null lifts it. */
254
+ until: string | null;
255
+ }
256
+
257
+ export interface DiscordBulkDeleteMessagesArgs extends DiscordAuditReason {
258
+ channelId: string;
259
+ /** 2 to 100 distinct ids; Discord refuses a message older than 14 days. */
260
+ messageIds: string[];
261
+ }
262
+
263
+ /** `emoji` is a unicode emoji or a server emoji `name:id`. */
264
+ export interface DiscordReactionArgs {
265
+ channelId: string;
266
+ messageId: string;
267
+ emoji: string;
268
+ }
269
+
270
+ export interface DiscordRemoveReactionArgs extends DiscordReactionArgs {
271
+ /** Whose reaction to remove; the bot's own without it. */
272
+ userId?: string;
273
+ }
274
+
275
+ /** Discord's channel types a bot can create (`type` of `DiscordChannel` holds Discord's number). */
276
+ export enum DiscordChannelType {
277
+ TEXT = 0,
278
+ VOICE = 2,
279
+ CATEGORY = 4,
280
+ ANNOUNCEMENT = 5,
281
+ FORUM = 15,
282
+ }
283
+
284
+ /** What a channel permission applies to. */
285
+ export enum DiscordPermissionTargetType {
286
+ ROLE = 'ROLE',
287
+ MEMBER = 'MEMBER',
288
+ }
289
+
290
+ /** Permissions a channel allows or denies to one role or member; bits as decimal strings. */
291
+ export interface DiscordPermissionOverwrite {
292
+ /** The role or member id. */
293
+ id: string;
294
+ type: DiscordPermissionTargetType;
295
+ allow?: string;
296
+ deny?: string;
297
+ }
298
+
299
+ export interface DiscordCreateChannelArgs extends DiscordAuditReason {
300
+ guildId: string;
301
+ /** 1 to 100 characters. */
302
+ name: string;
303
+ type: DiscordChannelType;
304
+ /** The category the channel goes in. */
305
+ parentId?: string;
306
+ /** Up to 1024 characters. */
307
+ topic?: string;
308
+ nsfw?: boolean;
309
+ /** 100 at most. */
310
+ permissionOverwrites?: DiscordPermissionOverwrite[];
311
+ }
312
+
313
+ export interface DiscordDeleteChannelArgs extends DiscordAuditReason {
314
+ channelId: string;
315
+ }
316
+
317
+ export interface DiscordSetChannelPermissionArgs extends DiscordAuditReason {
318
+ channelId: string;
319
+ targetId: string;
320
+ targetType: DiscordPermissionTargetType;
321
+ /** Permission bits as a decimal string; none by default. */
322
+ allow?: string;
323
+ deny?: string;
324
+ }
325
+
326
+ export interface DiscordRemoveChannelPermissionArgs extends DiscordAuditReason {
327
+ channelId: string;
328
+ targetId: string;
329
+ }
330
+
331
+ export interface DiscordCreateRoleArgs extends DiscordAuditReason {
332
+ guildId: string;
333
+ /** 1 to 100 characters. */
334
+ name: string;
335
+ /** Permission bits as a decimal string; Discord's default without it. */
336
+ permissions?: string;
337
+ /** RGB color as an integer, 0 to 0xFFFFFF. */
338
+ color?: number;
339
+ hoist?: boolean;
340
+ mentionable?: boolean;
341
+ }
342
+
343
+ export interface DiscordDeleteRoleArgs extends DiscordAuditReason {
344
+ guildId: string;
345
+ roleId: string;
346
+ }
347
+
348
+ export interface DiscordCreateThreadArgs extends DiscordAuditReason {
349
+ channelId: string;
350
+ /** 1 to 100 characters. */
351
+ name: string;
352
+ /** Starts the thread from this message; without it the thread stands alone. */
353
+ messageId?: string;
354
+ /** A private thread; only without `messageId`. */
355
+ private?: boolean;
356
+ /** 60, 1440, 4320 or 10080. */
357
+ autoArchiveMinutes?: number;
358
+ }
359
+
360
+ export interface DiscordEndPollArgs {
361
+ channelId: string;
362
+ messageId: string;
363
+ }
364
+
365
+ export interface DiscordSendDirectMessageArgs {
366
+ userId: string;
367
+ message: DiscordMessage;
368
+ }
369
+
370
+ export interface DiscordGetMemberArgs {
371
+ guildId: string;
372
+ userId: string;
373
+ }
374
+
375
+ export interface DiscordGetGuildRolesArgs {
376
+ guildId: string;
377
+ }
378
+
379
+ export interface DiscordGetChannelArgs {
380
+ channelId: string;
381
+ }
382
+
383
+ export interface DiscordGetGuildArgs {
384
+ guildId: string;
385
+ }
386
+
387
+ /** A Discord user, as the reads return it. */
388
+ export interface DiscordUser {
389
+ id: string;
390
+ username: string;
391
+ globalName: string | null;
392
+ bot: boolean;
393
+ }
394
+
395
+ /**
396
+ * A member of a server (`discord.getMember`): Discord's guild member in camelCase. The read gives
397
+ * `null` instead when the user is not in the server. Dates are ISO 8601.
398
+ */
399
+ export interface DiscordMember {
400
+ userId: string;
401
+ nick: string | null;
402
+ /** Role ids. */
403
+ roles: string[];
404
+ joinedAt: string | null;
405
+ premiumSince: string | null;
406
+ communicationDisabledUntil: string | null;
407
+ /** True while the member has not passed the server's membership screening. */
408
+ pending: boolean;
409
+ /** Avatar hash specific to the server. */
410
+ avatar: string | null;
411
+ user: DiscordUser;
412
+ }
413
+
414
+ /**
415
+ * A role of a server (`discord.getGuildRoles`, `DiscordGuild.roles`). `permissions` is Discord's
416
+ * permission bit set as a decimal string: it exceeds 2^53.
417
+ */
418
+ export interface DiscordRole {
419
+ id: string;
420
+ name: string;
421
+ /** RGB color as an integer, 0 for none. */
422
+ color: number;
423
+ position: number;
424
+ permissions: string;
425
+ managed: boolean;
426
+ mentionable: boolean;
427
+ hoist: boolean;
428
+ }
429
+
430
+ /** A channel (`discord.getChannel`). `type` is Discord's channel type number. */
431
+ export interface DiscordChannel {
432
+ id: string;
433
+ type: number;
434
+ name: string | null;
435
+ guildId: string | null;
436
+ parentId: string | null;
437
+ topic: string | null;
438
+ nsfw: boolean;
439
+ position: number | null;
440
+ }
441
+
442
+ /** A server (`discord.getGuild`). The approximate counts are null when Discord does not give them. */
443
+ export interface DiscordGuild {
444
+ id: string;
445
+ name: string;
446
+ /** Icon hash. */
447
+ icon: string | null;
448
+ ownerId: string;
449
+ preferredLocale: string;
450
+ roles: DiscordRole[];
451
+ approximateMemberCount: number | null;
452
+ approximatePresenceCount: number | null;
453
+ }
454
+
455
+ /** The kind of interaction a function was started by. */
456
+ export enum DiscordInteractionKind {
457
+ COMMAND = 'COMMAND',
458
+ COMPONENT = 'COMPONENT',
459
+ MODAL = 'MODAL',
460
+ }
461
+
462
+ /** Discord's interaction types (`type` of the payload) a bot's edge serves. */
463
+ export enum DiscordInteractionType {
464
+ PING = 1,
465
+ APPLICATION_COMMAND = 2,
466
+ MESSAGE_COMPONENT = 3,
467
+ MODAL_SUBMIT = 5,
468
+ }
469
+
470
+ /** Discord's interaction callback types the edge answers with. */
471
+ export enum DiscordCallbackType {
472
+ PONG = 1,
473
+ CHANNEL_MESSAGE_WITH_SOURCE = 4,
474
+ DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE = 5,
475
+ DEFERRED_UPDATE_MESSAGE = 6,
476
+ UPDATE_MESSAGE = 7,
477
+ MODAL = 9,
478
+ }
479
+
480
+ /** Discord's message flag that makes a message visible to its recipient only. */
481
+ export const EPHEMERAL_FLAG = 64;
482
+
483
+ /** A member as an interaction resolves it: partial, without its user (see `DiscordResolved.users`). */
484
+ export interface DiscordResolvedMember {
485
+ nick: string | null;
486
+ /** Role ids. */
487
+ roles: string[];
488
+ joinedAt: string | null;
489
+ /** The member's permissions in the channel, decimal string; null when Discord does not give them. */
490
+ permissions: string | null;
491
+ }
492
+
493
+ /** A file a user attached to an ATTACHMENT option. */
494
+ export interface DiscordAttachment {
495
+ id: string;
496
+ filename: string;
497
+ /** Bytes. */
498
+ size: number;
499
+ url: string;
500
+ contentType: string | null;
501
+ }
502
+
503
+ /**
504
+ * The users, members, roles, channels and files an interaction names, by id, as Discord resolves
505
+ * them for its options and typed select menus. A key is absent when Discord resolved none of its kind.
506
+ */
507
+ export interface DiscordResolved {
508
+ users?: Record<string, DiscordUser>;
509
+ members?: Record<string, DiscordResolvedMember>;
510
+ roles?: Record<string, DiscordRole>;
511
+ /** Partial channels: what Discord gives in an interaction. */
512
+ channels?: Record<string, DiscordChannel>;
513
+ attachments?: Record<string, DiscordAttachment>;
514
+ }
515
+
516
+ /**
517
+ * The `input` of a function triggered by a Discord interaction. The interaction token never
518
+ * appears here: it stays in the edge.
519
+ */
520
+ export interface DiscordInteractionInput {
521
+ kind: DiscordInteractionKind;
522
+ /** Resolved option values, by option name; `resolved` details the ids they hold, when there are some. */
523
+ command?: {
524
+ name: string;
525
+ options: Record<string, string | number | boolean>;
526
+ resolved?: DiscordResolved;
527
+ };
528
+ /** `values` of a select menu; `resolved` details them for a user, role, channel or mentionable menu. */
529
+ component?: { customId: string; values?: string[]; resolved?: DiscordResolved };
530
+ modal?: { customId: string; fields: Record<string, string> };
531
+ user: { id: string; username: string; globalName: string | null };
532
+ member?: { roles: string[]; permissions: string };
533
+ guildId?: string;
534
+ channelId?: string;
535
+ locale: string;
536
+ /** Discord's payload, without the interaction token. */
537
+ interaction: { id: string; applicationId: string; type: number; data: JSONValue };
538
+ }
539
+
540
+ /** The Gateway events a bot's function can be triggered by. */
541
+ export enum DiscordEventType {
542
+ GUILD_MEMBER_ADD = 'GUILD_MEMBER_ADD',
543
+ GUILD_MEMBER_REMOVE = 'GUILD_MEMBER_REMOVE',
544
+ MESSAGE_CREATE = 'MESSAGE_CREATE',
545
+ MESSAGE_REACTION_ADD = 'MESSAGE_REACTION_ADD',
546
+ }
547
+
548
+ /** `data` of a `GUILD_MEMBER_ADD` event. */
549
+ export interface DiscordMemberAddData {
550
+ user: {
551
+ id: string;
552
+ username: string;
553
+ globalName: string | null;
554
+ bot: boolean;
555
+ avatar: string | null;
556
+ };
557
+ /** Role ids. */
558
+ roles: string[];
559
+ joinedAt: string | null;
560
+ /** True while the member has not passed the server's membership screening. */
561
+ pending: boolean;
562
+ }
563
+
564
+ /** `data` of a `GUILD_MEMBER_REMOVE` event. */
565
+ export interface DiscordMemberRemoveData {
566
+ user: { id: string; username: string; globalName: string | null; bot: boolean };
567
+ }
568
+
569
+ /** `data` of a `MESSAGE_CREATE` event. */
570
+ export interface DiscordMessageCreateData {
571
+ id: string;
572
+ channelId: string;
573
+ author: { id: string; username: string; globalName: string | null; bot: boolean };
574
+ member: { roles: string[]; nick: string | null } | null;
575
+ /** Ids of the mentioned users. */
576
+ mentions: string[];
577
+ referencedMessageId: string | null;
578
+ attachmentCount: number;
579
+ /** ISO 8601. */
580
+ createdAt: string;
581
+ /** Present only when the trigger asks for the content (`includeContent`). */
582
+ content?: string;
583
+ }
584
+
585
+ /** `data` of a `MESSAGE_REACTION_ADD` event. */
586
+ export interface DiscordReactionAddData {
587
+ userId: string;
588
+ channelId: string;
589
+ messageId: string;
590
+ messageAuthorId: string | null;
591
+ member: { roles: string[] } | null;
592
+ emoji: { id: string | null; name: string | null; animated: boolean };
593
+ }
594
+
595
+ /** The `kind` of a `DiscordEventInput`. */
596
+ export const DISCORD_EVENT_INPUT_KIND = 'DISCORD_EVENT';
597
+
598
+ /** The `input` of a function triggered by a Discord Gateway event. */
599
+ export interface DiscordEventInput {
600
+ kind: typeof DISCORD_EVENT_INPUT_KIND;
601
+ event: { id: string; type: DiscordEventType; trigger: { id: string; name: string } };
602
+ guildId: string;
603
+ /** When the Gateway received it, ISO 8601. */
604
+ receivedAt: string;
605
+ firedAt: string;
606
+ attempt: number;
607
+ data:
608
+ | DiscordMemberAddData
609
+ | DiscordMemberRemoveData
610
+ | DiscordMessageCreateData
611
+ | DiscordReactionAddData;
612
+ }
613
+
614
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
615
+ typeof value === 'object' && value !== null && !Array.isArray(value);
616
+
617
+ /**
618
+ * Tells a `DiscordEventInput` from the other inputs of a function (interaction, scheduled run). It
619
+ * checks the envelope (`kind`, event, guild, dates, attempt, `data` object), not the shape of `data`.
620
+ */
621
+ export const isDiscordEventInput = (value: unknown): value is DiscordEventInput => {
622
+ if (!isRecord(value) || value.kind !== DISCORD_EVENT_INPUT_KIND) {
623
+ return false;
624
+ }
625
+ const { event } = value;
626
+ return (
627
+ isRecord(event) &&
628
+ typeof event.id === 'string' &&
629
+ Object.values(DiscordEventType).includes(event.type as DiscordEventType) &&
630
+ isRecord(event.trigger) &&
631
+ typeof event.trigger.id === 'string' &&
632
+ typeof event.trigger.name === 'string' &&
633
+ typeof value.guildId === 'string' &&
634
+ typeof value.receivedAt === 'string' &&
635
+ typeof value.firedAt === 'string' &&
636
+ typeof value.attempt === 'number' &&
637
+ isRecord(value.data)
638
+ );
639
+ };