@abyss-project/runtime 3.3.0 → 3.4.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,279 +1,380 @@
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
- }
38
-
39
- /**
40
- * Codes of the errors `abyss.discord.*` throws. The host fails the call with the message
41
- * `<code>: <detail>` (or the bare code); the helper rethrows an `Error` named `DiscordError`
42
- * whose `code` is the code and whose `message` is the detail.
43
- */
44
- export enum DiscordErrorCode {
45
- /** `showModal` after the interaction was acknowledged (more than 2.5 s after it arrived). */
46
- MODAL_TOO_LATE = 'MODAL_TOO_LATE',
47
- /** The bot's token is refused (circuit breaker open): no call was made to Discord. */
48
- DISCORD_TOKEN_INVALID = 'DISCORD_TOKEN_INVALID',
49
- /** Discord answered 403. */
50
- DISCORD_FORBIDDEN = 'DISCORD_FORBIDDEN',
51
- /** Discord answered 404. */
52
- DISCORD_NOT_FOUND = 'DISCORD_NOT_FOUND',
53
- /** Discord answered 429. */
54
- DISCORD_RATE_LIMITED = 'DISCORD_RATE_LIMITED',
55
- /**
56
- * `reply`, `update`, `showModal` or `followUp` in an execution no interaction started (a task,
57
- * a delayed run): there is no interaction to answer.
58
- */
59
- NO_INTERACTION = 'NO_INTERACTION',
60
- }
61
-
62
- /** Embed and component payloads are Discord's own JSON, relayed as is. */
63
- export type DiscordJsonObject = { [key: string]: JSONValue };
64
-
65
- /**
66
- * What a message may notify. `users` and `roles` let the mentions of every user or role ping;
67
- * `everyone` lets `@everyone` and `@here` notify the whole server.
68
- */
69
- export enum DiscordMentionType {
70
- USERS = 'users',
71
- ROLES = 'roles',
72
- EVERYONE = 'everyone',
73
- }
74
-
75
- /**
76
- * Who a message may notify: Discord's `allowed_mentions` in camelCase. Without it a message pings
77
- * the users it mentions and nobody else: `@everyone`, `@here` and roles stay plain text. An object
78
- * replaces that default as a whole, so what it leaves out notifies nobody. `parse` allows every
79
- * mention of a type; `users` and `roles` allow those ids only (at most 100 each). A type is given
80
- * by `parse` or by its list, never both. `repliedUser` is Discord's reply ping.
81
- */
82
- export interface DiscordAllowedMentions {
83
- parse?: DiscordMentionType[];
84
- users?: string[];
85
- roles?: string[];
86
- repliedUser?: boolean;
87
- }
88
-
89
- export interface DiscordMessage {
90
- content?: string;
91
- embeds?: DiscordJsonObject[];
92
- components?: DiscordJsonObject[];
93
- /** Only the reply to an interaction can be ephemeral. */
94
- ephemeral?: boolean;
95
- /** Who the message may notify; by default the mentioned users only (see `DiscordAllowedMentions`). */
96
- allowedMentions?: DiscordAllowedMentions;
97
- }
98
-
99
- /** Text input style of a modal field. */
100
- export enum DiscordModalFieldStyle {
101
- SHORT = 'SHORT',
102
- PARAGRAPH = 'PARAGRAPH',
103
- }
104
-
105
- export interface DiscordModalField {
106
- customId: string;
107
- label: string;
108
- style?: DiscordModalFieldStyle;
109
- required?: boolean;
110
- placeholder?: string;
111
- value?: string;
112
- minLength?: number;
113
- maxLength?: number;
114
- }
115
-
116
- export interface DiscordShowModalArgs {
117
- customId: string;
118
- title: string;
119
- fields: DiscordModalField[];
120
- }
121
-
122
- export interface DiscordSendMessageArgs {
123
- channelId: string;
124
- message: DiscordMessage;
125
- }
126
-
127
- export interface DiscordEditMessageArgs {
128
- channelId: string;
129
- messageId: string;
130
- message: DiscordMessage;
131
- }
132
-
133
- export interface DiscordDeleteMessageArgs {
134
- channelId: string;
135
- messageId: string;
136
- }
137
-
138
- export interface DiscordRoleArgs {
139
- guildId: string;
140
- userId: string;
141
- roleId: string;
142
- }
143
-
144
- export interface DiscordSendDirectMessageArgs {
145
- userId: string;
146
- message: DiscordMessage;
147
- }
148
-
149
- export interface DiscordGetMemberArgs {
150
- guildId: string;
151
- userId: string;
152
- }
153
-
154
- export interface DiscordGetGuildRolesArgs {
155
- guildId: string;
156
- }
157
-
158
- export interface DiscordGetChannelArgs {
159
- channelId: string;
160
- }
161
-
162
- export interface DiscordGetGuildArgs {
163
- guildId: string;
164
- }
165
-
166
- /** A Discord user, as the reads return it. */
167
- export interface DiscordUser {
168
- id: string;
169
- username: string;
170
- globalName: string | null;
171
- bot: boolean;
172
- }
173
-
174
- /**
175
- * A member of a server (`discord.getMember`): Discord's guild member in camelCase. The read gives
176
- * `null` instead when the user is not in the server. Dates are ISO 8601.
177
- */
178
- export interface DiscordMember {
179
- userId: string;
180
- nick: string | null;
181
- /** Role ids. */
182
- roles: string[];
183
- joinedAt: string | null;
184
- premiumSince: string | null;
185
- communicationDisabledUntil: string | null;
186
- /** True while the member has not passed the server's membership screening. */
187
- pending: boolean;
188
- /** Avatar hash specific to the server. */
189
- avatar: string | null;
190
- user: DiscordUser;
191
- }
192
-
193
- /**
194
- * A role of a server (`discord.getGuildRoles`, `DiscordGuild.roles`). `permissions` is Discord's
195
- * permission bit set as a decimal string: it exceeds 2^53.
196
- */
197
- export interface DiscordRole {
198
- id: string;
199
- name: string;
200
- /** RGB color as an integer, 0 for none. */
201
- color: number;
202
- position: number;
203
- permissions: string;
204
- managed: boolean;
205
- mentionable: boolean;
206
- hoist: boolean;
207
- }
208
-
209
- /** A channel (`discord.getChannel`). `type` is Discord's channel type number. */
210
- export interface DiscordChannel {
211
- id: string;
212
- type: number;
213
- name: string | null;
214
- guildId: string | null;
215
- parentId: string | null;
216
- topic: string | null;
217
- nsfw: boolean;
218
- position: number | null;
219
- }
220
-
221
- /** A server (`discord.getGuild`). The approximate counts are null when Discord does not give them. */
222
- export interface DiscordGuild {
223
- id: string;
224
- name: string;
225
- /** Icon hash. */
226
- icon: string | null;
227
- ownerId: string;
228
- preferredLocale: string;
229
- roles: DiscordRole[];
230
- approximateMemberCount: number | null;
231
- approximatePresenceCount: number | null;
232
- }
233
-
234
- /** The kind of interaction a function was started by. */
235
- export enum DiscordInteractionKind {
236
- COMMAND = 'COMMAND',
237
- COMPONENT = 'COMPONENT',
238
- MODAL = 'MODAL',
239
- }
240
-
241
- /** Discord's interaction types (`type` of the payload) a bot's edge serves. */
242
- export enum DiscordInteractionType {
243
- PING = 1,
244
- APPLICATION_COMMAND = 2,
245
- MESSAGE_COMPONENT = 3,
246
- MODAL_SUBMIT = 5,
247
- }
248
-
249
- /** Discord's interaction callback types the edge answers with. */
250
- export enum DiscordCallbackType {
251
- PONG = 1,
252
- CHANNEL_MESSAGE_WITH_SOURCE = 4,
253
- DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE = 5,
254
- DEFERRED_UPDATE_MESSAGE = 6,
255
- UPDATE_MESSAGE = 7,
256
- MODAL = 9,
257
- }
258
-
259
- /** Discord's message flag that makes a message visible to its recipient only. */
260
- export const EPHEMERAL_FLAG = 64;
261
-
262
- /**
263
- * The `input` of a function triggered by a Discord interaction. The interaction token never
264
- * appears here: it stays in the edge.
265
- */
266
- export interface DiscordInteractionInput {
267
- kind: DiscordInteractionKind;
268
- /** Resolved option values, by option name. */
269
- command?: { name: string; options: Record<string, string | number | boolean> };
270
- component?: { customId: string; values?: string[] };
271
- modal?: { customId: string; fields: Record<string, string> };
272
- user: { id: string; username: string; globalName: string | null };
273
- member?: { roles: string[]; permissions: string };
274
- guildId?: string;
275
- channelId?: string;
276
- locale: string;
277
- /** Discord's payload, without the interaction token. */
278
- interaction: { id: string; applicationId: string; type: number; data: JSONValue };
279
- }
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
+ }
38
+
39
+ /**
40
+ * Codes of the errors `abyss.discord.*` throws. The host fails the call with the message
41
+ * `<code>: <detail>` (or the bare code); the helper rethrows an `Error` named `DiscordError`
42
+ * whose `code` is the code and whose `message` is the detail.
43
+ */
44
+ export enum DiscordErrorCode {
45
+ /** `showModal` after the interaction was acknowledged (more than 2.5 s after it arrived). */
46
+ MODAL_TOO_LATE = 'MODAL_TOO_LATE',
47
+ /** The bot's token is refused (circuit breaker open): no call was made to Discord. */
48
+ DISCORD_TOKEN_INVALID = 'DISCORD_TOKEN_INVALID',
49
+ /** Discord answered 403. */
50
+ DISCORD_FORBIDDEN = 'DISCORD_FORBIDDEN',
51
+ /** Discord answered 404. */
52
+ DISCORD_NOT_FOUND = 'DISCORD_NOT_FOUND',
53
+ /** Discord answered 429. */
54
+ DISCORD_RATE_LIMITED = 'DISCORD_RATE_LIMITED',
55
+ /**
56
+ * `reply`, `update`, `showModal` or `followUp` in an execution no interaction started (a task,
57
+ * a delayed run): there is no interaction to answer.
58
+ */
59
+ NO_INTERACTION = 'NO_INTERACTION',
60
+ }
61
+
62
+ /** Embed and component payloads are Discord's own JSON, relayed as is. */
63
+ export type DiscordJsonObject = { [key: string]: JSONValue };
64
+
65
+ /**
66
+ * What a message may notify. `users` and `roles` let the mentions of every user or role ping;
67
+ * `everyone` lets `@everyone` and `@here` notify the whole server.
68
+ */
69
+ export enum DiscordMentionType {
70
+ USERS = 'users',
71
+ ROLES = 'roles',
72
+ EVERYONE = 'everyone',
73
+ }
74
+
75
+ /**
76
+ * Who a message may notify: Discord's `allowed_mentions` in camelCase. Without it a message pings
77
+ * the users it mentions and nobody else: `@everyone`, `@here` and roles stay plain text. An object
78
+ * replaces that default as a whole, so what it leaves out notifies nobody. `parse` allows every
79
+ * mention of a type; `users` and `roles` allow those ids only (at most 100 each). A type is given
80
+ * by `parse` or by its list, never both. `repliedUser` is Discord's reply ping.
81
+ */
82
+ export interface DiscordAllowedMentions {
83
+ parse?: DiscordMentionType[];
84
+ users?: string[];
85
+ roles?: string[];
86
+ repliedUser?: boolean;
87
+ }
88
+
89
+ export interface DiscordMessage {
90
+ content?: string;
91
+ embeds?: DiscordJsonObject[];
92
+ components?: DiscordJsonObject[];
93
+ /** Only the reply to an interaction can be ephemeral. */
94
+ ephemeral?: boolean;
95
+ /** Who the message may notify; by default the mentioned users only (see `DiscordAllowedMentions`). */
96
+ allowedMentions?: DiscordAllowedMentions;
97
+ }
98
+
99
+ /** Text input style of a modal field. */
100
+ export enum DiscordModalFieldStyle {
101
+ SHORT = 'SHORT',
102
+ PARAGRAPH = 'PARAGRAPH',
103
+ }
104
+
105
+ export interface DiscordModalField {
106
+ customId: string;
107
+ label: string;
108
+ style?: DiscordModalFieldStyle;
109
+ required?: boolean;
110
+ placeholder?: string;
111
+ value?: string;
112
+ minLength?: number;
113
+ maxLength?: number;
114
+ }
115
+
116
+ export interface DiscordShowModalArgs {
117
+ customId: string;
118
+ title: string;
119
+ fields: DiscordModalField[];
120
+ }
121
+
122
+ export interface DiscordSendMessageArgs {
123
+ channelId: string;
124
+ message: DiscordMessage;
125
+ }
126
+
127
+ export interface DiscordEditMessageArgs {
128
+ channelId: string;
129
+ messageId: string;
130
+ message: DiscordMessage;
131
+ }
132
+
133
+ export interface DiscordDeleteMessageArgs {
134
+ channelId: string;
135
+ messageId: string;
136
+ }
137
+
138
+ export interface DiscordRoleArgs {
139
+ guildId: string;
140
+ userId: string;
141
+ roleId: string;
142
+ }
143
+
144
+ export interface DiscordSendDirectMessageArgs {
145
+ userId: string;
146
+ message: DiscordMessage;
147
+ }
148
+
149
+ export interface DiscordGetMemberArgs {
150
+ guildId: string;
151
+ userId: string;
152
+ }
153
+
154
+ export interface DiscordGetGuildRolesArgs {
155
+ guildId: string;
156
+ }
157
+
158
+ export interface DiscordGetChannelArgs {
159
+ channelId: string;
160
+ }
161
+
162
+ export interface DiscordGetGuildArgs {
163
+ guildId: string;
164
+ }
165
+
166
+ /** A Discord user, as the reads return it. */
167
+ export interface DiscordUser {
168
+ id: string;
169
+ username: string;
170
+ globalName: string | null;
171
+ bot: boolean;
172
+ }
173
+
174
+ /**
175
+ * A member of a server (`discord.getMember`): Discord's guild member in camelCase. The read gives
176
+ * `null` instead when the user is not in the server. Dates are ISO 8601.
177
+ */
178
+ export interface DiscordMember {
179
+ userId: string;
180
+ nick: string | null;
181
+ /** Role ids. */
182
+ roles: string[];
183
+ joinedAt: string | null;
184
+ premiumSince: string | null;
185
+ communicationDisabledUntil: string | null;
186
+ /** True while the member has not passed the server's membership screening. */
187
+ pending: boolean;
188
+ /** Avatar hash specific to the server. */
189
+ avatar: string | null;
190
+ user: DiscordUser;
191
+ }
192
+
193
+ /**
194
+ * A role of a server (`discord.getGuildRoles`, `DiscordGuild.roles`). `permissions` is Discord's
195
+ * permission bit set as a decimal string: it exceeds 2^53.
196
+ */
197
+ export interface DiscordRole {
198
+ id: string;
199
+ name: string;
200
+ /** RGB color as an integer, 0 for none. */
201
+ color: number;
202
+ position: number;
203
+ permissions: string;
204
+ managed: boolean;
205
+ mentionable: boolean;
206
+ hoist: boolean;
207
+ }
208
+
209
+ /** A channel (`discord.getChannel`). `type` is Discord's channel type number. */
210
+ export interface DiscordChannel {
211
+ id: string;
212
+ type: number;
213
+ name: string | null;
214
+ guildId: string | null;
215
+ parentId: string | null;
216
+ topic: string | null;
217
+ nsfw: boolean;
218
+ position: number | null;
219
+ }
220
+
221
+ /** A server (`discord.getGuild`). The approximate counts are null when Discord does not give them. */
222
+ export interface DiscordGuild {
223
+ id: string;
224
+ name: string;
225
+ /** Icon hash. */
226
+ icon: string | null;
227
+ ownerId: string;
228
+ preferredLocale: string;
229
+ roles: DiscordRole[];
230
+ approximateMemberCount: number | null;
231
+ approximatePresenceCount: number | null;
232
+ }
233
+
234
+ /** The kind of interaction a function was started by. */
235
+ export enum DiscordInteractionKind {
236
+ COMMAND = 'COMMAND',
237
+ COMPONENT = 'COMPONENT',
238
+ MODAL = 'MODAL',
239
+ }
240
+
241
+ /** Discord's interaction types (`type` of the payload) a bot's edge serves. */
242
+ export enum DiscordInteractionType {
243
+ PING = 1,
244
+ APPLICATION_COMMAND = 2,
245
+ MESSAGE_COMPONENT = 3,
246
+ MODAL_SUBMIT = 5,
247
+ }
248
+
249
+ /** Discord's interaction callback types the edge answers with. */
250
+ export enum DiscordCallbackType {
251
+ PONG = 1,
252
+ CHANNEL_MESSAGE_WITH_SOURCE = 4,
253
+ DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE = 5,
254
+ DEFERRED_UPDATE_MESSAGE = 6,
255
+ UPDATE_MESSAGE = 7,
256
+ MODAL = 9,
257
+ }
258
+
259
+ /** Discord's message flag that makes a message visible to its recipient only. */
260
+ export const EPHEMERAL_FLAG = 64;
261
+
262
+ /**
263
+ * The `input` of a function triggered by a Discord interaction. The interaction token never
264
+ * appears here: it stays in the edge.
265
+ */
266
+ export interface DiscordInteractionInput {
267
+ kind: DiscordInteractionKind;
268
+ /** Resolved option values, by option name. */
269
+ command?: { name: string; options: Record<string, string | number | boolean> };
270
+ component?: { customId: string; values?: string[] };
271
+ modal?: { customId: string; fields: Record<string, string> };
272
+ user: { id: string; username: string; globalName: string | null };
273
+ member?: { roles: string[]; permissions: string };
274
+ guildId?: string;
275
+ channelId?: string;
276
+ locale: string;
277
+ /** Discord's payload, without the interaction token. */
278
+ interaction: { id: string; applicationId: string; type: number; data: JSONValue };
279
+ }
280
+
281
+ /** The Gateway events a bot's function can be triggered by. */
282
+ export enum DiscordEventType {
283
+ GUILD_MEMBER_ADD = 'GUILD_MEMBER_ADD',
284
+ GUILD_MEMBER_REMOVE = 'GUILD_MEMBER_REMOVE',
285
+ MESSAGE_CREATE = 'MESSAGE_CREATE',
286
+ MESSAGE_REACTION_ADD = 'MESSAGE_REACTION_ADD',
287
+ }
288
+
289
+ /** `data` of a `GUILD_MEMBER_ADD` event. */
290
+ export interface DiscordMemberAddData {
291
+ user: {
292
+ id: string;
293
+ username: string;
294
+ globalName: string | null;
295
+ bot: boolean;
296
+ avatar: string | null;
297
+ };
298
+ /** Role ids. */
299
+ roles: string[];
300
+ joinedAt: string | null;
301
+ /** True while the member has not passed the server's membership screening. */
302
+ pending: boolean;
303
+ }
304
+
305
+ /** `data` of a `GUILD_MEMBER_REMOVE` event. */
306
+ export interface DiscordMemberRemoveData {
307
+ user: { id: string; username: string; globalName: string | null; bot: boolean };
308
+ }
309
+
310
+ /** `data` of a `MESSAGE_CREATE` event. */
311
+ export interface DiscordMessageCreateData {
312
+ id: string;
313
+ channelId: string;
314
+ author: { id: string; username: string; globalName: string | null; bot: boolean };
315
+ member: { roles: string[]; nick: string | null } | null;
316
+ /** Ids of the mentioned users. */
317
+ mentions: string[];
318
+ referencedMessageId: string | null;
319
+ attachmentCount: number;
320
+ /** ISO 8601. */
321
+ createdAt: string;
322
+ /** Present only when the trigger asks for the content (`includeContent`). */
323
+ content?: string;
324
+ }
325
+
326
+ /** `data` of a `MESSAGE_REACTION_ADD` event. */
327
+ export interface DiscordReactionAddData {
328
+ userId: string;
329
+ channelId: string;
330
+ messageId: string;
331
+ messageAuthorId: string | null;
332
+ member: { roles: string[] } | null;
333
+ emoji: { id: string | null; name: string | null; animated: boolean };
334
+ }
335
+
336
+ /** The `kind` of a `DiscordEventInput`. */
337
+ export const DISCORD_EVENT_INPUT_KIND = 'DISCORD_EVENT';
338
+
339
+ /** The `input` of a function triggered by a Discord Gateway event. */
340
+ export interface DiscordEventInput {
341
+ kind: typeof DISCORD_EVENT_INPUT_KIND;
342
+ event: { id: string; type: DiscordEventType; trigger: { id: string; name: string } };
343
+ guildId: string;
344
+ /** When the Gateway received it, ISO 8601. */
345
+ receivedAt: string;
346
+ firedAt: string;
347
+ attempt: number;
348
+ data:
349
+ | DiscordMemberAddData
350
+ | DiscordMemberRemoveData
351
+ | DiscordMessageCreateData
352
+ | DiscordReactionAddData;
353
+ }
354
+
355
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
356
+ typeof value === 'object' && value !== null && !Array.isArray(value);
357
+
358
+ /**
359
+ * Tells a `DiscordEventInput` from the other inputs of a function (interaction, scheduled run). It
360
+ * checks the envelope (`kind`, event, guild, dates, attempt, `data` object), not the shape of `data`.
361
+ */
362
+ export const isDiscordEventInput = (value: unknown): value is DiscordEventInput => {
363
+ if (!isRecord(value) || value.kind !== DISCORD_EVENT_INPUT_KIND) {
364
+ return false;
365
+ }
366
+ const { event } = value;
367
+ return (
368
+ isRecord(event) &&
369
+ typeof event.id === 'string' &&
370
+ Object.values(DiscordEventType).includes(event.type as DiscordEventType) &&
371
+ isRecord(event.trigger) &&
372
+ typeof event.trigger.id === 'string' &&
373
+ typeof event.trigger.name === 'string' &&
374
+ typeof value.guildId === 'string' &&
375
+ typeof value.receivedAt === 'string' &&
376
+ typeof value.firedAt === 'string' &&
377
+ typeof value.attempt === 'number' &&
378
+ isRecord(value.data)
379
+ );
380
+ };