@wordbombio/sdk 0.1.0 → 0.2.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.cjs CHANGED
@@ -484,10 +484,14 @@ var AppRoom = class extends Emitter {
484
484
  break;
485
485
  }
486
486
  case "j": {
487
- const [sid = "", id = "", bot = "", spec = "", hearts = "", name = ""] = fields(line, 6);
487
+ const [sid = "", id = "", bot = "", spec = "", life = "", name = ""] = fields(line, 6);
488
488
  if (!sid) break;
489
- const fresh = !this.seats.has(sid);
490
- const seat = { sid, id, name: name || "Guest", bot: bot === "1", spectating: spec === "1", hearts: num(hearts), eliminated: false };
489
+ const known = this.seats.get(sid);
490
+ const fresh = !known;
491
+ const spectating = spec === "1";
492
+ const hearts = num(life);
493
+ const out = !spectating && (hearts <= 0 || !!known?.eliminated && !snapshot);
494
+ const seat = { sid, id, name: name || "Guest", bot: bot === "1", spectating, hearts, eliminated: this.running && out };
491
495
  this.seats.set(sid, seat);
492
496
  this.gone.delete(sid);
493
497
  if (fresh && !snapshot) this.emit("room.player.joined", { room: this, player: this.toPlayer(seat) });
@@ -672,6 +676,7 @@ var BUILT_IN = /* @__PURE__ */ new Set([
672
676
  "admin",
673
677
  "bm",
674
678
  "bookmark",
679
+ "cards",
675
680
  "clear",
676
681
  "discord",
677
682
  "draw",
@@ -679,13 +684,16 @@ var BUILT_IN = /* @__PURE__ */ new Set([
679
684
  "end",
680
685
  "i",
681
686
  "invite",
687
+ "kraken",
682
688
  "lootbox",
683
689
  "love",
690
+ "missing",
684
691
  "monster",
685
692
  "play",
686
693
  "s",
687
694
  "settings",
688
695
  "solve",
696
+ "suggest",
689
697
  "surrender",
690
698
  "syl",
691
699
  "title",
@@ -1131,7 +1139,6 @@ function avatarUrl(userId, avatar) {
1131
1139
  var REPLY_WINDOW = 14e3;
1132
1140
  var POST_LIMIT = 6;
1133
1141
  var POST_WINDOW = 6e4;
1134
- var SENT_RING = 50;
1135
1142
  function exactPlayer(all, ref) {
1136
1143
  const want = ref.trim().toLowerCase();
1137
1144
  if (!want) return null;
@@ -1173,23 +1180,6 @@ function readOptions(raw, def, players) {
1173
1180
  });
1174
1181
  return out;
1175
1182
  }
1176
- function enabledButtons(wire) {
1177
- const ids = /* @__PURE__ */ new Set();
1178
- for (const row of wire.e?.components ?? []) {
1179
- for (const button2 of row.components) if (!button2.disabled) ids.add(button2.custom_id);
1180
- }
1181
- return ids;
1182
- }
1183
- function remember(sent, messageId, wire) {
1184
- sent.delete(messageId);
1185
- const ids = enabledButtons(wire);
1186
- if (!ids.size) return;
1187
- sent.set(messageId, ids);
1188
- if (sent.size > SENT_RING) {
1189
- const oldest = sent.keys().next().value;
1190
- if (oldest !== void 0) sent.delete(oldest);
1191
- }
1192
- }
1193
1183
  var Interactions = class {
1194
1184
  constructor(realtime, session, commands) {
1195
1185
  this.realtime = realtime;
@@ -1203,14 +1193,13 @@ var Interactions = class {
1203
1193
  attach(room, sink, subscribe) {
1204
1194
  let key = "";
1205
1195
  const posts = [];
1206
- const link = { subscribe, sent: /* @__PURE__ */ new Map(), receive: (e, frame) => this.deliver(room, sink, link.sent, e, frame) };
1196
+ const link = { subscribe, receive: (e, frame) => this.deliver(room, sink, e, frame) };
1207
1197
  const drop = () => {
1208
1198
  if (!key) return;
1209
1199
  if (this.links.get(key) === link) {
1210
1200
  this.links.delete(key);
1211
1201
  if (subscribe) this.realtime?.send("bot:unsub", { k: key });
1212
1202
  }
1213
- link.sent.clear();
1214
1203
  key = "";
1215
1204
  };
1216
1205
  return {
@@ -1237,10 +1226,7 @@ var Interactions = class {
1237
1226
  throw new WordBombError("RATE_LIMITED", `at most ${POST_LIMIT} posts a minute per room, next one in ${Math.ceil(wait / 1e3)}s`, void 0, wait);
1238
1227
  }
1239
1228
  posts.push(now);
1240
- const sentKey = key;
1241
- const messageId = await this.say({ k: sentKey, ...wire });
1242
- if (key === sentKey) remember(link.sent, messageId, wire);
1243
- return messageId;
1229
+ return this.say({ k: key, ...wire });
1244
1230
  },
1245
1231
  close: drop
1246
1232
  };
@@ -1254,7 +1240,7 @@ var Interactions = class {
1254
1240
  const frame = record(d);
1255
1241
  this.links.get(str(frame.k))?.receive(e, frame);
1256
1242
  }
1257
- deliver(room, sink, sent, e, frame) {
1243
+ deliver(room, sink, e, frame) {
1258
1244
  const id = str(frame.i);
1259
1245
  const key = str(frame.k);
1260
1246
  const userId = str(frame.u);
@@ -1267,7 +1253,7 @@ var Interactions = class {
1267
1253
  const def = this.commands.get(name);
1268
1254
  if (!name || !def && this.commands.all.length) return;
1269
1255
  const options = readOptions(frame.o, def, players);
1270
- const answer = this.answerer(key, id, sent);
1256
+ const answer = this.answerer(key, id);
1271
1257
  sink.command({
1272
1258
  id,
1273
1259
  room,
@@ -1293,8 +1279,8 @@ var Interactions = class {
1293
1279
  if (e === "bot:btn") {
1294
1280
  const messageId = str(frame.m);
1295
1281
  const customId = str(frame.c);
1296
- if (!messageId || !customId || !sent.get(messageId)?.has(customId)) return;
1297
- const answer = this.answerer(key, id, sent);
1282
+ if (!messageId || !customId) return;
1283
+ const answer = this.answerer(key, id);
1298
1284
  sink.button({
1299
1285
  id,
1300
1286
  room,
@@ -1306,7 +1292,7 @@ var Interactions = class {
1306
1292
  });
1307
1293
  }
1308
1294
  }
1309
- answerer(key, id, sent) {
1295
+ answerer(key, id) {
1310
1296
  const deadline = Date.now() + REPLY_WINDOW;
1311
1297
  let answered = false;
1312
1298
  let sending = false;
@@ -1318,8 +1304,6 @@ var Interactions = class {
1318
1304
  try {
1319
1305
  const messageId = await this.say({ k: key, i: id, ...wire, ...extra });
1320
1306
  answered = true;
1321
- if (extra.up) sent.delete(extra.up);
1322
- remember(sent, messageId, wire);
1323
1307
  return messageId;
1324
1308
  } finally {
1325
1309
  sending = false;
@@ -1454,11 +1438,14 @@ var Realtime = class extends Emitter {
1454
1438
  this.emit("banned", void 0);
1455
1439
  return;
1456
1440
  }
1441
+ if (ev.code === REALTIME_CLOSE.REPLACED || ev.code === REALTIME_CLOSE.REPLACED_ELSEWHERE) {
1442
+ this.emit("replaced", void 0);
1443
+ return;
1444
+ }
1457
1445
  const expired = ev.code === REALTIME_CLOSE.TOKEN_EXPIRED;
1458
1446
  let delay = this.backoff;
1459
1447
  this.backoff = Math.min(this.backoff * 2, MAX_BACKOFF);
1460
- if (ev.code === REALTIME_CLOSE.REPLACED || ev.code === REALTIME_CLOSE.REPLACED_ELSEWHERE) delay = MAX_BACKOFF;
1461
- else if (expired && !this.fastRetryUsed) {
1448
+ if (expired && !this.fastRetryUsed) {
1462
1449
  this.fastRetryUsed = true;
1463
1450
  delay = FAST_RETRY;
1464
1451
  }
@@ -1476,7 +1463,8 @@ var Realtime = class extends Emitter {
1476
1463
  this.ws.send(JSON.stringify({ e, d }));
1477
1464
  return true;
1478
1465
  }
1479
- request(e, d) {
1466
+ async request(e, d) {
1467
+ await this.opened();
1480
1468
  return new Promise((resolve, reject) => {
1481
1469
  const ws = this.ws;
1482
1470
  if (!ws || ws.readyState !== WebSocket.OPEN) {
@@ -1501,6 +1489,20 @@ var Realtime = class extends Emitter {
1501
1489
  ws.send(JSON.stringify({ e, d, i }));
1502
1490
  });
1503
1491
  }
1492
+ opened() {
1493
+ if (this.closed || this.isOpen) return Promise.resolve();
1494
+ return new Promise((resolve) => {
1495
+ const done = () => {
1496
+ clearTimeout(timer);
1497
+ offUp();
1498
+ offClosed();
1499
+ resolve();
1500
+ };
1501
+ const timer = setTimeout(done, REQUEST_TIMEOUT);
1502
+ const offUp = this.once("up", done);
1503
+ const offClosed = this.once("closed", done);
1504
+ });
1505
+ }
1504
1506
  close() {
1505
1507
  if (this.closed) return;
1506
1508
  this.closed = true;
@@ -2045,8 +2047,9 @@ var GameRoom = class extends Emitter {
2045
2047
  r.onMessage("server-message", (m) => {
2046
2048
  const text = str(m.msg);
2047
2049
  if (!text || text.startsWith("/")) return;
2048
- if (/No active players remain/i.test(text)) this.closing = true;
2049
- this.emit("room.server", { room: this, text });
2050
+ if (text === "sn_room_empty_closing" || /No active players remain/i.test(text)) this.closing = true;
2051
+ const args = Array.isArray(m.args) ? m.args.filter((arg) => typeof arg === "string" || typeof arg === "number") : [];
2052
+ this.emit("room.server", { room: this, text, args });
2050
2053
  });
2051
2054
  r.onMessage("popup", (m) => this.emit("room.popup", { room: this, title: str(m.title), text: str(m.message) }));
2052
2055
  r.onMessage("love", (m) => this.emit("love.received", { room: this, from: this.lookup(m.from) }));
@@ -2245,11 +2248,13 @@ var RESTART_FIRST_DELAY = 3e3;
2245
2248
  var RESTART_MAX_DELAY = 3e4;
2246
2249
  var RESTART_ATTEMPTS = 10;
2247
2250
  var RESTART_FATAL = /* @__PURE__ */ new Set(["BANNED", "KICKED", "BOTS_NOT_ALLOWED", "AUTH_FAILED", "APP_NO_ROOMS", "BOT_ROOM_LIMIT"]);
2251
+ var ROOM_CODE = /^[A-Z]{4}$/;
2248
2252
  function normalizeId(roomId) {
2249
2253
  const id = roomId.trim();
2250
2254
  return id.length === 4 ? id.toUpperCase() : id;
2251
2255
  }
2252
2256
  var appNoRooms = () => new WordBombError("APP_NO_ROOMS", "applications never join rooms, they get them from bot.apps");
2257
+ var notRoomCode = (roomId) => new WordBombError("INVALID", `${JSON.stringify(String(roomId).slice(0, 32))} is not a room code, codes are 4 letters like ABCD`);
2253
2258
  var unpackr = new import_msgpackr.Unpackr({ useRecords: true, structuredClone: false });
2254
2259
  var Lobby = class {
2255
2260
  constructor(client, locale) {
@@ -2327,8 +2332,9 @@ var Rooms = class {
2327
2332
  return this.open.get(normalizeId(roomId)) ?? null;
2328
2333
  }
2329
2334
  join(roomId, opts = {}) {
2330
- const id = normalizeId(roomId);
2331
2335
  if (this.ctx.isApp()) return Promise.reject(appNoRooms());
2336
+ const id = typeof roomId === "string" ? normalizeId(roomId) : "";
2337
+ if (!ROOM_CODE.test(id)) return Promise.reject(notRoomCode(roomId));
2332
2338
  return this.serial(async () => this.open.get(id) ?? this.adopt(await this.connectById(id, opts), opts));
2333
2339
  }
2334
2340
  create(opts = {}) {
@@ -2830,6 +2836,7 @@ var WordBomb = class _WordBomb extends Emitter {
2830
2836
  client;
2831
2837
  interactions;
2832
2838
  closed = false;
2839
+ requested = /* @__PURE__ */ new Set();
2833
2840
  static async connect(opts) {
2834
2841
  const bot = new _WordBomb(opts);
2835
2842
  await bot.session.refresh();
@@ -2914,6 +2921,7 @@ var WordBomb = class _WordBomb extends Emitter {
2914
2921
  });
2915
2922
  rt.on("down", (info) => this.emit("realtime.down", info));
2916
2923
  rt.on("banned", () => this.emit("banned", void 0));
2924
+ rt.on("replaced", () => this.fail(new WordBombError("REPLACED", "another process connected with this key, so this one stopped getting DMs, invites and commands")));
2917
2925
  rt.connect();
2918
2926
  }
2919
2927
  fail(e) {
@@ -2927,6 +2935,20 @@ var WordBomb = class _WordBomb extends Emitter {
2927
2935
  friendName(id) {
2928
2936
  return this.users.nameOf(id) || "a friend";
2929
2937
  }
2938
+ request(from) {
2939
+ if (this.requested.has(from.id)) return;
2940
+ this.requested.add(from.id);
2941
+ const req = this.friends.friendRequest(from);
2942
+ this.emit("friend.request", { ...req, decline: () => {
2943
+ this.requested.delete(from.id);
2944
+ return req.decline();
2945
+ } });
2946
+ }
2947
+ async pendingRequests() {
2948
+ if (this.kind === "app") return;
2949
+ const { incoming } = await this.friends.list();
2950
+ for (const from of incoming) this.request(from);
2951
+ }
2930
2952
  onRealtime(e, d) {
2931
2953
  const data = record(d);
2932
2954
  const id = str(data.id);
@@ -2955,6 +2977,7 @@ var WordBomb = class _WordBomb extends Emitter {
2955
2977
  const fid = str(f.id);
2956
2978
  if (fid) this.friends.online.set(fid, { status: str(f.status, "online"), room: friendRoom(f.room) });
2957
2979
  }
2980
+ if (num(data.requestsCount) > 0) this.pendingRequests().catch((e2) => this.fail(e2));
2958
2981
  return;
2959
2982
  case "friend:room": {
2960
2983
  if (!id) return;
@@ -2976,15 +2999,19 @@ var WordBomb = class _WordBomb extends Emitter {
2976
2999
  return;
2977
3000
  case "friend:request": {
2978
3001
  const ref = this.userRef(data.friend);
2979
- if (ref) this.emit("friend.request", this.friends.friendRequest(ref));
3002
+ if (ref) this.request(ref);
2980
3003
  return;
2981
3004
  }
2982
3005
  case "friend:request_canceled":
2983
- if (id) this.emit("friend.request.canceled", { id });
3006
+ if (!id) return;
3007
+ this.requested.delete(id);
3008
+ this.emit("friend.request.canceled", { id });
2984
3009
  return;
2985
3010
  case "friend:added": {
2986
3011
  const ref = this.userRef(data.friend);
2987
- if (ref) this.emit("friend.added", ref);
3012
+ if (!ref) return;
3013
+ this.requested.delete(ref.id);
3014
+ this.emit("friend.added", ref);
2988
3015
  return;
2989
3016
  }
2990
3017
  case "friend:removed":
@@ -3041,7 +3068,7 @@ function modeName(mode) {
3041
3068
  // package.json
3042
3069
  var package_default = {
3043
3070
  name: "@wordbombio/sdk",
3044
- version: "0.1.0",
3071
+ version: "0.2.0",
3045
3072
  description: "Build official bots for Word Bomb with a bot key",
3046
3073
  type: "module",
3047
3074
  main: "./dist/index.cjs",