@theholocron/holocron-plugin-discord 5.0.0-alpha.204 → 5.0.0-alpha.206
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 +21 -1
- package/dist/index.mjs +137 -3
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AuthError, EnsureWebhookResult, EnsuredResource, Notifications, NotificationsAccess, NotificationsApplicationLayout, NotificationsDelivery, NotificationsEveryoneLayout, NotificationsLayout, NotificationsMember, NotificationsOnboardingLayout, NotificationsServerLayout, NotificationsSignIn, NotificationsWelcomeLayout, ResolveTokenInput, ServerInspection, VerifyTokenFailure, VerifyTokenResult, VerifyTokenResult as VerifyTokenResult$1, VerifyTokenSuccess } from "@theholocron/cli";
|
|
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
2
|
import { DiscordBotClient, DiscordClient, DiscordClientOptions, DiscordOAuthClient, DiscordWebhookInfo, createDiscordClient, parseWebhookUrl } from "@theholocron/discord-client";
|
|
3
3
|
//#region src/auth.d.ts
|
|
4
4
|
declare const resolveToken: (input?: ResolveTokenInput) => string;
|
|
@@ -66,6 +66,8 @@ declare class DiscordNotifications implements Notifications {
|
|
|
66
66
|
* the layout, not with the server.
|
|
67
67
|
*/
|
|
68
68
|
inspect(layout: NotificationsLayout): Promise<ServerInspection>;
|
|
69
|
+
/** What the bot keeps: the usual set, and Manage Messages when the layout pins messages. */
|
|
70
|
+
private needs;
|
|
69
71
|
/** Where to re-invite the bot so its role holds what `setup` needs. Re-authorising updates the role in place. */
|
|
70
72
|
private inviteUrl;
|
|
71
73
|
/** Changing server settings needs Manage Server; an empty edit proves the bot has it without changing anything. */
|
|
@@ -85,6 +87,24 @@ declare class DiscordNotifications implements Notifications {
|
|
|
85
87
|
ensureServer(settings: NotificationsServerLayout): Promise<{
|
|
86
88
|
updated: string[];
|
|
87
89
|
}>;
|
|
90
|
+
holdsPermission(permission: string): Promise<boolean>;
|
|
91
|
+
/** The messages this bot posted in a channel, pinned or not, among the channel's last hundred. */
|
|
92
|
+
private ownMessages;
|
|
93
|
+
/** The layout's messages grouped by channel, in the order the channels first appear. */
|
|
94
|
+
private static pinsByChannel;
|
|
95
|
+
/**
|
|
96
|
+
* What differs in one channel: the declared messages that are not pinned yet, and the bot's pinned messages the
|
|
97
|
+
* layout does not declare (or declares once and the bot pinned twice). The layout is the whole set of the bot's
|
|
98
|
+
* pins in a channel it names.
|
|
99
|
+
*/
|
|
100
|
+
private static pinDrift;
|
|
101
|
+
/** 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. */
|
|
102
|
+
private pinnedStates;
|
|
103
|
+
ensurePinned(pinned: NotificationsPinnedLayout[]): Promise<{
|
|
104
|
+
updated: string[];
|
|
105
|
+
}>;
|
|
106
|
+
/** A 403 on pinning becomes the missing permission and a link that grants it. */
|
|
107
|
+
private pinHint;
|
|
88
108
|
/** A 403 on the welcome screen or onboarding becomes the likely reason; anything else is passed on. */
|
|
89
109
|
private static communityFeatureHint;
|
|
90
110
|
/** The numeric code Discord puts in the JSON body of an error, or undefined when the body is not that. */
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AuthError, ProviderApiError, createResolveToken } from "@theholocron/cli";
|
|
1
|
+
import { AuthError, ElevationRequiredError, ProviderApiError, createResolveToken } from "@theholocron/cli";
|
|
2
2
|
import { errorMessage } from "@theholocron/misc-utils";
|
|
3
3
|
import { ApplicationFlag, ChannelType, createDiscordBotClient, createDiscordClient, createDiscordOAuthClient, findVerifiedConnection, parseWebhookUrl, permissionNames, permissions } from "@theholocron/discord-client";
|
|
4
4
|
//#region src/auth.ts
|
|
@@ -344,6 +344,13 @@ var DiscordNotifications = class DiscordNotifications {
|
|
|
344
344
|
} catch (err) {
|
|
345
345
|
serverError = reason(err);
|
|
346
346
|
}
|
|
347
|
+
let pinned;
|
|
348
|
+
let pinnedError;
|
|
349
|
+
if (layout.pinned) try {
|
|
350
|
+
pinned = await this.pinnedStates(layout.pinned);
|
|
351
|
+
} catch (err) {
|
|
352
|
+
pinnedError = reason(err);
|
|
353
|
+
}
|
|
347
354
|
let welcome;
|
|
348
355
|
let welcomeError;
|
|
349
356
|
if (layout.welcome) try {
|
|
@@ -428,6 +435,8 @@ var DiscordNotifications = class DiscordNotifications {
|
|
|
428
435
|
...serverError ? { serverError } : {},
|
|
429
436
|
...welcome ? { welcome } : {},
|
|
430
437
|
...welcomeError ? { welcomeError } : {},
|
|
438
|
+
...pinned ? { pinned } : {},
|
|
439
|
+
...pinnedError ? { pinnedError } : {},
|
|
431
440
|
...onboarding ? { onboarding } : {},
|
|
432
441
|
...onboardingError ? { onboardingError } : {},
|
|
433
442
|
...application ? { application } : {},
|
|
@@ -439,8 +448,12 @@ var DiscordNotifications = class DiscordNotifications {
|
|
|
439
448
|
...rolesError ? { rolesError } : {}
|
|
440
449
|
};
|
|
441
450
|
}
|
|
451
|
+
/** What the bot keeps: the usual set, and Manage Messages when the layout pins messages. */
|
|
452
|
+
needs() {
|
|
453
|
+
return this.opts.layout?.pinned?.length ? [...BOT_NEEDS, "ManageMessages"] : BOT_NEEDS;
|
|
454
|
+
}
|
|
442
455
|
/** Where to re-invite the bot so its role holds what `setup` needs. Re-authorising updates the role in place. */
|
|
443
|
-
async inviteUrl(names =
|
|
456
|
+
async inviteUrl(names = this.needs()) {
|
|
444
457
|
const url = new URL("https://discord.com/oauth2/authorize");
|
|
445
458
|
url.searchParams.set("client_id", await this.botId());
|
|
446
459
|
url.searchParams.set("scope", "bot");
|
|
@@ -489,7 +502,13 @@ var DiscordNotifications = class DiscordNotifications {
|
|
|
489
502
|
const link = await this.inviteOrNothing(["Administrator"]);
|
|
490
503
|
const byHand = turningOn ? "turn them on once in Discord (Server Settings → Enable Community; setup then keeps everything else in line)" : "turn them off once in Discord (Server Settings → Community Settings → Disable Community)";
|
|
491
504
|
const viaBot = link ? `give Holonet Administrator for one run and take it away again afterwards: ${link}` : "re-invite Holonet with the Administrator permission for one run, and take it away again afterwards";
|
|
492
|
-
|
|
505
|
+
const revoke = await this.inviteOrNothing();
|
|
506
|
+
return new ElevationRequiredError(`${appliedPrefix(applied)}turning community features ${turningOn ? "on" : "off"} was refused (Discord said: ${String(err.details).slice(0, 120)}), which means Holonet lacks the Administrator permission it needs for that. Either ${byHand}, or ${viaBot}. If Holonet already has Administrator, check that it is still in the server`, {
|
|
507
|
+
permission: "Administrator",
|
|
508
|
+
...link ? { grantUrl: link } : {},
|
|
509
|
+
...revoke ? { revokeUrl: revoke } : {},
|
|
510
|
+
...applied.length > 0 ? { applied } : {}
|
|
511
|
+
}, { cause: err });
|
|
493
512
|
}
|
|
494
513
|
/** A 403 on a server edit becomes the explanation and the invite link; anything else is passed on as it is. */
|
|
495
514
|
async manageServerHint(err) {
|
|
@@ -580,6 +599,119 @@ var DiscordNotifications = class DiscordNotifications {
|
|
|
580
599
|
}
|
|
581
600
|
return { updated };
|
|
582
601
|
}
|
|
602
|
+
async holdsPermission(permission) {
|
|
603
|
+
const bot = this.bot();
|
|
604
|
+
const me = await this.botId();
|
|
605
|
+
if ((await this.serverOwner()).isBot) return true;
|
|
606
|
+
const [member, roles] = await Promise.all([bot.guilds.member(this.guild(), me), bot.guilds.roles(this.guild())]);
|
|
607
|
+
const held = /* @__PURE__ */ new Set([this.guild(), ...member.roles]);
|
|
608
|
+
const bits = roles.filter((r) => held.has(r.id)).reduce((all, r) => all | BigInt(r.permissions ?? "0"), 0n);
|
|
609
|
+
const has = (name) => {
|
|
610
|
+
const bit = BigInt(permissions([name]));
|
|
611
|
+
return (bits & bit) === bit;
|
|
612
|
+
};
|
|
613
|
+
return has("Administrator") || has(permission);
|
|
614
|
+
}
|
|
615
|
+
/** The messages this bot posted in a channel, pinned or not, among the channel's last hundred. */
|
|
616
|
+
async ownMessages(channelId) {
|
|
617
|
+
const me = await this.botId();
|
|
618
|
+
return (await this.bot().channels.messages(channelId, 100)).filter((m) => m.author?.id === me).map((m) => ({
|
|
619
|
+
id: m.id,
|
|
620
|
+
content: m.content,
|
|
621
|
+
pinned: m.pinned === true
|
|
622
|
+
}));
|
|
623
|
+
}
|
|
624
|
+
/** The layout's messages grouped by channel, in the order the channels first appear. */
|
|
625
|
+
static pinsByChannel(pinned) {
|
|
626
|
+
const byChannel = /* @__PURE__ */ new Map();
|
|
627
|
+
for (const pin of pinned) {
|
|
628
|
+
const key = pin.channel.toLowerCase();
|
|
629
|
+
byChannel.set(key, [.../* @__PURE__ */ new Set([...byChannel.get(key) ?? [], pin.content])]);
|
|
630
|
+
}
|
|
631
|
+
return byChannel;
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* What differs in one channel: the declared messages that are not pinned yet, and the bot's pinned messages the
|
|
635
|
+
* layout does not declare (or declares once and the bot pinned twice). The layout is the whole set of the bot's
|
|
636
|
+
* pins in a channel it names.
|
|
637
|
+
*/
|
|
638
|
+
static pinDrift(own, wanted) {
|
|
639
|
+
const pinned = own.filter((m) => m.pinned).sort((a, b) => BigInt(a.id) < BigInt(b.id) ? -1 : 1);
|
|
640
|
+
const kept = /* @__PURE__ */ new Set();
|
|
641
|
+
const extras = pinned.filter((m) => {
|
|
642
|
+
if (!wanted.includes(m.content) || kept.has(m.content)) return true;
|
|
643
|
+
kept.add(m.content);
|
|
644
|
+
return false;
|
|
645
|
+
});
|
|
646
|
+
return {
|
|
647
|
+
missing: wanted.filter((c) => !kept.has(c)),
|
|
648
|
+
extras
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
/** 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. */
|
|
652
|
+
async pinnedStates(pinned) {
|
|
653
|
+
const channels = await this.channelList();
|
|
654
|
+
const verdicts = /* @__PURE__ */ new Map();
|
|
655
|
+
for (const [channel, wanted] of DiscordNotifications.pinsByChannel(pinned)) {
|
|
656
|
+
const own = await this.ownMessages(DiscordNotifications.pickChannelId(channels, channel, "pinned messages"));
|
|
657
|
+
const drift = DiscordNotifications.pinDrift(own, wanted);
|
|
658
|
+
verdicts.set(channel, {
|
|
659
|
+
missing: new Set(drift.missing),
|
|
660
|
+
extras: drift.extras.length,
|
|
661
|
+
anyPinned: own.some((m) => m.pinned)
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
return pinned.map((pin) => {
|
|
665
|
+
const v = verdicts.get(pin.channel.toLowerCase());
|
|
666
|
+
const state = v.missing.has(pin.content) ? v.anyPinned ? "different" : "missing" : v.extras > 0 ? "different" : "ok";
|
|
667
|
+
return {
|
|
668
|
+
channel: pin.channel,
|
|
669
|
+
state
|
|
670
|
+
};
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
async ensurePinned(pinned) {
|
|
674
|
+
const bot = this.bot();
|
|
675
|
+
const channels = await this.channelList();
|
|
676
|
+
const updated = [];
|
|
677
|
+
for (const [channel, wanted] of DiscordNotifications.pinsByChannel(pinned)) {
|
|
678
|
+
const channelId = DiscordNotifications.pickChannelId(channels, channel, "pinned messages");
|
|
679
|
+
const own = await this.ownMessages(channelId);
|
|
680
|
+
const drift = DiscordNotifications.pinDrift(own, wanted);
|
|
681
|
+
if (drift.missing.length === 0 && drift.extras.length === 0) continue;
|
|
682
|
+
let extras = drift.extras;
|
|
683
|
+
for (const content of drift.missing) {
|
|
684
|
+
const orphan = own.find((m) => !m.pinned && m.content === content);
|
|
685
|
+
const posted = orphan ?? await bot.channels.createMessage(channelId, { content });
|
|
686
|
+
try {
|
|
687
|
+
try {
|
|
688
|
+
await bot.channels.pin(channelId, posted.id);
|
|
689
|
+
} catch (err) {
|
|
690
|
+
if (!(err instanceof ProviderApiError && /\b30003\b/.test(String(err.details))) || extras.length === 0) throw err;
|
|
691
|
+
for (const old of extras) await bot.channels.deleteMessage(channelId, old.id);
|
|
692
|
+
extras = [];
|
|
693
|
+
await bot.channels.pin(channelId, posted.id);
|
|
694
|
+
}
|
|
695
|
+
} catch (err) {
|
|
696
|
+
if (!orphan) await bot.channels.deleteMessage(channelId, posted.id).catch(() => void 0);
|
|
697
|
+
throw await this.pinHint(err, channel);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
for (const old of extras) await bot.channels.deleteMessage(channelId, old.id);
|
|
701
|
+
if (drift.missing.length > 0) {
|
|
702
|
+
const after = DiscordNotifications.pinDrift(await this.ownMessages(channelId), wanted);
|
|
703
|
+
for (const dup of after.extras) await bot.channels.deleteMessage(channelId, dup.id);
|
|
704
|
+
}
|
|
705
|
+
updated.push(`#${channel}`);
|
|
706
|
+
}
|
|
707
|
+
return { updated };
|
|
708
|
+
}
|
|
709
|
+
/** A 403 on pinning becomes the missing permission and a link that grants it. */
|
|
710
|
+
async pinHint(err, channel) {
|
|
711
|
+
if (!DiscordNotifications.missingPermissions(err)) return err;
|
|
712
|
+
const link = await this.inviteOrNothing([...BOT_NEEDS, "ManageMessages"]);
|
|
713
|
+
return new Error(`the bot could not pin the message in #${channel}: its role lacks Manage Messages. Re-invite it with that permission${link ? ` (this updates its role in place): ${link}` : ""}`, { cause: err });
|
|
714
|
+
}
|
|
583
715
|
/** A 403 on the welcome screen or onboarding becomes the likely reason; anything else is passed on. */
|
|
584
716
|
static communityFeatureHint(err, what) {
|
|
585
717
|
if (!(err instanceof ProviderApiError && err.status === 403)) return err;
|
|
@@ -851,6 +983,8 @@ var DiscordNotifications = class DiscordNotifications {
|
|
|
851
983
|
const long = welcome.channels.find((c) => c.description.length > 50);
|
|
852
984
|
if (long) return `welcome: the description of \`${long.channel}\` is ${long.description.length} characters; Discord allows up to 50`;
|
|
853
985
|
}
|
|
986
|
+
const long2000 = layout.pinned?.find((p) => p.content.length > 2e3);
|
|
987
|
+
if (long2000) return `pinned: the message for \`${long2000.channel}\` is ${long2000.content.length} characters; Discord allows up to 2000`;
|
|
854
988
|
const server = layout.server;
|
|
855
989
|
for (const [field, map] of [
|
|
856
990
|
["verification", VERIFICATION],
|
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.206",
|
|
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.206"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"@theholocron/discord-client": "^1.49.0",
|
|
55
55
|
"@theholocron/misc-utils": "^1.16.0",
|
|
56
|
-
"@theholocron/cli": "5.0.0-alpha.
|
|
56
|
+
"@theholocron/cli": "5.0.0-alpha.206"
|
|
57
57
|
},
|
|
58
58
|
"engines": {
|
|
59
59
|
"node": ">=22"
|