@theholocron/holocron-plugin-discord 5.0.0-alpha.196 → 5.0.0-alpha.198

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/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { AuthError, EnsureWebhookResult, EnsuredResource, Notifications, NotificationsAccess, NotificationsLayout, NotificationsOnboardingLayout, NotificationsServerLayout, NotificationsWelcomeLayout, ResolveTokenInput, ServerInspection, VerifyTokenFailure, VerifyTokenResult, VerifyTokenResult as VerifyTokenResult$1, VerifyTokenSuccess } from "@theholocron/cli";
1
+ import { AuthError, EnsureWebhookResult, EnsuredResource, Notifications, NotificationsAccess, NotificationsApplicationLayout, NotificationsEveryoneLayout, NotificationsLayout, NotificationsOnboardingLayout, NotificationsServerLayout, NotificationsWelcomeLayout, ResolveTokenInput, ServerInspection, VerifyTokenFailure, VerifyTokenResult, VerifyTokenResult as VerifyTokenResult$1, VerifyTokenSuccess } from "@theholocron/cli";
2
2
  import { DiscordBotClient, DiscordClient, DiscordClientOptions, DiscordWebhookInfo, createDiscordClient, parseWebhookUrl } from "@theholocron/discord-client";
3
3
  //#region src/auth.d.ts
4
4
  declare const resolveToken: (input?: ResolveTokenInput) => string;
@@ -85,6 +85,12 @@ declare class DiscordNotifications implements Notifications {
85
85
  ensureOnboarding(onboarding: NotificationsOnboardingLayout): Promise<{
86
86
  updated: string[];
87
87
  }>;
88
+ ensureEveryone(everyone: NotificationsEveryoneLayout): Promise<{
89
+ updated: string[];
90
+ }>;
91
+ ensureApplication(app: NotificationsApplicationLayout): Promise<{
92
+ updated: string[];
93
+ }>;
88
94
  validateLayout(layout: NotificationsLayout): string | undefined;
89
95
  private botUser?;
90
96
  private botId;
@@ -151,6 +157,7 @@ declare class DiscordNotifications implements Notifications {
151
157
  channel: string;
152
158
  name: string;
153
159
  category?: string;
160
+ avatar?: string;
154
161
  }): Promise<EnsureWebhookResult>;
