@effect/chat-discord 0.0.0 → 4.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/AGENTS.md +434 -0
  2. package/CLAUDE.md +434 -0
  3. package/LICENSE +21 -0
  4. package/README.md +27 -0
  5. package/ai-docs/README.md +44 -0
  6. package/ai-docs/package.json +36 -0
  7. package/ai-docs/src/01_effect/01_basics/01_effect-gen.ts +30 -0
  8. package/ai-docs/src/01_effect/01_basics/02_effect-fn.ts +52 -0
  9. package/ai-docs/src/01_effect/01_basics/10_creating-effects.ts +74 -0
  10. package/ai-docs/src/01_effect/01_basics/index.md +7 -0
  11. package/ai-docs/src/01_effect/02_schema/10_schema-basics.ts +43 -0
  12. package/ai-docs/src/01_effect/02_schema/index.md +7 -0
  13. package/ai-docs/src/01_effect/03_services/01_service.ts +45 -0
  14. package/ai-docs/src/01_effect/03_services/10_reference.ts +10 -0
  15. package/ai-docs/src/01_effect/03_services/20_layer-composition.ts +70 -0
  16. package/ai-docs/src/01_effect/03_services/20_layer-unwrap.ts +66 -0
  17. package/ai-docs/src/01_effect/03_services/index.md +5 -0
  18. package/ai-docs/src/01_effect/04_errors/01_error-handling.ts +30 -0
  19. package/ai-docs/src/01_effect/04_errors/10_catch-tags.ts +24 -0
  20. package/ai-docs/src/01_effect/04_errors/20_reason-errors.ts +64 -0
  21. package/ai-docs/src/01_effect/04_errors/index.md +1 -0
  22. package/ai-docs/src/01_effect/05_resources/10_acquire-release.ts +105 -0
  23. package/ai-docs/src/01_effect/05_resources/20_layer-side-effects.ts +31 -0
  24. package/ai-docs/src/01_effect/05_resources/30_layer-map.ts +86 -0
  25. package/ai-docs/src/01_effect/05_resources/index.md +3 -0
  26. package/ai-docs/src/01_effect/06_running/10_run-main.ts +30 -0
  27. package/ai-docs/src/01_effect/06_running/20_layer-launch.ts +27 -0
  28. package/ai-docs/src/01_effect/06_running/index.md +1 -0
  29. package/ai-docs/src/01_effect/07_pubsub/10_pubsub.ts +56 -0
  30. package/ai-docs/src/01_effect/07_pubsub/index.md +3 -0
  31. package/ai-docs/src/03_stream/10_creating-streams.ts +103 -0
  32. package/ai-docs/src/03_stream/20_consuming-streams.ts +137 -0
  33. package/ai-docs/src/03_stream/30_encoding.ts +163 -0
  34. package/ai-docs/src/03_stream/index.md +4 -0
  35. package/ai-docs/src/04_integration/10_managed-runtime.ts +129 -0
  36. package/ai-docs/src/04_integration/index.md +5 -0
  37. package/ai-docs/src/05_batching/10_request-resolver.ts +89 -0
  38. package/ai-docs/src/05_batching/index.md +3 -0
  39. package/ai-docs/src/06_schedule/10_schedules.ts +111 -0
  40. package/ai-docs/src/06_schedule/index.md +3 -0
  41. package/ai-docs/src/07_datetime/10_creating-and-formatting.ts +30 -0
  42. package/ai-docs/src/07_datetime/20_time-zones.ts +44 -0
  43. package/ai-docs/src/07_datetime/index.md +5 -0
  44. package/ai-docs/src/08_observability/10_logging.ts +66 -0
  45. package/ai-docs/src/08_observability/20_otlp-tracing.ts +95 -0
  46. package/ai-docs/src/08_observability/index.md +7 -0
  47. package/ai-docs/src/09_testing/10_effect-tests.ts +55 -0
  48. package/ai-docs/src/09_testing/20_layer-tests.ts +138 -0
  49. package/ai-docs/src/09_testing/index.md +1 -0
  50. package/ai-docs/src/10_predicate/01_basics.ts +14 -0
  51. package/ai-docs/src/10_predicate/index.md +9 -0
  52. package/ai-docs/src/40_sql/10_basics.ts +175 -0
  53. package/ai-docs/src/40_sql/index.md +6 -0
  54. package/ai-docs/src/50_http-client/10_basics.ts +102 -0
  55. package/ai-docs/src/50_http-client/index.md +3 -0
  56. package/ai-docs/src/51_http-server/10_basics.ts +118 -0
  57. package/ai-docs/src/51_http-server/20_testing.ts +100 -0
  58. package/ai-docs/src/51_http-server/fixtures/api/Api.ts +14 -0
  59. package/ai-docs/src/51_http-server/fixtures/api/Authorization.ts +36 -0
  60. package/ai-docs/src/51_http-server/fixtures/api/System.ts +10 -0
  61. package/ai-docs/src/51_http-server/fixtures/api/Users.ts +107 -0
  62. package/ai-docs/src/51_http-server/fixtures/domain/User.ts +21 -0
  63. package/ai-docs/src/51_http-server/fixtures/domain/UserErrors.ts +22 -0
  64. package/ai-docs/src/51_http-server/fixtures/server/Authorization.ts +37 -0
  65. package/ai-docs/src/51_http-server/fixtures/server/Users/http.ts +80 -0
  66. package/ai-docs/src/51_http-server/fixtures/server/Users.ts +183 -0
  67. package/ai-docs/src/51_http-server/index.md +3 -0
  68. package/ai-docs/src/52_graphql/10_basics.ts +117 -0
  69. package/ai-docs/src/52_graphql/20_testing.ts +45 -0
  70. package/ai-docs/src/52_graphql/30_batching.ts +118 -0
  71. package/ai-docs/src/52_graphql/40_aliased-batching.ts +124 -0
  72. package/ai-docs/src/52_graphql/50_errors-and-retries.ts +74 -0
  73. package/ai-docs/src/52_graphql/index.md +6 -0
  74. package/ai-docs/src/60_child-process/10_working-with-child-processes.ts +117 -0
  75. package/ai-docs/src/60_child-process/index.md +3 -0
  76. package/ai-docs/src/70_cli/10_basics.ts +162 -0
  77. package/ai-docs/src/70_cli/index.md +5 -0
  78. package/ai-docs/src/71_ai/10_language-model.ts +156 -0
  79. package/ai-docs/src/71_ai/20_tools.ts +226 -0
  80. package/ai-docs/src/71_ai/30_chat.ts +158 -0
  81. package/ai-docs/src/71_ai/fixtures/domain/LaunchPlan.ts +9 -0
  82. package/ai-docs/src/71_ai/index.md +5 -0
  83. package/ai-docs/src/80_cluster/10_entities.ts +97 -0
  84. package/ai-docs/src/80_cluster/index.md +4 -0
  85. package/ai-docs/src/index.md +10 -0
  86. package/ai-docs/tsconfig.json +28 -0
  87. package/dist/DiscordChat.d.ts +530 -0
  88. package/dist/DiscordChat.d.ts.map +1 -0
  89. package/dist/DiscordChat.js +574 -0
  90. package/dist/DiscordChat.js.map +1 -0
  91. package/dist/index.d.ts +10 -0
  92. package/dist/index.d.ts.map +1 -0
  93. package/dist/index.js +11 -0
  94. package/dist/index.js.map +1 -0
  95. package/dist/internal/adapter.d.ts +2 -0
  96. package/dist/internal/adapter.d.ts.map +1 -0
  97. package/dist/internal/adapter.js +492 -0
  98. package/dist/internal/adapter.js.map +1 -0
  99. package/dist/internal/errors.d.ts +2 -0
  100. package/dist/internal/errors.d.ts.map +1 -0
  101. package/dist/internal/errors.js +236 -0
  102. package/dist/internal/errors.js.map +1 -0
  103. package/dist/internal/interactions.d.ts +2 -0
  104. package/dist/internal/interactions.d.ts.map +1 -0
  105. package/dist/internal/interactions.js +490 -0
  106. package/dist/internal/interactions.js.map +1 -0
  107. package/dist/internal/length.d.ts +2 -0
  108. package/dist/internal/length.d.ts.map +1 -0
  109. package/dist/internal/length.js +31 -0
  110. package/dist/internal/length.js.map +1 -0
  111. package/dist/internal/mentions.d.ts +2 -0
  112. package/dist/internal/mentions.d.ts.map +1 -0
  113. package/dist/internal/mentions.js +93 -0
  114. package/dist/internal/mentions.js.map +1 -0
  115. package/dist/internal/normalize.d.ts +2 -0
  116. package/dist/internal/normalize.d.ts.map +1 -0
  117. package/dist/internal/normalize.js +322 -0
  118. package/dist/internal/normalize.js.map +1 -0
  119. package/dist/internal/options.d.ts +2 -0
  120. package/dist/internal/options.d.ts.map +1 -0
  121. package/dist/internal/options.js +25 -0
  122. package/dist/internal/options.js.map +1 -0
  123. package/package.json +63 -1
  124. package/src/DiscordChat.ts +703 -0
  125. package/src/index.ts +12 -0
  126. package/src/internal/adapter.ts +653 -0
  127. package/src/internal/errors.ts +184 -0
  128. package/src/internal/interactions.ts +451 -0
  129. package/src/internal/length.ts +33 -0
  130. package/src/internal/mentions.ts +101 -0
  131. package/src/internal/normalize.ts +363 -0
  132. package/src/internal/options.ts +29 -0
