@abyss-project/runtime 3.10.0 → 3.11.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.
@@ -1,694 +1,696 @@
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
- /** Answers an autocomplete interaction with choices. Arguments: `DiscordAutocompleteArgs`. */
68
- AUTOCOMPLETE = 'discord.autocomplete',
69
- /** Downloads an attachment of the interaction or event. Arguments: `DiscordReadAttachmentArgs`; result: `DiscordReadAttachmentResult`. */
70
- READ_ATTACHMENT = 'discord.readAttachment',
71
- }
72
-
73
- /** Choices an autocomplete answer carries at most (Discord's limit). */
74
- export const DISCORD_AUTOCOMPLETE_MAX_CHOICES = 25;
75
- /** Files a message carries at most (Discord's limit). */
76
- export const DISCORD_MESSAGE_MAX_FILES = 10;
77
- /** Total size of the files of a message, in bytes. */
78
- export const DISCORD_MESSAGE_MAX_FILES_BYTES = 10 * 1024 * 1024;
79
- /** Upper bound of `budget.maxFilePayloadBytes`: the largest payload a file call may carry. */
80
- export const REFLEX_ATTACHMENT_MAX_BYTES_BOUND = 8 * 1024 * 1024;
81
- /** Default size an attachment read may reach when the Service sets no limit. */
82
- export const REFLEX_ATTACHMENT_DEFAULT_MAX_BYTES = 2 * 1024 * 1024;
83
-
84
- /**
85
- * Bounds the host applies to the arguments of `abyss.discord.*` before any call to Discord: a call
86
- * outside them fails with an argument error, without any call to Discord (it is still billed as one call).
87
- */
88
- export const DISCORD_CAPABILITY_LIMITS = {
89
- /** `reason` of an action, sent as Discord's audit log reason. */
90
- auditReasonMaxLength: 512,
91
- /** Longest timeout Discord allows, from now. */
92
- timeoutMaxDays: 28,
93
- /** Messages a ban may delete, in seconds back from now (7 days). */
94
- banDeleteMessageSecondsMax: 604_800,
95
- bulkDeleteMin: 2,
96
- bulkDeleteMax: 100,
97
- channelNameMaxLength: 100,
98
- channelTopicMaxLength: 1024,
99
- permissionOverwritesMax: 100,
100
- roleNameMaxLength: 100,
101
- roleColorMax: 0xffffff,
102
- threadNameMaxLength: 100,
103
- /** Auto-archive durations Discord accepts, in minutes. */
104
- threadAutoArchiveMinutes: [60, 1440, 4320, 10080],
105
- pollQuestionMaxLength: 300,
106
- pollAnswersMax: 10,
107
- pollAnswerTextMaxLength: 55,
108
- /** 32 days. */
109
- pollDurationHoursMax: 768,
110
- pollDurationHoursDefault: 24,
111
- } as const;
112
-
113
- /**
114
- * Codes of the errors `abyss.discord.*` throws. The host fails the call with the message
115
- * `<code>: <detail>` (or the bare code); the helper rethrows an `Error` named `DiscordError`
116
- * whose `code` is the code and whose `message` is the detail.
117
- */
118
- export enum DiscordErrorCode {
119
- /** `showModal` after the interaction was acknowledged (more than 2.5 s after it arrived). */
120
- MODAL_TOO_LATE = 'MODAL_TOO_LATE',
121
- /** `autocomplete` called after the autocomplete answer was sent, or a second time. */
122
- INTERACTION_ALREADY_ANSWERED = 'INTERACTION_ALREADY_ANSWERED',
123
- /** The bot's token is refused (circuit breaker open): no call was made to Discord. */
124
- DISCORD_TOKEN_INVALID = 'DISCORD_TOKEN_INVALID',
125
- /** Discord answered 403. */
126
- DISCORD_FORBIDDEN = 'DISCORD_FORBIDDEN',
127
- /** Discord answered 404. */
128
- DISCORD_NOT_FOUND = 'DISCORD_NOT_FOUND',
129
- /** Discord answered 429. */
130
- DISCORD_RATE_LIMITED = 'DISCORD_RATE_LIMITED',
131
- /**
132
- * `reply`, `update`, `showModal` or `followUp` in an execution no interaction started (a task,
133
- * a delayed run): there is no interaction to answer.
134
- */
135
- NO_INTERACTION = 'NO_INTERACTION',
136
- }
137
-
138
- /** Embed and component payloads are Discord's own JSON, relayed as is. */
139
- export type DiscordJsonObject = { [key: string]: JSONValue };
140
-
141
- /**
142
- * What a message may notify. `users` and `roles` let the mentions of every user or role ping;
143
- * `everyone` lets `@everyone` and `@here` notify the whole server.
144
- */
145
- export enum DiscordMentionType {
146
- USERS = 'users',
147
- ROLES = 'roles',
148
- EVERYONE = 'everyone',
149
- }
150
-
151
- /**
152
- * Who a message may notify: Discord's `allowed_mentions` in camelCase. Without it a message pings
153
- * the users it mentions and nobody else: `@everyone`, `@here` and roles stay plain text. An object
154
- * replaces that default as a whole, so what it leaves out notifies nobody. `parse` allows every
155
- * mention of a type; `users` and `roles` allow those ids only (at most 100 each). A type is given
156
- * by `parse` or by its list, never both. `repliedUser` is Discord's reply ping.
157
- */
158
- export interface DiscordAllowedMentions {
159
- parse?: DiscordMentionType[];
160
- users?: string[];
161
- roles?: string[];
162
- repliedUser?: boolean;
163
- }
164
-
165
- /** An answer of a poll. `emoji` is a unicode emoji or a server emoji `name:id`. */
166
- export interface DiscordPollAnswer {
167
- text: string;
168
- emoji?: string;
169
- }
170
-
171
- /**
172
- * A native Discord poll attached to a new message. Discord never edits a poll: `editMessage` and
173
- * `update` refuse a message that carries one.
174
- */
175
- export interface DiscordPoll {
176
- /** 1 to 300 characters. */
177
- question: string;
178
- /** 1 to 10 answers. */
179
- answers: DiscordPollAnswer[];
180
- /** 1 to 768 hours (32 days), 24 by default. */
181
- durationHours?: number;
182
- allowMultiselect?: boolean;
183
- }
184
-
185
- /** A file of a message: its content inline (base64) or an attachment of the interaction to forward. */
186
- export type DiscordMessageFile = { name: string; description?: string } & (
187
- | { contentBase64: string }
188
- | { attachmentId: string }
189
- );
190
-
191
- export interface DiscordMessage {
192
- content?: string;
193
- /** At most `DISCORD_MESSAGE_MAX_FILES` files, `DISCORD_MESSAGE_MAX_FILES_BYTES` in total. */
194
- files?: DiscordMessageFile[];
195
- embeds?: DiscordJsonObject[];
196
- components?: DiscordJsonObject[];
197
- /** Only the reply to an interaction can be ephemeral. */
198
- ephemeral?: boolean;
199
- /** Who the message may notify; by default the mentioned users only (see `DiscordAllowedMentions`). */
200
- allowedMentions?: DiscordAllowedMentions;
201
- /** A poll; only on a new message (`sendMessage`, `sendDirectMessage`, `reply`, `followUp`). */
202
- poll?: DiscordPoll;
203
- }
204
-
205
- /** Text input style of a modal field. */
206
- export enum DiscordModalFieldStyle {
207
- SHORT = 'SHORT',
208
- PARAGRAPH = 'PARAGRAPH',
209
- }
210
-
211
- export interface DiscordModalField {
212
- customId: string;
213
- label: string;
214
- style?: DiscordModalFieldStyle;
215
- required?: boolean;
216
- placeholder?: string;
217
- value?: string;
218
- minLength?: number;
219
- maxLength?: number;
220
- }
221
-
222
- export interface DiscordShowModalArgs {
223
- customId: string;
224
- title: string;
225
- fields: DiscordModalField[];
226
- }
227
-
228
- export interface DiscordSendMessageArgs {
229
- channelId: string;
230
- message: DiscordMessage;
231
- }
232
-
233
- export interface DiscordEditMessageArgs {
234
- channelId: string;
235
- messageId: string;
236
- message: DiscordMessage;
237
- }
238
-
239
- /**
240
- * The audit log reason an action may carry: 1 to 512 characters, shown in the server's audit log
241
- * (Discord's `X-Audit-Log-Reason`).
242
- */
243
- export interface DiscordAuditReason {
244
- reason?: string;
245
- }
246
-
247
- export interface DiscordDeleteMessageArgs extends DiscordAuditReason {
248
- channelId: string;
249
- messageId: string;
250
- }
251
-
252
- export interface DiscordRoleArgs extends DiscordAuditReason {
253
- guildId: string;
254
- userId: string;
255
- roleId: string;
256
- }
257
-
258
- export interface DiscordKickMemberArgs extends DiscordAuditReason {
259
- guildId: string;
260
- userId: string;
261
- }
262
-
263
- export interface DiscordBanMemberArgs extends DiscordAuditReason {
264
- guildId: string;
265
- userId: string;
266
- /** Deletes the user's messages of that many seconds back, 0 to 604 800 (7 days); 0 by default. */
267
- deleteMessageSeconds?: number;
268
- }
269
-
270
- export interface DiscordUnbanMemberArgs extends DiscordAuditReason {
271
- guildId: string;
272
- userId: string;
273
- }
274
-
275
- export interface DiscordTimeoutMemberArgs extends DiscordAuditReason {
276
- guildId: string;
277
- userId: string;
278
- /** End of the timeout, ISO 8601, in the future and at most 28 days away; null lifts it. */
279
- until: string | null;
280
- }
281
-
282
- export interface DiscordBulkDeleteMessagesArgs extends DiscordAuditReason {
283
- channelId: string;
284
- /** 2 to 100 distinct ids; Discord refuses a message older than 14 days. */
285
- messageIds: string[];
286
- }
287
-
288
- /** `emoji` is a unicode emoji or a server emoji `name:id`. */
289
- export interface DiscordReactionArgs {
290
- channelId: string;
291
- messageId: string;
292
- emoji: string;
293
- }
294
-
295
- export interface DiscordRemoveReactionArgs extends DiscordReactionArgs {
296
- /** Whose reaction to remove; the bot's own without it. */
297
- userId?: string;
298
- }
299
-
300
- /** Discord's channel types a bot can create (`type` of `DiscordChannel` holds Discord's number). */
301
- export enum DiscordChannelType {
302
- TEXT = 0,
303
- VOICE = 2,
304
- CATEGORY = 4,
305
- ANNOUNCEMENT = 5,
306
- FORUM = 15,
307
- }
308
-
309
- /** What a channel permission applies to. */
310
- export enum DiscordPermissionTargetType {
311
- ROLE = 'ROLE',
312
- MEMBER = 'MEMBER',
313
- }
314
-
315
- /** Permissions a channel allows or denies to one role or member; bits as decimal strings. */
316
- export interface DiscordPermissionOverwrite {
317
- /** The role or member id. */
318
- id: string;
319
- type: DiscordPermissionTargetType;
320
- allow?: string;
321
- deny?: string;
322
- }
323
-
324
- export interface DiscordCreateChannelArgs extends DiscordAuditReason {
325
- guildId: string;
326
- /** 1 to 100 characters. */
327
- name: string;
328
- type: DiscordChannelType;
329
- /** The category the channel goes in. */
330
- parentId?: string;
331
- /** Up to 1024 characters. */
332
- topic?: string;
333
- nsfw?: boolean;
334
- /** 100 at most. */
335
- permissionOverwrites?: DiscordPermissionOverwrite[];
336
- }
337
-
338
- export interface DiscordDeleteChannelArgs extends DiscordAuditReason {
339
- channelId: string;
340
- }
341
-
342
- export interface DiscordSetChannelPermissionArgs extends DiscordAuditReason {
343
- channelId: string;
344
- targetId: string;
345
- targetType: DiscordPermissionTargetType;
346
- /** Permission bits as a decimal string; none by default. */
347
- allow?: string;
348
- deny?: string;
349
- }
350
-
351
- export interface DiscordRemoveChannelPermissionArgs extends DiscordAuditReason {
352
- channelId: string;
353
- targetId: string;
354
- }
355
-
356
- export interface DiscordCreateRoleArgs extends DiscordAuditReason {
357
- guildId: string;
358
- /** 1 to 100 characters. */
359
- name: string;
360
- /** Permission bits as a decimal string; Discord's default without it. */
361
- permissions?: string;
362
- /** RGB color as an integer, 0 to 0xFFFFFF. */
363
- color?: number;
364
- hoist?: boolean;
365
- mentionable?: boolean;
366
- }
367
-
368
- export interface DiscordDeleteRoleArgs extends DiscordAuditReason {
369
- guildId: string;
370
- roleId: string;
371
- }
372
-
373
- export interface DiscordCreateThreadArgs extends DiscordAuditReason {
374
- channelId: string;
375
- /** 1 to 100 characters. */
376
- name: string;
377
- /** Starts the thread from this message; without it the thread stands alone. */
378
- messageId?: string;
379
- /** A private thread; only without `messageId`. */
380
- private?: boolean;
381
- /** 60, 1440, 4320 or 10080. */
382
- autoArchiveMinutes?: number;
383
- }
384
-
385
- export interface DiscordAutocompleteChoice {
386
- name: string;
387
- value: string | number;
388
- nameLocalizations?: Record<string, string>;
389
- }
390
-
391
- /** At most `DISCORD_AUTOCOMPLETE_MAX_CHOICES` choices. */
392
- export interface DiscordAutocompleteArgs {
393
- choices: DiscordAutocompleteChoice[];
394
- }
395
-
396
- export interface DiscordReadAttachmentArgs {
397
- attachmentId: string;
398
- }
399
-
400
- export interface DiscordReadAttachmentResult {
401
- filename: string;
402
- contentType: string | null;
403
- /** Bytes. */
404
- size: number;
405
- contentBase64: string;
406
- }
407
-
408
- export interface DiscordEndPollArgs {
409
- channelId: string;
410
- messageId: string;
411
- }
412
-
413
- export interface DiscordSendDirectMessageArgs {
414
- userId: string;
415
- message: DiscordMessage;
416
- }
417
-
418
- export interface DiscordGetMemberArgs {
419
- guildId: string;
420
- userId: string;
421
- }
422
-
423
- export interface DiscordGetGuildRolesArgs {
424
- guildId: string;
425
- }
426
-
427
- export interface DiscordGetChannelArgs {
428
- channelId: string;
429
- }
430
-
431
- export interface DiscordGetGuildArgs {
432
- guildId: string;
433
- }
434
-
435
- /** A Discord user, as the reads return it. */
436
- export interface DiscordUser {
437
- id: string;
438
- username: string;
439
- globalName: string | null;
440
- bot: boolean;
441
- }
442
-
443
- /**
444
- * A member of a server (`discord.getMember`): Discord's guild member in camelCase. The read gives
445
- * `null` instead when the user is not in the server. Dates are ISO 8601.
446
- */
447
- export interface DiscordMember {
448
- userId: string;
449
- nick: string | null;
450
- /** Role ids. */
451
- roles: string[];
452
- joinedAt: string | null;
453
- premiumSince: string | null;
454
- communicationDisabledUntil: string | null;
455
- /** True while the member has not passed the server's membership screening. */
456
- pending: boolean;
457
- /** Avatar hash specific to the server. */
458
- avatar: string | null;
459
- user: DiscordUser;
460
- }
461
-
462
- /**
463
- * A role of a server (`discord.getGuildRoles`, `DiscordGuild.roles`). `permissions` is Discord's
464
- * permission bit set as a decimal string: it exceeds 2^53.
465
- */
466
- export interface DiscordRole {
467
- id: string;
468
- name: string;
469
- /** RGB color as an integer, 0 for none. */
470
- color: number;
471
- position: number;
472
- permissions: string;
473
- managed: boolean;
474
- mentionable: boolean;
475
- hoist: boolean;
476
- }
477
-
478
- /** A channel (`discord.getChannel`). `type` is Discord's channel type number. */
479
- export interface DiscordChannel {
480
- id: string;
481
- type: number;
482
- name: string | null;
483
- guildId: string | null;
484
- parentId: string | null;
485
- topic: string | null;
486
- nsfw: boolean;
487
- position: number | null;
488
- }
489
-
490
- /** A server (`discord.getGuild`). The approximate counts are null when Discord does not give them. */
491
- export interface DiscordGuild {
492
- id: string;
493
- name: string;
494
- /** Icon hash. */
495
- icon: string | null;
496
- ownerId: string;
497
- preferredLocale: string;
498
- roles: DiscordRole[];
499
- approximateMemberCount: number | null;
500
- approximatePresenceCount: number | null;
501
- }
502
-
503
- /** The kind of interaction a function was started by. */
504
- export enum DiscordInteractionKind {
505
- COMMAND = 'COMMAND',
506
- AUTOCOMPLETE = 'AUTOCOMPLETE',
507
- COMPONENT = 'COMPONENT',
508
- MODAL = 'MODAL',
509
- }
510
-
511
- /** Discord's interaction types (`type` of the payload) a bot's edge serves. */
512
- export enum DiscordInteractionType {
513
- PING = 1,
514
- APPLICATION_COMMAND = 2,
515
- MESSAGE_COMPONENT = 3,
516
- APPLICATION_COMMAND_AUTOCOMPLETE = 4,
517
- MODAL_SUBMIT = 5,
518
- }
519
-
520
- /** Discord's interaction callback types the edge answers with. */
521
- export enum DiscordCallbackType {
522
- PONG = 1,
523
- CHANNEL_MESSAGE_WITH_SOURCE = 4,
524
- DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE = 5,
525
- DEFERRED_UPDATE_MESSAGE = 6,
526
- UPDATE_MESSAGE = 7,
527
- APPLICATION_COMMAND_AUTOCOMPLETE_RESULT = 8,
528
- MODAL = 9,
529
- }
530
-
531
- /** Discord's message flag that makes a message visible to its recipient only. */
532
- export const EPHEMERAL_FLAG = 64;
533
-
534
- /** A member as an interaction resolves it: partial, without its user (see `DiscordResolved.users`). */
535
- export interface DiscordResolvedMember {
536
- nick: string | null;
537
- /** Role ids. */
538
- roles: string[];
539
- joinedAt: string | null;
540
- /** The member's permissions in the channel, decimal string; null when Discord does not give them. */
541
- permissions: string | null;
542
- }
543
-
544
- /** A file a user attached to an ATTACHMENT option. */
545
- export interface DiscordAttachment {
546
- id: string;
547
- filename: string;
548
- /** Bytes. */
549
- size: number;
550
- url: string;
551
- contentType: string | null;
552
- }
553
-
554
- /**
555
- * The users, members, roles, channels and files an interaction names, by id, as Discord resolves
556
- * them for its options and typed select menus. A key is absent when Discord resolved none of its kind.
557
- */
558
- export interface DiscordResolved {
559
- users?: Record<string, DiscordUser>;
560
- members?: Record<string, DiscordResolvedMember>;
561
- roles?: Record<string, DiscordRole>;
562
- /** Partial channels: what Discord gives in an interaction. */
563
- channels?: Record<string, DiscordChannel>;
564
- attachments?: Record<string, DiscordAttachment>;
565
- }
566
-
567
- /**
568
- * The `input` of a function triggered by a Discord interaction. The interaction token never
569
- * appears here: it stays in the edge.
570
- */
571
- export interface DiscordInteractionInput {
572
- kind: DiscordInteractionKind;
573
- /** Resolved option values, by option name; `resolved` details the ids they hold, when there are some. */
574
- command?: {
575
- name: string;
576
- options: Record<string, string | number | boolean>;
577
- resolved?: DiscordResolved;
578
- /** The option being typed; present only for an AUTOCOMPLETE interaction. */
579
- focused?: { name: string; value: string };
580
- };
581
- /** `values` of a select menu; `resolved` details them for a user, role, channel or mentionable menu. */
582
- component?: { customId: string; values?: string[]; resolved?: DiscordResolved };
583
- modal?: { customId: string; fields: Record<string, string> };
584
- user: { id: string; username: string; globalName: string | null };
585
- member?: { roles: string[]; permissions: string };
586
- guildId?: string;
587
- channelId?: string;
588
- locale: string;
589
- /** Discord's payload, without the interaction token. */
590
- interaction: { id: string; applicationId: string; type: number; data: JSONValue };
591
- }
592
-
593
- /** The Gateway events a bot's function can be triggered by. */
594
- export enum DiscordEventType {
595
- GUILD_MEMBER_ADD = 'GUILD_MEMBER_ADD',
596
- GUILD_MEMBER_REMOVE = 'GUILD_MEMBER_REMOVE',
597
- MESSAGE_CREATE = 'MESSAGE_CREATE',
598
- MESSAGE_REACTION_ADD = 'MESSAGE_REACTION_ADD',
599
- }
600
-
601
- /** `data` of a `GUILD_MEMBER_ADD` event. */
602
- export interface DiscordMemberAddData {
603
- user: {
604
- id: string;
605
- username: string;
606
- globalName: string | null;
607
- bot: boolean;
608
- avatar: string | null;
609
- };
610
- /** Role ids. */
611
- roles: string[];
612
- joinedAt: string | null;
613
- /** True while the member has not passed the server's membership screening. */
614
- pending: boolean;
615
- }
616
-
617
- /** `data` of a `GUILD_MEMBER_REMOVE` event. */
618
- export interface DiscordMemberRemoveData {
619
- user: { id: string; username: string; globalName: string | null; bot: boolean };
620
- }
621
-
622
- /** `data` of a `MESSAGE_CREATE` event. */
623
- export interface DiscordMessageCreateData {
624
- id: string;
625
- channelId: string;
626
- author: { id: string; username: string; globalName: string | null; bot: boolean };
627
- member: { roles: string[]; nick: string | null } | null;
628
- /** Ids of the mentioned users. */
629
- mentions: string[];
630
- referencedMessageId: string | null;
631
- attachmentCount: number;
632
- /** The files of the message, with the ids `discord.readAttachment` takes. */
633
- attachments: DiscordAttachment[];
634
- /** ISO 8601. */
635
- createdAt: string;
636
- /** Present only when the trigger asks for the content (`includeContent`). */
637
- content?: string;
638
- }
639
-
640
- /** `data` of a `MESSAGE_REACTION_ADD` event. */
641
- export interface DiscordReactionAddData {
642
- userId: string;
643
- channelId: string;
644
- messageId: string;
645
- messageAuthorId: string | null;
646
- member: { roles: string[] } | null;
647
- emoji: { id: string | null; name: string | null; animated: boolean };
648
- }
649
-
650
- /** The `kind` of a `DiscordEventInput`. */
651
- export const DISCORD_EVENT_INPUT_KIND = 'DISCORD_EVENT';
652
-
653
- /** The `input` of a function triggered by a Discord Gateway event. */
654
- export interface DiscordEventInput {
655
- kind: typeof DISCORD_EVENT_INPUT_KIND;
656
- event: { id: string; type: DiscordEventType; trigger: { id: string; name: string } };
657
- guildId: string;
658
- /** When the Gateway received it, ISO 8601. */
659
- receivedAt: string;
660
- firedAt: string;
661
- attempt: number;
662
- data:
663
- | DiscordMemberAddData
664
- | DiscordMemberRemoveData
665
- | DiscordMessageCreateData
666
- | DiscordReactionAddData;
667
- }
668
-
669
- const isRecord = (value: unknown): value is Record<string, unknown> =>
670
- typeof value === 'object' && value !== null && !Array.isArray(value);
671
-
672
- /**
673
- * Tells a `DiscordEventInput` from the other inputs of a function (interaction, scheduled run). It
674
- * checks the envelope (`kind`, event, guild, dates, attempt, `data` object), not the shape of `data`.
675
- */
676
- export const isDiscordEventInput = (value: unknown): value is DiscordEventInput => {
677
- if (!isRecord(value) || value.kind !== DISCORD_EVENT_INPUT_KIND) {
678
- return false;
679
- }
680
- const { event } = value;
681
- return (
682
- isRecord(event) &&
683
- typeof event.id === 'string' &&
684
- Object.values(DiscordEventType).includes(event.type as DiscordEventType) &&
685
- isRecord(event.trigger) &&
686
- typeof event.trigger.id === 'string' &&
687
- typeof event.trigger.name === 'string' &&
688
- typeof value.guildId === 'string' &&
689
- typeof value.receivedAt === 'string' &&
690
- typeof value.firedAt === 'string' &&
691
- typeof value.attempt === 'number' &&
692
- isRecord(value.data)
693
- );
694
- };
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
+ /** Answers an autocomplete interaction with choices. Arguments: `DiscordAutocompleteArgs`. */
68
+ AUTOCOMPLETE = 'discord.autocomplete',
69
+ /** Downloads an attachment of the interaction or event. Arguments: `DiscordReadAttachmentArgs`; result: `DiscordReadAttachmentResult`. */
70
+ READ_ATTACHMENT = 'discord.readAttachment',
71
+ }
72
+
73
+ /** Choices an autocomplete answer carries at most (Discord's limit). */
74
+ export const DISCORD_AUTOCOMPLETE_MAX_CHOICES = 25;
75
+ /** Files a message carries at most (Discord's limit). */
76
+ export const DISCORD_MESSAGE_MAX_FILES = 10;
77
+ /** Total size of the files of a message, in bytes. */
78
+ export const DISCORD_MESSAGE_MAX_FILES_BYTES = 10 * 1024 * 1024;
79
+ /** Upper bound of `budget.maxFilePayloadBytes`: the largest payload a file call may carry. */
80
+ export const REFLEX_ATTACHMENT_MAX_BYTES_BOUND = 8 * 1024 * 1024;
81
+ /** Default size an attachment read may reach when the Service sets no limit. */
82
+ export const REFLEX_ATTACHMENT_DEFAULT_MAX_BYTES = 2 * 1024 * 1024;
83
+
84
+ /**
85
+ * Bounds the host applies to the arguments of `abyss.discord.*` before any call to Discord: a call
86
+ * outside them fails with an argument error, without any call to Discord (it is still billed as one call).
87
+ */
88
+ export const DISCORD_CAPABILITY_LIMITS = {
89
+ /** `reason` of an action, sent as Discord's audit log reason. */
90
+ auditReasonMaxLength: 512,
91
+ /** Longest timeout Discord allows, from now. */
92
+ timeoutMaxDays: 28,
93
+ /** Messages a ban may delete, in seconds back from now (7 days). */
94
+ banDeleteMessageSecondsMax: 604_800,
95
+ bulkDeleteMin: 2,
96
+ bulkDeleteMax: 100,
97
+ channelNameMaxLength: 100,
98
+ channelTopicMaxLength: 1024,
99
+ permissionOverwritesMax: 100,
100
+ roleNameMaxLength: 100,
101
+ roleColorMax: 0xffffff,
102
+ threadNameMaxLength: 100,
103
+ /** Auto-archive durations Discord accepts, in minutes. */
104
+ threadAutoArchiveMinutes: [60, 1440, 4320, 10080],
105
+ pollQuestionMaxLength: 300,
106
+ pollAnswersMax: 10,
107
+ pollAnswerTextMaxLength: 55,
108
+ /** 32 days. */
109
+ pollDurationHoursMax: 768,
110
+ pollDurationHoursDefault: 24,
111
+ } as const;
112
+
113
+ /**
114
+ * Codes of the errors `abyss.discord.*` throws. The host fails the call with the message
115
+ * `<code>: <detail>` (or the bare code); the helper rethrows an `Error` named `DiscordError`
116
+ * whose `code` is the code and whose `message` is the detail.
117
+ */
118
+ export enum DiscordErrorCode {
119
+ /** `showModal` after the interaction was acknowledged (more than 2.5 s after it arrived). */
120
+ MODAL_TOO_LATE = 'MODAL_TOO_LATE',
121
+ /** `autocomplete` called after the autocomplete answer was sent, or a second time. */
122
+ INTERACTION_ALREADY_ANSWERED = 'INTERACTION_ALREADY_ANSWERED',
123
+ /** Discord refused the files of a message as too large (HTTP 413 or JSON code 40005): nothing was sent. */
124
+ DISCORD_FILE_TOO_LARGE = 'DISCORD_FILE_TOO_LARGE',
125
+ /** The bot's token is refused (circuit breaker open): no call was made to Discord. */
126
+ DISCORD_TOKEN_INVALID = 'DISCORD_TOKEN_INVALID',
127
+ /** Discord answered 403. */
128
+ DISCORD_FORBIDDEN = 'DISCORD_FORBIDDEN',
129
+ /** Discord answered 404. */
130
+ DISCORD_NOT_FOUND = 'DISCORD_NOT_FOUND',
131
+ /** Discord answered 429. */
132
+ DISCORD_RATE_LIMITED = 'DISCORD_RATE_LIMITED',
133
+ /**
134
+ * `reply`, `update`, `showModal` or `followUp` in an execution no interaction started (a task,
135
+ * a delayed run): there is no interaction to answer.
136
+ */
137
+ NO_INTERACTION = 'NO_INTERACTION',
138
+ }
139
+
140
+ /** Embed and component payloads are Discord's own JSON, relayed as is. */
141
+ export type DiscordJsonObject = { [key: string]: JSONValue };
142
+
143
+ /**
144
+ * What a message may notify. `users` and `roles` let the mentions of every user or role ping;
145
+ * `everyone` lets `@everyone` and `@here` notify the whole server.
146
+ */
147
+ export enum DiscordMentionType {
148
+ USERS = 'users',
149
+ ROLES = 'roles',
150
+ EVERYONE = 'everyone',
151
+ }
152
+
153
+ /**
154
+ * Who a message may notify: Discord's `allowed_mentions` in camelCase. Without it a message pings
155
+ * the users it mentions and nobody else: `@everyone`, `@here` and roles stay plain text. An object
156
+ * replaces that default as a whole, so what it leaves out notifies nobody. `parse` allows every
157
+ * mention of a type; `users` and `roles` allow those ids only (at most 100 each). A type is given
158
+ * by `parse` or by its list, never both. `repliedUser` is Discord's reply ping.
159
+ */
160
+ export interface DiscordAllowedMentions {
161
+ parse?: DiscordMentionType[];
162
+ users?: string[];
163
+ roles?: string[];
164
+ repliedUser?: boolean;
165
+ }
166
+
167
+ /** An answer of a poll. `emoji` is a unicode emoji or a server emoji `name:id`. */
168
+ export interface DiscordPollAnswer {
169
+ text: string;
170
+ emoji?: string;
171
+ }
172
+
173
+ /**
174
+ * A native Discord poll attached to a new message. Discord never edits a poll: `editMessage` and
175
+ * `update` refuse a message that carries one.
176
+ */
177
+ export interface DiscordPoll {
178
+ /** 1 to 300 characters. */
179
+ question: string;
180
+ /** 1 to 10 answers. */
181
+ answers: DiscordPollAnswer[];
182
+ /** 1 to 768 hours (32 days), 24 by default. */
183
+ durationHours?: number;
184
+ allowMultiselect?: boolean;
185
+ }
186
+
187
+ /** A file of a message: its content inline (base64) or an attachment of the interaction to forward. */
188
+ export type DiscordMessageFile = { name: string; description?: string } & (
189
+ | { contentBase64: string }
190
+ | { attachmentId: string }
191
+ );
192
+
193
+ export interface DiscordMessage {
194
+ content?: string;
195
+ /** At most `DISCORD_MESSAGE_MAX_FILES` files, `DISCORD_MESSAGE_MAX_FILES_BYTES` in total. */
196
+ files?: DiscordMessageFile[];
197
+ embeds?: DiscordJsonObject[];
198
+ components?: DiscordJsonObject[];
199
+ /** Only the reply to an interaction can be ephemeral. */
200
+ ephemeral?: boolean;
201
+ /** Who the message may notify; by default the mentioned users only (see `DiscordAllowedMentions`). */
202
+ allowedMentions?: DiscordAllowedMentions;
203
+ /** A poll; only on a new message (`sendMessage`, `sendDirectMessage`, `reply`, `followUp`). */
204
+ poll?: DiscordPoll;
205
+ }
206
+
207
+ /** Text input style of a modal field. */
208
+ export enum DiscordModalFieldStyle {
209
+ SHORT = 'SHORT',
210
+ PARAGRAPH = 'PARAGRAPH',
211
+ }
212
+
213
+ export interface DiscordModalField {
214
+ customId: string;
215
+ label: string;
216
+ style?: DiscordModalFieldStyle;
217
+ required?: boolean;
218
+ placeholder?: string;
219
+ value?: string;
220
+ minLength?: number;
221
+ maxLength?: number;
222
+ }
223
+
224
+ export interface DiscordShowModalArgs {
225
+ customId: string;
226
+ title: string;
227
+ fields: DiscordModalField[];
228
+ }
229
+
230
+ export interface DiscordSendMessageArgs {
231
+ channelId: string;
232
+ message: DiscordMessage;
233
+ }
234
+
235
+ export interface DiscordEditMessageArgs {
236
+ channelId: string;
237
+ messageId: string;
238
+ message: DiscordMessage;
239
+ }
240
+
241
+ /**
242
+ * The audit log reason an action may carry: 1 to 512 characters, shown in the server's audit log
243
+ * (Discord's `X-Audit-Log-Reason`).
244
+ */
245
+ export interface DiscordAuditReason {
246
+ reason?: string;
247
+ }
248
+
249
+ export interface DiscordDeleteMessageArgs extends DiscordAuditReason {
250
+ channelId: string;
251
+ messageId: string;
252
+ }
253
+
254
+ export interface DiscordRoleArgs extends DiscordAuditReason {
255
+ guildId: string;
256
+ userId: string;
257
+ roleId: string;
258
+ }
259
+
260
+ export interface DiscordKickMemberArgs extends DiscordAuditReason {
261
+ guildId: string;
262
+ userId: string;
263
+ }
264
+
265
+ export interface DiscordBanMemberArgs extends DiscordAuditReason {
266
+ guildId: string;
267
+ userId: string;
268
+ /** Deletes the user's messages of that many seconds back, 0 to 604 800 (7 days); 0 by default. */
269
+ deleteMessageSeconds?: number;
270
+ }
271
+
272
+ export interface DiscordUnbanMemberArgs extends DiscordAuditReason {
273
+ guildId: string;
274
+ userId: string;
275
+ }
276
+
277
+ export interface DiscordTimeoutMemberArgs extends DiscordAuditReason {
278
+ guildId: string;
279
+ userId: string;
280
+ /** End of the timeout, ISO 8601, in the future and at most 28 days away; null lifts it. */
281
+ until: string | null;
282
+ }
283
+
284
+ export interface DiscordBulkDeleteMessagesArgs extends DiscordAuditReason {
285
+ channelId: string;
286
+ /** 2 to 100 distinct ids; Discord refuses a message older than 14 days. */
287
+ messageIds: string[];
288
+ }
289
+
290
+ /** `emoji` is a unicode emoji or a server emoji `name:id`. */
291
+ export interface DiscordReactionArgs {
292
+ channelId: string;
293
+ messageId: string;
294
+ emoji: string;
295
+ }
296
+
297
+ export interface DiscordRemoveReactionArgs extends DiscordReactionArgs {
298
+ /** Whose reaction to remove; the bot's own without it. */
299
+ userId?: string;
300
+ }
301
+
302
+ /** Discord's channel types a bot can create (`type` of `DiscordChannel` holds Discord's number). */
303
+ export enum DiscordChannelType {
304
+ TEXT = 0,
305
+ VOICE = 2,
306
+ CATEGORY = 4,
307
+ ANNOUNCEMENT = 5,
308
+ FORUM = 15,
309
+ }
310
+
311
+ /** What a channel permission applies to. */
312
+ export enum DiscordPermissionTargetType {
313
+ ROLE = 'ROLE',
314
+ MEMBER = 'MEMBER',
315
+ }
316
+
317
+ /** Permissions a channel allows or denies to one role or member; bits as decimal strings. */
318
+ export interface DiscordPermissionOverwrite {
319
+ /** The role or member id. */
320
+ id: string;
321
+ type: DiscordPermissionTargetType;
322
+ allow?: string;
323
+ deny?: string;
324
+ }
325
+
326
+ export interface DiscordCreateChannelArgs extends DiscordAuditReason {
327
+ guildId: string;
328
+ /** 1 to 100 characters. */
329
+ name: string;
330
+ type: DiscordChannelType;
331
+ /** The category the channel goes in. */
332
+ parentId?: string;
333
+ /** Up to 1024 characters. */
334
+ topic?: string;
335
+ nsfw?: boolean;
336
+ /** 100 at most. */
337
+ permissionOverwrites?: DiscordPermissionOverwrite[];
338
+ }
339
+
340
+ export interface DiscordDeleteChannelArgs extends DiscordAuditReason {
341
+ channelId: string;
342
+ }
343
+
344
+ export interface DiscordSetChannelPermissionArgs extends DiscordAuditReason {
345
+ channelId: string;
346
+ targetId: string;
347
+ targetType: DiscordPermissionTargetType;
348
+ /** Permission bits as a decimal string; none by default. */
349
+ allow?: string;
350
+ deny?: string;
351
+ }
352
+
353
+ export interface DiscordRemoveChannelPermissionArgs extends DiscordAuditReason {
354
+ channelId: string;
355
+ targetId: string;
356
+ }
357
+
358
+ export interface DiscordCreateRoleArgs extends DiscordAuditReason {
359
+ guildId: string;
360
+ /** 1 to 100 characters. */
361
+ name: string;
362
+ /** Permission bits as a decimal string; Discord's default without it. */
363
+ permissions?: string;
364
+ /** RGB color as an integer, 0 to 0xFFFFFF. */
365
+ color?: number;
366
+ hoist?: boolean;
367
+ mentionable?: boolean;
368
+ }
369
+
370
+ export interface DiscordDeleteRoleArgs extends DiscordAuditReason {
371
+ guildId: string;
372
+ roleId: string;
373
+ }
374
+
375
+ export interface DiscordCreateThreadArgs extends DiscordAuditReason {
376
+ channelId: string;
377
+ /** 1 to 100 characters. */
378
+ name: string;
379
+ /** Starts the thread from this message; without it the thread stands alone. */
380
+ messageId?: string;
381
+ /** A private thread; only without `messageId`. */
382
+ private?: boolean;
383
+ /** 60, 1440, 4320 or 10080. */
384
+ autoArchiveMinutes?: number;
385
+ }
386
+
387
+ export interface DiscordAutocompleteChoice {
388
+ name: string;
389
+ value: string | number;
390
+ nameLocalizations?: Record<string, string>;
391
+ }
392
+
393
+ /** At most `DISCORD_AUTOCOMPLETE_MAX_CHOICES` choices. */
394
+ export interface DiscordAutocompleteArgs {
395
+ choices: DiscordAutocompleteChoice[];
396
+ }
397
+
398
+ export interface DiscordReadAttachmentArgs {
399
+ attachmentId: string;
400
+ }
401
+
402
+ export interface DiscordReadAttachmentResult {
403
+ filename: string;
404
+ contentType: string | null;
405
+ /** Bytes. */
406
+ size: number;
407
+ contentBase64: string;
408
+ }
409
+
410
+ export interface DiscordEndPollArgs {
411
+ channelId: string;
412
+ messageId: string;
413
+ }
414
+
415
+ export interface DiscordSendDirectMessageArgs {
416
+ userId: string;
417
+ message: DiscordMessage;
418
+ }
419
+
420
+ export interface DiscordGetMemberArgs {
421
+ guildId: string;
422
+ userId: string;
423
+ }
424
+
425
+ export interface DiscordGetGuildRolesArgs {
426
+ guildId: string;
427
+ }
428
+
429
+ export interface DiscordGetChannelArgs {
430
+ channelId: string;
431
+ }
432
+
433
+ export interface DiscordGetGuildArgs {
434
+ guildId: string;
435
+ }
436
+
437
+ /** A Discord user, as the reads return it. */
438
+ export interface DiscordUser {
439
+ id: string;
440
+ username: string;
441
+ globalName: string | null;
442
+ bot: boolean;
443
+ }
444
+
445
+ /**
446
+ * A member of a server (`discord.getMember`): Discord's guild member in camelCase. The read gives
447
+ * `null` instead when the user is not in the server. Dates are ISO 8601.
448
+ */
449
+ export interface DiscordMember {
450
+ userId: string;
451
+ nick: string | null;
452
+ /** Role ids. */
453
+ roles: string[];
454
+ joinedAt: string | null;
455
+ premiumSince: string | null;
456
+ communicationDisabledUntil: string | null;
457
+ /** True while the member has not passed the server's membership screening. */
458
+ pending: boolean;
459
+ /** Avatar hash specific to the server. */
460
+ avatar: string | null;
461
+ user: DiscordUser;
462
+ }
463
+
464
+ /**
465
+ * A role of a server (`discord.getGuildRoles`, `DiscordGuild.roles`). `permissions` is Discord's
466
+ * permission bit set as a decimal string: it exceeds 2^53.
467
+ */
468
+ export interface DiscordRole {
469
+ id: string;
470
+ name: string;
471
+ /** RGB color as an integer, 0 for none. */
472
+ color: number;
473
+ position: number;
474
+ permissions: string;
475
+ managed: boolean;
476
+ mentionable: boolean;
477
+ hoist: boolean;
478
+ }
479
+
480
+ /** A channel (`discord.getChannel`). `type` is Discord's channel type number. */
481
+ export interface DiscordChannel {
482
+ id: string;
483
+ type: number;
484
+ name: string | null;
485
+ guildId: string | null;
486
+ parentId: string | null;
487
+ topic: string | null;
488
+ nsfw: boolean;
489
+ position: number | null;
490
+ }
491
+
492
+ /** A server (`discord.getGuild`). The approximate counts are null when Discord does not give them. */
493
+ export interface DiscordGuild {
494
+ id: string;
495
+ name: string;
496
+ /** Icon hash. */
497
+ icon: string | null;
498
+ ownerId: string;
499
+ preferredLocale: string;
500
+ roles: DiscordRole[];
501
+ approximateMemberCount: number | null;
502
+ approximatePresenceCount: number | null;
503
+ }
504
+
505
+ /** The kind of interaction a function was started by. */
506
+ export enum DiscordInteractionKind {
507
+ COMMAND = 'COMMAND',
508
+ AUTOCOMPLETE = 'AUTOCOMPLETE',
509
+ COMPONENT = 'COMPONENT',
510
+ MODAL = 'MODAL',
511
+ }
512
+
513
+ /** Discord's interaction types (`type` of the payload) a bot's edge serves. */
514
+ export enum DiscordInteractionType {
515
+ PING = 1,
516
+ APPLICATION_COMMAND = 2,
517
+ MESSAGE_COMPONENT = 3,
518
+ APPLICATION_COMMAND_AUTOCOMPLETE = 4,
519
+ MODAL_SUBMIT = 5,
520
+ }
521
+
522
+ /** Discord's interaction callback types the edge answers with. */
523
+ export enum DiscordCallbackType {
524
+ PONG = 1,
525
+ CHANNEL_MESSAGE_WITH_SOURCE = 4,
526
+ DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE = 5,
527
+ DEFERRED_UPDATE_MESSAGE = 6,
528
+ UPDATE_MESSAGE = 7,
529
+ APPLICATION_COMMAND_AUTOCOMPLETE_RESULT = 8,
530
+ MODAL = 9,
531
+ }
532
+
533
+ /** Discord's message flag that makes a message visible to its recipient only. */
534
+ export const EPHEMERAL_FLAG = 64;
535
+
536
+ /** A member as an interaction resolves it: partial, without its user (see `DiscordResolved.users`). */
537
+ export interface DiscordResolvedMember {
538
+ nick: string | null;
539
+ /** Role ids. */
540
+ roles: string[];
541
+ joinedAt: string | null;
542
+ /** The member's permissions in the channel, decimal string; null when Discord does not give them. */
543
+ permissions: string | null;
544
+ }
545
+
546
+ /** A file a user attached to an ATTACHMENT option. */
547
+ export interface DiscordAttachment {
548
+ id: string;
549
+ filename: string;
550
+ /** Bytes. */
551
+ size: number;
552
+ url: string;
553
+ contentType: string | null;
554
+ }
555
+
556
+ /**
557
+ * The users, members, roles, channels and files an interaction names, by id, as Discord resolves
558
+ * them for its options and typed select menus. A key is absent when Discord resolved none of its kind.
559
+ */
560
+ export interface DiscordResolved {
561
+ users?: Record<string, DiscordUser>;
562
+ members?: Record<string, DiscordResolvedMember>;
563
+ roles?: Record<string, DiscordRole>;
564
+ /** Partial channels: what Discord gives in an interaction. */
565
+ channels?: Record<string, DiscordChannel>;
566
+ attachments?: Record<string, DiscordAttachment>;
567
+ }
568
+
569
+ /**
570
+ * The `input` of a function triggered by a Discord interaction. The interaction token never
571
+ * appears here: it stays in the edge.
572
+ */
573
+ export interface DiscordInteractionInput {
574
+ kind: DiscordInteractionKind;
575
+ /** Resolved option values, by option name; `resolved` details the ids they hold, when there are some. */
576
+ command?: {
577
+ name: string;
578
+ options: Record<string, string | number | boolean>;
579
+ resolved?: DiscordResolved;
580
+ /** The option being typed; present only for an AUTOCOMPLETE interaction. */
581
+ focused?: { name: string; value: string };
582
+ };
583
+ /** `values` of a select menu; `resolved` details them for a user, role, channel or mentionable menu. */
584
+ component?: { customId: string; values?: string[]; resolved?: DiscordResolved };
585
+ modal?: { customId: string; fields: Record<string, string> };
586
+ user: { id: string; username: string; globalName: string | null };
587
+ member?: { roles: string[]; permissions: string };
588
+ guildId?: string;
589
+ channelId?: string;
590
+ locale: string;
591
+ /** Discord's payload, without the interaction token. */
592
+ interaction: { id: string; applicationId: string; type: number; data: JSONValue };
593
+ }
594
+
595
+ /** The Gateway events a bot's function can be triggered by. */
596
+ export enum DiscordEventType {
597
+ GUILD_MEMBER_ADD = 'GUILD_MEMBER_ADD',
598
+ GUILD_MEMBER_REMOVE = 'GUILD_MEMBER_REMOVE',
599
+ MESSAGE_CREATE = 'MESSAGE_CREATE',
600
+ MESSAGE_REACTION_ADD = 'MESSAGE_REACTION_ADD',
601
+ }
602
+
603
+ /** `data` of a `GUILD_MEMBER_ADD` event. */
604
+ export interface DiscordMemberAddData {
605
+ user: {
606
+ id: string;
607
+ username: string;
608
+ globalName: string | null;
609
+ bot: boolean;
610
+ avatar: string | null;
611
+ };
612
+ /** Role ids. */
613
+ roles: string[];
614
+ joinedAt: string | null;
615
+ /** True while the member has not passed the server's membership screening. */
616
+ pending: boolean;
617
+ }
618
+
619
+ /** `data` of a `GUILD_MEMBER_REMOVE` event. */
620
+ export interface DiscordMemberRemoveData {
621
+ user: { id: string; username: string; globalName: string | null; bot: boolean };
622
+ }
623
+
624
+ /** `data` of a `MESSAGE_CREATE` event. */
625
+ export interface DiscordMessageCreateData {
626
+ id: string;
627
+ channelId: string;
628
+ author: { id: string; username: string; globalName: string | null; bot: boolean };
629
+ member: { roles: string[]; nick: string | null } | null;
630
+ /** Ids of the mentioned users. */
631
+ mentions: string[];
632
+ referencedMessageId: string | null;
633
+ attachmentCount: number;
634
+ /** The files of the message, with the ids `discord.readAttachment` takes. */
635
+ attachments: DiscordAttachment[];
636
+ /** ISO 8601. */
637
+ createdAt: string;
638
+ /** Present only when the trigger asks for the content (`includeContent`). */
639
+ content?: string;
640
+ }
641
+
642
+ /** `data` of a `MESSAGE_REACTION_ADD` event. */
643
+ export interface DiscordReactionAddData {
644
+ userId: string;
645
+ channelId: string;
646
+ messageId: string;
647
+ messageAuthorId: string | null;
648
+ member: { roles: string[] } | null;
649
+ emoji: { id: string | null; name: string | null; animated: boolean };
650
+ }
651
+
652
+ /** The `kind` of a `DiscordEventInput`. */
653
+ export const DISCORD_EVENT_INPUT_KIND = 'DISCORD_EVENT';
654
+
655
+ /** The `input` of a function triggered by a Discord Gateway event. */
656
+ export interface DiscordEventInput {
657
+ kind: typeof DISCORD_EVENT_INPUT_KIND;
658
+ event: { id: string; type: DiscordEventType; trigger: { id: string; name: string } };
659
+ guildId: string;
660
+ /** When the Gateway received it, ISO 8601. */
661
+ receivedAt: string;
662
+ firedAt: string;
663
+ attempt: number;
664
+ data:
665
+ | DiscordMemberAddData
666
+ | DiscordMemberRemoveData
667
+ | DiscordMessageCreateData
668
+ | DiscordReactionAddData;
669
+ }
670
+
671
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
672
+ typeof value === 'object' && value !== null && !Array.isArray(value);
673
+
674
+ /**
675
+ * Tells a `DiscordEventInput` from the other inputs of a function (interaction, scheduled run). It
676
+ * checks the envelope (`kind`, event, guild, dates, attempt, `data` object), not the shape of `data`.
677
+ */
678
+ export const isDiscordEventInput = (value: unknown): value is DiscordEventInput => {
679
+ if (!isRecord(value) || value.kind !== DISCORD_EVENT_INPUT_KIND) {
680
+ return false;
681
+ }
682
+ const { event } = value;
683
+ return (
684
+ isRecord(event) &&
685
+ typeof event.id === 'string' &&
686
+ Object.values(DiscordEventType).includes(event.type as DiscordEventType) &&
687
+ isRecord(event.trigger) &&
688
+ typeof event.trigger.id === 'string' &&
689
+ typeof event.trigger.name === 'string' &&
690
+ typeof value.guildId === 'string' &&
691
+ typeof value.receivedAt === 'string' &&
692
+ typeof value.firedAt === 'string' &&
693
+ typeof value.attempt === 'number' &&
694
+ isRecord(value.data)
695
+ );
696
+ };