@theholocron/holocron-plugin-discord 5.0.0-alpha.26 → 5.0.0-alpha.260

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/README.md CHANGED
@@ -61,3 +61,29 @@ server membership is required — generate one in Discord under
61
61
  The webhook id and token are embedded in the URL path — no `Authorization`
62
62
  header is sent. `parseWebhookUrl` is exported as a utility for callers
63
63
  that need to split the URL into its parts.
64
+
65
+ ## Provisioning
66
+
67
+ `holocron notifications setup` and `holocron notifications channel add` create
68
+ channels, roles and webhooks through the `notifications` capability's `ensure*`
69
+ methods. They use the **Holonet bot**, not a webhook URL:
70
+
71
+ - `HOLOCRON_DISCORD_BOT_TOKEN` (or `DISCORD_BOT_TOKEN`), or the keyring entry
72
+ `discord.holonet` (`holocron auth set discord.holonet <token>`) — the bot token
73
+ from the Developer Portal. Only read when a provisioning command runs, or when
74
+ `holocron doctor` checks a provider that has a `guildId`; posting never needs it.
75
+ - `HOLOCRON_DISCORD_CLIENT_SECRET` (or `DISCORD_CLIENT_SECRET`), or the keyring entry
76
+ `discord.holonet.oauth` — the application's OAuth client secret (Developer Portal →
77
+ OAuth2 → Reset Secret). Only `holocron notifications link` reads it, to learn which
78
+ GitHub account a member owns; the API can neither create nor read it.
79
+ - `guildId` — the server id (not a secret), set in the provider options.
80
+ - `layout` — categories, channels (each optionally with a `webhook`) and roles.
81
+
82
+ The bot needs Manage Channels, Manage Roles, Manage Webhooks and View Channels.
83
+ Everything is idempotent (looked up by name). Discord returns a webhook's URL
84
+ only to the application that created it, so a webhook the bot did not create
85
+ cannot have its URL read back.
86
+
87
+ `baseUrl` only redirects posting. The bot token is always sent to the real
88
+ Discord API unless `unsafeBotBaseUrl: true` is also set (meant for tests that
89
+ stub the API), so a changed config cannot redirect the token to another host.
package/dist/index.d.mts CHANGED
@@ -1,32 +1,8 @@
1
- import { AuthError, Notifications, ResolveTokenInput } from "@theholocron/cli";
2
- //#region src/auth.d.ts
3
- declare const resolveToken: (input?: ResolveTokenInput) => string;
4
- //#endregion
5
- //#region src/rest.d.ts
6
- interface DiscordClientOptions {
7
- /** Override base URL for tests. Default: https://discord.com/api/v10 */
8
- baseUrl?: string;
9
- fetch?: typeof fetch;
10
- }
11
- interface DiscordWebhookInfo {
12
- id: string;
13
- name: string;
14
- guild_id?: string;
15
- }
16
- interface DiscordClient {
17
- webhooks: {
18
- get(id: string, token: string): Promise<DiscordWebhookInfo>;
19
- execute(id: string, token: string, content: string): Promise<void>;
20
- };
21
- }
22
- declare function createDiscordClient({ baseUrl, fetch: fetchImpl }?: DiscordClientOptions): DiscordClient;
23
- /** Parse a Discord webhook URL into its id and token parts. */
24
- declare function parseWebhookUrl(url: string): {
25
- id: string;
26
- token: string;
27
- };
28
- //#endregion
29
- //#region src/capabilities/notifications.d.ts
1
+ import { AuthError, EnsureWebhookResult, EnsuredResource, Notifications, NotificationsAccess, NotificationsApplicationLayout, NotificationsDelivery, NotificationsEveryoneLayout, NotificationsLayout, NotificationsMember, NotificationsOnboardingLayout, NotificationsPinnedLayout, NotificationsServerLayout, NotificationsSignIn, NotificationsWelcomeLayout, ResolveTokenInput, ServerInspection, VerifyTokenFailure, VerifyTokenResult, VerifyTokenResult as VerifyTokenResult$1, VerifyTokenSuccess } from "@theholocron/cli";
2
+ import { DiscordBotClient, DiscordClient, DiscordClientOptions, DiscordOAuthClient, DiscordWebhookInfo, createDiscordClient, parseWebhookUrl } from "@theholocron/discord-client";
3
+ export declare const resolveToken: (input?: ResolveTokenInput) => string;
4
+ export declare const resolveBotToken: (input?: ResolveTokenInput) => string;
5
+ export declare const resolveClientSecret: (input?: ResolveTokenInput) => string;
30
6
  interface DiscordNotificationsOptions {
31
7
  /**
32
8
  * Named aliases mapping a logical channel name to its webhook URL.
@@ -38,44 +14,254 @@ interface DiscordNotificationsOptions {
38
14
  * without an explicit channel, or with an empty string.
39
15
  */
40
16
  defaultChannel?: string;
17
+ /** Discord server (guild) id that `notifications setup` provisions. Not a secret. */
18
+ guildId?: string;
19
+ /** The server layout `notifications setup` applies. */
20
+ layout?: NotificationsLayout;
21
+ /** A custom API host, for tests. Webhook tokens are only sent to it when `unsafeBotBaseUrl` is also set. */
22
+ baseUrl?: string;
23
+ unsafeBotBaseUrl?: boolean;
41
24
  }
