@theholocron/holocron-plugin-discord 5.0.0-alpha.202 → 5.0.0-alpha.204

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
@@ -127,6 +127,8 @@ declare class DiscordNotifications implements Notifications {
127
127
  /** The server's roles, fetched once and forgotten whenever setup changes a role. */
128
128
  private roleLoad?;
129
129
  private roleList;
130
+ /** The server this run created, which takes over from `guildId` for the rest of the run. */
131
+ private createdGuild?;
130
132
  private guild;
131
133
  /**
132
134
  * The server's channel list, fetched once per instance. Every ensure* call used
@@ -181,7 +183,25 @@ declare class DiscordNotifications implements Notifications {
181
183
  name: string;
182
184
  category?: string;
183
185
  text: string;
186
+ keep?: boolean;
184
187
  }): Promise<NotificationsDelivery>;
188
+ createServer(input: {
189
+ name: string;
190
+ force?: boolean;
191
+ }): Promise<{
192
+ id: string;
193
+ name: string;
194
+ }>;
195
+ /** A server this bot owns (not one it only belongs to) with exactly this name, read across every page. */
196
+ private botOwnedServer;
197
+ inviteLink(channelName: string): Promise<string>;
198
+ hasMember(memberId: string): Promise<boolean>;
199
+ transferServer(memberId: string): Promise<void>;
200
+ serverOwner(): Promise<{
201
+ id: string;
202
+ isBot: boolean;
203
+ name?: string;
204
+ }>;
185
205
  /** The one role with this name, or an error that says what to fix: none, or more than one. */
186
206
  private roleNamed;
187
207
  roleMembers(roleName: string): Promise<NotificationsMember[]>;
package/dist/index.mjs CHANGED
@@ -953,7 +953,10 @@ var DiscordNotifications = class DiscordNotifications {
953
953
  throw err;
954
954
  });
955
955
  }
