@statewavedev/connectors-discord 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # @statewavedev/connectors-discord
2
+
3
+ Discord connector for Statewave — turns server channel + thread activity into normalized episodes under `community:<guild_id>`.
4
+
5
+ > Part of the [Statewave Connectors](https://github.com/smaramwbc/statewave-connectors) ecosystem.
6
+
7
+ ## What it ingests
8
+
9
+ | Source event | Episode `kind` |
10
+ |---|---|
11
+ | Top-level channel message | `discord.message.posted` |
12
+ | Reply inside a thread | `discord.thread.replied` |
13
+
14
+ `v0.1` is pull-mode only — it walks `GET /channels/{id}/messages` for each channel you list, paging back via Discord's snowflake-based `before=<id>` cursor. Real-time ingestion via Discord's Gateway WebSocket (the equivalent of Slack's Socket Mode) is on the roadmap.
15
+
16
+ ## Quickstart
17
+
18
+ ```bash
19
+ export DISCORD_BOT_TOKEN=...
20
+ statewave-connectors sync discord \
21
+ --guild 1100000000000000000 \
22
+ --channels general,help \
23
+ --since 2026-01-01 \
24
+ --dry-run
25
+ ```
26
+
27
+ `--guild` takes a server snowflake id (enable **Developer Mode** in Discord → right-click the server icon → **Copy Server ID**). `--channels` accepts ids (snowflake) or names (`general` / `#general`); the bot must already be in the guild for the lookup to work.
28
+
29
+ ## Bot setup
30
+
31
+ 1. Create a Discord application + bot at <https://discord.com/developers/applications>.
32
+ 2. Under **Bot → Privileged Gateway Intents**, enable **Message Content Intent** if you want to read message text (required for ingestion).
33
+ 3. Generate an invite URL via **OAuth2 → URL Generator** with the `bot` scope and these permissions:
34
+ - `View Channels`
35
+ - `Read Message History`
36
+ - `Read Public Threads` (and `Read Private Threads` for private ones, if applicable)
37
+ 4. Open the invite URL and add the bot to your server.
38
+ 5. Copy the **Bot Token** from the Bot tab and export `DISCORD_BOT_TOKEN`.
39
+
40
+ ## Options
41
+
42
+ ```
43
+ --guild ID guild (server) snowflake id (required)
44
+ --channels LIST comma-separated ids (snowflake) or names (required)
45
+ --subject SUBJECT override the default `community:<guild_id>` subject
46
+ --since YYYY-MM-DD earliest message to consider
47
+ --max-items N cap mapped episodes (the client honors this during paging)
48
+ --include LIST allow-list: messages (default)
49
+ --dry-run preview mapped episodes without ingesting (recommended for new use)
50
+ ```
51
+
52
+ ## Auth
53
+
54
+ Bot token only (`DISCORD_BOT_TOKEN`). User tokens are explicitly disallowed by Discord's TOS and the connector won't accept them. The token is sent only as the `Authorization: Bot …` header to `discord.com/api/v10/*` and never anywhere else.
55
+
56
+ ## Status
57
+
58
+ `v0.1.0` — pull-mode ingestion. See [RELEASE_NOTES.md](https://github.com/smaramwbc/statewave-connectors/blob/main/RELEASE_NOTES.md).
59
+
60
+ Out of scope for v0.1 (planned):
61
+
62
+ - Realtime ingestion via the Gateway WebSocket protocol
63
+ - Forum channels (different structure: posts with implicit threads)
64
+ - Reactions and pinned messages as signal episodes
65
+ - DM ingestion (out of scope; Discord's privacy posture is different from a workspace tool)
@@ -0,0 +1,42 @@
1
+ import type { DiscordChannel, DiscordGuild, DiscordMessage, DiscordUser } from "./types.js";
2
+ export interface DiscordClientOptions {
3
+ /** Bot token. Required — there is no useful unauthenticated mode. */
4
+ token: string;
5
+ baseUrl?: string;
6
+ fetchImpl?: typeof fetch;
7
+ userAgent?: string;
8
+ }
9
+ export declare class DiscordClient {
10
+ private readonly token;
11
+ private readonly baseUrl;
12
+ private readonly fetchImpl;
13
+ private readonly userAgent;
14
+ constructor(options: DiscordClientOptions);
15
+ /** Verify the token + return the bot's identity. */
16
+ authMe(): Promise<DiscordUser>;
17
+ /** Resolve a guild's display name. Used to build a friendlier subject. */
18
+ getGuild(guildId: string): Promise<DiscordGuild>;
19
+ /**
20
+ * Resolve channel selectors against a guild's channel list. Selectors
21
+ * may be IDs (snowflake) or names (`general` or `#general`). The bot
22
+ * must already be in the guild for this to work — Discord's API doesn't
23
+ * expose channels of guilds the bot can't see.
24
+ */
25
+ resolveChannels(guildId: string, selectors: ReadonlyArray<string>): Promise<ReadonlyArray<DiscordChannel>>;
26
+ /**
27
+ * Page through `GET /channels/{id}/messages` using snowflake-based
28
+ * pagination (`before=<id>`). Discord returns messages newest-first;
29
+ * we reverse the accumulated array so callers see chronological order.
30
+ *
31
+ * `since` filters by the message's ISO timestamp (Discord doesn't have a
32
+ * native "since" param — the conventional approach is to convert your
33
+ * cutoff to a snowflake via the unix-millis encoding, but that adds
34
+ * complexity for marginal benefit at v0.1 volumes).
35
+ */
36
+ listChannelMessages(channel: DiscordChannel, guild: DiscordGuild, options?: {
37
+ since?: string;
38
+ maxItems?: number;
39
+ }): Promise<ReadonlyArray<DiscordMessage>>;
40
+ private callJson;
41
+ }
42
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,cAAc,EAAE,YAAY,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAK5F,MAAM,WAAW,oBAAoB;IACnC,qEAAqE;IACrE,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,OAAO,KAAK,CAAC;IACzB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAmCD,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAC/B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAe;IACzC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;gBAEvB,OAAO,EAAE,oBAAoB;IAsBzC,oDAAoD;IAC9C,MAAM,IAAI,OAAO,CAAC,WAAW,CAAC;IAWpC,0EAA0E;IACpE,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAKtD;;;;;OAKG;IACG,eAAe,CACnB,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,aAAa,CAAC,MAAM,CAAC,GAC/B,OAAO,CAAC,aAAa,CAAC,cAAc,CAAC,CAAC;IAyCzC;;;;;;;;;OASG;IACG,mBAAmB,CACvB,OAAO,EAAE,cAAc,EACvB,KAAK,EAAE,YAAY,EACnB,OAAO,GAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAO,GAClD,OAAO,CAAC,aAAa,CAAC,cAAc,CAAC,CAAC;YA4C3B,QAAQ;CAyCvB"}
package/dist/client.js ADDED
@@ -0,0 +1,219 @@
1
+ // Minimal Discord REST API client for the v0.1 pull-mode connector.
2
+ // We hit four endpoints — `GET /users/@me` (auth probe), `GET /guilds/{id}`
3
+ // (guild metadata), `GET /guilds/{id}/channels` (channel resolution), and
4
+ // `GET /channels/{id}/messages` (message pagination by snowflake). The
5
+ // connector does NOT use the realtime Gateway protocol; that's a separate
6
+ // daemon-shape effort similar to Slack's Socket Mode.
7
+ import { ConnectorError } from "@statewavedev/connectors-core";
8
+ const DISCORD_API_BASE = "https://discord.com/api/v10";
9
+ const DEFAULT_PAGE_LIMIT = 100;
10
+ export class DiscordClient {
11
+ token;
12
+ baseUrl;
13
+ fetchImpl;
14
+ userAgent;
15
+ constructor(options) {
16
+ if (!options.token) {
17
+ throw new ConnectorError("DISCORD_BOT_TOKEN is required for the discord connector", {
18
+ code: "auth_missing",
19
+ connector: "discord",
20
+ hint: "create a Discord bot at https://discord.com/developers/applications, copy the Bot token, then export DISCORD_BOT_TOKEN=…",
21
+ });
22
+ }
23
+ this.token = options.token;
24
+ this.baseUrl = options.baseUrl ?? DISCORD_API_BASE;
25
+ this.fetchImpl = options.fetchImpl ?? globalThis.fetch;
26
+ this.userAgent =
27
+ options.userAgent ??
28
+ "DiscordBot (https://github.com/smaramwbc/statewave-connectors, 0.1.0)";
29
+ if (!this.fetchImpl) {
30
+ throw new ConnectorError("global fetch is unavailable; pass options.fetchImpl", {
31
+ code: "config_invalid",
32
+ connector: "discord",
33
+ });
34
+ }
35
+ }
36
+ /** Verify the token + return the bot's identity. */
37
+ async authMe() {
38
+ const r = await this.callJson(`/users/@me`);
39
+ if (!r.id) {
40
+ throw new ConnectorError("discord /users/@me returned no id", {
41
+ code: "auth_failed",
42
+ connector: "discord",
43
+ });
44
+ }
45
+ return { id: r.id, username: r.username };
46
+ }
47
+ /** Resolve a guild's display name. Used to build a friendlier subject. */
48
+ async getGuild(guildId) {
49
+ const r = await this.callJson(`/guilds/${encodeURIComponent(guildId)}`);
50
+ return { id: r.id, name: r.name };
51
+ }
52
+ /**
53
+ * Resolve channel selectors against a guild's channel list. Selectors
54
+ * may be IDs (snowflake) or names (`general` or `#general`). The bot
55
+ * must already be in the guild for this to work — Discord's API doesn't
56
+ * expose channels of guilds the bot can't see.
57
+ */
58
+ async resolveChannels(guildId, selectors) {
59
+ if (selectors.length === 0)
60
+ return [];
61
+ const channels = await this.callJson(`/guilds/${encodeURIComponent(guildId)}/channels`);
62
+ const byId = new Map(channels.map((c) => [c.id, c]));
63
+ const byName = new Map();
64
+ for (const c of channels) {
65
+ if (c.name)
66
+ byName.set(c.name, c);
67
+ }
68
+ const out = [];
69
+ const missing = [];
70
+ for (const sel of selectors) {
71
+ const cleaned = sel.replace(/^#/, "");
72
+ const direct = byId.get(cleaned);
73
+ if (direct) {
74
+ out.push({ id: direct.id, name: direct.name, type: direct.type, parent_id: direct.parent_id });
75
+ continue;
76
+ }
77
+ const named = byName.get(cleaned);
78
+ if (named) {
79
+ out.push({ id: named.id, name: named.name, type: named.type, parent_id: named.parent_id });
80
+ continue;
81
+ }
82
+ missing.push(sel);
83
+ }
84
+ if (missing.length > 0) {
85
+ throw new ConnectorError(`discord: channels not found in guild ${guildId}: ${missing.join(", ")}`, {
86
+ code: "not_found",
87
+ connector: "discord",
88
+ hint: "the bot must be invited to the guild AND have View Channel permission on the channels you want to ingest",
89
+ });
90
+ }
91
+ return out;
92
+ }
93
+ /**
94
+ * Page through `GET /channels/{id}/messages` using snowflake-based
95
+ * pagination (`before=<id>`). Discord returns messages newest-first;
96
+ * we reverse the accumulated array so callers see chronological order.
97
+ *
98
+ * `since` filters by the message's ISO timestamp (Discord doesn't have a
99
+ * native "since" param — the conventional approach is to convert your
100
+ * cutoff to a snowflake via the unix-millis encoding, but that adds
101
+ * complexity for marginal benefit at v0.1 volumes).
102
+ */
103
+ async listChannelMessages(channel, guild, options = {}) {
104
+ const sinceMs = options.since ? new Date(options.since).getTime() : undefined;
105
+ const cap = options.maxItems ?? Number.POSITIVE_INFINITY;
106
+ const out = [];
107
+ let before;
108
+ while (out.length < cap) {
109
+ const path = before
110
+ ? `/channels/${encodeURIComponent(channel.id)}/messages?limit=${DEFAULT_PAGE_LIMIT}&before=${encodeURIComponent(before)}`
111
+ : `/channels/${encodeURIComponent(channel.id)}/messages?limit=${DEFAULT_PAGE_LIMIT}`;
112
+ const page = await this.callJson(path);
113
+ if (page.length === 0)
114
+ break;
115
+ let stop = false;
116
+ for (const m of page) {
117
+ if (sinceMs !== undefined) {
118
+ const tsMs = new Date(m.timestamp).getTime();
119
+ if (Number.isFinite(tsMs) && tsMs < sinceMs) {
120
+ stop = true;
121
+ break;
122
+ }
123
+ }
124
+ const adopted = adoptMessage(m, channel, guild);
125
+ if (adopted)
126
+ out.push(adopted);
127
+ if (out.length >= cap) {
128
+ stop = true;
129
+ break;
130
+ }
131
+ }
132
+ if (stop)
133
+ break;
134
+ // Page back further. Discord returns newest-first, so the oldest id
135
+ // in this page is the cursor for the next call.
136
+ const last = page[page.length - 1];
137
+ if (!last)
138
+ break;
139
+ before = last.id;
140
+ if (page.length < DEFAULT_PAGE_LIMIT)
141
+ break;
142
+ }
143
+ return out.reverse();
144
+ }
145
+ // -- internals -----------------------------------------------------------
146
+ async callJson(path) {
147
+ const url = `${this.baseUrl}${path}`;
148
+ const res = await this.fetchImpl(url, {
149
+ method: "GET",
150
+ headers: {
151
+ Authorization: `Bot ${this.token}`,
152
+ Accept: "application/json",
153
+ "User-Agent": this.userAgent,
154
+ },
155
+ });
156
+ if (res.status === 401) {
157
+ throw new ConnectorError(`discord ${path} returned 401`, {
158
+ code: "auth_failed",
159
+ connector: "discord",
160
+ hint: "verify DISCORD_BOT_TOKEN is valid and matches the bot in your application",
161
+ });
162
+ }
163
+ if (res.status === 403) {
164
+ throw new ConnectorError(`discord ${path} returned 403`, {
165
+ code: "permission_denied",
166
+ connector: "discord",
167
+ hint: "the bot needs 'View Channel' + 'Read Message History' permissions on the target channels; for thread support also 'Read Threads'",
168
+ });
169
+ }
170
+ if (res.status === 429) {
171
+ throw new ConnectorError(`discord ${path} rate-limited (HTTP 429)`, {
172
+ code: "rate_limited",
173
+ connector: "discord",
174
+ hint: "Discord's per-route limits are tight; back off and retry",
175
+ });
176
+ }
177
+ if (!res.ok) {
178
+ throw new ConnectorError(`discord ${path} returned HTTP ${res.status}`, {
179
+ code: "network",
180
+ connector: "discord",
181
+ });
182
+ }
183
+ return (await res.json());
184
+ }
185
+ }
186
+ /**
187
+ * Translate the slice of a raw Discord message we care about into our typed
188
+ * shape. Returns `null` for messages we deliberately skip — empty content
189
+ * (embed-only messages), system messages (channel pins, member joins, …),
190
+ * and bot-author messages where we don't have meaningful text.
191
+ */
192
+ function adoptMessage(m, channel, guild) {
193
+ // Skip system messages: types 1-12, 14-21 are member joins, pins,
194
+ // boost notifications, etc. We only ingest user-authored chat (0) and
195
+ // replies (19) for v0.1.
196
+ const t = m.type ?? 0;
197
+ if (t !== 0 && t !== 19)
198
+ return null;
199
+ if (!m.content || m.content.trim() === "")
200
+ return null;
201
+ if (!m.author)
202
+ return null;
203
+ return {
204
+ type: "message",
205
+ id: m.id,
206
+ channel,
207
+ guild,
208
+ author: {
209
+ id: m.author.id,
210
+ username: m.author.username,
211
+ global_name: m.author.global_name,
212
+ },
213
+ content: m.content,
214
+ timestamp: m.timestamp,
215
+ edited_timestamp: m.edited_timestamp ?? null,
216
+ message_type: t,
217
+ };
218
+ }
219
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,oEAAoE;AACpE,4EAA4E;AAC5E,0EAA0E;AAC1E,uEAAuE;AACvE,0EAA0E;AAC1E,sDAAsD;AAEtD,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAG/D,MAAM,gBAAgB,GAAG,6BAA6B,CAAC;AACvD,MAAM,kBAAkB,GAAG,GAAG,CAAC;AA2C/B,MAAM,OAAO,aAAa;IACP,KAAK,CAAS;IACd,OAAO,CAAS;IAChB,SAAS,CAAe;IACxB,SAAS,CAAS;IAEnC,YAAY,OAA6B;QACvC,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;YACnB,MAAM,IAAI,cAAc,CAAC,yDAAyD,EAAE;gBAClF,IAAI,EAAE,cAAc;gBACpB,SAAS,EAAE,SAAS;gBACpB,IAAI,EAAE,0HAA0H;aACjI,CAAC,CAAC;QACL,CAAC;QACD,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;QAC3B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,gBAAgB,CAAC;QACnD,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,UAAU,CAAC,KAAK,CAAC;QACvD,IAAI,CAAC,SAAS;YACZ,OAAO,CAAC,SAAS;gBACjB,uEAAuE,CAAC;QAC1E,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,MAAM,IAAI,cAAc,CAAC,qDAAqD,EAAE;gBAC9E,IAAI,EAAE,gBAAgB;gBACtB,SAAS,EAAE,SAAS;aACrB,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED,oDAAoD;IACpD,KA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;YAC3B,WAAW,EAAE,CAAC,CAAC,MAAM,CAAC,WAAW;SAClC;QACD,OAAO,EAAE,CAAC,CAAC,OAAO;QAClB,SAAS,EAAE,CAAC,CAAC,SAAS;QACtB,gBAAgB,EAAE,CAAC,CAAC,gBAAgB,IAAI,IAAI;QAC5C,YAAY,EAAE,CAAC;KAChB,CAAC;AACJ,CAAC"}
@@ -0,0 +1,7 @@
1
+ export { createDiscordConnector } from "./sync.js";
2
+ export type { DiscordConnectorConfig } from "./sync.js";
3
+ export { DiscordClient } from "./client.js";
4
+ export type { DiscordClientOptions } from "./client.js";
5
+ export { defaultSubject, mapDiscordEvent } from "./mapper.js";
6
+ export type { DiscordChannel, DiscordEvent, DiscordEventKind, DiscordGuild, DiscordMessage, DiscordUser, } from "./types.js";
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;AACnD,YAAY,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,YAAY,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9D,YAAY,EACV,cAAc,EACd,YAAY,EACZ,gBAAgB,EAChB,YAAY,EACZ,cAAc,EACd,WAAW,GACZ,MAAM,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { createDiscordConnector } from "./sync.js";
2
+ export { DiscordClient } from "./client.js";
3
+ export { defaultSubject, mapDiscordEvent } from "./mapper.js";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;AAEnD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,9 @@
1
+ import { type StatewaveEpisode } from "@statewavedev/connectors-core";
2
+ import type { DiscordGuild, DiscordMessage } from "./types.js";
3
+ export interface MapperOptions {
4
+ /** Override for the default `community:<guild_id>` subject. */
5
+ subject?: string;
6
+ }
7
+ export declare function defaultSubject(guild: DiscordGuild): string;
8
+ export declare function mapDiscordEvent(message: DiscordMessage, options?: MapperOptions): StatewaveEpisode;
9
+ //# sourceMappingURL=mapper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mapper.d.ts","sourceRoot":"","sources":["../src/mapper.ts"],"names":[],"mappings":"AAIA,OAAO,EAAkB,KAAK,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AACtF,OAAO,KAAK,EAEV,YAAY,EACZ,cAAc,EACf,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,aAAa;IAC5B,+DAA+D;IAC/D,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,YAAY,GAAG,MAAM,CAK1D;AAED,wBAAgB,eAAe,CAC7B,OAAO,EAAE,cAAc,EACvB,OAAO,GAAE,aAAkB,GAC1B,gBAAgB,CAkDlB"}
package/dist/mapper.js ADDED
@@ -0,0 +1,83 @@
1
+ // Discord-message → Statewave-episode mapping. Side-effect-free; the
2
+ // connector resolves channels and authors before calling this so the
3
+ // mapper itself is a pure transformation.
4
+ import { EpisodeBuilder } from "@statewavedev/connectors-core";
5
+ export function defaultSubject(guild) {
6
+ // Guild IDs (snowflakes) are stable — using them keeps subject names
7
+ // safe to reuse across guild renames. Operators who want a friendlier
8
+ // subject can pass `--subject community:<name>` instead.
9
+ return `community:${guild.id}`;
10
+ }
11
+ export function mapDiscordEvent(message, options = {}) {
12
+ const subject = options.subject ?? defaultSubject(message.guild);
13
+ const isThreadReply = isThreadChannel(message.channel.type);
14
+ const kind = isThreadReply
15
+ ? "discord.thread.replied"
16
+ : "discord.message.posted";
17
+ const author = resolveAuthorLabel(message);
18
+ const channelLabel = message.channel.name
19
+ ? `#${message.channel.name}`
20
+ : message.channel.id;
21
+ const text = `${author} in ${channelLabel}: ${message.content}`;
22
+ const builder = new EpisodeBuilder({
23
+ subject,
24
+ metadata: {
25
+ guild_id: message.guild.id,
26
+ guild_name: message.guild.name,
27
+ channel_id: message.channel.id,
28
+ channel_name: message.channel.name,
29
+ channel_type: message.channel.type,
30
+ },
31
+ });
32
+ return builder.build({
33
+ kind,
34
+ text,
35
+ occurred_at: message.timestamp,
36
+ source: {
37
+ type: isThreadReply ? "discord.thread.reply" : "discord.message",
38
+ id: `${message.channel.id}:${message.id}`,
39
+ url: buildPermalink(message),
40
+ },
41
+ metadata: {
42
+ author_id: message.author.id,
43
+ author_label: author,
44
+ message_type: message.message_type ?? null,
45
+ edited_timestamp: message.edited_timestamp ?? null,
46
+ parent_id: message.channel.parent_id ?? null,
47
+ },
48
+ // Channel id + message id is unique within Discord, so this idempotency
49
+ // shape is safe across re-runs of the same `--since` window.
50
+ idempotency_parts: [
51
+ "discord",
52
+ message.guild.id,
53
+ message.channel.id,
54
+ message.id,
55
+ kind,
56
+ ],
57
+ });
58
+ }
59
+ /**
60
+ * Discord channel types: 0 = text, 5 = announcement, 10 = announcement
61
+ * thread, 11 = public thread, 12 = private thread, 15 = forum. We treat
62
+ * any thread-like channel (10/11/12) as a thread reply, and everything
63
+ * else as a top-level message.
64
+ */
65
+ function isThreadChannel(type) {
66
+ return type === 10 || type === 11 || type === 12;
67
+ }
68
+ function resolveAuthorLabel(message) {
69
+ const a = message.author;
70
+ if (a.global_name)
71
+ return a.global_name;
72
+ if (a.username)
73
+ return a.username;
74
+ return `<@${a.id}>`;
75
+ }
76
+ /**
77
+ * Discord permalinks have the shape `https://discord.com/channels/<guild>/<channel>/<message>`.
78
+ * Reachable to anyone with View Channel permission on the channel.
79
+ */
80
+ function buildPermalink(message) {
81
+ return `https://discord.com/channels/${message.guild.id}/${message.channel.id}/${message.id}`;
82
+ }
83
+ //# sourceMappingURL=mapper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mapper.js","sourceRoot":"","sources":["../src/mapper.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,qEAAqE;AACrE,0CAA0C;AAE1C,OAAO,EAAE,cAAc,EAAyB,MAAM,+BAA+B,CAAC;AAYtF,MAAM,UAAU,cAAc,CAAC,KAAmB;IAChD,qEAAqE;IACrE,sEAAsE;IACtE,yDAAyD;IACzD,OAAO,aAAa,KAAK,CAAC,EAAE,EAAE,CAAC;AACjC,CAAC;AAED,MAAM,UAAU,eAAe,CAC7B,OAAuB,EACvB,UAAyB,EAAE;IAE3B,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,cAAc,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IACjE,MAAM,aAAa,GAAG,eAAe,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5D,MAAM,IAAI,GAAqB,aAAa;QAC1C,CAAC,CAAC,wBAAwB;QAC1B,CAAC,CAAC,wBAAwB,CAAC;IAE7B,MAAM,MAAM,GAAG,kBAAkB,CAAC,OAAO,CAAC,CAAC;IAC3C,MAAM,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI;QACvC,CAAC,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE;QAC5B,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;IACvB,MAAM,IAAI,GAAG,GAAG,MAAM,OAAO,YAAY,KAAK,OAAO,CAAC,OAAO,EAAE,CAAC;IAEhE,MAAM,OAAO,GAAG,IAAI,cAAc,CAAC;QACjC,OAAO;QACP,QAAQ,EAAE;YACR,QAAQ,EAAE,OAAO,CAAC,KAAK,CAAC,EAAE;YAC1B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,CAAC,CAAC,WAAW;QAAE,OAAO,CAAC,CAAC,WAAW,CAAC;IACxC,IAAI,CAAC,CAAC,QAAQ;QAAE,OAAO,CAAC,CAAC,QAAQ,CAAC;IAClC,OAAO,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC;AACtB,CAAC;AAED;;;GAGG;AACH,SAAS,cAAc,CAAC,OAAuB;IAC7C,OAAO,gCAAgC,OAAO,CAAC,KAAK,CAAC,EAAE,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,IAAI,OAAO,CAAC,EAAE,EAAE,CAAC;AAChG,CAAC"}
package/dist/sync.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { type StatewaveConnector } from "@statewavedev/connectors-core";
2
+ import type { DiscordEvent } from "./types.js";
3
+ export interface DiscordConnectorConfig {
4
+ /** Bot token (required). Auth + REST calls go through this. */
5
+ token: string;
6
+ /** Guild (server) id to scope ingestion to. Required because a bot may
7
+ * belong to multiple guilds and ingesting all of them would be expensive
8
+ * + surprising. */
9
+ guildId: string;
10
+ /**
11
+ * Channel selectors — either IDs (snowflakes) or names (`general` /
12
+ * `#general`). At least one is required so first-run never walks the
13
+ * entire guild.
14
+ */
15
+ channels: ReadonlyArray<string>;
16
+ /** Override subject. Defaults to `community:<guild_id>`. */
17
+ subject?: string;
18
+ baseUrl?: string;
19
+ fetchImpl?: typeof fetch;
20
+ }
21
+ export declare function createDiscordConnector(config: DiscordConnectorConfig): StatewaveConnector<DiscordConnectorConfig, DiscordEvent>;
22
+ //# sourceMappingURL=sync.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync.d.ts","sourceRoot":"","sources":["../src/sync.ts"],"names":[],"mappings":"AAIA,OAAO,EAKL,KAAK,kBAAkB,EAIxB,MAAM,+BAA+B,CAAC;AAGvC,OAAO,KAAK,EAAkB,YAAY,EAAgC,MAAM,YAAY,CAAC;AAE7F,MAAM,WAAW,sBAAsB;IACrC,+DAA+D;IAC/D,KAAK,EAAE,MAAM,CAAC;IACd;;uBAEmB;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB;;;;OAIG;IACH,QAAQ,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IAChC,4DAA4D;IAC5D,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,OAAO,KAAK,CAAC;CAC1B;AAKD,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,sBAAsB,GAC7B,kBAAkB,CAAC,sBAAsB,EAAE,YAAY,CAAC,CA6K1D"}
package/dist/sync.js ADDED
@@ -0,0 +1,176 @@
1
+ // `createDiscordConnector` — pull-mode source connector for Discord.
2
+ // Reads channel messages from a single guild via the Discord REST API
3
+ // and emits `discord.message.posted` / `discord.thread.replied` episodes.
4
+ import { ConnectorError, redactEpisodeText, summarizeEpisodes, } from "@statewavedev/connectors-core";
5
+ import { DiscordClient } from "./client.js";
6
+ import { defaultSubject, mapDiscordEvent } from "./mapper.js";
7
+ const DEFAULT_INCLUDE = ["messages"];
8
+ export function createDiscordConnector(config) {
9
+ if (!config.guildId) {
10
+ throw new ConnectorError("the discord connector requires a guildId — pass --guild <id>", {
11
+ code: "config_invalid",
12
+ connector: "discord",
13
+ hint: "find the guild id by enabling Developer Mode in Discord, then right-clicking the server icon → Copy Server ID",
14
+ });
15
+ }
16
+ if (!config.channels || config.channels.length === 0) {
17
+ throw new ConnectorError("the discord connector requires at least one channel — pass --channels <id-or-name>[,…]", {
18
+ code: "config_invalid",
19
+ connector: "discord",
20
+ hint: "ingesting an entire guild by default would be expensive and surprising",
21
+ });
22
+ }
23
+ const clientOptions = {
24
+ token: config.token,
25
+ baseUrl: config.baseUrl,
26
+ fetchImpl: config.fetchImpl,
27
+ };
28
+ const client = new DiscordClient(clientOptions);
29
+ // Cache resolved guild + channels across `check()` and `sync()` to avoid
30
+ // paying the auth/list cost twice within a single process lifetime.
31
+ let guild;
32
+ let resolvedChannels;
33
+ async function ensureGuild() {
34
+ if (guild)
35
+ return guild;
36
+ guild = await client.getGuild(config.guildId);
37
+ return guild;
38
+ }
39
+ async function ensureChannels() {
40
+ if (resolvedChannels)
41
+ return resolvedChannels;
42
+ resolvedChannels = await client.resolveChannels(config.guildId, config.channels);
43
+ return resolvedChannels;
44
+ }
45
+ return {
46
+ id: `discord:${config.guildId}`,
47
+ name: "Discord",
48
+ source: "discord",
49
+ async configure(_next) {
50
+ throw new ConnectorError("discord connector is configured at construction time", {
51
+ code: "unsupported",
52
+ connector: "discord",
53
+ });
54
+ },
55
+ async check() {
56
+ const details = [];
57
+ let status = "ok";
58
+ try {
59
+ const me = await client.authMe();
60
+ details.push({
61
+ name: "auth",
62
+ status: "ok",
63
+ message: me.username ? `@${me.username} (${me.id})` : me.id,
64
+ });
65
+ }
66
+ catch (err) {
67
+ status = "error";
68
+ details.push({
69
+ name: "auth",
70
+ status: "error",
71
+ message: err instanceof Error ? err.message : String(err),
72
+ });
73
+ return { connector: "discord", status, details };
74
+ }
75
+ try {
76
+ const g = await ensureGuild();
77
+ details.push({
78
+ name: "guild",
79
+ status: "ok",
80
+ message: g.name ? `${g.name} (${g.id})` : g.id,
81
+ });
82
+ }
83
+ catch (err) {
84
+ status = "error";
85
+ details.push({
86
+ name: "guild",
87
+ status: "error",
88
+ message: err instanceof Error ? err.message : String(err),
89
+ });
90
+ return { connector: "discord", status, details };
91
+ }
92
+ try {
93
+ const channels = await ensureChannels();
94
+ details.push({
95
+ name: "channels",
96
+ status: "ok",
97
+ message: channels.map((c) => c.name ?? c.id).join(", "),
98
+ });
99
+ }
100
+ catch (err) {
101
+ status = "error";
102
+ details.push({
103
+ name: "channels",
104
+ status: "error",
105
+ message: err instanceof Error ? err.message : String(err),
106
+ });
107
+ }
108
+ return { connector: "discord", status, details };
109
+ },
110
+ async sync(options) {
111
+ const startedAt = new Date().toISOString();
112
+ const groups = resolveGroups(options.include, options.exclude);
113
+ const g = await ensureGuild();
114
+ const channels = await ensureChannels();
115
+ const subject = options.subject ?? config.subject ?? defaultSubject(g);
116
+ const since = options.since ? new Date(options.since).toISOString() : undefined;
117
+ const events = [];
118
+ if (groups.has("messages")) {
119
+ for (const channel of channels) {
120
+ const msgs = await client.listChannelMessages(channel, g, {
121
+ since,
122
+ maxItems: options.maxItems,
123
+ });
124
+ for (const m of msgs)
125
+ events.push(m);
126
+ }
127
+ }
128
+ const max = options.maxItems ?? Number.POSITIVE_INFINITY;
129
+ const limited = events.slice(0, max);
130
+ const episodes = limited.map((ev) => {
131
+ const ep = mapDiscordEvent(ev, { subject });
132
+ return options.redaction ? redactEpisodeText(ep, options.redaction) : ep;
133
+ });
134
+ const dryRun = !!options.dryRun;
135
+ const ingested = dryRun ? 0 : episodes.length;
136
+ const finishedAt = new Date().toISOString();
137
+ const threadReplies = limited.filter((m) => isThreadChannelType(m.channel.type)).length;
138
+ const topLevel = limited.length - threadReplies;
139
+ const details = {
140
+ events_fetched: events.length,
141
+ events_mapped: episodes.length,
142
+ events_messages: topLevel,
143
+ events_thread_replies: threadReplies,
144
+ channels_synced: channels.length,
145
+ };
146
+ return {
147
+ connector: "discord",
148
+ source: "discord",
149
+ subject,
150
+ episodes,
151
+ ingested,
152
+ skipped: events.length - episodes.length,
153
+ dryRun,
154
+ startedAt,
155
+ finishedAt,
156
+ summary: summarizeEpisodes(episodes, details),
157
+ };
158
+ },
159
+ async mapEvent(event) {
160
+ return mapDiscordEvent(event, {
161
+ subject: config.subject ?? defaultSubject(event.guild),
162
+ });
163
+ },
164
+ };
165
+ }
166
+ function resolveGroups(include, exclude) {
167
+ const base = new Set(include?.length ? include : DEFAULT_INCLUDE);
168
+ if (exclude)
169
+ for (const e of exclude)
170
+ base.delete(e);
171
+ return base;
172
+ }
173
+ function isThreadChannelType(type) {
174
+ return type === 10 || type === 11 || type === 12;
175
+ }
176
+ //# sourceMappingURL=sync.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync.js","sourceRoot":"","sources":["../src/sync.ts"],"names":[],"mappings":"AA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SAAS;gBACT,UAAU;gBACV,OAAO,EAAE,iBAAiB,CAAC,QAAQ,EAAE,OAAO,CAAC;aAC9C,CAAC;QACJ,CAAC;QAED,KAAK,CAAC,QAAQ,CAAC,KAAmB;YAChC,OAAO,eAAe,CAAC,KAAK,EAAE;gBAC5B,OAAO,EAAE,MAAM,CAAC,OAAO,IAAI,cAAc,CAAC,KAAK,CAAC,KAAK,CAAC;aACvD,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC;AAED,SAAS,aAAa,CACpB,OAA0C,EAC1C,OAA0C;IAE1C,MAAM,IAAI,GAAG,IAAI,GAAG,CAClB,OAAO,EAAE,MAAM,CAAC,CAAC,CAAE,OAA8B,CAAC,CAAC,CAAC,eAAe,CACpE,CAAC;IACF,IAAI,OAAO;QAAE,KAAK,MAAM,CAAC,IAAI,OAAO;YAAE,IAAI,CAAC,MAAM,CAAC,CAAqB,CAAC,CAAC;IACzE,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,mBAAmB,CAAC,IAAwB;IACnD,OAAO,IAAI,KAAK,EAAE,IAAI,IAAI,KAAK,EAAE,IAAI,IAAI,KAAK,EAAE,CAAC;AACnD,CAAC"}
@@ -0,0 +1,49 @@
1
+ export type DiscordEventKind = "discord.message.posted" | "discord.thread.replied";
2
+ /** A Discord guild ("server" in the UI). Used to root the default subject. */
3
+ export interface DiscordGuild {
4
+ id: string;
5
+ name?: string;
6
+ }
7
+ /** A Discord channel — either a normal text channel or a thread. */
8
+ export interface DiscordChannel {
9
+ id: string;
10
+ /** Channel name without leading `#`. May be undefined when the connector
11
+ * resolved by id only. */
12
+ name?: string;
13
+ /** Discord channel types: 0 = text, 11 = public thread, 12 = private thread. */
14
+ type?: number;
15
+ /** Set on threads — id of the parent text channel. */
16
+ parent_id?: string;
17
+ }
18
+ /** Discord user record (we only ingest `id` + `username` in v0.1). */
19
+ export interface DiscordUser {
20
+ id: string;
21
+ username?: string;
22
+ global_name?: string;
23
+ }
24
+ /**
25
+ * One Discord message we ingest. The mapper picks `discord.message.posted`
26
+ * vs `discord.thread.replied` based on whether the channel is a thread
27
+ * (parent_id present) or a top-level text channel.
28
+ */
29
+ export interface DiscordMessage {
30
+ type: "message";
31
+ /** Discord message id (snowflake). Doubles as the timestamp source. */
32
+ id: string;
33
+ channel: DiscordChannel;
34
+ guild: DiscordGuild;
35
+ author: DiscordUser;
36
+ /** Message text. Discord allows empty strings for embed-only messages;
37
+ * the connector skips those. */
38
+ content: string;
39
+ /** ISO-8601 timestamp of when Discord stored the message. */
40
+ timestamp: string;
41
+ /** Set when the message was edited; we don't ingest edits as separate
42
+ * episodes in v0.1 but pass it through metadata. */
43
+ edited_timestamp?: string | null;
44
+ /** Discord type code: 0 = default, 19 = reply, 21 = thread starter, …
45
+ * v0.1 ingests only types we recognise as conversational content. */
46
+ message_type?: number;
47
+ }
48
+ export type DiscordEvent = DiscordMessage;
49
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,gBAAgB,GACxB,wBAAwB,GACxB,wBAAwB,CAAC;AAE7B,8EAA8E;AAC9E,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,oEAAoE;AACpE,MAAM,WAAW,cAAc;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX;8BAC0B;IAC1B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,gFAAgF;IAChF,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,sDAAsD;IACtD,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,sEAAsE;AACtE,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,SAAS,CAAC;IAChB,uEAAuE;IACvE,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,cAAc,CAAC;IACxB,KAAK,EAAE,YAAY,CAAC;IACpB,MAAM,EAAE,WAAW,CAAC;IACpB;oCACgC;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,6DAA6D;IAC7D,SAAS,EAAE,MAAM,CAAC;IAClB;wDACoD;IACpD,gBAAgB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC;yEACqE;IACrE,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,MAAM,YAAY,GAAG,cAAc,CAAC"}
package/dist/types.js ADDED
@@ -0,0 +1,7 @@
1
+ // Public types for the Discord connector. Models the slice of Discord's
2
+ // API the v0.1 pull connector reads — channel messages and active
3
+ // threads. Forum channels, reactions, and the realtime Gateway protocol
4
+ // are deliberately out of scope here; if/when we add them they bring
5
+ // their own event kinds.
6
+ export {};
7
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,kEAAkE;AAClE,wEAAwE;AACxE,qEAAqE;AACrE,yBAAyB"}
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@statewavedev/connectors-discord",
3
+ "version": "0.1.0",
4
+ "description": "Discord connector for Statewave — server channels and thread replies as normalized episodes",
5
+ "license": "Apache-2.0",
6
+ "author": "Statewave <hello@statewave.ai>",
7
+ "homepage": "https://statewave.ai",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/smaramwbc/statewave-connectors.git",
11
+ "directory": "packages/discord"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/smaramwbc/statewave-connectors/issues"
15
+ },
16
+ "keywords": [
17
+ "statewave",
18
+ "connectors",
19
+ "discord",
20
+ "ai",
21
+ "agents",
22
+ "memory",
23
+ "community"
24
+ ],
25
+ "type": "module",
26
+ "main": "dist/index.js",
27
+ "types": "dist/index.d.ts",
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "import": "./dist/index.js"
32
+ },
33
+ "./package.json": "./package.json"
34
+ },
35
+ "files": [
36
+ "dist",
37
+ "README.md",
38
+ "LICENSE",
39
+ "!**/*.tsbuildinfo",
40
+ "!**/*.test.*"
41
+ ],
42
+ "engines": {
43
+ "node": ">=20"
44
+ },
45
+ "publishConfig": {
46
+ "access": "public",
47
+ "provenance": true
48
+ },
49
+ "sideEffects": false,
50
+ "dependencies": {
51
+ "@statewavedev/connectors-core": "^0.1.0"
52
+ },
53
+ "devDependencies": {
54
+ "typescript": "^5.6.0",
55
+ "vitest": "^2.1.0",
56
+ "@types/node": "^22.0.0"
57
+ },
58
+ "scripts": {
59
+ "build": "tsc -p tsconfig.json",
60
+ "typecheck": "tsc -p tsconfig.json --noEmit",
61
+ "test": "vitest run",
62
+ "lint": "tsc -p tsconfig.json --noEmit"
63
+ }
64
+ }