42
- declare class DiscordNotifications implements Notifications {
25
+ export declare class DiscordNotifications implements Notifications {
43
26
  private readonly client;
44
27
  private readonly opts;
45
28
  /** Memoized thunk — resolves the default webhook URL on the first `send()`. */
46
29
  private readonly defaultWebhookUrl;
30
+ /** Memoized thunk — resolves the bot token on first provisioning call, never on `send()`. */
31
+ private readonly bot;
32
+ /** Builds the OAuth client for one redirect URI; needs the application's client secret. */
33
+ private readonly oauth;
47
34
  readonly key: "notifications";
48
35
  readonly providerName = "discord";
49
36
  constructor(client: DiscordClient, opts: DiscordNotificationsOptions,
50
37
  /** Memoized thunk — resolves the default webhook URL on the first `send()`. */
51
- defaultWebhookUrl: () => string);
38
+ defaultWebhookUrl: () => string,
39
+ /** Memoized thunk — resolves the bot token on first provisioning call, never on `send()`. */
40
+ bot?: () => DiscordBotClient,
41
+ /** Builds the OAuth client for one redirect URI; needs the application's client secret. */
42
+ oauth?: (redirectUri: string) => Promise<DiscordOAuthClient>);
43
+ /**
44
+ * Sign a member in with Discord to learn which GitHub account they own. Asks only for `identify` and
45
+ * `connections`, reads the account Discord has verified, and revokes the token before returning: nothing
46
+ * about the sign-in is kept.
47
+ */
48
+ signIn(): NotificationsSignIn;
49
+ get layout(): NotificationsLayout | undefined;
52
50
  send(channel: string, message: string): Promise<void>;
51
+ /**
52
+ * `holocron doctor`: the bot token is valid and can see the configured server.
53
+ * Null when no `guildId` is set -- a post-only config has no bot to check. A
54
+ * `layout` without a `guildId` is a half-written provisioning config, so that
55
+ * fails instead of passing as post-only.
56
+ */
57
+ whoami(): Promise<{
58
+ target: string;
59
+ } | null>;
60
+ /**
61
+ * Read-only: what is on the server, for the parts of `layout` we will compare. Webhooks are
62
+ * listed only for channels the layout gives one (one request each), so the cost scales with
63
+ * the layout, not with the server.
64
+ */
65
+ inspect(layout: NotificationsLayout): Promise<ServerInspection>;
66
+ /** What the bot keeps: the usual set, and Pin Messages when the layout pins messages. */
67
+ private needs;
68
+ /** Where to re-invite the bot so its role holds what `setup` needs. Re-authorising updates the role in place. */
69
+ private inviteUrl;
70
+ /** Changing server settings needs Manage Server; an empty edit proves the bot has it without changing anything. */
71
+ preflight(layout: NotificationsLayout): Promise<void>;
72
+ /** The one role with this name, or an error saying why not: two would mean granting something to the wrong members. */
73
+ private static pickRole;
74
+ /** The one text or announcement channel with this name, or an error saying why not. */
75
+ private static pickChannelId;
76
+ /** True for a 403 that is about permissions, not about the bot being unable to see the server at all. */
77
+ private static missingPermissions;
78
+ /** An invite link, or nothing when building it needs a request that fails: the advice must not depend on one. */
79
+ private inviteOrNothing;
80
+ /** Turning community on or off needs Administrator, which is more than the bot should keep. */
81
+ private communityHint;
82
+ /** A 403 on a server edit becomes the explanation and the invite link; anything else is passed on as it is. */
83
+ private manageServerHint;
84
+ ensureServer(settings: NotificationsServerLayout): Promise<{
85
+ updated: string[];
86
+ }>;
87
+ /** What the bot's roles give it server-wide (the owner holds every permission), before any channel overwrite. */
88
+ private serverPermissions;
89
+ holdsPermission(permission: string): Promise<boolean>;
90
+ /**
91
+ * Whether a channel's own overwrites take a permission away from a bot whose roles grant it, in the order Discord
92
+ * applies them: @everyone, then the bot's roles, then the bot itself. Read fresh, since a person may have just edited them.
93
+ */
94
+ private channelDenies;
95
+ /** The messages this bot posted in a channel, pinned or not, among the channel's last hundred. */
96
+ private ownMessages;
97
+ /**
98
+ * The layout's text with each `#name` that is one of the server's channels turned into Discord's own link to it
99
+ * (`<#id>`), so the message shows clickable channels. A name that matches no channel, or more than one, is left as written.
100
+ */
101
+ private static linkChannels;
102
+ /** The layout's messages grouped by channel, in the order the channels first appear, with channel names linked. */
103
+ private static pinsByChannel;
104
+ /**
105
+ * What differs in one channel: the declared messages that are not pinned yet, and the bot's pinned messages the
106
+ * layout does not declare (or declares once and the bot pinned twice). The layout is the whole set of the bot's
107
+ * pins in a channel it names.
108
+ */
109
+ private static pinDrift;
110
+ /** Whether each pinned message is in place: pinned by this bot with exactly the layout's text, and nothing else of the bot's pinned beside it. */
111
+ private pinnedStates;
112
+ ensurePinned(pinned: NotificationsPinnedLayout[]): Promise<{
113
+ updated: string[];
114
+ }>;
115
+ /** A 403 on pinning becomes the missing permission and a link that grants it, which `setup` can walk a person through. */
116
+ private pinHint;
117
+ /** A 403 on the welcome screen or onboarding becomes the likely reason; anything else is passed on. */
118
+ private static communityFeatureHint;
119
+ /** The numeric code Discord puts in the JSON body of an error, or undefined when the body is not that. */
120
+ private static codeOf;
121
+ /** The welcome screen; a server that never had one answers 404 (10069), which is an empty screen. */
122
+ private welcomeScreenOf;
123
+ ensureWelcome(welcome: NotificationsWelcomeLayout): Promise<{
124
+ updated: string[];
125
+ }>;
126
+ /** An onboarding prompt reduced to what the layout can state, so the server's and the layout's can be compared. */
127
+ private static onboardingKey;
128
+ ensureOnboarding(onboarding: NotificationsOnboardingLayout): Promise<{
129
+ updated: string[];
130
+ }>;
131
+ ensureEveryone(everyone: NotificationsEveryoneLayout): Promise<{
132
+ updated: string[];
133
+ }>;
134
+ ensureApplication(app: NotificationsApplicationLayout): Promise<{
135
+ updated: string[];
136
+ todo?: string[];
137
+ }>;
138
+ /** Whether the Server Members Intent is on: the unverified app's toggle, or the verified app's own bit. */
139
+ private static membersIntentOn;
140
+ validateLayout(layout: NotificationsLayout): string | undefined;
141
+ private botUser?;
142
+ private botId;
143
+ /**
144
+ * The overwrites that make a channel look like `access`. The list is exact: it replaces
145
+ * whatever was set by hand. `[]` (public) when nothing is restricted.
146
+ */
147
+ private overwritesFor;
148
+ /**
149
+ * Read an access back out of a channel's overwrites, naming roles as the layout does. Only
150
+ * overwrites of the shape {@link overwritesFor} writes can be read back; anything else
151
+ * (a role-specific deny, a member overwrite, only some of the posting bits) means the
152
+ * channel's real access is not something the layout can state, and is reported as `"custom"`
153
+ * rather than guessed at.
154
+ */
155
+ private accessOf;
156
+ /** The server's roles, fetched once and forgotten whenever setup changes a role. */
157
+ private roleLoad?;
158
+ private roleList;
159
+ /** The server this run created, which takes over from `guildId` for the rest of the run. */
160
+ private createdGuild?;
161
+ private guild;
162
+ /**
163
+ * The server's channel list, fetched once per instance. Every ensure* call used
164
+ * to refetch it -- about 40 identical GETs in one setup run -- and Discord
165
+ * rate-limited the route. Our own creates and edits update the copy, so it
166
+ * stays accurate for the run; a new run builds a new instance.
167
+ *
168
+ * It is the in-flight promise that is shared, not the array: callers that start
169
+ * while the first fetch is pending all get the same snapshot, so a later
170
+ * response can never overwrite what an earlier caller added.
171
+ */
172
+ private channelLoad?;
173
+ private channelList;
174
+ /**
175
+ * Create a channel and add it to the shared list. If the create fails, Discord
176
+ * may still have applied it (a timeout looks the same as a refusal), so forget the
177
+ * list: the next ensure refetches the server's real state instead of creating a
178
+ * duplicate.
179
+ */
180
+ private createChannel;
181
+ private static same;
182
+ ensureCategory(name: string, opts?: {
183
+ access?: NotificationsAccess;
184
+ }): Promise<EnsuredResource>;
185
+ /**
186
+ * Edit a channel and keep the shared list in step; forget the list if the edit may have half-applied.
187
+ * Discord answers with the channel as it now is: if the overwrites that come back are not the ones we
188
+ * sent, something else changed it at the same time, so say so instead of reporting success.
189
+ */
190
+ private modifyChannel;
191
+ /**
192
+ * Find a channel by name. A name can exist in several categories, so with a
193
+ * parent: prefer the one already there, then an uncategorised one (moved in);
194
+ * never grab a channel that lives under a different category. Announcement
195
+ * channels count: reusing one beats creating a same-named text channel.
196
+ */
197
+ private static findChannel;
198
+ ensureChannel(input: {
199
+ name: string;
200
+ category?: string;
201
+ topic?: string;
202
+ access?: NotificationsAccess;
203
+ }): Promise<EnsuredResource>;
204
+ ensureWebhook(input: {
205
+ channel: string;
206
+ name: string;
207
+ category?: string;
208
+ avatar?: string;
209
+ }): Promise<EnsureWebhookResult>;
210
+ checkDelivery(input: {
211
+ channel: string;
212
+ name: string;
213
+ category?: string;
214
+ text: string;
215
+ keep?: boolean;
216
+ }): Promise<NotificationsDelivery>;
217
+ createServer(input: {
218
+ name: string;
219
+ force?: boolean;
220
+ }): Promise<{
221
+ id: string;
222
+ name: string;
223
+ }>;
224
+ /** A server this bot owns (not one it only belongs to) with exactly this name, read across every page. */
225
+ private botOwnedServer;
226
+ inviteLink(channelName: string): Promise<string>;
227
+ hasMember(memberId: string): Promise<boolean>;
228
+ transferServer(memberId: string): Promise<void>;
229
+ serverOwner(): Promise<{
230
+ id: string;
231
+ isBot: boolean;
232
+ name?: string;
233
+ }>;
234
+ /** The one role with this name, or an error that says what to fix: none, or more than one. */
235
+ private roleNamed;
236
+ roleMembers(roleName: string): Promise<NotificationsMember[]>;
237
+ addRoleMember(roleName: string, memberId: string): Promise<void>;
238
+ removeRoleMember(roleName: string, memberId: string): Promise<void>;
239
+ ensureRole(input: {
240
+ name: string;
241
+ mentionable?: boolean;
242
+ color?: string;
243
+ hoist?: boolean;
244
+ permissions?: string[];
245
+ }): Promise<EnsuredResource>;
53
246
  private resolve;
54
247
  }
