@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.
- package/AGENTS.md +434 -0
- package/CLAUDE.md +434 -0
- package/LICENSE +21 -0
- package/README.md +27 -0
- package/ai-docs/README.md +44 -0
- package/ai-docs/package.json +36 -0
- package/ai-docs/src/01_effect/01_basics/01_effect-gen.ts +30 -0
- package/ai-docs/src/01_effect/01_basics/02_effect-fn.ts +52 -0
- package/ai-docs/src/01_effect/01_basics/10_creating-effects.ts +74 -0
- package/ai-docs/src/01_effect/01_basics/index.md +7 -0
- package/ai-docs/src/01_effect/02_schema/10_schema-basics.ts +43 -0
- package/ai-docs/src/01_effect/02_schema/index.md +7 -0
- package/ai-docs/src/01_effect/03_services/01_service.ts +45 -0
- package/ai-docs/src/01_effect/03_services/10_reference.ts +10 -0
- package/ai-docs/src/01_effect/03_services/20_layer-composition.ts +70 -0
- package/ai-docs/src/01_effect/03_services/20_layer-unwrap.ts +66 -0
- package/ai-docs/src/01_effect/03_services/index.md +5 -0
- package/ai-docs/src/01_effect/04_errors/01_error-handling.ts +30 -0
- package/ai-docs/src/01_effect/04_errors/10_catch-tags.ts +24 -0
- package/ai-docs/src/01_effect/04_errors/20_reason-errors.ts +64 -0
- package/ai-docs/src/01_effect/04_errors/index.md +1 -0
- package/ai-docs/src/01_effect/05_resources/10_acquire-release.ts +105 -0
- package/ai-docs/src/01_effect/05_resources/20_layer-side-effects.ts +31 -0
- package/ai-docs/src/01_effect/05_resources/30_layer-map.ts +86 -0
- package/ai-docs/src/01_effect/05_resources/index.md +3 -0
- package/ai-docs/src/01_effect/06_running/10_run-main.ts +30 -0
- package/ai-docs/src/01_effect/06_running/20_layer-launch.ts +27 -0
- package/ai-docs/src/01_effect/06_running/index.md +1 -0
- package/ai-docs/src/01_effect/07_pubsub/10_pubsub.ts +56 -0
- package/ai-docs/src/01_effect/07_pubsub/index.md +3 -0
- package/ai-docs/src/03_stream/10_creating-streams.ts +103 -0
- package/ai-docs/src/03_stream/20_consuming-streams.ts +137 -0
- package/ai-docs/src/03_stream/30_encoding.ts +163 -0
- package/ai-docs/src/03_stream/index.md +4 -0
- package/ai-docs/src/04_integration/10_managed-runtime.ts +129 -0
- package/ai-docs/src/04_integration/index.md +5 -0
- package/ai-docs/src/05_batching/10_request-resolver.ts +89 -0
- package/ai-docs/src/05_batching/index.md +3 -0
- package/ai-docs/src/06_schedule/10_schedules.ts +111 -0
- package/ai-docs/src/06_schedule/index.md +3 -0
- package/ai-docs/src/07_datetime/10_creating-and-formatting.ts +30 -0
- package/ai-docs/src/07_datetime/20_time-zones.ts +44 -0
- package/ai-docs/src/07_datetime/index.md +5 -0
- package/ai-docs/src/08_observability/10_logging.ts +66 -0
- package/ai-docs/src/08_observability/20_otlp-tracing.ts +95 -0
- package/ai-docs/src/08_observability/index.md +7 -0
- package/ai-docs/src/09_testing/10_effect-tests.ts +55 -0
- package/ai-docs/src/09_testing/20_layer-tests.ts +138 -0
- package/ai-docs/src/09_testing/index.md +1 -0
- package/ai-docs/src/10_predicate/01_basics.ts +14 -0
- package/ai-docs/src/10_predicate/index.md +9 -0
- package/ai-docs/src/40_sql/10_basics.ts +175 -0
- package/ai-docs/src/40_sql/index.md +6 -0
- package/ai-docs/src/50_http-client/10_basics.ts +102 -0
- package/ai-docs/src/50_http-client/index.md +3 -0
- package/ai-docs/src/51_http-server/10_basics.ts +118 -0
- package/ai-docs/src/51_http-server/20_testing.ts +100 -0
- package/ai-docs/src/51_http-server/fixtures/api/Api.ts +14 -0
- package/ai-docs/src/51_http-server/fixtures/api/Authorization.ts +36 -0
- package/ai-docs/src/51_http-server/fixtures/api/System.ts +10 -0
- package/ai-docs/src/51_http-server/fixtures/api/Users.ts +107 -0
- package/ai-docs/src/51_http-server/fixtures/domain/User.ts +21 -0
- package/ai-docs/src/51_http-server/fixtures/domain/UserErrors.ts +22 -0
- package/ai-docs/src/51_http-server/fixtures/server/Authorization.ts +37 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users/http.ts +80 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users.ts +183 -0
- package/ai-docs/src/51_http-server/index.md +3 -0
- package/ai-docs/src/52_graphql/10_basics.ts +117 -0
- package/ai-docs/src/52_graphql/20_testing.ts +45 -0
- package/ai-docs/src/52_graphql/30_batching.ts +118 -0
- package/ai-docs/src/52_graphql/40_aliased-batching.ts +124 -0
- package/ai-docs/src/52_graphql/50_errors-and-retries.ts +74 -0
- package/ai-docs/src/52_graphql/index.md +6 -0
- package/ai-docs/src/60_child-process/10_working-with-child-processes.ts +117 -0
- package/ai-docs/src/60_child-process/index.md +3 -0
- package/ai-docs/src/70_cli/10_basics.ts +162 -0
- package/ai-docs/src/70_cli/index.md +5 -0
- package/ai-docs/src/71_ai/10_language-model.ts +156 -0
- package/ai-docs/src/71_ai/20_tools.ts +226 -0
- package/ai-docs/src/71_ai/30_chat.ts +158 -0
- package/ai-docs/src/71_ai/fixtures/domain/LaunchPlan.ts +9 -0
- package/ai-docs/src/71_ai/index.md +5 -0
- package/ai-docs/src/80_cluster/10_entities.ts +97 -0
- package/ai-docs/src/80_cluster/index.md +4 -0
- package/ai-docs/src/index.md +10 -0
- package/ai-docs/tsconfig.json +28 -0
- package/dist/DiscordChat.d.ts +530 -0
- package/dist/DiscordChat.d.ts.map +1 -0
- package/dist/DiscordChat.js +574 -0
- package/dist/DiscordChat.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/adapter.d.ts +2 -0
- package/dist/internal/adapter.d.ts.map +1 -0
- package/dist/internal/adapter.js +492 -0
- package/dist/internal/adapter.js.map +1 -0
- package/dist/internal/errors.d.ts +2 -0
- package/dist/internal/errors.d.ts.map +1 -0
- package/dist/internal/errors.js +236 -0
- package/dist/internal/errors.js.map +1 -0
- package/dist/internal/interactions.d.ts +2 -0
- package/dist/internal/interactions.d.ts.map +1 -0
- package/dist/internal/interactions.js +490 -0
- package/dist/internal/interactions.js.map +1 -0
- package/dist/internal/length.d.ts +2 -0
- package/dist/internal/length.d.ts.map +1 -0
- package/dist/internal/length.js +31 -0
- package/dist/internal/length.js.map +1 -0
- package/dist/internal/mentions.d.ts +2 -0
- package/dist/internal/mentions.d.ts.map +1 -0
- package/dist/internal/mentions.js +93 -0
- package/dist/internal/mentions.js.map +1 -0
- package/dist/internal/normalize.d.ts +2 -0
- package/dist/internal/normalize.d.ts.map +1 -0
- package/dist/internal/normalize.js +322 -0
- package/dist/internal/normalize.js.map +1 -0
- package/dist/internal/options.d.ts +2 -0
- package/dist/internal/options.d.ts.map +1 -0
- package/dist/internal/options.js +25 -0
- package/dist/internal/options.js.map +1 -0
- package/package.json +63 -1
- package/src/DiscordChat.ts +703 -0
- package/src/index.ts +12 -0
- package/src/internal/adapter.ts +653 -0
- package/src/internal/errors.ts +184 -0
- package/src/internal/interactions.ts +451 -0
- package/src/internal/length.ts +33 -0
- package/src/internal/mentions.ts +101 -0
- package/src/internal/normalize.ts +363 -0
- package/src/internal/options.ts +29 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { Mention } from "effect/chat/ChatMessage"
|
|
2
|
+
|
|
3
|
+
type Kind = Mention["kind"]
|
|
4
|
+
|
|
5
|
+
interface Target {
|
|
6
|
+
readonly kind: Kind
|
|
7
|
+
readonly id: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** A mention token as Discord writes it in `content`. */
|
|
11
|
+
const tokenPattern = /<@!?(\d+)>|<@&(\d+)>|<#(\d+)>|@(everyone|here)/y
|
|
12
|
+
|
|
13
|
+
const targetOf = (match: RegExpExecArray): Target => {
|
|
14
|
+
const [, user, role, channel, broadcast] = match
|
|
15
|
+
if (user !== undefined) return { kind: "user", id: user }
|
|
16
|
+
if (role !== undefined) return { kind: "role", id: role }
|
|
17
|
+
if (channel !== undefined) return { kind: "channel", id: channel }
|
|
18
|
+
return { kind: "broadcast", id: broadcast! }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const tokenAt = (text: string, index: number): RegExpExecArray | null => {
|
|
22
|
+
tokenPattern.lastIndex = index
|
|
23
|
+
return tokenPattern.exec(text)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Parses one token, `<@id>`, `<@!id>`, `<@&id>`, `<#id>`, `@everyone`
|
|
28
|
+
* or `@here`, as the whole of `slice`.
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
export const parseToken = (slice: string): Target | undefined => {
|
|
33
|
+
const match = tokenAt(slice, 0)
|
|
34
|
+
return match === null || match[0].length !== slice.length ? undefined : targetOf(match)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A token in `content` outside code, with its offsets.
|
|
39
|
+
*
|
|
40
|
+
* @internal
|
|
41
|
+
*/
|
|
42
|
+
export interface Token extends Target {
|
|
43
|
+
readonly start: number
|
|
44
|
+
readonly end: number
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const isEscapable = (char: string | undefined): boolean => char !== undefined && /[^0-9A-Za-z\s]/.test(char)
|
|
48
|
+
|
|
49
|
+
/** The index past the closing run of exactly `n` backticks, or -1. */
|
|
50
|
+
const closingRun = (text: string, from: number, n: number): number => {
|
|
51
|
+
for (let i = from; i < text.length; i++) {
|
|
52
|
+
if (text[i] !== "`") continue
|
|
53
|
+
let run = 1
|
|
54
|
+
while (text[i + run] === "`") run++
|
|
55
|
+
if (run === n && i > from) return i + run
|
|
56
|
+
i += run - 1
|
|
57
|
+
}
|
|
58
|
+
return -1
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The mention tokens in `content`, in order, skipping code blocks, inline
|
|
63
|
+
* code and tokens escaped with a backslash, as Discord's Markdown does.
|
|
64
|
+
*
|
|
65
|
+
* @internal
|
|
66
|
+
*/
|
|
67
|
+
export const tokens = (text: string): Array<Token> => {
|
|
68
|
+
const found: Array<Token> = []
|
|
69
|
+
let i = 0
|
|
70
|
+
while (i < text.length) {
|
|
71
|
+
const char = text[i]
|
|
72
|
+
if (char === "\\" && isEscapable(text[i + 1])) {
|
|
73
|
+
i += 2
|
|
74
|
+
continue
|
|
75
|
+
}
|
|
76
|
+
if (char === "`") {
|
|
77
|
+
let n = 1
|
|
78
|
+
while (text[i + n] === "`") n++
|
|
79
|
+
if (n >= 3) {
|
|
80
|
+
const end = text.indexOf("```", i + 3)
|
|
81
|
+
if (end !== -1) {
|
|
82
|
+
i = end + 3
|
|
83
|
+
continue
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const end = closingRun(text, i + n, n)
|
|
87
|
+
i = end === -1 ? i + n : end
|
|
88
|
+
continue
|
|
89
|
+
}
|
|
90
|
+
if (char === "<" || char === "@") {
|
|
91
|
+
const match = tokenAt(text, i)
|
|
92
|
+
if (match !== null) {
|
|
93
|
+
found.push({ ...targetOf(match), start: i, end: i + match[0].length })
|
|
94
|
+
i += match[0].length
|
|
95
|
+
continue
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
i++
|
|
99
|
+
}
|
|
100
|
+
return found
|
|
101
|
+
}
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import type { ChatEvent, ReactionAdded, ReactionRemoved, ThreadKind } from "effect/chat/ChatEvent"
|
|
2
|
+
import type { Attachment, ChatMessage, Mention } from "effect/chat/ChatMessage"
|
|
3
|
+
import * as ChatThread from "effect/chat/ChatThread"
|
|
4
|
+
import * as DateTime from "effect/DateTime"
|
|
5
|
+
import * as Option from "effect/Option"
|
|
6
|
+
import * as Predicate from "effect/Predicate"
|
|
7
|
+
import * as Schema from "effect/Schema"
|
|
8
|
+
import * as Mentions from "./mentions.ts"
|
|
9
|
+
|
|
10
|
+
/** @internal */
|
|
11
|
+
export const platform = "discord"
|
|
12
|
+
|
|
13
|
+
/** @internal */
|
|
14
|
+
export const thread = (channelId: string): ChatThread.ChatThread => ChatThread.make(platform, channelId)
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The message types that are a message: `DEFAULT`, `REPLY`,
|
|
18
|
+
* `CHAT_INPUT_COMMAND` and `CONTEXT_MENU_COMMAND`. Inbound and history
|
|
19
|
+
* both read it, and a type Discord adds later stays out.
|
|
20
|
+
*
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export const messageTypes: ReadonlySet<number> = new Set([0, 19, 20, 23])
|
|
24
|
+
|
|
25
|
+
/** @internal */
|
|
26
|
+
export const REPLY = 19
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The thread starter placeholder in a thread started from a message.
|
|
30
|
+
*
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
export const THREAD_STARTER_MESSAGE = 21
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Discord's channel type as a `kind`: 10, 11 and 12 are threads, 1 and 3
|
|
37
|
+
* directs, everything else a channel, unknown types included.
|
|
38
|
+
*
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
export const kindOf = (channelType: number): ThreadKind =>
|
|
42
|
+
channelType === 10 || channelType === 11 || channelType === 12
|
|
43
|
+
? "thread"
|
|
44
|
+
: channelType === 1 || channelType === 3
|
|
45
|
+
? "direct"
|
|
46
|
+
: "channel"
|
|
47
|
+
|
|
48
|
+
/** @internal */
|
|
49
|
+
export const isThreadType = (channelType: number): boolean => kindOf(channelType) === "thread"
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A field Discord may leave out or send as `null`.
|
|
53
|
+
*
|
|
54
|
+
* @internal
|
|
55
|
+
*/
|
|
56
|
+
export const optional = <S extends Schema.Top>(schema: S) => Schema.optionalKey(Schema.NullOr(schema))
|
|
57
|
+
|
|
58
|
+
/** @internal */
|
|
59
|
+
export const UserFields = Schema.Struct({
|
|
60
|
+
id: Schema.String,
|
|
61
|
+
username: optional(Schema.String),
|
|
62
|
+
global_name: optional(Schema.String),
|
|
63
|
+
bot: optional(Schema.Boolean)
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
const MemberFields = Schema.Struct({
|
|
67
|
+
nick: optional(Schema.String),
|
|
68
|
+
roles: optional(Schema.Array(Schema.String))
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
const AttachmentFields = Schema.Struct({
|
|
72
|
+
id: Schema.String,
|
|
73
|
+
filename: optional(Schema.String),
|
|
74
|
+
content_type: optional(Schema.String),
|
|
75
|
+
size: optional(Schema.Int)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
const ReferenceFields = Schema.Struct({
|
|
79
|
+
type: optional(Schema.Number),
|
|
80
|
+
message_id: optional(Schema.String),
|
|
81
|
+
channel_id: optional(Schema.String)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* What the normaliser reads from a message.
|
|
86
|
+
*
|
|
87
|
+
* @internal
|
|
88
|
+
*/
|
|
89
|
+
export const MessageFields = Schema.Struct({
|
|
90
|
+
id: Schema.String,
|
|
91
|
+
channel_id: Schema.String,
|
|
92
|
+
type: Schema.Number,
|
|
93
|
+
content: optional(Schema.String),
|
|
94
|
+
timestamp: Schema.DateTimeUtcFromString,
|
|
95
|
+
author: UserFields,
|
|
96
|
+
webhook_id: optional(Schema.String),
|
|
97
|
+
guild_id: optional(Schema.String),
|
|
98
|
+
channel_type: optional(Schema.Number),
|
|
99
|
+
member: optional(MemberFields),
|
|
100
|
+
mentions: optional(Schema.Array(Schema.Struct({ id: Schema.String }))),
|
|
101
|
+
mention_roles: optional(Schema.Array(Schema.String)),
|
|
102
|
+
mention_everyone: optional(Schema.Boolean),
|
|
103
|
+
attachments: optional(Schema.Array(AttachmentFields)),
|
|
104
|
+
message_reference: optional(ReferenceFields),
|
|
105
|
+
referenced_message: optional(Schema.Struct({ author: optional(Schema.Struct({ id: Schema.String })) }))
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
/** @internal */
|
|
109
|
+
export type MessageFields = typeof MessageFields.Type
|
|
110
|
+
|
|
111
|
+
const ReactionFields = Schema.Struct({
|
|
112
|
+
user_id: Schema.String,
|
|
113
|
+
channel_id: Schema.String,
|
|
114
|
+
message_id: Schema.String,
|
|
115
|
+
guild_id: optional(Schema.String),
|
|
116
|
+
message_author_id: optional(Schema.String),
|
|
117
|
+
member: optional(MemberFields),
|
|
118
|
+
emoji: Schema.Struct({ id: optional(Schema.String), name: optional(Schema.String) })
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
const decodeMessage = Schema.decodeUnknownOption(MessageFields)
|
|
122
|
+
const decodeReaction = Schema.decodeUnknownOption(ReactionFields)
|
|
123
|
+
|
|
124
|
+
/** @internal */
|
|
125
|
+
export const decodeMessageFields = (raw: unknown): MessageFields | undefined =>
|
|
126
|
+
Option.getOrUndefined(decodeMessage(raw))
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* An attachment's id: the message's id and the attachment's, since a download reads the message again.
|
|
130
|
+
*
|
|
131
|
+
* @internal
|
|
132
|
+
*/
|
|
133
|
+
export const attachmentId = (messageId: string, id: string): string => `${messageId}/${id}`
|
|
134
|
+
|
|
135
|
+
/** @internal */
|
|
136
|
+
export const parseAttachmentId = (
|
|
137
|
+
id: string
|
|
138
|
+
): { readonly messageId: string; readonly attachmentId: string } | undefined => {
|
|
139
|
+
const parts = id.split("/")
|
|
140
|
+
return parts.length === 2 && parts[0] !== "" && parts[1] !== ""
|
|
141
|
+
? { messageId: parts[0], attachmentId: parts[1] }
|
|
142
|
+
: undefined
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The mentions in `content`: a token outside code that Discord says
|
|
147
|
+
* resolved, through `mentions` for a user, `mention_roles` for a role and
|
|
148
|
+
* `mention_everyone` for `@everyone` and `@here`. A channel token has no
|
|
149
|
+
* confirming field, so the token alone counts.
|
|
150
|
+
*/
|
|
151
|
+
const mentionsOf = (fields: MessageFields, text: string, botUserId: string): Array<Mention> => {
|
|
152
|
+
const users = new Set((fields.mentions ?? []).map((user) => user.id))
|
|
153
|
+
const roles = new Set(fields.mention_roles ?? [])
|
|
154
|
+
const everyone = fields.mention_everyone === true
|
|
155
|
+
const found: Array<Mention> = []
|
|
156
|
+
for (const token of Mentions.tokens(text)) {
|
|
157
|
+
const resolved = token.kind === "user"
|
|
158
|
+
? users.has(token.id)
|
|
159
|
+
: token.kind === "role"
|
|
160
|
+
? roles.has(token.id)
|
|
161
|
+
: token.kind === "broadcast"
|
|
162
|
+
? everyone
|
|
163
|
+
: true
|
|
164
|
+
if (!resolved) continue
|
|
165
|
+
found.push({
|
|
166
|
+
kind: token.kind,
|
|
167
|
+
id: token.id,
|
|
168
|
+
start: token.start,
|
|
169
|
+
end: token.end,
|
|
170
|
+
isSelf: token.kind === "user" && token.id === botUserId
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
return found
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** @internal */
|
|
177
|
+
export const nonEmpty = (value: string | null | undefined): string | undefined =>
|
|
178
|
+
value === null || value === undefined || value === "" ? undefined : value
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* A message payload, from the gateway or REST, as a `ChatMessage`.
|
|
182
|
+
* `undefined` for a type that is not a message.
|
|
183
|
+
*
|
|
184
|
+
* @internal
|
|
185
|
+
*/
|
|
186
|
+
export const message = (
|
|
187
|
+
raw: Schema.Json,
|
|
188
|
+
fields: MessageFields,
|
|
189
|
+
context: { readonly botUserId: string }
|
|
190
|
+
): ChatMessage | undefined => {
|
|
191
|
+
if (!messageTypes.has(fields.type)) return undefined
|
|
192
|
+
const text = fields.content ?? ""
|
|
193
|
+
const webhook = nonEmpty(fields.webhook_id)
|
|
194
|
+
const name = webhook === undefined
|
|
195
|
+
? nonEmpty(fields.member?.nick) ?? nonEmpty(fields.author.global_name) ?? nonEmpty(fields.author.username)
|
|
196
|
+
: nonEmpty(fields.author.username)
|
|
197
|
+
const reference = fields.message_reference
|
|
198
|
+
const replyId = fields.type === REPLY && (reference?.type ?? 0) === 0 ? nonEmpty(reference?.message_id) : undefined
|
|
199
|
+
const replyAuthor = fields.referenced_message?.author?.id
|
|
200
|
+
const attachments = (fields.attachments ?? []).map((attachment): Attachment => {
|
|
201
|
+
const name = nonEmpty(attachment.filename)
|
|
202
|
+
const contentType = nonEmpty(attachment.content_type)
|
|
203
|
+
return {
|
|
204
|
+
id: attachmentId(fields.id, attachment.id),
|
|
205
|
+
...(name === undefined ? {} : { name }),
|
|
206
|
+
...(contentType === undefined ? {} : { contentType }),
|
|
207
|
+
...(typeof attachment.size === "number" ? { size: attachment.size } : {})
|
|
208
|
+
}
|
|
209
|
+
})
|
|
210
|
+
return {
|
|
211
|
+
id: fields.id,
|
|
212
|
+
thread: thread(fields.channel_id),
|
|
213
|
+
author: {
|
|
214
|
+
id: webhook ?? fields.author.id,
|
|
215
|
+
...(name === undefined ? {} : { name }),
|
|
216
|
+
isBot: webhook !== undefined || fields.author.bot === true,
|
|
217
|
+
isSelf: webhook === undefined && fields.author.id === context.botUserId
|
|
218
|
+
},
|
|
219
|
+
text,
|
|
220
|
+
mentions: mentionsOf(fields, text, context.botUserId),
|
|
221
|
+
...(replyId === undefined
|
|
222
|
+
? {}
|
|
223
|
+
: {
|
|
224
|
+
replyTo: {
|
|
225
|
+
messageId: replyId,
|
|
226
|
+
...(replyAuthor === undefined ? {} : { authorId: replyAuthor, isSelf: replyAuthor === context.botUserId })
|
|
227
|
+
}
|
|
228
|
+
}),
|
|
229
|
+
attachments,
|
|
230
|
+
timestamp: fields.timestamp,
|
|
231
|
+
raw
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* A reaction's emoji: the Unicode emoji, or `custom:{name}` for a guild's own.
|
|
237
|
+
*
|
|
238
|
+
* @internal
|
|
239
|
+
*/
|
|
240
|
+
export const emojiOf = (
|
|
241
|
+
emoji: { readonly id?: string | null | undefined; readonly name?: string | null | undefined }
|
|
242
|
+
) =>
|
|
243
|
+
nonEmpty(emoji.id) === undefined
|
|
244
|
+
? nonEmpty(emoji.name)
|
|
245
|
+
: `custom:${nonEmpty(emoji.name) ?? emoji.id}`
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* The result of `normalize`: the events, or the channel whose type is missing.
|
|
249
|
+
*
|
|
250
|
+
* @internal
|
|
251
|
+
*/
|
|
252
|
+
export type Normalized =
|
|
253
|
+
| { readonly _tag: "Events"; readonly events: ReadonlyArray<ChatEvent> }
|
|
254
|
+
| { readonly _tag: "MissingChannelType"; readonly channelId: string }
|
|
255
|
+
|
|
256
|
+
const none: Normalized = { _tag: "Events", events: [] }
|
|
257
|
+
|
|
258
|
+
const events = (events: ReadonlyArray<ChatEvent>): Normalized => ({ _tag: "Events", events })
|
|
259
|
+
|
|
260
|
+
/** @internal */
|
|
261
|
+
export const normalize = (
|
|
262
|
+
dispatch: { readonly t: string; readonly d: unknown },
|
|
263
|
+
context: { readonly botUserId: string; readonly channelType?: number | undefined }
|
|
264
|
+
): Normalized => {
|
|
265
|
+
const raw = dispatch.d as Schema.Json
|
|
266
|
+
switch (dispatch.t) {
|
|
267
|
+
case "MESSAGE_CREATE": {
|
|
268
|
+
const fields = decodeMessageFields(raw)
|
|
269
|
+
if (fields === undefined || !messageTypes.has(fields.type)) return none
|
|
270
|
+
const channelType = fields.channel_type ?? context.channelType
|
|
271
|
+
if (channelType === undefined) return { _tag: "MissingChannelType", channelId: fields.channel_id }
|
|
272
|
+
const created = message(raw, fields, context)
|
|
273
|
+
return created === undefined ? none : events([{
|
|
274
|
+
_tag: "MessageCreated",
|
|
275
|
+
thread: created.thread,
|
|
276
|
+
kind: kindOf(channelType),
|
|
277
|
+
message: created,
|
|
278
|
+
raw
|
|
279
|
+
}])
|
|
280
|
+
}
|
|
281
|
+
case "MESSAGE_REACTION_ADD":
|
|
282
|
+
case "MESSAGE_REACTION_REMOVE": {
|
|
283
|
+
const fields = Option.getOrUndefined(decodeReaction(raw))
|
|
284
|
+
const emoji = fields === undefined ? undefined : emojiOf(fields.emoji)
|
|
285
|
+
if (fields === undefined || emoji === undefined) return none
|
|
286
|
+
const authorId = nonEmpty(fields.message_author_id)
|
|
287
|
+
const reaction = {
|
|
288
|
+
thread: thread(fields.channel_id),
|
|
289
|
+
messageId: fields.message_id,
|
|
290
|
+
...(authorId === undefined ? {} : { messageAuthorId: authorId }),
|
|
291
|
+
userId: fields.user_id,
|
|
292
|
+
emoji,
|
|
293
|
+
raw
|
|
294
|
+
}
|
|
295
|
+
return events([
|
|
296
|
+
dispatch.t === "MESSAGE_REACTION_ADD"
|
|
297
|
+
? { _tag: "ReactionAdded", ...reaction } satisfies ReactionAdded
|
|
298
|
+
: { _tag: "ReactionRemoved", ...reaction } satisfies ReactionRemoved
|
|
299
|
+
])
|
|
300
|
+
}
|
|
301
|
+
default:
|
|
302
|
+
return none
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** @internal */
|
|
307
|
+
export const guildIdOf = (raw: unknown): Option.Option<string> =>
|
|
308
|
+
Predicate.isObject(raw) && typeof raw.guild_id === "string" && raw.guild_id !== ""
|
|
309
|
+
? Option.some(raw.guild_id)
|
|
310
|
+
: Option.none()
|
|
311
|
+
|
|
312
|
+
/** @internal */
|
|
313
|
+
export const memberOf = (raw: unknown): Option.Option<{
|
|
314
|
+
readonly guildId: string
|
|
315
|
+
readonly roles: ReadonlyArray<string>
|
|
316
|
+
readonly nick?: string | undefined
|
|
317
|
+
}> =>
|
|
318
|
+
Option.flatMap(guildIdOf(raw), (guildId) => {
|
|
319
|
+
const member = (raw as { readonly member?: unknown }).member
|
|
320
|
+
if (!Predicate.isObject(member)) return Option.none()
|
|
321
|
+
const roles = Array.isArray(member.roles) ? member.roles.filter((role) => typeof role === "string") : []
|
|
322
|
+
const nick = typeof member.nick === "string" && member.nick !== "" ? member.nick : undefined
|
|
323
|
+
return Option.some({ guildId, roles, ...(nick === undefined ? {} : { nick }) })
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
/** The start of Discord's snowflake clock, in Unix milliseconds. */
|
|
327
|
+
const discordEpoch = 1_420_070_400_000
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* When a snowflake was made, in Unix milliseconds. Throws for an id that is
|
|
331
|
+
* not a number.
|
|
332
|
+
*
|
|
333
|
+
* @internal
|
|
334
|
+
*/
|
|
335
|
+
export const snowflakeMillis = (id: string): number => Number(BigInt(id) >> BigInt(22)) + discordEpoch
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* When a snowflake was made.
|
|
339
|
+
*
|
|
340
|
+
* @internal
|
|
341
|
+
*/
|
|
342
|
+
export const snowflakeTime = (id: string): DateTime.Utc | undefined => {
|
|
343
|
+
try {
|
|
344
|
+
return DateTime.makeUnsafe(snowflakeMillis(id))
|
|
345
|
+
} catch {
|
|
346
|
+
return undefined
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Orders two snowflakes.
|
|
352
|
+
*
|
|
353
|
+
* @internal
|
|
354
|
+
*/
|
|
355
|
+
export const compareIds = (a: string, b: string): number => {
|
|
356
|
+
try {
|
|
357
|
+
const x = BigInt(a)
|
|
358
|
+
const y = BigInt(b)
|
|
359
|
+
return x < y ? -1 : x > y ? 1 : 0
|
|
360
|
+
} catch {
|
|
361
|
+
return a.length !== b.length ? a.length - b.length : a < b ? -1 : a > b ? 1 : 0
|
|
362
|
+
}
|
|
363
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as Context from "effect/Context"
|
|
2
|
+
import type { DiscordPostOptions } from "../DiscordChat.ts"
|
|
3
|
+
|
|
4
|
+
/** @internal */
|
|
5
|
+
export const CurrentPostOptions = Context.Reference<DiscordPostOptions>("@effect/chat-discord/CurrentPostOptions", {
|
|
6
|
+
defaultValue: () => ({})
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
/** Discord's `SUPPRESS_EMBEDS` message flag. */
|
|
10
|
+
const SUPPRESS_EMBEDS = 1 << 2
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The `allowed_mentions` for a write: nobody, unless `notify` says otherwise.
|
|
14
|
+
*
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export const allowedMentions = (notify: DiscordPostOptions["notify"]) => ({
|
|
18
|
+
parse: notify?.everyone === true ? ["everyone" as const] : [],
|
|
19
|
+
users: notify?.users ?? [],
|
|
20
|
+
roles: notify?.roles ?? [],
|
|
21
|
+
replied_user: notify?.repliedUser === true
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The flags for a write.
|
|
26
|
+
*
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
export const flags = (options: DiscordPostOptions): number => options.suppressEmbeds === false ? 0 : SUPPRESS_EMBEDS
|