@satorijs/adapter-discord 3.8.5 → 3.8.7

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/lib/index.js CHANGED
@@ -1322,15 +1322,17 @@ __name(setupReaction, "setupReaction");
1322
1322
  async function adaptSession(bot, input) {
1323
1323
  const session = bot.session({}, input);
1324
1324
  if (input.t === "MESSAGE_CREATE") {
1325
- if (input.d.webhook_id) {
1326
- const webhook = await bot.ensureWebhook(input.d.channel_id);
1327
- if (webhook.id === input.d.webhook_id) {
1328
- return;
1325
+ setupMessageGuildId(session, input.d.guild_id);
1326
+ if (input.d.webhook_id && !session.isDirect) {
1327
+ try {
1328
+ const webhook = await bot.ensureWebhook(input.d.channel_id);
1329
+ if (webhook.id === input.d.webhook_id)
1330
+ return;
1331
+ } catch (e) {
1329
1332
  }
1330
1333
  }
1331
1334
  session.type = "message";
1332
1335
  await decodeMessage(bot, input.d, session);
1333
- setupMessageGuildId(session, input.d.guild_id);
1334
1336
  } else if (input.t === "MESSAGE_UPDATE") {
1335
1337
  session.type = "message-updated";
1336
1338
  const message = await bot.internal.getChannelMessage(input.d.channel_id, input.d.id);
@@ -1384,7 +1386,7 @@ async function adaptSession(bot, input) {
1384
1386
  session.subtype = input.d.guild_id ? "group" : "private";
1385
1387
  session.channelId = input.d.channel_id;
1386
1388
  session.guildId = input.d.guild_id;
1387
- session.userId = input.d.member.user.id;
1389
+ session.userId = session.isDirect ? input.d.user.id : input.d.member.user.id;
1388
1390
  session.messageId = input.d.id;
1389
1391
  session.content = "";
1390
1392
  session.data.argv = decodeArgv(data, command);
@@ -1410,11 +1412,19 @@ var types = {
1410
1412
  channel: ApplicationCommand2.OptionType.STRING,
1411
1413
  guild: ApplicationCommand2.OptionType.STRING
1412
1414
  };
1415
+ var trimDescription = /* @__PURE__ */ __name((source) => {
1416
+ if (!source || source.length < 96)
1417
+ return source;
1418
+ return source.slice(0, 93) + "...";
1419
+ }, "trimDescription");
1420
+ var encodeDescription = /* @__PURE__ */ __name((object) => ({
1421
+ description: trimDescription(object.description[""] || object.name),
1422
+ description_localizations: (0, import_satori2.valueMap)((0, import_satori2.pick)(object.description, Locale3), trimDescription)
1423
+ }), "encodeDescription");
1413
1424
  var encodeCommand = /* @__PURE__ */ __name((cmd) => ({
1425
+ ...encodeDescription(cmd),
1414
1426
  name: cmd.name,
1415
1427
  type: ApplicationCommand2.Type.CHAT_INPUT,
1416
- description: cmd.description[""] || cmd.name,
1417
- description_localizations: (0, import_satori2.pick)(cmd.description, Locale3),
1418
1428
  options: encodeCommandOptions(cmd)
1419
1429
  }), "encodeCommand");
1420
1430
  var decodeArgv = /* @__PURE__ */ __name((data, command) => {
@@ -1433,7 +1443,7 @@ var decodeArgv = /* @__PURE__ */ __name((data, command) => {
1433
1443
  return result;
1434
1444
  }, "decodeArgv");
1435
1445
  function encodeCommandOptions(cmd) {
1436
- var _a, _b, _c, _d;
1446
+ var _a, _b;
1437
1447
  const result = [];
1438
1448
  if (cmd.children.length) {
1439
1449
  result.push(...cmd.children.map((child) => ({
@@ -1446,20 +1456,18 @@ function encodeCommandOptions(cmd) {
1446
1456
  } else {
1447
1457
  for (const arg of cmd.arguments) {
1448
1458
  result.push({
1459
+ ...encodeDescription(arg),
1449
1460
  name: arg.name.toLowerCase().replace(/[^a-z0-9]/g, ""),
1450
- description: arg.description[""] || arg.name,
1451
- description_localizations: (0, import_satori2.pick)(arg.description, Locale3),
1452
- type: (_a = types[arg.type]) != null ? _a : types.text,
1453
- required: (_b = arg.required) != null ? _b : false
1461
+ type: (_a = types[arg.type]) != null ? _a : types.text
1462
+ // required: arg.required ?? false,
1454
1463
  });
1455
1464
  }
1456
1465
  for (const option of cmd.options) {
1457
1466
  result.push({
1467
+ ...encodeDescription(option),
1458
1468
  name: option.name.toLowerCase(),
1459
- description: option.description[""] || option.name,
1460
- description_localizations: (0, import_satori2.pick)(option.description, Locale3),
1461
- type: (_c = types[option.type]) != null ? _c : types.text,
1462
- required: (_d = option.required) != null ? _d : false,
1469
+ type: (_b = types[option.type]) != null ? _b : types.text,
1470
+ // required: option.required ?? false,
1463
1471
  min_value: option.type === "posint" ? 1 : void 0
1464
1472
  });
1465
1473
  }
@@ -2066,7 +2074,8 @@ var _DiscordBot = class _DiscordBot extends import_satori5.Bot {
2066
2074
  async updateCommands(commands) {
2067
2075
  this.commands = commands;
2068
2076
  const local = Object.fromEntries(commands.map((cmd) => [cmd.name, cmd]));
2069
- const remote = Object.fromEntries((await this.internal.getGlobalApplicationCommands(this.selfId)).filter((cmd) => cmd.type === ApplicationCommand2.Type.CHAT_INPUT).map((cmd) => [cmd.name, cmd]));
2077
+ const remote = Object.fromEntries((await this.internal.getGlobalApplicationCommands(this.selfId, { with_localizations: true })).filter((cmd) => cmd.type === ApplicationCommand2.Type.CHAT_INPUT).map((cmd) => [cmd.name, cmd]));
2078
+ const updates = [];
2070
2079
  for (const key in { ...local, ...remote }) {
2071
2080
  if (!local[key]) {
2072
2081
  logger3.debug("delete command %s", key);
@@ -2077,26 +2086,31 @@ var _DiscordBot = class _DiscordBot extends import_satori5.Bot {
2077
2086
  logger3.debug(data, remote[key]);
2078
2087
  if (!remote[key]) {
2079
2088
  logger3.debug("create command: %s", local[key].name);
2080
- await this.internal.createGlobalApplicationCommand(this.selfId, data);
2081
- } else if (shapeEqual(data, remote[key])) {
2089
+ updates.push(data);
2090
+ } else if (!shapeEqual(data, remote[key])) {
2082
2091
  logger3.debug("edit command: %s", local[key].name);
2083
- await this.internal.editGlobalApplicationCommand(this.selfId, remote[key].id, data);
2092
+ updates.push(data);
2084
2093
  }
2085
2094
  }
2095
+ if (updates.length) {
2096
+ await this.internal.bulkOverwriteGlobalApplicationCommands(this.selfId, updates);
2097
+ }
2086
2098
  }
2087
2099
  };
2088
2100
  __name(_DiscordBot, "DiscordBot");
2089
2101
  __publicField(_DiscordBot, "MessageEncoder", DiscordMessageEncoder);
2090
2102
  var DiscordBot = _DiscordBot;
2091
- function shapeEqual(a, b, strict = false) {
2103
+ function shapeEqual(a, b) {
2092
2104
  if (a === b)
2093
2105
  return true;
2094
- if (!strict && (0, import_satori5.isNullable)(a) && (0, import_satori5.isNullable)(b))
2106
+ if ((0, import_satori5.isNullable)(a) && (0, import_satori5.isNullable)(b))
2095
2107
  return true;
2096
2108
  if (typeof a !== typeof b)
2097
2109
  return false;
2098
2110
  if (typeof a !== "object")
2099
2111
  return false;
2112
+ if (Object.values(a).every(import_satori5.isNullable) && (0, import_satori5.isNullable)(b))
2113
+ return true;
2100
2114
  if (!a || !b)
2101
2115
  return false;
2102
2116
  if (Array.isArray(a)) {
@@ -2106,7 +2120,7 @@ function shapeEqual(a, b, strict = false) {
2106
2120
  } else if (Array.isArray(b)) {
2107
2121
  return false;
2108
2122
  }
2109
- return Object.keys(a).every((key) => shapeEqual(a[key], b[key], strict));
2123
+ return Object.keys(a).every((key) => shapeEqual(a[key], b[key]));
2110
2124
  }
2111
2125
  __name(shapeEqual, "shapeEqual");
2112
2126
  ((DiscordBot2) => {