@@ -0,0 +1,28 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "extends": "../tsconfig.base.json",
4
+ "include": ["src/**/*.ts", "src/**/*.tsx"],
5
+ "compilerOptions": {
6
+ "rootDir": ".",
7
+ "noEmit": true,
8
+ "declaration": false,
9
+ "declarationMap": false,
10
+ "composite": false,
11
+ "incremental": false,
12
+ "types": ["node"],
13
+ "plugins": [
14
+ {
15
+ "name": "@effect/language-service",
16
+ "effectFn": ["untraced"],
17
+ "namespaceImportPackages": [],
18
+ "diagnosticSeverity": {
19
+ "floatingEffect": "off",
20
+ "globalJson": "off",
21
+ "globalJsonInEffect": "off",
22
+ "unstableApiUsage": "off",
23
+ "effectFnOpportunity": "error"
24
+ }
25
+ }
26
+ ]
27
+ }
28
+ }
@@ -0,0 +1,530 @@
1
+ import { DiscordGateway } from "dfx/DiscordGateway";
2
+ import { DiscordREST } from "dfx/DiscordREST";
3
+ import * as Cause from "effect/Cause";
4
+ import { Registry } from "effect/chat/ChatAdapter";
5
+ import type { ChatError } from "effect/chat/ChatError";
6
+ import type { ChatEvent, MessageCreated, ReactionAdded } from "effect/chat/ChatEvent";
7
+ import { ChatHandler, NotAccepted } from "effect/chat/ChatHandler";
8
+ import * as ChatInteraction from "effect/chat/ChatInteraction";
9
+ import type { ParseMention } from "effect/chat/ChatTest";
10
+ import type { ChatThread } from "effect/chat/ChatThread";
11
+ import * as Context from "effect/Context";
12
+ import * as Duration from "effect/Duration";
13
+ import * as Effect from "effect/Effect";
14
+ import * as HttpClient from "effect/http/HttpClient";
15
+ import * as Layer from "effect/Layer";
16
+ import * as Option from "effect/Option";
17
+ import * as Result from "effect/Result";
18
+ import * as Schema from "effect/Schema";
19
+ declare const DiscordChat_base: Context.ServiceClass<DiscordChat, "@effect/chat-discord/DiscordChat", {
20
+ readonly botUserId: Effect.Effect<string, ChatError>;
21
+ readonly dispatch: (dispatch: {
22
+ readonly t: string;
23
+ readonly d: unknown;
24
+ }) => Effect.Effect<void, NotAccepted, ChatHandler>;
25
+ readonly interaction: (interaction: unknown, options?: {
26
+ /** The handler's share of Discord's 3 seconds. Default 2 seconds. */
27
+ readonly deadline?: Duration.Duration | undefined;
28
+ }) => Effect.Effect<void, never, ChatInteraction.Handler>;
29
+ readonly guild: (thread: ChatThread) => Effect.Effect<Option.Option<string>, ChatError>;
30
+ }>;
31
+ /**
32
+ * The Discord side of the adapter, for what the core has no op for.
33
+ *
34
+ * **Details**
35
+ *
36
+ * - `botUserId` comes from `GET /users/@me`, read once. A failed read is
37
+ * tried again next time.
38
+ * - `dispatch` takes one gateway dispatch, `{ t, d }`, and hands its
39
+ * events to the `ChatHandler` one at a time. It reads `d` with a Schema
40
+ * and ignores what it doesn't know, so it takes no dfx type.
41
+ * - When a `MESSAGE_CREATE` carries no `channel_type`, `dispatch` looks
42
+ * the channel up, cached and with a five second timeout. A failed lookup
43
+ * is logged and fails with `NotAccepted`. A lookup that would fail the same
44
+ * way again, such as a deleted channel, is logged and the event dropped.
45
+ * - `interaction` takes one `INTERACTION_CREATE` payload, from the gateway
46
+ * or from an HTTP interactions endpoint. It reads it with a Schema, so it
47
+ * takes no dfx type, and ignores an interaction it can't express: a
48
+ * message or user command, an autocomplete request, a slash command with
49
+ * anything but at most one string option. It calls the handler through
50
+ * `ChatInteraction.handle` and answers through the interaction callback
51
+ * endpoint, which has to land inside Discord's 3 seconds: `deadline` is
52
+ * the handler's share, 2 seconds by default.
53
+ * - A click is answered with callback type 6, or type 9 for `OpenModal`.
54
+ * A submit's `close` is type 6. An `errors` reply is type 4 with the
55
+ * ephemeral flag, listing each message as plain text, and the modal closes: Discord
56
+ * can't show an error inside a modal, so the descriptor has no
57
+ * `modalErrors`.
58
+ * - A slash command goes to `Handler.command`. `respond` is callback type
59
+ * 4 with the message a `post` sends, `defer` type 5 and `OpenModal` type
60
+ * 9, each with the ephemeral flag when private. An `Ack`, which is what a
61
+ * command nobody handles gets, sends nothing, so a command another library
62
+ * owns is left to it. `withPostOptions` does not reach a command's
63
+ * answer, `respond` or `followUp`: a mention in it renders and notifies
64
+ * nobody.
65
+ * - Commands were recorded in a guild channel over the gateway. A command
66
+ * in a direct is from Discord's docs and was not run.
67
+ * - A click on a private answer carries the message, with the ephemeral flag
68
+ * (64). Its `messageId` tells which answer was clicked. Discord's docs
69
+ * reach an interaction's messages only through its token, under
70
+ * `/webhooks`, and say nothing of the channel routes `Chat.edit`,
71
+ * `delete` and `fetchMessage` use on an ephemeral message. Treat the id
72
+ * as one no message op works with. This is from the docs: no op on it was
73
+ * run.
74
+ * - `followUp` edits the deferred command's original response, with the
75
+ * application id and token from its `raw`, so a second call replaces the
76
+ * first answer. It fails `NotFound` with detail `interaction_expired`
77
+ * from 15 minutes after the command, read from the interaction's id,
78
+ * without calling Discord. Otherwise it makes one HTTP request, through
79
+ * the layer's `HttpClient` and not dfx's client, and never sends it
80
+ * again, unless that `HttpClient` retries by itself: a 429 fails
81
+ * `RateLimited`, for the caller to retry or not.
82
+ * Discord answers 401 with code 50027 past 15 minutes, which is
83
+ * `interaction_expired` too. Code 10015 is mapped from the docs and was
84
+ * not seen.
85
+ * - A modal that can't be built, a reply to a command that fails the checks
86
+ * of a `post`, or a callback that fails, goes to `Handler.refused`. A
87
+ * callback that comes back 404 `Unknown interaction` was late: that is
88
+ * `NotFound` with detail `interaction_expired`. A failed ack is logged.
89
+ * - `guild` reads the channel, cached. `None` for a direct.
90
+ *
91
+ * @stability unstable
92
+ * @category services
93
+ * @since 4.1.5
94
+ */
95
+ export declare class DiscordChat extends DiscordChat_base {
96
+ }
97
+ /**
98
+ * Registers the Discord adapter in the `Registry`.
99
+ *
100
+ * **Details**
101
+ *
102
+ * - Every REST call but `followUp` goes through the `DiscordREST` in
103
+ * context. dfx waits on its rate-limit buckets and sends again after a
104
+ * 429, so a rate limit shows up as a slower call.
105
+ * - `HttpClient` downloads attachments from Discord's CDN, with no token,
106
+ * and sends `followUp`'s edit, which needs no bot token either. That edit
107
+ * skips dfx's client so a 429 is not sent again past the 15 minutes. It
108
+ * goes to the `rest.baseUrl` of the `DiscordConfig` in context, or
109
+ * Discord's own API when there is none.
110
+ * - Content goes out as written: Discord reads Markdown itself. Every write
111
+ * sends `allowed_mentions: { parse: [] }` and the `SUPPRESS_EMBEDS`
112
+ * flag, on the post and on every edit, unless `withPostOptions` says
113
+ * otherwise. A reply does not ping its author.
114
+ * - `post` and `edit` fail with `TooLarge` before calling Discord past
115
+ * 2,000 code points.
116
+ * - A refused file fails `denied` with detail `file_refused` and leaves
117
+ * nothing posted: more than 10 files or 10 MiB, by the adapter's own
118
+ * check, or any `denied` answer to a post with files. The reason Discord
119
+ * gave is the `cause`.
120
+ * - A message's elements go out as classic components beside `content`,
121
+ * in action rows: five buttons to a row and a select alone in its row, in
122
+ * the order given, up to five rows. More fail `TooLarge` before the call.
123
+ * An `edit` without `actions` removes the ones there were.
124
+ * - A button's id and encoded value share one `custom_id`, joined by one
125
+ * character, and so do a modal's id and its metadata. An id may not
126
+ * contain U+241F, the join. `limits.actionDataLength` and
127
+ * `limits.modalMetadataLength` are 99 code points, the 100 of
128
+ * `custom_id` less the join.
129
+ * - Button styles: `default` is secondary, `primary` primary and
130
+ * `danger` danger.
131
+ * - A modal's inputs are Label components around a text input or a string
132
+ * select, and a `text` is a text display. Inputs and texts together are
133
+ * 5 at most. A modal over that, or with a title or label over 45
134
+ * characters, reaches `Handler.refused` as `TooLarge`. Discord has no
135
+ * submit label, so `submitLabel` is not shown.
136
+ * - `replyTo` sends a message reference. Discord refuses a reply to a
137
+ * message that is gone, unless `withPostOptions({ failIfNotExists: false })`
138
+ * lets it through as a plain message.
139
+ * - Only `DEFAULT`, `REPLY`, `CHAT_INPUT_COMMAND` and
140
+ * `CONTEXT_MENU_COMMAND` messages are messages. A forward, a poll, a
141
+ * sticker or a components-only message is `DEFAULT` and comes through,
142
+ * often with `text: ""`. Its content stays on `raw`.
143
+ * - A deferred command reply arrives empty. Its text comes in a later edit,
144
+ * which is not delivered, so only `history` and `fetchMessage` have it.
145
+ * - A webhook message has the webhook id as its author id and `isBot`.
146
+ * `getUser` on that id fails `NotFound`.
147
+ * - A mention is reported when its token is in `content` outside code and
148
+ * Discord says it resolved. A reply that pings its author is not a
149
+ * mention. Only the bot's user mention is `isSelf`: a mention of its
150
+ * managed role is the consumer's to recognise.
151
+ * - The address of a thread is its channel id. `startThread` names the
152
+ * thread after the message's text, cut to 100 characters, and gives the
153
+ * existing thread when the message already has one.
154
+ * - `history` of a thread started from a message puts the opener, from the
155
+ * parent channel, in place of Discord's placeholder. When the opener is
156
+ * deleted it starts at the first reply. A forum post's starter is in the
157
+ * thread itself.
158
+ * - `getUser` with `in` gives the guild nick, and its `raw` is Discord's
159
+ * guild member, whose `roles` are the member's role ids. Without `in`, in
160
+ * a direct, or for someone who left the guild, `raw` is the user.
161
+ * - The layer provides the `DiscordChat` service as well as registering the
162
+ * adapter, and makes every call through the `DiscordREST` it is given. A
163
+ * consumer that needs more of Discord than `Chat` has reads either from
164
+ * context beside `Chat`. dfx keeps one rate-limit state for both.
165
+ * - There is no native streaming: streamed replies post, then edit, at
166
+ * least 1.5 seconds apart.
167
+ *
168
+ * @stability unstable
169
+ * @category layers
170
+ * @since 4.1.5
171
+ */
172
+ export declare const layer: Layer.Layer<DiscordChat, never, DiscordREST | HttpClient.HttpClient | Registry>;
173
+ /**
174
+ * The address of a Discord channel, thread or direct: its channel id.
175
+ *
176
+ * @stability unstable
177
+ * @category constructors
178
+ * @since 4.1.5
179
+ */
180
+ export declare const thread: (channelId: string) => ChatThread;
181
+ declare const MissingChannelType_base: Schema.Class<MissingChannelType, Schema.TaggedStruct<"MissingChannelType", {
182
+ readonly channelId: Schema.String;
183
+ }>, Cause.YieldableError>;
184
+ /**
185
+ * Error for a message whose channel type is in neither the payload nor the
186
+ * context, from `normalize`.
187
+ *
188
+ * @stability unstable
189
+ * @category errors
190
+ * @since 4.1.5
191
+ */
192
+ export declare class MissingChannelType extends MissingChannelType_base {
193
+ }
194
+ /**
195
+ * The normaliser behind `dispatch`, without delivering.
196
+ *
197
+ * **Details**
198
+ *
199
+ * - Pure: it never looks a channel up. It reads the channel type from the
200
+ * payload's `channel_type`, then from `context.channelType`, Discord's
201
+ * numeric type, and fails with `MissingChannelType` when neither has one.
202
+ * - Types 10, 11 and 12 are a `thread`, 1 and 3 a `direct`, and every
203
+ * other type a `channel`.
204
+ * - `MESSAGE_CREATE`, `MESSAGE_REACTION_ADD` and
205
+ * `MESSAGE_REACTION_REMOVE` give an event. Every other dispatch, and a
206
+ * message type that is not a message, gives none.
207
+ * - A custom emoji is `custom:{name}`, its id left on `raw.emoji.id`.
208
+ * - `raw` is `d`, undecoded.
209
+ *
210
+ * @stability unstable
211
+ * @category normalization
212
+ * @since 4.1.5
213
+ */
214
+ export declare const normalize: (dispatch: {
215
+ readonly t: string;
216
+ readonly d: unknown;
217
+ }, context: {
218
+ readonly botUserId: string;
219
+ readonly channelType?: number | undefined;
220
+ }) => Result.Result<ReadonlyArray<ChatEvent>, MissingChannelType>;
221
+ /**
222
+ * The member on a message's or added reaction's `raw`: the guild, the
223
+ * roles and the nick.
224
+ *
225
+ * **Details**
226
+ *
227
+ * Pure, with no REST call. `None` for a direct, a webhook message, or a
228
+ * payload with no member. Discord sends no member on a removed reaction, so
229
+ * it takes none.
230
+ *
231
+ * @stability unstable
232
+ * @category accessors
233
+ * @since 4.1.5
234
+ */
235
+ export declare const member: (event: MessageCreated | ReactionAdded) => Option.Option<{
236
+ readonly guildId: string;
237
+ readonly roles: ReadonlyArray<string>;
238
+ readonly nick?: string | undefined;
239
+ }>;
240
+ /**
241
+ * The guild on an event's `raw`.
242
+ *
243
+ * **Details**
244
+ *
245
+ * Pure, with no REST call. It works for every event, webhook messages and
246
+ * removed reactions included. `None` for a direct.
247
+ *
248
+ * @stability unstable
249
+ * @category accessors
250
+ * @since 4.1.5
251
+ */
252
+ export declare const guildId: (event: ChatEvent) => Option.Option<string>;
253
+ /**
254
+ * For a long-running process: subscribes to dfx's gateway and feeds every
255
+ * dispatch to `DiscordChat.dispatch`, one at a time.
256
+ *
257
+ * **Details**
258
+ *
259
+ * - Intents and sharding stay in the consumer's dfx config. So does the
260
+ * gateway session: dfx keeps the session id, the resume URL and the
261
+ * sequence in the `ShardStateStore` given to `DiscordGatewayLive`.
262
+ * - It reads dfx's stream of dispatches, which a dispatch reaches after its
263
+ * sequence is stored. An event that fails with `NotAccepted` is logged
264
+ * and dropped, and so is one the process stops in the middle of: Discord
265
+ * does not send it again.
266
+ *
267
+ * @see {@link layerDispatchHook} for a host that is stopped and started again
268
+ *
269
+ * @stability unstable
270
+ * @category layers
271
+ * @since 4.1.5
272
+ */
273
+ export declare const layerGateway: Layer.Layer<never, never, DiscordGateway | DiscordChat | ChatHandler>;
274
+ /**
275
+ * For a host that is stopped and started again, a Durable Object for one:
276
+ * feeds every dispatch to `DiscordChat.dispatch` from dfx's `DispatchHook`,
277
+ * before the shard stores the dispatch's sequence.
278
+ *
279
+ * **When to use**
280
+ *
281
+ * Use when an event must not be lost to a restart, in place of
282
+ * `layerGateway`. Never provide both: every event would be delivered twice.
283
+ *
284
+ * **Details**
285
+ *
286
+ * - Provide it to the dfx layer that builds the shards, `DiscordGatewayLive`
287
+ * or `ShardLive`, beside a `ShardStateStore` over storage that outlives
288
+ * the host. dfx keeps the session id, the resume URL and the sequence
289
+ * there, so a host built again sends RESUME, not IDENTIFY, and Discord
290
+ * sends what it missed.
291
+ * - The handler runs before the sequence is stored. A host stopped in the
292
+ * middle of an event never stored that event's sequence, so the RESUME
293
+ * brings the event back.
294
+ * - An event that fails with `NotAccepted` fails the hook. dfx drops the
295
+ * connection, resumes from the last stored sequence and gets the event
296
+ * again, after 1 second, doubling with every failure in a row up to 2
297
+ * minutes. An event that is refused every time holds back every event
298
+ * after it.
299
+ * - A defect in the handler is logged and the event dropped, as a replay
300
+ * would only fail the same way.
301
+ * - Delivery is at least once. A host stopped after the handler returned and
302
+ * before the sequence was stored gets that event a second time, so a
303
+ * handler that can't take an event twice remembers the ids it took.
304
+ * - The hook runs on the shard's read loop, one dispatch at a time. A
305
+ * handler slower than Discord's heartbeat interval, about 41 seconds,
306
+ * makes the shard reconnect.
307
+ * - Interactions stay with `layerGatewayInteractions`: each has 3 seconds,
308
+ * so there is nothing to resume.
309
+ *
310
+ * **Gotchas**
311
+ *
312
+ * - Discord replays only while it keeps the session. When it refuses the
313
+ * RESUME, dfx identifies again and the events in between are gone.
314
+ * - Waking the host, restarting a shard that went quiet and backing off
315
+ * after a close Discord won't change its mind on, an invalid token (4004)
316
+ * or a disallowed intent (4014), stay with the host: dfx reconnects
317
+ * whatever the close code.
318
+ *
319
+ * **Example** (Resuming from the host's own storage)
320
+ *
321
+ * ```ts
322
+ * import { DiscordChat } from "@effect/chat-discord"
323
+ * import { ShardLive } from "dfx/DiscordGateway/Shard"
324
+ * import { type ShardState, ShardStateStore } from "dfx/DiscordGateway/Shard/StateStore"
325
+ * import { Effect, Layer, Option } from "effect"
326
+ *
327
+ * // Stands in for storage that outlives the host.
328
+ * const storage = new Map<string, ShardState>()
329
+ *
330
+ * const SessionStore = Layer.succeed(
331
+ * ShardStateStore,
332
+ * ShardStateStore.of({
333
+ * forShard: ([id, count]) => {
334
+ * const key = `${id}/${count}`
335
+ * return {
336
+ * get: Effect.sync(() => Option.fromUndefinedOr(storage.get(key))),
337
+ * set: (state) => Effect.sync(() => void storage.set(key, state)),
338
+ * clear: Effect.sync(() => void storage.delete(key))
339
+ * }
340
+ * }
341
+ * })
342
+ * )
343
+ *
344
+ * // Still needs `DiscordChat`, a `ChatHandler` and the rest of dfx's gateway.
345
+ * export const ShardResuming = ShardLive.pipe(
346
+ * Layer.provide([DiscordChat.layerDispatchHook, SessionStore])
347
+ * )
348
+ * ```
349
+ *
350
+ * @see {@link layerGateway} for a process that runs until it is told to stop
351
+ *
352
+ * @stability unstable
353
+ * @category layers
354
+ * @since 4.1.5
355
+ */
356
+ export declare const layerDispatchHook: Layer.Layer<never, never, DiscordChat | ChatHandler>;
357
+ /**
358
+ * For a long-running process: subscribes to dfx's gateway and feeds every
359
+ * `INTERACTION_CREATE` to `DiscordChat.interaction`.
360
+ *
361
+ * **Details**
362
+ *
363
+ * Interactions run concurrently, since each has its own 3 seconds. It is
364
+ * separate from `layerGateway`, so a consumer with no buttons needs no
365
+ * `ChatInteraction.Handler`.
366
+ *
367
+ * @stability unstable
368
+ * @category layers
369
+ * @since 4.1.5
370
+ */
371
+ export declare const layerGatewayInteractions: Layer.Layer<never, never, DiscordGateway | DiscordChat | ChatInteraction.Handler>;
372
+ /**
373
+ * The normaliser behind `DiscordChat.interaction`.
374
+ *
375
+ * **Details**
376
+ *
377
+ * - Pure. A click on a button gives `ButtonClicked`, with the id and value
378
+ * split from the `custom_id`. A string select gives `SelectChanged`, and
379
+ * a modal's submit `ModalSubmitted`, its metadata split from the modal's
380
+ * `custom_id`.
381
+ * - The thread and message come from the `message` Discord sends with the
382
+ * interaction, so a modal's submit carries the message whose element
383
+ * opened it. A modal a command opened has no message, so its submit has
384
+ * no `messageId`, and its thread is the channel.
385
+ * - A submit's `values` hold each answered input by id. An input left
386
+ * empty is absent.
387
+ * - A slash command with no options, or one string option, gives
388
+ * `CommandInvoked`: `id` is its name and `text` the option's value or
389
+ * the empty string.
390
+ * - Anything else, a message or user command, a slash command with other
391
+ * options, or a click with no message, is `None`.
392
+ * - `raw` is the payload, undecoded. For a command it holds the token that
393
+ * answers it, so it is a credential for 15 minutes.
394
+ *
395
+ * @stability unstable
396
+ * @category normalization
397
+ * @since 4.1.5
398
+ */
399
+ export declare const normalizeInteraction: (interaction: unknown) => Option.Option<ChatInteraction.Interaction>;
400
+ /**
401
+ * The create payload of each command in `group`, in the group's order, for
402
+ * Discord's application commands endpoint.
403
+ *
404
+ * **Details**
405
+ *
406
+ * - Each is a `CHAT_INPUT` command with the definition's name and
407
+ * description, and one `STRING` option when it has an `argument`. Each
408
+ * fits dfx's `ApplicationCommandCreateRequest`.
409
+ * - The definitions in `group` that are not commands are skipped.
410
+ *
411
+ * **Gotchas**
412
+ *
413
+ * - The adapter registers nothing. Discord's bulk overwrite replaces the
414
+ * application's whole list, so the consumer sends these beside its other
415
+ * commands, in one call.
416
+ * - dfx's gateway runner syncs its own commands the same way when it
417
+ * starts, which would delete these. A consumer running both adds these to
418
+ * that list, or switches the sync off with `setInteractionsSync(false)`.
419
+ * - A field the definition lacks, such as `default_member_permissions`, is
420
+ * spread onto the payload by the consumer.
421
+ *
422
+ * **Example** (Registering the commands beside dfx's own)
423
+ *
424
+ * ```ts import.meta.vitest
425
+ * import { DiscordChat } from "@effect/chat-discord"
426
+ * import { DiscordREST } from "dfx/DiscordREST"
427
+ * import { Effect } from "effect"
428
+ * import { ChatInteraction } from "effect/chat"
429
+ *
430
+ * const Commands = ChatInteraction.group(
431
+ * ChatInteraction.command("summarize", { description: "Summarize this thread" })
432
+ * )
433
+ *
434
+ * export const register = Effect.gen(function*() {
435
+ * const rest = yield* DiscordREST
436
+ * const app = yield* rest.getMyApplication()
437
+ * yield* rest.bulkSetApplicationCommands(app.id, [...DiscordChat.commandRequests(Commands)])
438
+ * })
439
+ * ```
440
+ *
441
+ * @stability unstable
442
+ * @category commands
443
+ * @since 4.1.5
444
+ */
445
+ export declare const commandRequests: (group: ChatInteraction.Group<any>) => ReadonlyArray<{
446
+ /** `CHAT_INPUT`. */
447
+ readonly type: 1;
448
+ readonly name: string;
449
+ readonly description: string;
450
+ /** One `STRING` option when the command has an `argument`, absent otherwise. */
451
+ readonly options?: ReadonlyArray<{
452
+ readonly type: 3;
453
+ readonly name: string;
454
+ readonly description: string;
455
+ readonly required: boolean;
456
+ }> | undefined;
457
+ }>;
458
+ /**
459
+ * Discord options for every write inside an effect.
460
+ *
461
+ * @stability unstable
462
+ * @category models
463
+ * @since 4.1.5
464
+ */
465
+ export interface DiscordPostOptions {
466
+ /**
467
+ * Who a write may notify. Default: nobody, and a reply does not ping its
468
+ * author. `everyone` allows `@everyone` and `@here`.
469
+ */
470
+ readonly notify?: {
471
+ readonly users?: ReadonlyArray<string> | undefined;
472
+ readonly roles?: ReadonlyArray<string> | undefined;
473
+ readonly everyone?: boolean | undefined;
474
+ readonly repliedUser?: boolean | undefined;
475
+ } | undefined;
476
+ /**
477
+ * Link embeds. Default true, which sends the `SUPPRESS_EMBEDS` flag on
478
+ * the post and on every edit, so a link a later edit adds gets no embed
479
+ * either. False sends no flag on a post and clears it on an edit.
480
+ */
481
+ readonly suppressEmbeds?: boolean | undefined;
482
+ /**
483
+ * What a post with `replyTo` does when that message is gone. Default
484
+ * true: Discord refuses the post. False sends `fail_if_not_exists: false`,
485
+ * and Discord posts the message without the reference.
486
+ */
487
+ readonly failIfNotExists?: boolean | undefined;
488
+ /** For `startThread`, in minutes. Default: the channel's. */
489
+ readonly autoArchiveDuration?: 60 | 1440 | 4320 | 10080 | undefined;
490
+ }
491
+ /**
492
+ * Scoped Discord options for every write inside `effect`. Nested calls
493
+ * merge, the inner one winning field by field.
494
+ *
495
+ * @stability unstable
496
+ * @category combinators
497
+ * @since 4.1.5
498
+ */
499
+ export declare const withPostOptions: {
500
+ /**
501
+ * Scoped Discord options for every write inside `effect`. Nested calls
502
+ * merge, the inner one winning field by field.
503
+ *
504
+ * @stability unstable
505
+ * @category combinators
506
+ * @since 4.1.5
507
+ */
508
+ (options: DiscordPostOptions): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
509
+ /**
510
+ * Scoped Discord options for every write inside `effect`. Nested calls
511
+ * merge, the inner one winning field by field.
512
+ *
513
+ * @stability unstable
514
+ * @category combinators
515
+ * @since 4.1.5
516
+ */
517
+ <A, E, R>(effect: Effect.Effect<A, E, R>, options: DiscordPostOptions): Effect.Effect<A, E, R>;
518
+ };
519
+ /**
520
+ * Reads one Discord mention token, `<@id>`, `<@!id>`, `<@&id>`,
521
+ * `<#id>`, `@everyone` or `@here`, for `ChatTest.checkMessage`,
522
+ * `ChatTest.checkEvent` and `ChatTest.conformance`.
523
+ *
524
+ * @stability unstable
525
+ * @category testing
526
+ * @since 4.1.5
527
+ */
528
+ export declare const parseMention: ParseMention;
529
+ export {};
530
+ //# sourceMappingURL=DiscordChat.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DiscordChat.d.ts","sourceRoot":"","sources":["../src/DiscordChat.ts"],"names":[],"mappings":"AAwBA,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