155
162
  ensureRole(input: {
156
163
  name: string;
package/dist/index.mjs CHANGED
@@ -47,6 +47,35 @@ const BOT_NEEDS = [
47
47
  "CreatePrivateThreads",
48
48
  "SendMessagesInThreads"
49
49
  ];
50
+ /** The OAuth2 scopes Discord offers an application, as its own application page lists them. Exact, lower-case. */
51
+ const SCOPES = /* @__PURE__ */ new Set([
52
+ "applications.builds.read",
53
+ "applications.commands",
54
+ "applications.commands.permissions.update",
55
+ "applications.entitlements",
56
+ "applications.store.update",
57
+ "bot",
58
+ "connections",
59
+ "email",
60
+ "gdm.join",
61
+ "guilds",
62
+ "guilds.join",
63
+ "guilds.members.read",
64
+ "identify",
65
+ "messages.read",
66
+ "openid",
67
+ "role_connections.write",
68
+ "rpc",
69
+ "rpc.activities.write",
70
+ "rpc.notifications.read",
71
+ "rpc.screenshare.read",
72
+ "rpc.screenshare.write",
73
+ "rpc.video.read",
74
+ "rpc.video.write",
75
+ "rpc.voice.read",
76
+ "rpc.voice.write",
77
+ "webhook.incoming"
78
+ ]);
50
79
  /** The locales Discord accepts for a server's preferred language. */
51
80
  const LOCALES = /* @__PURE__ */ new Set([
52
81
  "id",
@@ -216,9 +245,12 @@ var DiscordNotifications = class DiscordNotifications {
216
245
  const found = [];
217
246
  for (const c of text) {
218
247
  let webhooks = [];
248
+ let webhookAvatars = [];
219
249
  let webhooksError;
220
250
  if (wantsWebhook.has(c.name.toLowerCase())) try {
221
- webhooks = (await bot.channels.webhooks(c.id)).map((w) => w.name ?? "");
251
+ const hooks = await bot.channels.webhooks(c.id);
252
+ webhooks = hooks.map((w) => w.name ?? "");
253
+ webhookAvatars = hooks.filter((w) => w.avatar).map((w) => w.name ?? "");
222
254
  } catch (err) {
223
255
  webhooksError = reason(err);
224
256
  }
@@ -228,6 +260,7 @@ var DiscordNotifications = class DiscordNotifications {
228
260
  ...category ? { category } : {},
229
261
  ...c.topic ? { topic: c.topic } : {},
230
262
  webhooks,
263
+ webhookAvatars,
231
264
  ...webhooksError ? { webhooksError } : {}
232
265
  });
233
266
  }
@@ -314,6 +347,34 @@ var DiscordNotifications = class DiscordNotifications {
314
347
  } catch (err) {
315
348
  onboardingError = reason(err);
316
349
  }
350
+ let application;
351
+ let applicationError;
352
+ if (layout.application) try {
353
+ const app = await bot.application.get();
354
+ const install = app.install_params;
355
+ application = {
356
+ ...app.description ? { description: app.description } : {},
357
+ tags: app.tags ?? [],
358
+ ...install ? { install: {
359
+ scopes: install.scopes,
360
+ permissions: permissionNames(install.permissions)
361
+ } } : {},
362
+ ...app.terms_of_service_url ? { termsUrl: app.terms_of_service_url } : {},
363
+ ...app.privacy_policy_url ? { privacyUrl: app.privacy_policy_url } : {},
364
+ ...app.role_connections_verification_url ? { roleConnectionsUrl: app.role_connections_verification_url } : {}
365
+ };
366
+ } catch (err) {
367
+ applicationError = reason(err);
368
+ }
369
+ let everyone;
370
+ let everyoneError;
371
+ if (layout.everyone) try {
372
+ const role = (await bot.guilds.roles(this.guild())).find((r) => r.id === this.guild());
373
+ if (role?.permissions === void 0) throw new Error("the server has no @everyone role to read");
374
+ everyone = { permissions: permissionNames(role.permissions) };
375
+ } catch (err) {
376
+ everyoneError = reason(err);
377
+ }
317
378
  const roleName = (id) => roles.find((r) => r.id === id)?.name ?? id;
318
379
  found.forEach((entry, i) => {
319
380
  entry.access = this.accessOf(text[i].permission_overwrites, roleName);
@@ -329,6 +390,10 @@ var DiscordNotifications = class DiscordNotifications {
329
390
  ...welcomeError ? { welcomeError } : {},
330
391
  ...onboarding ? { onboarding } : {},
331
392
  ...onboardingError ? { onboardingError } : {},
393
+ ...application ? { application } : {},
394
+ ...applicationError ? { applicationError } : {},
395
+ ...everyone ? { everyone } : {},
396
+ ...everyoneError ? { everyoneError } : {},
332
397
  channels: found,
333
398
  roles,
334
399
  ...rolesError ? { rolesError } : {}
@@ -620,7 +685,97 @@ var DiscordNotifications = class DiscordNotifications {
620
685
  if (after.enabled !== enabled || JSON.stringify([...after.default_channel_ids].sort()) !== JSON.stringify([...defaultChannels].sort()) || DiscordNotifications.onboardingKey(after.prompts) !== DiscordNotifications.onboardingKey(prompts)) throw new Error("sent onboarding but it came back different -- Discord may have refused a part of it");
621
686
  return { updated };
622
687
  }
688
+ async ensureEveryone(everyone) {
689
+ const bot = this.bot();
690
+ const role = (await bot.guilds.roles(this.guild())).find((r) => r.id === this.guild());
691
+ if (role?.permissions === void 0) throw new Error("the server has no @everyone role to change");
692
+ const bit = (name) => BigInt(permissions([name]));
693
+ const have = BigInt(role.permissions);
694
+ const allow = everyone.allow ?? [];
695
+ const deny = everyone.deny ?? [];
696
+ const want = allow.reduce((acc, n) => acc | bit(n), have);
697
+ const final = deny.reduce((acc, n) => acc & ~bit(n), want);
698
+ if (final === have) return { updated: [] };
699
+ const updated = [...allow.filter((n) => (have & bit(n)) === 0n), ...deny.filter((n) => (have & bit(n)) !== 0n)];
700
+ this.roleLoad = void 0;
701
+ if ((await bot.guilds.modifyRole(this.guild(), role.id, { permissions: String(final) })).permissions !== String(final)) throw new Error("sent the @everyone permissions but they came back different -- Discord may have refused a part of it");
702
+ return { updated };
703
+ }
704
+ async ensureApplication(app) {
705
+ const bot = this.bot();
706
+ const current = await bot.application.get();
707
+ const set = (list) => JSON.stringify([...new Set(list.map((n) => n.toLowerCase()))].sort());
708
+ const exact = (list) => JSON.stringify([...new Set(list)].sort());
709
+ const urls = [
710
+ ["termsUrl", "terms_of_service_url"],
711
+ ["privacyUrl", "privacy_policy_url"],
712
+ ["roleConnectionsUrl", "role_connections_verification_url"]
713
+ ];
714
+ const body = {};
715
+ const updated = [];
716
+ if (app.description !== void 0 && (current.description ?? "") !== app.description) {
717
+ body.description = app.description;
718
+ updated.push("description");
719
+ }
720
+ if (app.tags !== void 0 && set(current.tags ?? []) !== set(app.tags)) {
721
+ body.tags = app.tags;
722
+ updated.push("tags");
723
+ }
724
+ const install = app.install && {
725
+ scopes: app.install.scopes,
726
+ permissions: permissions(app.install.permissions)
727
+ };
728
+ const haveInstall = current.install_params;
729
+ if (install && (!haveInstall || exact(haveInstall.scopes) !== exact(install.scopes) || haveInstall.permissions !== install.permissions)) {
730
+ body.install_params = install;
731
+ updated.push("install");
732
+ }
733
+ for (const [field, key] of urls) if (app[field] !== void 0 && (current[key] ?? "") !== app[field]) {
734
+ body[key] = app[field];
735
+ updated.push(field);
736
+ }
737
+ if (updated.length === 0) return { updated };
738
+ const after = await bot.application.modify(body);
739
+ const stuck = updated.filter((name) => {
740
+ if (name === "description") return (after.description ?? "") !== app.description;
741
+ if (name === "tags") return set(after.tags ?? []) !== set(app.tags);
742
+ if (name === "install") return !after.install_params || exact(after.install_params.scopes) !== exact(install.scopes) || after.install_params.permissions !== install.permissions;
743
+ const key = urls.find(([f]) => f === name)[1];
744
+ return (after[key] ?? "") !== app[name];
745
+ });
746
+ if (stuck.length > 0) throw new Error(`sent ${stuck.join(", ")} but Discord did not keep ${stuck.length === 1 ? "it" : "them"} -- it may not accept that field from a bot, so set it in the developer portal`);
747
+ return { updated };
748
+ }
623
749
  validateLayout(layout) {
750
+ const app = layout.application;
751
+ if (app) {
752
+ if (app.description !== void 0 && app.description.length > 400) return `application: \`description\` is ${app.description.length} characters; Discord allows up to 400`;
753
+ if (app.tags !== void 0) {
754
+ if (app.tags.length > 5) return `application: ${app.tags.length} tags; Discord allows up to 5`;
755
+ const long = app.tags.find((t) => t.length > 20);
756
+ if (long !== void 0) return `application: the tag ${JSON.stringify(long)} is over 20 characters`;
757
+ }
758
+ for (const [field, url] of [
759
+ ["termsUrl", app.termsUrl],
760
+ ["privacyUrl", app.privacyUrl],
761
+ ["roleConnectionsUrl", app.roleConnectionsUrl]
762
+ ]) if (url !== void 0 && !/^https:\/\//.test(url)) return `application: \`${field}\` ${JSON.stringify(url)} must start with https://`;
763
+ if (app.install) {
764
+ if (app.install.scopes.length === 0) return "application: `install.scopes` is empty; the link needs at least `bot`";
765
+ const unknown = app.install.scopes.find((s) => !SCOPES.has(s));
766
+ if (unknown !== void 0) return `application: \`install.scopes\` has ${JSON.stringify(unknown)}, which is not an OAuth2 scope Discord offers (scopes are lower-case, for example bot, applications.commands)`;
767
+ try {
768
+ permissions(app.install.permissions);
769
+ } catch (err) {
770
+ return `application: \`install.permissions\`: ${errorMessage(err)}`;
771
+ }
772
+ }
773
+ }
774
+ if (layout.everyone) try {
775
+ permissions([...layout.everyone.allow ?? [], ...layout.everyone.deny ?? []]);
776
+ } catch (err) {
777
+ return `everyone: ${errorMessage(err)}`;
778
+ }
624
779
  const questions = layout.onboarding?.prompts?.length ?? 0;
625
780
  if (questions > 5) return `onboarding: ${questions} questions; Discord allows up to 5`;
626
781
  const welcome = layout.welcome;
@@ -872,15 +1027,18 @@ var DiscordNotifications = class DiscordNotifications {
872
1027
  const channel = DiscordNotifications.findChannel(await this.channelList(), input.channel, parent);
873
1028
  if (!channel) throw new Error(`no channel named \`${input.channel}\` — create it first`);
874
1029
  const existing = (await bot.channels.webhooks(channel.id)).find((w) => DiscordNotifications.same(w.name ?? "", input.name));
875
- if (existing) return {
876
- created: false,
877
- ...existing.url ? { url: existing.url } : {}
878
- };
879
- const made = await bot.channels.createWebhook(channel.id, { name: input.name });
880
- return {
881
- created: true,
882
- ...made.url ? { url: made.url } : {}
1030
+ const hook = existing ?? await bot.channels.createWebhook(channel.id, { name: input.name });
1031
+ const result = {
1032
+ created: !existing,
1033
+ ...hook.url ? { url: hook.url } : {}
883
1034
  };
1035
+ if (input.avatar !== void 0 && !hook.avatar) try {
1036
+ await bot.webhooks.modify(hook.id, { avatar: input.avatar });
1037
+ result.updated = ["avatar"];
1038
+ } catch (err) {
1039
+ result.avatarError = reason(err);
1040
+ }
1041
+ return result;
884
1042
  }
885
1043
  async ensureRole(input) {
886
1044
  const bot = this.bot();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/holocron-plugin-discord",
3
- "version": "5.0.0-alpha.196",
3
+ "version": "5.0.0-alpha.198",
4
4
  "description": "Holocron plugin for Discord. Implements the notifications capability via Discord incoming webhooks.",
5
5
  "keywords": [
6
6
  "discord",
@@ -30,7 +30,7 @@
30
30
  "dist"
31
31
  ],
32
32
  "devDependencies": {
33
- "@theholocron/discord-client": "^1.44.0",
33
+ "@theholocron/discord-client": "^1.45.0",
34
34
  "@theholocron/eslint-config": "^8.8.2",
35
35
  "@theholocron/http-client": "^1.43.1",
36
36
  "@theholocron/misc-utils": "^1.16.0",
@@ -48,12 +48,12 @@
48
48
  "tsx": "4.23.15",
49
49
  "typescript": "^5.9.3",
50
50
  "vitest": "^4.1.11",
51
- "@theholocron/cli": "5.0.0-alpha.196"
51
+ "@theholocron/cli": "5.0.0-alpha.198"
52
52
  },
53
53
  "peerDependencies": {
54
- "@theholocron/discord-client": "^1.44.0",
54
+ "@theholocron/discord-client": "^1.45.0",
55
55
  "@theholocron/misc-utils": "^1.16.0",
56
- "@theholocron/cli": "5.0.0-alpha.196"
56
+ "@theholocron/cli": "5.0.0-alpha.198"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">=22"