@fluxerjs/core 3.0.0 → 3.1.0

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.mjs CHANGED
@@ -611,6 +611,13 @@ function toSudoBody(options) {
611
611
  return body;
612
612
  }
613
613
 
614
+ // src/Helpers/AuditReason.ts
615
+ var AUDIT_LOG_REASON_HEADER = "X-Audit-Log-Reason";
616
+ function auditReasonHeaders(reason) {
617
+ if (reason == null || reason === "") return {};
618
+ return { headers: { [AUDIT_LOG_REASON_HEADER]: encodeURIComponent(reason) } };
619
+ }
620
+
614
621
  // src/Helpers/MessageUtils/Body.ts
615
622
  import { AttachmentBuilder, EmbedBuilder, MessagePayload } from "@fluxerjs/builders";
616
623
  import { MessageAttachmentFlags, MessageReferenceType } from "@fluxerjs/types";
@@ -1265,6 +1272,67 @@ var CDN_URL = "https://fluxerusercontent.com";
1265
1272
  var STATIC_CDN_URL = "https://fluxerstatic.com";
1266
1273
 
1267
1274
  // src/Helpers/Cdn.ts
1275
+ var CDN_SIZE_LADDER = [
1276
+ 16,
1277
+ 20,
1278
+ 22,
1279
+ 24,
1280
+ 28,
1281
+ 32,
1282
+ 40,
1283
+ 44,
1284
+ 48,
1285
+ 56,
1286
+ 60,
1287
+ 64,
1288
+ 80,
1289
+ 96,
1290
+ 100,
1291
+ 128,
1292
+ 160,
1293
+ 240,
1294
+ 256,
1295
+ 300,
1296
+ 320,
1297
+ 480,
1298
+ 512,
1299
+ 600,
1300
+ 640,
1301
+ 1024,
1302
+ 1280,
1303
+ 1536,
1304
+ 2048,
1305
+ 3072,
1306
+ 4096,
1307
+ 8192,
1308
+ 16384
1309
+ ];
1310
+ var CDN_ASSET_CLASS_RANGE = {
1311
+ icon: { min: 128, max: 1024 },
1312
+ banner: { min: 480, max: 2400 },
1313
+ emoji: { min: 32, max: 512 },
1314
+ sticker: { min: 128, max: 512 }
1315
+ };
1316
+ function nearestSize(requested, candidates) {
1317
+ let best = candidates[0];
1318
+ let bestDist = Math.abs(requested - best);
1319
+ for (let i = 1; i < candidates.length; i++) {
1320
+ const step = candidates[i];
1321
+ const dist = Math.abs(requested - step);
1322
+ if (dist < bestDist || dist === bestDist && step > best) {
1323
+ best = step;
1324
+ bestDist = dist;
1325
+ }
1326
+ }
1327
+ return best;
1328
+ }
1329
+ function snapCdnSize(requested, assetClass) {
1330
+ const { min, max } = CDN_ASSET_CLASS_RANGE[assetClass];
1331
+ const snapped = nearestSize(requested, CDN_SIZE_LADDER);
1332
+ const clamped = Math.min(max, Math.max(min, snapped));
1333
+ const allowed = CDN_SIZE_LADDER.filter((step) => step >= min && step <= max);
1334
+ return nearestSize(clamped, allowed);
1335
+ }
1268
1336
  function mediaBase(options) {
1269
1337
  return (options?.mediaBase ?? CDN_URL).replace(/\/+$/, "");
1270
1338
  }
@@ -1276,13 +1344,15 @@ function getExtension(hash, options) {
1276
1344
  if (hash?.startsWith("a_")) return "gif";
1277
1345
  return ext;
1278
1346
  }
