@theholocron/holocron-plugin-discord 5.0.0-alpha.195 → 5.0.0-alpha.197
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 +5 -0
- package/dist/index.mjs +67 -25
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -73,6 +73,10 @@ declare class DiscordNotifications implements Notifications {
|
|
|
73
73
|
}>;
|
|
74
74
|
/** A 403 on the welcome screen or onboarding becomes the likely reason; anything else is passed on. */
|
|
75
75
|
private static communityFeatureHint;
|
|
76
|
+
/** The numeric code Discord puts in the JSON body of an error, or undefined when the body is not that. */
|
|
77
|
+
private static codeOf;
|
|
78
|
+
/** The welcome screen; a server that never had one answers 404 (10069), which is an empty screen. */
|
|
79
|
+
private welcomeScreenOf;
|
|
76
80
|
ensureWelcome(welcome: NotificationsWelcomeLayout): Promise<{
|
|
77
81
|
updated: string[];
|
|
78
82
|
}>;
|
|
@@ -147,6 +151,7 @@ declare class DiscordNotifications implements Notifications {
|
|
|
147
151
|
channel: string;
|
|
148
152
|
name: string;
|
|
149
153
|
category?: string;
|
|
154
|
+
avatar?: string;
|
|
150
155
|
}): Promise<EnsureWebhookResult>;
|
|
151
156
|
ensureRole(input: {
|
|
152
157
|
name: string;
|
package/dist/index.mjs
CHANGED
|
@@ -161,6 +161,9 @@ function keepThreadBits(desired, existing) {
|
|
|
161
161
|
}
|
|
162
162
|
return merged;
|
|
163
163
|
}
|
|
164
|
+
let idCounter = 0;
|
|
165
|
+
/** A snowflake-shaped id for a new onboarding question or answer, which Discord asks the sender to make. */
|
|
166
|
+
const newId = () => String((BigInt(Date.now()) - 1420070400000n << 22n) + BigInt(idCounter++ % 4096));
|
|
164
167
|
var DiscordNotifications = class DiscordNotifications {
|
|
165
168
|
client;
|
|
166
169
|
opts;
|
|
@@ -213,9 +216,12 @@ var DiscordNotifications = class DiscordNotifications {
|
|
|
213
216
|
const found = [];
|
|
214
217
|
for (const c of text) {
|
|
215
218
|
let webhooks = [];
|
|
219
|
+
let webhookAvatars = [];
|
|
216
220
|
let webhooksError;
|
|
217
221
|
if (wantsWebhook.has(c.name.toLowerCase())) try {
|
|
218
|
-
|
|
222
|
+
const hooks = await bot.channels.webhooks(c.id);
|
|
223
|
+
webhooks = hooks.map((w) => w.name ?? "");
|
|
224
|
+
webhookAvatars = hooks.filter((w) => w.avatar).map((w) => w.name ?? "");
|
|
219
225
|
} catch (err) {
|
|
220
226
|
webhooksError = reason(err);
|
|
221
227
|
}
|
|
@@ -225,6 +231,7 @@ var DiscordNotifications = class DiscordNotifications {
|
|
|
225
231
|
...category ? { category } : {},
|
|
226
232
|
...c.topic ? { topic: c.topic } : {},
|
|
227
233
|
webhooks,
|
|
234
|
+
webhookAvatars,
|
|
228
235
|
...webhooksError ? { webhooksError } : {}
|
|
229
236
|
});
|
|
230
237
|
}
|
|
@@ -274,7 +281,7 @@ var DiscordNotifications = class DiscordNotifications {
|
|
|
274
281
|
let welcomeError;
|
|
275
282
|
if (layout.welcome) try {
|
|
276
283
|
const guild = await bot.guilds.get(this.guild());
|
|
277
|
-
const screen = await
|
|
284
|
+
const screen = await this.welcomeScreenOf();
|
|
278
285
|
welcome = {
|
|
279
286
|
enabled: (guild.features ?? []).includes("WELCOME_SCREEN_ENABLED"),
|
|
280
287
|
description: screen.description ?? "",
|
|
@@ -477,6 +484,26 @@ var DiscordNotifications = class DiscordNotifications {
|
|
|
477
484
|
if (!(err instanceof ProviderApiError && err.status === 403)) return err;
|
|
478
485
|
return new Error(`the bot is not allowed to change ${what} (Discord said: ${String(err.details).slice(0, 120)}). It needs Manage Server, and the server needs community features on`, { cause: err });
|
|
479
486
|
}
|
|
487
|
+
/** The numeric code Discord puts in the JSON body of an error, or undefined when the body is not that. */
|
|
488
|
+
static codeOf(err) {
|
|
489
|
+
try {
|
|
490
|
+
return JSON.parse(String(err.details)).code;
|
|
491
|
+
} catch {
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
/** The welcome screen; a server that never had one answers 404 (10069), which is an empty screen. */
|
|
496
|
+
async welcomeScreenOf() {
|
|
497
|
+
try {
|
|
498
|
+
return await this.bot().guilds.welcomeScreen(this.guild());
|
|
499
|
+
} catch (err) {
|
|
500
|
+
if (err instanceof ProviderApiError && err.status === 404 && DiscordNotifications.codeOf(err) === 10069) return {
|
|
501
|
+
description: null,
|
|
502
|
+
welcome_channels: []
|
|
503
|
+
};
|
|
504
|
+
throw err;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
480
507
|
async ensureWelcome(welcome) {
|
|
481
508
|
const bot = this.bot();
|
|
482
509
|
const channels = await this.channelList();
|
|
@@ -501,7 +528,7 @@ var DiscordNotifications = class DiscordNotifications {
|
|
|
501
528
|
let screen;
|
|
502
529
|
try {
|
|
503
530
|
guild = await bot.guilds.get(this.guild());
|
|
504
|
-
screen = await
|
|
531
|
+
screen = await this.welcomeScreenOf();
|
|
505
532
|
} catch (err) {
|
|
506
533
|
throw DiscordNotifications.communityFeatureHint(err, "the welcome screen");
|
|
507
534
|
}
|
|
@@ -549,20 +576,32 @@ var DiscordNotifications = class DiscordNotifications {
|
|
|
549
576
|
} catch (err) {
|
|
550
577
|
throw DiscordNotifications.communityFeatureHint(err, "onboarding");
|
|
551
578
|
}
|
|
552
|
-
const
|
|
553
|
-
|
|
554
|
-
title
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
579
|
+
const usedIds = /* @__PURE__ */ new Set();
|
|
580
|
+
const prompts = onboarding.prompts === void 0 ? current.prompts : onboarding.prompts.map((p) => {
|
|
581
|
+
const had = current.prompts.find((c) => c.title === p.title && !usedIds.has(c.id));
|
|
582
|
+
if (had) usedIds.add(had.id);
|
|
583
|
+
const hadOptions = /* @__PURE__ */ new Set();
|
|
584
|
+
return {
|
|
585
|
+
id: had?.id ?? newId(),
|
|
586
|
+
type: 0,
|
|
587
|
+
title: p.title,
|
|
588
|
+
single_select: p.singleSelect ?? false,
|
|
589
|
+
required: p.required ?? false,
|
|
590
|
+
in_onboarding: true,
|
|
591
|
+
options: p.options.map((o) => ({
|
|
592
|
+
id: (() => {
|
|
593
|
+
const old = had?.options.find((c) => c.title === o.title && !hadOptions.has(c.id));
|
|
594
|
+
if (old) hadOptions.add(old.id);
|
|
595
|
+
return old?.id ?? newId();
|
|
596
|
+
})(),
|
|
597
|
+
title: o.title,
|
|
598
|
+
description: o.description ?? null,
|
|
599
|
+
channel_ids: (o.channels ?? []).map(channelId),
|
|
600
|
+
role_ids: (o.roles ?? []).map(roleId),
|
|
601
|
+
emoji_name: o.emoji ?? null
|
|
602
|
+
}))
|
|
603
|
+
};
|
|
604
|
+
});
|
|
566
605
|
const defaultChannels = onboarding.defaultChannels === void 0 ? current.default_channel_ids : onboarding.defaultChannels.map(channelId);
|
|
567
606
|
const enabled = onboarding.enabled ?? current.enabled;
|
|
568
607
|
const updated = [];
|
|
@@ -837,15 +876,18 @@ var DiscordNotifications = class DiscordNotifications {
|
|
|
837
876
|
const channel = DiscordNotifications.findChannel(await this.channelList(), input.channel, parent);
|
|
838
877
|
if (!channel) throw new Error(`no channel named \`${input.channel}\` — create it first`);
|
|
839
878
|
const existing = (await bot.channels.webhooks(channel.id)).find((w) => DiscordNotifications.same(w.name ?? "", input.name));
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
const made = await bot.channels.createWebhook(channel.id, { name: input.name });
|
|
845
|
-
return {
|
|
846
|
-
created: true,
|
|
847
|
-
...made.url ? { url: made.url } : {}
|
|
879
|
+
const hook = existing ?? await bot.channels.createWebhook(channel.id, { name: input.name });
|
|
880
|
+
const result = {
|
|
881
|
+
created: !existing,
|
|
882
|
+
...hook.url ? { url: hook.url } : {}
|
|
848
883
|
};
|
|
884
|
+
if (input.avatar !== void 0 && !hook.avatar) try {
|
|
885
|
+
await bot.webhooks.modify(hook.id, { avatar: input.avatar });
|
|
886
|
+
result.updated = ["avatar"];
|
|
887
|
+
} catch (err) {
|
|
888
|
+
result.avatarError = reason(err);
|
|
889
|
+
}
|
|
890
|
+
return result;
|
|
849
891
|
}
|
|
850
892
|
async ensureRole(input) {
|
|
851
893
|
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.
|
|
3
|
+
"version": "5.0.0-alpha.197",
|
|
4
4
|
"description": "Holocron plugin for Discord. Implements the notifications capability via Discord incoming webhooks.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"discord",
|
|
@@ -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.
|
|
51
|
+
"@theholocron/cli": "5.0.0-alpha.197"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"@theholocron/discord-client": "^1.44.0",
|
|
55
55
|
"@theholocron/misc-utils": "^1.16.0",
|
|
56
|
-
"@theholocron/cli": "5.0.0-alpha.
|
|
56
|
+
"@theholocron/cli": "5.0.0-alpha.197"
|
|
57
57
|
},
|
|
58
58
|
"engines": {
|
|
59
59
|
"node": ">=22"
|