55
- //#endregion
56
- //#region src/verify-token.d.ts
57
- interface VerifyTokenSuccess {
58
- ok: true;
59
- subject: string;
60
- }
61
- interface VerifyTokenFailure {
62
- ok: false;
63
- message: string;
64
- }
65
- type VerifyTokenResult = VerifyTokenSuccess | VerifyTokenFailure;
66
248
  interface VerifyTokenOptions {
67
249
  baseUrl?: string;
68
250
  fetch?: typeof fetch;
69
251
  }
70
- declare function verifyToken(webhookUrl: string, opts?: VerifyTokenOptions): Promise<VerifyTokenResult>;
71
- //#endregion
72
- //#region src/index.d.ts
73
- interface DiscordPluginOptions extends ResolveTokenInput, DiscordNotificationsOptions {
252
+ export declare function verifyToken(webhookUrl: string, opts?: VerifyTokenOptions): Promise<VerifyTokenResult$1>;
253
+ export interface DiscordPluginOptions extends ResolveTokenInput, DiscordNotificationsOptions {
74
254
  /** Override base URL for tests. Default: https://discord.com/api/v10 */
75
255
  baseUrl?: string;
76
256
  fetch?: typeof fetch;
257
+ /**
258
+ * Also send the BOT token to `baseUrl`. Off by default: `baseUrl` comes from a
259
+ * repo's config, and a changed config must not be able to redirect the bot
260
+ * token to another host. Tests that stub the API set it.
261
+ */
262
+ unsafeBotBaseUrl?: boolean;
77
263
  }
78
- interface PluginContext {
264
+ export interface PluginContext {
79
265
  options: DiscordPluginOptions;
80
266
  client: DiscordClient;
81
267
  /**
@@ -83,15 +269,21 @@ interface PluginContext {
83
269
  * resolved on first `send()`, not at plugin load. `describe()` never calls it.
84
270
  */
85
271
  defaultWebhookUrl: () => string;
272
+ /** Memoized — resolves the bot token on the first provisioning call, or when `holocron doctor` checks a provider that has a `guildId`; `send()` and `describe()` never touch it. */
273
+ bot: () => DiscordBotClient;
274
+ /** Builds the OAuth client for one redirect URI; resolves the application id and the client secret on first use. */
275
+ oauth: (redirectUri: string) => Promise<DiscordOAuthClient>;
86
276
  }
87
- declare function createContext(options?: DiscordPluginOptions): PluginContext;
88
- declare function notifications(ctx: PluginContext): Notifications;
89
- declare function createPlugin(options?: DiscordPluginOptions): {
277
+ export declare function createContext(options?: DiscordPluginOptions): PluginContext;
278
+ export declare function notifications(ctx: PluginContext): Notifications;
279
+ export declare function createPlugin(options?: DiscordPluginOptions): {
90
280
  name: string;
281
+ probes: {
282
+ connection: import("@theholocron/cli").Probe;
283
+ };
91
284
  capabilities: {
92
285
  notifications: () => Notifications;
93
286
  };
94
287
  };
95
- declare const AUTH_HINT: string;
96
- //#endregion
97
- export { AUTH_HINT, AuthError, type DiscordClient, type DiscordClientOptions, DiscordNotifications, type DiscordNotificationsOptions, DiscordPluginOptions, type DiscordWebhookInfo, PluginContext, type ResolveTokenInput, type VerifyTokenFailure, type VerifyTokenResult, type VerifyTokenSuccess, createContext, createDiscordClient, createPlugin, notifications, parseWebhookUrl, resolveToken, verifyToken };
288
+ export declare const AUTH_HINT: string;
289
+ export { AuthError, type DiscordClient, type DiscordClientOptions, type DiscordNotificationsOptions, type DiscordWebhookInfo, type ResolveTokenInput, type VerifyTokenFailure, type VerifyTokenResult, type VerifyTokenSuccess, createDiscordClient, parseWebhookUrl };