@theholocron/holocron-plugin-discord 5.0.0-alpha.203 → 5.0.0-alpha.205

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
@@ -183,6 +185,23 @@ declare class DiscordNotifications implements Notifications {
183
185
  text: string;
184
186
  keep?: boolean;
185
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
+ }>;
186
205
  /** The one role with this name, or an error that says what to fix: none, or more than one. */
187
206
  private roleNamed;
188
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;
@@ -1181,6 +1184,65 @@ var DiscordNotifications = class DiscordNotifications {
1181
1184
  }
1182
1185
  return result;
1183
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
+ }
1184
1246
  /** The one role with this name, or an error that says what to fix: none, or more than one. */
1185
1247
  async roleNamed(name) {
1186
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.203",
3
+ "version": "5.0.0-alpha.205",
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.203"
51
+ "@theholocron/cli": "5.0.0-alpha.205"
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.203"
56
+ "@theholocron/cli": "5.0.0-alpha.205"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">=22"