@theholocron/holocron-plugin-discord 5.0.0-alpha.185 → 5.0.0-alpha.187
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 +7 -1
- package/dist/index.mjs +60 -6
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AuthError, EnsureWebhookResult, EnsuredResource, Notifications, NotificationsLayout, ResolveTokenInput, VerifyTokenFailure, VerifyTokenResult, VerifyTokenResult as VerifyTokenResult$1, VerifyTokenSuccess } from "@theholocron/cli";
|
|
1
|
+
import { AuthError, EnsureWebhookResult, EnsuredResource, Notifications, NotificationsLayout, 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;
|
|
@@ -46,6 +46,12 @@ declare class DiscordNotifications implements Notifications {
|
|
|
46
46
|
whoami(): Promise<{
|
|
47
47
|
target: string;
|
|
48
48
|
} | null>;
|
|
49
|
+
/**
|
|
50
|
+
* Read-only: what is on the server, for the parts of `layout` we will compare. Webhooks are
|
|
51
|
+
* listed only for channels the layout gives one (one request each), so the cost scales with
|
|
52
|
+
* the layout, not with the server.
|
|
53
|
+
*/
|
|
54
|
+
inspect(layout: NotificationsLayout): Promise<ServerInspection>;
|
|
49
55
|
private guild;
|
|
50
56
|
/**
|
|
51
57
|
* The server's channel list, fetched once per instance. Every ensure* call used
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { AuthError, createResolveToken } from "@theholocron/cli";
|
|
2
|
-
import { ChannelType, createDiscordBotClient, createDiscordClient, parseWebhookUrl } from "@theholocron/discord-client";
|
|
1
|
+
import { AuthError, ProviderApiError, createResolveToken } from "@theholocron/cli";
|
|
3
2
|
import { errorMessage } from "@theholocron/misc-utils";
|
|
3
|
+
import { ChannelType, createDiscordBotClient, createDiscordClient, parseWebhookUrl } from "@theholocron/discord-client";
|
|
4
4
|
//#region src/auth.ts
|
|
5
5
|
const resolveToken = createResolveToken({
|
|
6
6
|
envName: "HOLOCRON_DISCORD_WEBHOOK",
|
|
@@ -16,6 +16,10 @@ const resolveBotToken = createResolveToken({
|
|
|
16
16
|
});
|
|
17
17
|
//#endregion
|
|
18
18
|
//#region src/capabilities/notifications.ts
|
|
19
|
+
/** An error as one line: the message, plus Discord's own response body when there is one. */
|
|
20
|
+
function reason(err) {
|
|
21
|
+
return err instanceof ProviderApiError && err.details ? `${errorMessage(err)}: ${String(err.details).slice(0, 200)}` : errorMessage(err);
|
|
22
|
+
}
|
|
19
23
|
var DiscordNotifications = class DiscordNotifications {
|
|
20
24
|
client;
|
|
21
25
|
opts;
|
|
@@ -54,6 +58,53 @@ var DiscordNotifications = class DiscordNotifications {
|
|
|
54
58
|
const me = await bot.users.me();
|
|
55
59
|
return { target: `${(await bot.guilds.get(this.opts.guildId)).name} (as ${me.username})` };
|
|
56
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Read-only: what is on the server, for the parts of `layout` we will compare. Webhooks are
|
|
63
|
+
* listed only for channels the layout gives one (one request each), so the cost scales with
|
|
64
|
+
* the layout, not with the server.
|
|
65
|
+
*/
|
|
66
|
+
async inspect(layout) {
|
|
67
|
+
const bot = this.bot();
|
|
68
|
+
const channels = await this.channelList();
|
|
69
|
+
const categoryName = new Map(channels.filter((c) => c.type === ChannelType.Category).map((c) => [c.id, c.name]));
|
|
70
|
+
const wantsWebhook = new Set((layout.categories ?? []).flatMap((c) => (c.channels ?? []).filter((ch) => ch.webhook).map((ch) => ch.name.toLowerCase())));
|
|
71
|
+
const text = channels.filter((c) => c.type === ChannelType.Text || c.type === ChannelType.Announcement);
|
|
72
|
+
const found = [];
|
|
73
|
+
for (const c of text) {
|
|
74
|
+
let webhooks = [];
|
|
75
|
+
let webhooksError;
|
|
76
|
+
if (wantsWebhook.has(c.name.toLowerCase())) try {
|
|
77
|
+
webhooks = (await bot.channels.webhooks(c.id)).map((w) => w.name ?? "");
|
|
78
|
+
} catch (err) {
|
|
79
|
+
webhooksError = reason(err);
|
|
80
|
+
}
|
|
81
|
+
const category = c.parent_id ? categoryName.get(c.parent_id) : void 0;
|
|
82
|
+
found.push({
|
|
83
|
+
name: c.name,
|
|
84
|
+
...category ? { category } : {},
|
|
85
|
+
...c.topic ? { topic: c.topic } : {},
|
|
86
|
+
webhooks,
|
|
87
|
+
...webhooksError ? { webhooksError } : {}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
let roles = [];
|
|
91
|
+
let rolesError;
|
|
92
|
+
if ((layout.roles ?? []).length > 0) try {
|
|
93
|
+
roles = (await bot.guilds.roles(this.guild())).map((r) => ({
|
|
94
|
+
name: r.name,
|
|
95
|
+
id: r.id,
|
|
96
|
+
mentionable: r.mentionable
|
|
97
|
+
}));
|
|
98
|
+
} catch (err) {
|
|
99
|
+
rolesError = reason(err);
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
categories: [...categoryName.values()],
|
|
103
|
+
channels: found,
|
|
104
|
+
roles,
|
|
105
|
+
...rolesError ? { rolesError } : {}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
57
108
|
guild() {
|
|
58
109
|
if (!this.opts.guildId) throw new Error("no Discord server id — set `guildId` in the discord provider options in holocron.config.ts");
|
|
59
110
|
return this.opts.guildId;
|
|
@@ -171,10 +222,13 @@ var DiscordNotifications = class DiscordNotifications {
|
|
|
171
222
|
async ensureRole(input) {
|
|
172
223
|
const bot = this.bot();
|
|
173
224
|
const existing = (await bot.guilds.roles(this.guild())).find((r) => DiscordNotifications.same(r.name, input.name));
|
|
174
|
-
if (existing)
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
225
|
+
if (existing) {
|
|
226
|
+
if (input.mentionable !== void 0 && existing.mentionable !== input.mentionable) throw new Error(`role \`${input.name}\` exists with mentionable=${existing.mentionable} but the layout wants ${input.mentionable}; updating an existing role is not supported yet — change it in Discord (Role → Allow anyone to @mention this role)`);
|
|
227
|
+
return {
|
|
228
|
+
id: existing.id,
|
|
229
|
+
created: false
|
|
230
|
+
};
|
|
231
|
+
}
|
|
178
232
|
return {
|
|
179
233
|
id: (await bot.guilds.createRole(this.guild(), {
|
|
180
234
|
name: input.name,
|
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.187",
|
|
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.187"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"@theholocron/discord-client": "^1.43.1",
|
|
55
55
|
"@theholocron/misc-utils": "^1.16.0",
|
|
56
|
-
"@theholocron/cli": "5.0.0-alpha.
|
|
56
|
+
"@theholocron/cli": "5.0.0-alpha.187"
|
|
57
57
|
},
|
|
58
58
|
"engines": {
|
|
59
59
|
"node": ">=22"
|