1279
- function appendSize(options) {
1280
- return options?.size ? `?size=${options.size}` : "";
1347
+ function appendSize(options, assetClass) {
1348
+ const size = options?.size;
1349
+ if (size == null || !Number.isFinite(size) || size <= 0) return "";
1350
+ return `?size=${snapCdnSize(size, assetClass)}`;
1281
1351
  }
1282
1352
  function cdnAvatarURL(userId, avatarHash, options) {
1283
1353
  if (!avatarHash) return null;
1284
1354
  const ext = getExtension(avatarHash, options);
1285
- const size = appendSize(options);
1355
+ const size = appendSize(options, "icon");
1286
1356
  return `${mediaBase(options)}/avatars/${userId}/${avatarHash}.${ext}${size}`;
1287
1357
  }
1288
1358
  function cdnDisplayAvatarURL(userId, avatarHash, options) {
@@ -1291,19 +1361,19 @@ function cdnDisplayAvatarURL(userId, avatarHash, options) {
1291
1361
  function cdnBannerURL(resourceId, bannerHash, options) {
1292
1362
  if (!bannerHash) return null;
1293
1363
  const ext = getExtension(bannerHash, options);
1294
- const size = appendSize(options);
1364
+ const size = appendSize(options, "banner");
1295
1365
  return `${mediaBase(options)}/banners/${resourceId}/${bannerHash}.${ext}${size}`;
1296
1366
  }
1297
1367
  function cdnMemberAvatarURL(guildId, userId, avatarHash, options) {
1298
1368
  if (!avatarHash) return null;
1299
1369
  const ext = getExtension(avatarHash, options);
1300
- const size = appendSize(options);
1370
+ const size = appendSize(options, "icon");
1301
1371
  return `${mediaBase(options)}/guilds/${guildId}/users/${userId}/avatars/${avatarHash}.${ext}${size}`;
1302
1372
  }
1303
1373
  function cdnMemberBannerURL(guildId, userId, bannerHash, options) {
1304
1374
  if (!bannerHash) return null;
1305
1375
  const ext = getExtension(bannerHash, options);
1306
- const size = appendSize(options);
1376
+ const size = appendSize(options, "banner");
1307
1377
  return `${mediaBase(options)}/guilds/${guildId}/users/${userId}/banners/${bannerHash}.${ext}${size}`;
1308
1378
  }
1309
1379
  function cdnDefaultAvatarURL(userIdOrIndex, options) {
@@ -1312,16 +1382,19 @@ function cdnDefaultAvatarURL(userIdOrIndex, options) {
1312
1382
  }
1313
1383
  function cdnGuildAssetURL(kind, id, hash, options) {
1314
1384
  if (!hash) return null;
1315
- const size = options?.size ? `?size=${options.size}` : "";
1385
+ const assetClass = kind === "icons" ? "icon" : "banner";
1386
+ const size = appendSize(options, assetClass);
1316
1387
  return `${mediaBase(options)}/${kind}/${id}/${hash}.png${size}`;
1317
1388
  }
1318
1389
  function cdnEmojiURL(emojiId, animated, options) {
1319
1390
  const ext = animated ? "gif" : "png";
1320
- return `${mediaBase(options)}/emojis/${emojiId}.${ext}`;
1391
+ const size = appendSize(options, "emoji");
1392
+ return `${mediaBase(options)}/emojis/${emojiId}.${ext}${size}`;
1321
1393
  }
1322
1394
  function cdnStickerURL(stickerId, animated, options) {
1323
1395
  const ext = animated ? "gif" : "png";
1324
- return `${mediaBase(options)}/stickers/${stickerId}.${ext}`;
1396
+ const size = appendSize(options, "sticker");
1397
+ return `${mediaBase(options)}/stickers/${stickerId}.${ext}${size}`;
1325
1398
  }
1326
1399
 
1327
1400
  // src/Domain/Message/MessageSticker.ts
@@ -1549,6 +1622,8 @@ var Message = class _Message extends Base {
1549
1622
  mentionRoles;
1550
1623
  /** Client-side nonce for deduplication. */
1551
1624
  nonce;
1625
+ /** IDs of custom emojis in the message classified as explicit. */
1626
+ nsfwEmojis;
1552
1627
  /**
1553
1628
  * Cached text-capable channel (guild text, guild voice, or DM), or null if uncached / not text-based.
1554
1629
  * Narrowed type has {@link Channel.send} and {@link Channel.delete}.
@@ -1623,6 +1698,7 @@ var Message = class _Message extends Base {
1623
1698
  this.mentions = (data.mentions ?? []).map((u) => client.getOrCreateUser(u));
1624
1699
  this.mentionRoles = data.mention_roles ?? [];
1625
1700
  this.nonce = data.nonce ?? null;
1701
+ this.nsfwEmojis = data.nsfw_emojis ?? [];
1626
1702
  }
1627
1703
  /**
1628
1704
  * Apply an API message payload in place (cache identity / MESSAGE_UPDATE).
@@ -1655,6 +1731,7 @@ var Message = class _Message extends Base {
1655
1731
  this.mentions = (data.mentions ?? []).map((u) => this.client.getOrCreateUser(u));
1656
1732
  this.mentionRoles = data.mention_roles ?? [];
1657
1733
  this.nonce = data.nonce ?? null;
1734
+ this.nsfwEmojis = data.nsfw_emojis ?? [];
1658
1735
  }
1659
1736
  /**
1660
1737
  * Send a standalone message in this channel (not a reply).
@@ -1738,8 +1815,10 @@ var Message = class _Message extends Base {
1738
1815
  return this.client.channels.fetchMessage(this.channelId, this.id);
1739
1816
  }
1740
1817
  /** Delete this message. */
1741
- async delete() {
1742
- await this.client.rest.delete(Routes4.channelMessage(this.channelId, this.id));
1818
+ async delete(reason) {
1819
+ await this.client.rest.delete(Routes4.channelMessage(this.channelId, this.id), {
1820
+ ...auditReasonHeaders(reason)
1821
+ });
1743
1822
  this.client._removeMessageFromCache(this.channelId, this.id);
1744
1823
  }
1745
1824
  /** Delete a specific attachment from this message. */
@@ -2043,6 +2122,29 @@ var MessageCollector = class _MessageCollector extends EventEmitter2 {
2043
2122
 
2044
2123
  // src/Domain/Channel/Attachments.ts
2045
2124
  import { Routes as Routes6 } from "@fluxerjs/types";
2125
+ var BOT_ATTACHMENT_MAX_BYTES = 50 * 1024 * 1024;
2126
+ var ATTACHMENT_MULTIPART_THRESHOLD_BYTES = 10 * 1024 * 1024;
2127
+ function instanceLimitOverridesSnapshot(client) {
2128
+ const rules = client.instance.discovery?.limits?.rules;
2129
+ if (!rules?.length) return null;
2130
+ const snapshot = {};
2131
+ for (const rule of rules) {
2132
+ Object.assign(snapshot, rule.overrides);
2133
+ }
2134
+ return Object.keys(snapshot).length ? snapshot : null;
2135
+ }
2136
+ function assertBotAttachmentLimits(client, files) {
2137
+ const snapshot = instanceLimitOverridesSnapshot(client);
2138
+ for (const file of files) {
2139
+ const size = file.data.byteLength;
2140
+ if (size <= BOT_ATTACHMENT_MAX_BYTES) continue;
2141
+ const snapshotNote = snapshot ? ` Instance limit overrides: ${JSON.stringify(snapshot)}.` : "";
2142
+ throw new FluxerError(
2143
+ `Bot attachments are limited to 50 MiB (${BOT_ATTACHMENT_MAX_BYTES} bytes); "${file.filename}" is ${size} bytes.${snapshotNote}`,
2144
+ { code: ErrorCodes.AttachmentTooLarge }
2145
+ );
2146
+ }
2147
+ }
2046
2148
  function asBytes(data) {
2047
2149
  const src = data instanceof ArrayBuffer ? new Uint8Array(data) : new Uint8Array(data);
2048
2150
  const out = new Uint8Array(src.byteLength);
@@ -2062,6 +2164,7 @@ async function putBlob(url, body, contentType) {
2062
2164
  }
2063
2165
  }
2064
2166
  async function uploadAttachmentsForSend(client, channelId, files) {
2167
+ assertBotAttachmentLimits(client, files);
2065
2168
  const plan = await client.rest.post(
2066
2169
  Routes6.channelAttachments(channelId),
2067
2170
  {
@@ -2358,11 +2461,12 @@ var Channel = class extends Base {
2358
2461
  if (options?.silent) params.set("silent", "true");
2359
2462
  if (options?.deleteMessages) params.set("delete_messages", "true");
2360
2463
  const qs2 = params.toString();
2361
- const { silent: _s, deleteMessages: _d, ...sudo } = options ?? {};
2464
+ const { silent: _s, deleteMessages: _d, reason: _reason, ...sudo } = options ?? {};
2362
2465
  const body = Object.keys(sudo).length ? toSudoBody(sudo) : void 0;
2363
2466
  await this.client.rest.delete(Routes7.channel(this.id) + (qs2 ? `?${qs2}` : ""), {
2364
2467
  body,
2365
- auth: true
2468
+ auth: true,
2469
+ ...auditReasonHeaders(_reason)
2366
2470
  });
2367
2471
  this.client.channels.delete(this.id);
2368
2472
  this.client._clearMessageCache(this.id);
@@ -2527,7 +2631,7 @@ import {
2527
2631
  // src/Helpers/Instance.ts
2528
2632
  import { isRecord } from "@fluxerjs/util";
2529
2633
  var DEFAULT_INSTANCE_ENDPOINTS = Object.freeze({
2530
- api: "https://api.fluxer.app",
2634
+ api: "https://web.fluxer.app/api",
2531
2635
  api_client: "https://web.fluxer.app/api",
2532
2636
  api_public: "https://api.fluxer.app",
2533
2637
  gateway: "wss://gateway.fluxer.app",
@@ -2561,6 +2665,10 @@ function requireString(obj, key, path) {
2561
2665
  }
2562
2666
  return value;
2563
2667
  }
2668
+ var INSTANCE_DISCOVERY_PATH = "/.well-known/fluxer";
2669
+ function instanceDiscoveryUrl(origin) {
2670
+ return `${normalizeApiOrigin(origin)}${INSTANCE_DISCOVERY_PATH}`;
2671
+ }
2564
2672
  function normalizeApiOrigin(api) {
2565
2673
  const trimmed = api.trim().replace(/\/+$/, "");
2566
2674
  return trimmed.replace(/\/v\d+$/i, "");
@@ -2589,6 +2697,8 @@ function parseInstanceDiscovery(raw) {
2589
2697
  endpoints[key] = requireString(raw.endpoints, key, "endpoints");
2590
2698
  }
2591
2699
  endpoints.api = normalizeApiOrigin(endpoints.api);
2700
+ endpoints.api_client = normalizeApiOrigin(endpoints.api_client);
2701
+ endpoints.api_public = normalizeApiOrigin(endpoints.api_public);
2592
2702
  endpoints.invite = normalizeInviteBase(endpoints.invite);
2593
2703
  endpoints.media = endpoints.media.replace(/\/+$/, "");
2594
2704
  endpoints.static_cdn = endpoints.static_cdn.replace(/\/+$/, "");
@@ -2666,6 +2776,11 @@ function resolveInstanceEndpoints(input) {
2666
2776
  }
2667
2777
  }
2668
2778
  merged.api = normalizeApiOrigin(merged.api);
2779
+ merged.api_client = normalizeApiOrigin(merged.api_client);
2780
+ merged.api_public = normalizeApiOrigin(merged.api_public);
2781
+ if (typeof partial.api === "string" && partial.api.length > 0 && (typeof partial.api_public !== "string" || partial.api_public.length === 0)) {
2782
+ merged.api_public = merged.api;
2783
+ }
2669
2784
  merged.invite = normalizeInviteBase(merged.invite);
2670
2785
  merged.media = merged.media.replace(/\/+$/, "");
2671
2786
  merged.static_cdn = merged.static_cdn.replace(/\/+$/, "");
@@ -2674,13 +2789,13 @@ function resolveInstanceEndpoints(input) {
2674
2789
  discovery: null
2675
2790
  });
2676
2791
  }
2677
- function resolveRestApi(instanceApi, restApi) {
2678
- const fromInstance = normalizeApiOrigin(instanceApi);
2792
+ function resolveRestApi(instanceApiPublic, restApi) {
2793
+ const fromInstance = normalizeApiOrigin(instanceApiPublic);
2679
2794
  if (restApi === void 0) return fromInstance;
2680
2795
  const fromRest = normalizeApiOrigin(restApi);
2681
2796
  if (fromRest !== fromInstance) {
2682
2797
  throw new FluxerError(
2683
- `Conflicting API hosts: instance.api (${fromInstance}) vs rest.api (${fromRest}). Prefer ClientOptions.instance.`,
2798
+ `Conflicting API hosts: instance.api_public (${fromInstance}) vs rest.api (${fromRest}). Prefer ClientOptions.instance.`,
2684
2799
  { code: ErrorCodes.ConflictingInstanceConfig }
2685
2800
  );
2686
2801
  }
@@ -3162,7 +3277,8 @@ var GuildChannel = class extends Channel {
3162
3277
  async edit(options) {
3163
3278
  const data = await this.client.rest.patch(Routes13.channel(this.id), {
3164
3279
  body: toChannelEditBody(options),
3165
- auth: true
3280
+ auth: true,
3281
+ ...auditReasonHeaders(options.reason)
3166
3282
  });
3167
3283
  this.name = data.name ?? this.name;
3168
3284
  this.parentId = data.parent_id ?? this.parentId;
@@ -3377,9 +3493,10 @@ var GuildEmoji = class extends Base {
3377
3493
  return `<${this.animated ? "a" : ""}:${this.name}:${this.id}>`;
3378
3494
  }
3379
3495
  /** Delete this emoji. Requires Manage Emojis and Stickers permission. */
3380
- async delete() {
3496
+ async delete(reason) {
3381
3497
  await this.client.rest.delete(Routes14.guildEmoji(this.guildId, this.id), {
3382
- auth: true
3498
+ auth: true,
3499
+ ...auditReasonHeaders(reason)
3383
3500
  });
3384
3501
  const guild = this.client.guilds.get(this.guildId);
3385
3502
  if (guild) guild.emojis.delete(this.id);
@@ -3389,9 +3506,11 @@ var GuildEmoji = class extends Base {
3389
3506
  * Requires Manage Emojis and Stickers permission.
3390
3507
  */
3391
3508
  async edit(options) {
3509
+ const { reason, name } = options;
3392
3510
  const data = await this.client.rest.patch(Routes14.guildEmoji(this.guildId, this.id), {
3393
- body: options,
3394
- auth: true
3511
+ body: { name },
3512
+ auth: true,
3513
+ ...auditReasonHeaders(reason)
3395
3514
  });
3396
3515
  this.name = data.name;
3397
3516
  return this;
@@ -3407,6 +3526,9 @@ import { OverwriteType } from "@fluxerjs/types";
3407
3526
  import { ALL_PERMISSIONS_BIGINT, PermissionFlags as PermissionFlags2 } from "@fluxerjs/util";
3408
3527
  function computePermissions(basePermissions, overwrites, memberRoles, memberId, isOwner) {
3409
3528
  if (isOwner) return ALL_PERMISSIONS_BIGINT;
3529
+ if ((basePermissions & PermissionFlags2.Administrator) !== 0n) {
3530
+ return ALL_PERMISSIONS_BIGINT;
3531
+ }
3410
3532
  let perms = basePermissions;
3411
3533
  for (const overwrite of overwrites ?? []) {
3412
3534
  const applies = overwrite.type === OverwriteType.Role && memberRoles.includes(overwrite.id) || overwrite.type === OverwriteType.Member && overwrite.id === memberId;
@@ -3415,7 +3537,7 @@ function computePermissions(basePermissions, overwrites, memberRoles, memberId,
3415
3537
  const deny = BigInt(overwrite.deny || "0");
3416
3538
  perms = perms & ~deny | allow;
3417
3539
  }
3418
- return (perms & PermissionFlags2.Administrator) !== 0n ? ALL_PERMISSIONS_BIGINT : perms;
3540
+ return perms;
3419
3541
  }
3420
3542
 
3421
3543
  // src/Domain/Guild/GuildMemberRoleManager.ts
@@ -3621,7 +3743,8 @@ var GuildMember = class _GuildMember extends Base {
3621
3743
  const route = isMe ? Routes16.guildMemberMe(this.guild.id) : Routes16.guildMember(this.guild.id, this.id);
3622
3744
  const data = await this.client.rest.patch(route, {
3623
3745
  body: toMemberEditBody(options),
3624
- auth: true
3746
+ auth: true,
3747
+ ...auditReasonHeaders(options.reason ?? options.timeoutReason)
3625
3748
  });
3626
3749
  this._patch(data);
3627
3750
  if (options.bio !== void 0) this.bio = options.bio;
@@ -3655,8 +3778,8 @@ var GuildMember = class _GuildMember extends Base {
3655
3778
  await this.edit({ channelId, connectionId });
3656
3779
  }
3657
3780
  /** Kick this member. Requires Kick Members. */
3658
- async kick() {
3659
- await this.guild.kick(this.id);
3781
+ async kick(reason) {
3782
+ await this.guild.kick(this.id, reason);
3660
3783
  }
3661
3784
  /** Ban this member. Requires Ban Members. */
3662
3785
  async ban(options) {
@@ -3780,9 +3903,10 @@ var GuildSticker = class extends Base {
3780
3903
  return SnowflakeUtil6.dateFromSnowflake(this.id);
3781
3904
  }
3782
3905
  /** Delete this sticker. Requires Manage Emojis and Stickers permission. */
3783
- async delete() {
3906
+ async delete(reason) {
3784
3907
  await this.client.rest.delete(Routes17.guildSticker(this.guildId, this.id), {
3785
- auth: true
3908
+ auth: true,
3909
+ ...auditReasonHeaders(reason)
3786
3910
  });
3787
3911
  const guild = this.client.guilds.get(this.guildId);
3788
3912
  if (guild) guild.stickers.delete(this.id);
@@ -3792,9 +3916,11 @@ var GuildSticker = class extends Base {
3792
3916
  * Requires Manage Emojis and Stickers permission.
3793
3917
  */
3794
3918
  async edit(options) {
3919
+ const { reason, ...body } = options;
3795
3920
  const data = await this.client.rest.patch(Routes17.guildSticker(this.guildId, this.id), {
3796
- body: options,
3797
- auth: true
3921
+ body,
3922
+ auth: true,
3923
+ ...auditReasonHeaders(reason)
3798
3924
  });
3799
3925
  const s = data;
3800
3926
  this.name = s.name;
@@ -3921,16 +4047,21 @@ var Role = class _Role extends Base {
3921
4047
  const body = toRoleEditBody(options);
3922
4048
  const data = await this.client.rest.patch(Routes18.guildRole(this.guildId, this.id), {
3923
4049
  body: Object.keys(body).length ? body : void 0,
3924
- auth: true
4050
+ auth: true,
4051
+ ...auditReasonHeaders(options.reason)
3925
4052
  });
3926
4053
  this._patch(data);
3927
4054
  return this;
3928
4055
  }
3929
4056
  /**
3930
4057
  * DELETE role. Requires Manage Roles.
4058
+ * @param reason - Audit log reason (`X-Audit-Log-Reason`)
3931
4059
  */
3932
- async delete() {
3933
- await this.client.rest.delete(Routes18.guildRole(this.guildId, this.id), { auth: true });
4060
+ async delete(reason) {
4061
+ await this.client.rest.delete(Routes18.guildRole(this.guildId, this.id), {
4062
+ auth: true,
4063
+ ...auditReasonHeaders(reason)
4064
+ });
3934
4065
  this.client.guilds.get(this.guildId)?.roles.delete(this.id);
3935
4066
  }
3936
4067
  };
@@ -4363,7 +4494,10 @@ async function resolveClientEmoji(client, emoji, guildId) {
4363
4494
  return parsed.name;
4364
4495
  }
4365
4496
  async function fetchInstance(client) {
4366
- const raw = await client.rest.get(Routes21.instanceDiscovery(), { auth: false });
4497
+ const raw = await client.rest.get(Routes21.instanceDiscovery(), {
4498
+ auth: false,
4499
+ unversioned: true
4500
+ });
4367
4501
  return parseInstanceDiscovery(raw);
4368
4502
  }
4369
4503
  function fetchGatewayInfo(client) {
@@ -4742,9 +4876,11 @@ async function fetchEmoji(guild, emojiId) {
4742
4876
  }
4743
4877
  }
4744
4878
  async function createEmoji(guild, options) {
4879
+ const { reason, ...body } = options;
4745
4880
  const data = await guild.client.rest.post(Routes25.guildEmojis(guild.id), {
4746
- body: options,
4747
- auth: true
4881
+ body,
4882
+ auth: true,
4883
+ ...auditReasonHeaders(reason)
4748
4884
  });
4749
4885
  return cacheEmoji(guild, data);
4750
4886
  }
@@ -4763,6 +4899,19 @@ async function createEmojisBulk(guild, emojis) {
4763
4899
  return { success: data.success.map((e) => cacheEmoji(guild, e)), failed: data.failed };
4764
4900
  }
4765
4901
 
4902
+ // src/Domain/Guild/GuildRoleManager.ts
4903
+ import { LimitedCollection as LimitedCollection3 } from "@fluxerjs/collection";
4904
+ var GuildRoleManager = class extends LimitedCollection3 {
4905
+ constructor(guildId, options) {
4906
+ super(options);
4907
+ this.guildId = guildId;
4908
+ }
4909
+ /** The @everyone role (`id === guild.id`), or undefined if uncached. */
4910
+ get everyone() {
4911
+ return this.get(this.guildId);
4912
+ }
4913
+ };
4914
+
4766
4915
  // src/Domain/Guild/MemberHttp.ts
4767
4916
  import { Routes as Routes26 } from "@fluxerjs/types";
4768
4917
  async function fetchMember(guild, userId) {
@@ -4819,9 +4968,10 @@ var GuildBan = class extends Base {
4819
4968
  * Remove this ban (unban the user).
4820
4969
  * Requires Ban Members permission.
4821
4970
  */
4822
- async unban() {
4971
+ async unban(reason) {
4823
4972
  await this.client.rest.delete(Routes27.guildBan(this.guildId, this.user.id), {
4824
- auth: true
4973
+ auth: true,
4974
+ ...auditReasonHeaders(reason)
4825
4975
  });
4826
4976
  }
4827
4977
  };
@@ -4831,33 +4981,27 @@ async function ban(guild, userId, options) {
4831
4981
  const body = options ? toGuildBanBody(options) : {};
4832
4982
  await guild.client.rest.put(Routes28.guildBan(guild.id, userId), {
4833
4983
  body: Object.keys(body).length ? body : void 0,
4834
- auth: true
4984
+ auth: true,
4985
+ ...auditReasonHeaders(options?.reason)
4835
4986
  });
4836
4987
  }
4837
4988
  async function fetchBans(guild) {
4838
4989
  const data = await guild.client.rest.get(Routes28.guildBans(guild.id));
4839
4990
  return data.map((b) => new GuildBan(guild.client, { ...b, guild_id: guild.id }, guild.id));
4840
4991
  }
4841
- async function unban(guild, userId) {
4842
- await guild.client.rest.delete(Routes28.guildBan(guild.id, userId), { auth: true });
4992
+ async function unban(guild, userId, reason) {
4993
+ await guild.client.rest.delete(Routes28.guildBan(guild.id, userId), {
4994
+ auth: true,
4995
+ ...auditReasonHeaders(reason)
4996
+ });
4843
4997
  }
4844
- async function kick(guild, userId) {
4845
- await guild.client.rest.delete(Routes28.guildMember(guild.id, userId), { auth: true });
4998
+ async function kick(guild, userId, reason) {
4999
+ await guild.client.rest.delete(Routes28.guildMember(guild.id, userId), {
5000
+ auth: true,
5001
+ ...auditReasonHeaders(reason)
5002
+ });
4846
5003
  }
4847
5004
 
4848
- // src/Domain/Guild/GuildRoleManager.ts
4849
- import { LimitedCollection as LimitedCollection3 } from "@fluxerjs/collection";
4850
- var GuildRoleManager = class extends LimitedCollection3 {
4851
- constructor(guildId, options) {
4852
- super(options);
4853
- this.guildId = guildId;
4854
- }
4855
- /** The @everyone role (`id === guild.id`), or undefined if uncached. */
4856
- get everyone() {
4857
- return this.get(this.guildId);
4858
- }
4859
- };
4860
-
4861
5005
  // src/Domain/Guild/Roles.ts
4862
5006
  import { Routes as Routes29 } from "@fluxerjs/types";
4863
5007
  import { parseRoleMention } from "@fluxerjs/util";
@@ -4865,7 +5009,8 @@ async function createRole(guild, options = {}) {
4865
5009
  const body = toRoleCreateBody(options);
4866
5010
  const data = await guild.client.rest.post(Routes29.guildRoles(guild.id), {
4867
5011
  body: Object.keys(body).length ? body : void 0,
4868
- auth: true
5012
+ auth: true,
5013
+ ...auditReasonHeaders(options.reason)
4869
5014
  });
4870
5015
  return cacheRole(guild, data);
4871
5016
  }
@@ -4931,9 +5076,11 @@ async function resetRoleHoistPositions(guild) {
4931
5076
  // src/Domain/Guild/Stickers.ts
4932
5077
  import { Routes as Routes30 } from "@fluxerjs/types";
4933
5078
  async function createSticker(guild, options) {
5079
+ const { reason, ...body } = options;
4934
5080
  const data = await guild.client.rest.post(Routes30.guildStickers(guild.id), {
4935
- body: options,
4936
- auth: true
5081
+ body,
5082
+ auth: true,
5083
+ ...auditReasonHeaders(reason)
4937
5084
  });
4938
5085
  return cacheSticker(guild, data);
4939
5086
  }
@@ -5263,12 +5410,12 @@ var Guild = class extends Base {
5263
5410
  return fetchBans(this);
5264
5411
  }
5265
5412
  /** Unban a user from the guild. Requires Ban Members. */
5266
- unban(userId) {
5267
- return unban(this, userId);
5413
+ unban(userId, reason) {
5414
+ return unban(this, userId, reason);
5268
5415
  }
5269
5416
  /** Kick a user from the guild. Requires Kick Members. */
5270
- kick(userId) {
5271
- return kick(this, userId);
5417
+ kick(userId, reason) {
5418
+ return kick(this, userId, reason);
5272
5419
  }
5273
5420
  /** Fetch all emojis in this guild from the API. */
5274
5421
  fetchEmojis() {
@@ -5718,7 +5865,14 @@ function markGuildUnavailable(client, id) {
5718
5865
  guild.available = false;
5719
5866
  client.emit(Events.GuildUnavailable, guild);
5720
5867
  }
5868
+ function isStartupGuildHydration(client) {
5869
+ return client._guildStreamSettleTimeout !== null;
5870
+ }
5721
5871
  var guildHandlers = {
5872
+ /**
5873
+ * Available snapshots: {@link Events.GuildAvailable} for READY stubs / outage recovery,
5874
+ * {@link Events.GuildCreate} for real joins (unless {@link ClientOptions.emitGuildCreateOnStartup}).
5875
+ */
5722
5876
  GUILD_CREATE(client, d) {
5723
5877
  const raw = d;
5724
5878
  if (raw.unavailable === true) {
@@ -5731,11 +5885,32 @@ var guildHandlers = {
5731
5885
  }
5732
5886
  return;
5733
5887
  }
5888
+ const startupHydration = isStartupGuildHydration(client);
5889
+ if (startupHydration) {
5890
+ resetGuildStreamSettle(client);
5891
+ }
5734
5892
  const result = applyGuildSnapshotFromGateway(client, d);
5735
5893
  if (!result) return;
5736
5894
  const { guild, recovered } = result;
5737
- if (recovered) guild.available = true;
5738
- client.emit(recovered ? Events.GuildAvailable : Events.GuildCreate, guild);
5895
+ const legacyStartup = client.options.emitGuildCreateOnStartup === true;
5896
+ const wasReadyStub = client._readyStubGuildIds.delete(guild.id);
5897
+ const isStartupBackfill = wasReadyStub || startupHydration;
5898
+ if (recovered) {
5899
+ guild.available = true;
5900
+ if (legacyStartup && isStartupBackfill) {
5901
+ client.emit(Events.GuildCreate, guild);
5902
+ } else {
5903
+ client.emit(Events.GuildAvailable, guild);
5904
+ }
5905
+ } else if (startupHydration) {
5906
+ if (legacyStartup) {
5907
+ client.emit(Events.GuildCreate, guild);
5908
+ } else {
5909
+ client.emit(Events.GuildAvailable, guild);
5910
+ }
5911
+ } else {
5912
+ client.emit(Events.GuildCreate, guild);
5913
+ }
5739
5914
  client._onGuildReceived(guild.id);
5740
5915
  },
5741
5916
  GUILD_UPDATE(client, d) {
@@ -5774,6 +5949,7 @@ var guildHandlers = {
5774
5949
  markGuildUnavailable(client, id);
5775
5950
  return;
5776
5951
  }
5952
+ client._readyStubGuildIds.delete(id);
5777
5953
  const guild = client.guilds.get(id);
5778
5954
  if (!guild) return;
5779
5955
  client.guilds.delete(id);
@@ -6292,10 +6468,15 @@ function hydrateReadyGuilds(client, guilds, waitForGuilds) {
6292
6468
  for (const g of guilds ?? []) {
6293
6469
  if (g.unavailable === true) {
6294
6470
  if (typeof g.id === "string") {
6295
- const existing = client.guilds.get(g.id);
6296
- if (existing && existing.available !== false) {
6297
- existing.available = false;
6471
+ let guild = client.guilds.get(g.id);
6472
+ if (!guild) {
6473
+ guild = new Guild(client, { id: g.id });
6474
+ guild.available = false;
6475
+ client.guilds.set(g.id, guild);
6476
+ } else if (guild.available !== false) {
6477
+ guild.available = false;
6298
6478
  }
6479
+ client._readyStubGuildIds.add(g.id);
6299
6480
  if (pending !== null) pending.add(g.id);
6300
6481
  }
6301
6482
  continue;
@@ -6323,6 +6504,13 @@ function clearGuildStreamSettle(client) {
6323
6504
  clearTimeout(client._guildStreamSettleTimeout);
6324
6505
  client._guildStreamSettleTimeout = null;
6325
6506
  }
6507
+ function resetGuildStreamSettle(client) {
6508
+ clearGuildStreamSettle(client);
6509
+ client._guildStreamSettleTimeout = setTimeout(() => {
6510
+ client._guildStreamSettleTimeout = null;
6511
+ if (client.readyAt === null) finalizeClientReady(client);
6512
+ }, GUILD_STREAM_SETTLE_MS);
6513
+ }
6326
6514
  function handleReadyPayload(client, data) {
6327
6515
  client.user = new ClientUser(client, data.user);
6328
6516
  const waitForGuilds = client.options.waitForGuilds === true;
@@ -6342,14 +6530,9 @@ function handleReadyPayload(client, data) {
6342
6530
  client._pendingGuildIds = existing;
6343
6531
  return;
6344
6532
  }
6345
- if (waitForGuilds && guilds.length === 0) {
6346
- if (client._guildStreamSettleTimeout === null) {
6347
- client._guildStreamSettleTimeout = setTimeout(() => {
6348
- client._guildStreamSettleTimeout = null;
6349
- if (client.readyAt === null) finalizeClientReady(client);
6350
- }, GUILD_STREAM_SETTLE_MS);
6351
- }
6352
- return;
6533
+ if (guilds.length === 0) {
6534
+ resetGuildStreamSettle(client);
6535
+ if (waitForGuilds) return;
6353
6536
  }
6354
6537
  finalizeClientReady(client);
6355
6538
  }
@@ -6684,7 +6867,12 @@ var Client = class _Client extends EventEmitter3 {
6684
6867
  _ws = null;
6685
6868
  /** When waitForGuilds, guild IDs still expected via GUILD_CREATE. */
6686
6869
  _pendingGuildIds = null;
6687
- /** @internal Timeout when READY has no guilds but waitForGuilds is set. */
6870
+ /** Guild IDs inserted as unavailable stubs from READY (startup / reconnect backfill). */
6871
+ _readyStubGuildIds = /* @__PURE__ */ new Set();
6872
+ /**
6873
+ * @internal Empty-READY guild stream window.
6874
+ * Classifies incoming GUILD_CREATE as startup hydration. Also delays Ready when waitForGuilds.
6875
+ */
6688
6876
  _guildStreamSettleTimeout = null;
6689
6877
  /** @internal Dispatches queued until Ready when waitForGuilds delays Ready. */
6690
6878
  _deferredGatewayDispatches = [];
@@ -6715,7 +6903,7 @@ var Client = class _Client extends EventEmitter3 {
6715
6903
  const restApi = options.rest?.api;
6716
6904
  if (options.instance !== void 0) {
6717
6905
  this.instance = resolveInstanceEndpoints(options.instance);
6718
- resolveRestApi(this.instance.endpoints.api, restApi);
6906
+ resolveRestApi(this.instance.endpoints.api_public, restApi);
6719
6907
  } else {
6720
6908
  this.instance = resolveInstanceEndpoints(
6721
6909
  restApi !== void 0 ? { api: restApi } : void 0
@@ -6728,27 +6916,31 @@ var Client = class _Client extends EventEmitter3 {
6728
6916
  }
6729
6917
  this.rest = new REST({
6730
6918
  ...this.options.rest,
6731
- api: this.instance.endpoints.api,
6732
- version: this.options.rest?.version ?? "1"
6919
+ api: this.instance.endpoints.api_public,
6920
+ version: this.options.rest?.version ?? "1",
6921
+ locale: this.options.locale ?? this.options.rest?.locale
6733
6922
  });
6734
6923
  }
6735
6924
  /**
6736
6925
  * Create a client from instance discovery (`GET /.well-known/fluxer`).
6737
- * Does not log in — call {@link login} with a token afterward.
6926
+ * Fetches the unversioned well-known document from the given origin (not `{api}/v1/.well-known/fluxer`).
6927
+ * Wires REST from `endpoints.api_public`. `endpoints.api` / `api_client` stay on the instance for inspection.
6928
+ * Does not log in. Call {@link login} with a token afterward.
6738
6929
  *
6739
- * @param origin - API origin used only to fetch discovery (e.g. `https://api.example.com`)
6740
- * @param options - Client options (must not conflict with discovered `endpoints.api`)
6930
+ * @param origin - Origin used only to fetch discovery (e.g. `https://fluxer.app` or `https://my.instance`)
6931
+ * @param options - Client options (must not conflict with discovered `endpoints.api_public`)
6741
6932
  * @param connectOptions - Optional abort signal for the discovery request
6742
6933
  */
6743
6934
  static async fromDiscovery(origin, options = {}, connectOptions) {
6744
- const bootstrapApi = typeof origin === "string" ? normalizeApiOrigin(origin) : normalizeApiOrigin(origin.api);
6935
+ const bootstrapOrigin = typeof origin === "string" ? normalizeApiOrigin(origin) : normalizeApiOrigin(origin.api);
6745
6936
  const version = typeof origin === "string" ? options.rest?.version ?? "1" : origin.version ?? options.rest?.version ?? "1";
6746
6937
  const bootstrap = new REST({
6747
6938
  ...options.rest ?? {},
6748
- api: bootstrapApi,
6749
- version
6939
+ api: bootstrapOrigin,
6940
+ version,
6941
+ locale: options.locale ?? options.rest?.locale
6750
6942
  });
6751
- const raw = await bootstrap.get(Routes34.instanceDiscovery(), {
6943
+ const raw = await bootstrap.get(instanceDiscoveryUrl(bootstrapOrigin), {
6752
6944
  auth: false,
6753
6945
  ...connectOptions?.signal ? { signal: connectOptions.signal } : {}
6754
6946
  });
@@ -6756,7 +6948,7 @@ var Client = class _Client extends EventEmitter3 {
6756
6948
  const { rest, ...restOptions } = options;
6757
6949
  return new _Client({
6758
6950
  ...restOptions,
6759
- // Drop rest.api so discovered endpoints.api owns the host; keep other REST opts.
6951
+ // Drop rest.api so discovered endpoints.api_public owns the REST host; keep other REST opts.
6760
6952
  ...rest ? { rest: { ...rest, api: void 0 } } : {},
6761
6953
  instance: discovery
6762
6954
  });
@@ -6783,7 +6975,7 @@ var Client = class _Client extends EventEmitter3 {
6783
6975
  return resolveClientEmoji(this, emoji, guildId);
6784
6976
  }
6785
6977
  /**
6786
- * Fetch instance discovery document (`/.well-known/fluxer`).
6978
+ * Fetch instance discovery document (unversioned `GET /.well-known/fluxer` on the REST host).
6787
6979
  * @returns Instance configuration (endpoints, features, metadata)
6788
6980
  */
6789
6981
  fetchInstance() {
@@ -7052,6 +7244,7 @@ var Client = class _Client extends EventEmitter3 {
7052
7244
  this.user = null;
7053
7245
  this.readyAt = null;
7054
7246
  this._pendingGuildIds = null;
7247
+ this._readyStubGuildIds.clear();
7055
7248
  this.guilds.clear();
7056
7249
  this.channels.clear();
7057
7250
  this.users.clear();
@@ -7470,6 +7663,7 @@ export {
7470
7663
  GuildOperations,
7471
7664
  GuildRoleManager,
7472
7665
  GuildSticker,
7666
+ INSTANCE_DISCOVERY_PATH,
7473
7667
  Invite,
7474
7668
  InviteType3 as InviteType,
7475
7669
  LinkChannel,
@@ -7518,6 +7712,7 @@ export {
7518
7712
  cdnMemberBannerURL,
7519
7713
  cdnStickerURL,
7520
7714
  createLogger2 as createLogger,
7715
+ instanceDiscoveryUrl,
7521
7716
  inviteUrl,
7522
7717
  normalizeApiOrigin,
7523
7718
  normalizeCacheLimit,