956
+ /** The server this run created, which takes over from `guildId` for the rest of the run. */
957
+ createdGuild;
956
958
  guild() {
959
+ if (this.createdGuild) return this.createdGuild;
957
960
  if (!this.opts.guildId) throw new Error("no Discord server id — set `guildId` in the discord provider options in holocron.config.ts");
958
961
  if (!/^\d{1,20}$/.test(this.opts.guildId)) throw new Error(`\`guildId\` ${JSON.stringify(this.opts.guildId)} is not a Discord server id (digits only)`);
959
962
  return this.opts.guildId;
@@ -1120,6 +1123,7 @@ var DiscordNotifications = class DiscordNotifications {
1120
1123
  if (this.opts.baseUrl && !this.opts.unsafeBotBaseUrl) return fail("refusing to send a webhook token to a custom `baseUrl`; remove it, or set `unsafeBotBaseUrl` for a test server");
1121
1124
  let bot;
1122
1125
  let hook;
1126
+ let text = input.text;
1123
1127
  try {
1124
1128
  bot = this.bot();
1125
1129
  const channels = await this.channelList();
@@ -1139,18 +1143,23 @@ var DiscordNotifications = class DiscordNotifications {
1139
1143
  token: usable.token,
1140
1144
  channelId: channel.id
1141
1145
  };
1146
+ text = input.text.replaceAll("{channel}", `<#${channel.id}>`);
1142
1147
  } catch (err) {
1143
1148
  return fail(reason(err));
1144
1149
  }
1145
1150
  let messageId;
1146
1151
  try {
1147
- messageId = (await this.client.webhooks.executeAndWait(hook.id, hook.token, input.text)).id;
1152
+ messageId = (await this.client.webhooks.executeAndWait(hook.id, hook.token, text)).id;
1148
1153
  result.posted = true;
1149
1154
  } catch (err) {
1150
1155
  result.problems.push(`the post was refused (${reason(err)})`);
1151
1156
  try {
1152
- const stray = (await bot.channels.messages(hook.channelId, 100)).find((m) => m.webhook_id === hook.id && m.content === input.text);
1153
- if (stray) {
1157
+ const stray = (await bot.channels.messages(hook.channelId, 100)).find((m) => m.webhook_id === hook.id && m.content === text);
1158
+ if (stray && input.keep) {
1159
+ result.posted = true;
1160
+ result.kept = true;
1161
+ result.problems.push(`the test message ${stray.id} arrived but its response was lost; it was left in place`);
1162
+ } else if (stray) {
1154
1163
  await bot.channels.deleteMessage(hook.channelId, stray.id);
1155
1164
  result.problems.push(`the test message ${stray.id} had been posted, and was removed`);
1156
1165
  }
@@ -1163,6 +1172,10 @@ var DiscordNotifications = class DiscordNotifications {
1163
1172
  } catch (err) {
1164
1173
  result.problems.push(`could not read the channel back (${reason(err)})`);
1165
1174
  }
1175
+ if (input.keep) {
1176
+ result.kept = true;
1177
+ return result;
1178
+ }
1166
1179
  try {
1167
1180
  await this.client.webhooks.deleteMessage(hook.id, hook.token, messageId);
1168
1181
  result.removed = true;
@@ -1171,6 +1184,65 @@ var DiscordNotifications = class DiscordNotifications {
1171
1184
  }
1172
1185
  return result;
1173
1186
  }
1187
+ async createServer(input) {
1188
+ if (this.createdGuild) {
1189
+ const existing = await this.bot().guilds.get(this.createdGuild);
1190
+ return {
1191
+ id: existing.id,
1192
+ name: existing.name
1193
+ };
1194
+ }
1195
+ if (this.opts.guildId && !input.force) throw new Error(`this config already names a server (\`guildId\` ${this.opts.guildId}); pass --force to create another one`);
1196
+ const guild = await this.botOwnedServer(input.name) ?? await this.bot().guilds.create({ name: input.name });
1197
+ this.createdGuild = guild.id;
1198
+ this.channelLoad = void 0;
1199
+ return {
1200
+ id: guild.id,
1201
+ name: guild.name
1202
+ };
1203
+ }
1204
+ /** A server this bot owns (not one it only belongs to) with exactly this name, read across every page. */
1205
+ async botOwnedServer(name) {
1206
+ let after;
1207
+ for (;;) {
1208
+ const page = await this.bot().users.guilds(after ? { after } : {});
1209
+ const hit = page.find((g) => g.owner && g.name === name);
1210
+ if (hit) return hit;
1211
+ const last = page[page.length - 1];
1212
+ if (page.length < 200 || !last) return void 0;
1213
+ after = last.id;
1214
+ }
1215
+ }
1216
+ async inviteLink(channelName) {
1217
+ const channel = DiscordNotifications.findChannel(await this.channelList(), channelName);
1218
+ if (!channel) throw new Error(`no channel named \`${channelName}\` to invite into`);
1219
+ return `https://discord.gg/${(await this.bot().channels.createInvite(channel.id, {
1220
+ max_age: 86400,
1221
+ max_uses: 1,
1222
+ unique: true
1223
+ })).code}`;
1224
+ }
1225
+ async hasMember(memberId) {
1226
+ try {
1227
+ await this.bot().guilds.member(this.guild(), memberId);
1228
+ return true;
1229
+ } catch (err) {
1230
+ if (err instanceof ProviderApiError && err.status === 404) return false;
1231
+ throw err;
1232
+ }
1233
+ }
1234
+ async transferServer(memberId) {
1235
+ await this.bot().guilds.modify(this.guild(), { owner_id: memberId });
1236
+ }
1237
+ async serverOwner() {
1238
+ const guild = await this.bot().guilds.get(this.guild());
1239
+ if (!guild.owner_id) throw new Error("Discord did not say who owns the server");
1240
+ return {
1241
+ id: guild.owner_id,
1242
+ isBot: guild.owner_id === await this.botId(),
1243
+ ...guild.name ? { name: guild.name } : {}
1244
+ };
1245
+ }
1174
1246
  /** The one role with this name, or an error that says what to fix: none, or more than one. */
1175
1247
  async roleNamed(name) {
1176
1248
  const matches = (await this.bot().guilds.roles(this.guild())).filter((r) => DiscordNotifications.same(r.name, name));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/holocron-plugin-discord",
3
- "version": "5.0.0-alpha.202",
3
+ "version": "5.0.0-alpha.204",
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.48.0",
33
+ "@theholocron/discord-client": "^1.49.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.202"
51
+ "@theholocron/cli": "5.0.0-alpha.204"
52
52
  },
53
53
  "peerDependencies": {
54
- "@theholocron/discord-client": "^1.48.0",
54
+ "@theholocron/discord-client": "^1.49.0",
55
55
  "@theholocron/misc-utils": "^1.16.0",
56
- "@theholocron/cli": "5.0.0-alpha.202"
56
+ "@theholocron/cli": "5.0.0-alpha.204"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">=22"