@satorijs/adapter-discord 4.1.0 → 4.1.2

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
@@ -100,6 +100,7 @@ var import_satori2 = require("@satorijs/satori");
100
100
 
101
101
  // satori/adapters/discord/src/types/internal.ts
102
102
  var import_satori = require("@satorijs/satori");
103
+ var logger = new import_satori.Logger("discord");
103
104
  var _Internal = class _Internal {
104
105
  constructor(http) {
105
106
  this.http = http;
@@ -130,6 +131,7 @@ var _Internal = class _Internal {
130
131
  throw new Error(`too many arguments for ${path}, received ${raw}`);
131
132
  }
132
133
  try {
134
+ logger.debug(`${method} ${url}`, config);
133
135
  return await this.http(method, url, config);
134
136
  } catch (error) {
135
137
  if (!import_satori.Quester.isAxiosError(error) || !error.response)
@@ -1398,11 +1400,11 @@ async function adaptSession(bot, input) {
1398
1400
  session.event.argv = decodeArgv(data, command);
1399
1401
  } else if (input.t === "INTERACTION_CREATE" && input.d.type === Interaction.Type.MODAL_SUBMIT) {
1400
1402
  const data = input.d.data;
1401
- if (!data.custom_id.startsWith("input:"))
1403
+ if (!data.custom_id.startsWith("input") && !data.custom_id.includes(":"))
1402
1404
  return;
1403
1405
  const user_input = data.components[0].components[0].value;
1404
1406
  await bot.internal.createInteractionResponse(input.d.id, input.d.token, {
1405
- type: Interaction.CallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
1407
+ type: Interaction.CallbackType.DEFERRED_UPDATE_MESSAGE
1406
1408
  });
1407
1409
  session.type = "interaction/command";
1408
1410
  session.isDirect = !input.d.guild_id;
@@ -1414,19 +1416,19 @@ async function adaptSession(bot, input) {
1414
1416
  session.content = user_input;
1415
1417
  } else if (input.t === "INTERACTION_CREATE" && input.d.type === Interaction.Type.MESSAGE_COMPONENT) {
1416
1418
  const id = input.d.data.custom_id;
1417
- if (id.startsWith("input:")) {
1419
+ if (id.startsWith("input") && id.includes(":")) {
1418
1420
  await bot.internal.createInteractionResponse(input.d.id, input.d.token, {
1419
1421
  type: Interaction.CallbackType.MODAL,
1420
1422
  data: {
1421
1423
  custom_id: id,
1422
- title: "title",
1424
+ title: "Input",
1423
1425
  components: [{
1424
1426
  type: 1 /* ACTION_ROW */,
1425
1427
  components: [{
1426
1428
  custom_id: id,
1427
1429
  type: 4 /* TEXT_INPUT */,
1428
- label: "auto complete",
1429
- value: id.slice("input:".length),
1430
+ label: "Command",
1431
+ value: id.slice(id.indexOf(":") + 1),
1430
1432
  style: 1
1431
1433
  }]
1432
1434
  }]
@@ -1434,7 +1436,7 @@ async function adaptSession(bot, input) {
1434
1436
  });
1435
1437
  } else {
1436
1438
  await bot.internal.createInteractionResponse(input.d.id, input.d.token, {
1437
- type: Interaction.CallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
1439
+ type: Interaction.CallbackType.DEFERRED_UPDATE_MESSAGE
1438
1440
  });
1439
1441
  }
1440
1442
  session.type = "interaction/button";
@@ -1536,7 +1538,7 @@ __name(encodeCommandOptions, "encodeCommandOptions");
1536
1538
  // satori/adapters/discord/src/message.ts
1537
1539
  var import_satori3 = require("@satorijs/satori");
1538
1540
  var import_form_data = __toESM(require("form-data"));
1539
- var logger = new import_satori3.Logger("discord");
1541
+ var logger2 = new import_satori3.Logger("discord");
1540
1542
  var _State = class _State {
1541
1543
  // forward: send the first message and create a thread
1542
1544
  constructor(type) {
@@ -1559,7 +1561,6 @@ var _DiscordMessageEncoder = class _DiscordMessageEncoder extends import_satori3
1559
1561
  mode = "default";
1560
1562
  listType = null;
1561
1563
  rows = [];
1562
- buttonGroupState = false;
1563
1564
  async getUrl() {
1564
1565
  var _a, _b, _c, _d;
1565
1566
  const input = (_b = (_a = this.options) == null ? void 0 : _a.session) == null ? void 0 : _b.discord;
@@ -1606,7 +1607,7 @@ var _DiscordMessageEncoder = class _DiscordMessageEncoder extends import_satori3
1606
1607
  } catch (e) {
1607
1608
  if (import_satori3.Quester.isAxiosError(e) && e.response) {
1608
1609
  if (((_a = e.response.data) == null ? void 0 : _a.code) === 10015) {
1609
- logger.debug("webhook has been deleted, recreating..., %o", e.response.data);
1610
+ logger2.debug("webhook has been deleted, recreating..., %o", e.response.data);
1610
1611
  if (!this.bot.webhookLock[this.channelId])
1611
1612
  this.bot.webhooks[this.channelId] = null;
1612
1613
  await this.ensureWebhook();
@@ -1669,7 +1670,8 @@ var _DiscordMessageEncoder = class _DiscordMessageEncoder extends import_satori3
1669
1670
  }
1670
1671
  async flush() {
1671
1672
  const content = this.buffer.trim();
1672
- if (!content)
1673
+ this.trimButtons();
1674
+ if (!content && !this.rows.length)
1673
1675
  return;
1674
1676
  this.addition.components = this.rows;
1675
1677
  await this.post({ ...this.addition, content });
@@ -1678,29 +1680,57 @@ var _DiscordMessageEncoder = class _DiscordMessageEncoder extends import_satori3
1678
1680
  this.rows = [];
1679
1681
  }
1680
1682
  decodeButton(attrs, label) {
1683
+ let style = 1 /* PRIMARY */;
1684
+ if (attrs.class === "secondary")
1685
+ style = 2 /* SECONDARY */;
1686
+ if (attrs.class === "danger")
1687
+ style = 4 /* DANGER */;
1688
+ if (attrs.class === "success")
1689
+ style = 3 /* SUCCESS */;
1681
1690
  if (attrs.type === "link") {
1682
1691
  return {
1683
1692
  type: 2 /* BUTTON */,
1684
1693
  url: attrs.href,
1685
1694
  label,
1686
- style: 5
1695
+ style: 5 /* LINK */
1687
1696
  };
1688
- } else if (attrs.type === "action") {
1697
+ } else if (attrs.type === "input") {
1689
1698
  return {
1690
1699
  type: 2 /* BUTTON */,
1691
- custom_id: attrs.id,
1700
+ custom_id: `input${attrs.id}:${attrs.text}`,
1692
1701
  label,
1693
- style: 1
1702
+ style
1694
1703
  };
1695
- } else if (attrs.type === "input") {
1704
+ } else {
1696
1705
  return {
1697
1706
  type: 2 /* BUTTON */,
1698
- custom_id: "input:" + attrs.text,
1707
+ custom_id: attrs.id,
1699
1708
  label,
1700
- style: 1
1709
+ style
1701
1710
  };
1702
1711
  }
1703
1712
  }
1713
+ lastRow() {
1714
+ if (!this.rows.length) {
1715
+ this.rows.push({
1716
+ type: 1 /* ACTION_ROW */,
1717
+ components: []
1718
+ });
1719
+ }
1720
+ let last = this.rows[this.rows.length - 1];
1721
+ if (last.components.length >= 5) {
1722
+ this.rows.push({
1723
+ type: 1 /* ACTION_ROW */,
1724
+ components: []
1725
+ });
1726
+ last = this.rows[this.rows.length - 1];
1727
+ }
1728
+ return last;
1729
+ }
1730
+ trimButtons() {
1731
+ if (this.rows.length && this.rows[this.rows.length - 1].components.length === 0)
1732
+ this.rows.pop();
1733
+ }
1704
1734
  async visit(element) {
1705
1735
  var _a;
1706
1736
  const { type, attrs, children } = element;
@@ -1875,28 +1905,21 @@ var _DiscordMessageEncoder = class _DiscordMessageEncoder extends import_satori3
1875
1905
  }
1876
1906
  }
1877
1907
  } else if (type === "button") {
1878
- if (this.buttonGroupState) {
1879
- const last = this.rows[this.rows.length - 1];
1880
- last.components.push(this.decodeButton(
1881
- attrs,
1882
- children.join("")
1883
- ));
1884
- } else {
1885
- this.rows.push({
1886
- type: 1 /* ACTION_ROW */,
1887
- components: [
1888
- this.decodeButton(attrs, children.join(""))
1889
- ]
1890
- });
1891
- }
1908
+ const last = this.lastRow();
1909
+ last.components.push(this.decodeButton(
1910
+ attrs,
1911
+ children.join("")
1912
+ ));
1892
1913
  } else if (type === "button-group") {
1893
- this.buttonGroupState = true;
1894
1914
  this.rows.push({
1895
1915
  type: 1 /* ACTION_ROW */,
1896
1916
  components: []
1897
1917
  });
1898
1918
  await this.render(children);
1899
- this.buttonGroupState = false;
1919
+ this.rows.push({
1920
+ type: 1 /* ACTION_ROW */,
1921
+ components: []
1922
+ });
1900
1923
  } else if (type === "message" && attrs.forward) {
1901
1924
  this.stack.unshift(new State("forward"));
1902
1925
  await this.render(children);
@@ -1930,7 +1953,7 @@ var DiscordMessageEncoder = _DiscordMessageEncoder;
1930
1953
 
1931
1954
  // satori/adapters/discord/src/ws.ts
1932
1955
  var import_satori4 = require("@satorijs/satori");
1933
- var logger2 = new import_satori4.Logger("discord");
1956
+ var logger3 = new import_satori4.Logger("discord");
1934
1957
  var _WsClient = class _WsClient extends import_satori4.Adapter.WsClient {
1935
1958
  _d = 0;
1936
1959
  _ping;
@@ -1944,7 +1967,7 @@ var _WsClient = class _WsClient extends import_satori4.Adapter.WsClient {
1944
1967
  return this.bot.http.ws(url + "/?v=10&encoding=json");
1945
1968
  }
1946
1969
  heartbeat() {
1947
- logger2.debug(`heartbeat d ${this._d}`);
1970
+ logger3.debug(`heartbeat d ${this._d}`);
1948
1971
  this.socket.send(JSON.stringify({
1949
1972
  op: Gateway.Opcode.HEARTBEAT,
1950
1973
  d: this._d
@@ -1957,16 +1980,16 @@ var _WsClient = class _WsClient extends import_satori4.Adapter.WsClient {
1957
1980
  try {
1958
1981
  parsed = JSON.parse(data.toString());
1959
1982
  } catch (error) {
1960
- return logger2.warn("cannot parse message", data);
1983
+ return logger3.warn("cannot parse message", data);
1961
1984
  }
1962
- logger2.debug(require("util").inspect(parsed, false, null, true));
1985
+ logger3.debug(parsed);
1963
1986
  if (parsed.s) {
1964
1987
  this._d = parsed.s;
1965
1988
  }
1966
1989
  if (parsed.op === Gateway.Opcode.HELLO) {
1967
1990
  this._ping = setInterval(() => this.heartbeat(), parsed.d.heartbeat_interval);
1968
1991
  if (this._sessionId) {
1969
- logger2.debug("resuming");
1992
+ logger3.debug("resuming");
1970
1993
  this.socket.send(JSON.stringify({
1971
1994
  op: Gateway.Opcode.RESUME,
1972
1995
  d: {
@@ -1991,7 +2014,7 @@ var _WsClient = class _WsClient extends import_satori4.Adapter.WsClient {
1991
2014
  if (parsed.d)
1992
2015
  return;
1993
2016
  this._sessionId = "";
1994
- logger2.warn("offline: invalid session");
2017
+ logger3.warn("offline: invalid session");
1995
2018
  (_a = this.socket) == null ? void 0 : _a.close();
1996
2019
  }
1997
2020
  if (parsed.op === Gateway.Opcode.DISPATCH) {
@@ -2004,7 +2027,7 @@ var _WsClient = class _WsClient extends import_satori4.Adapter.WsClient {
2004
2027
  this._sessionId = parsed.d.session_id;
2005
2028
  this._resumeUrl = parsed.d.resume_gateway_url;
2006
2029
  this.bot.user = decodeUser(parsed.d.user);
2007
- logger2.debug("session_id " + this._sessionId);
2030
+ logger3.debug("session_id " + this._sessionId);
2008
2031
  return this.bot.online();
2009
2032
  }
2010
2033
  if (parsed.t === "RESUMED") {
@@ -2015,7 +2038,7 @@ var _WsClient = class _WsClient extends import_satori4.Adapter.WsClient {
2015
2038
  this.bot.dispatch(session);
2016
2039
  }
2017
2040
  if (parsed.op === Gateway.Opcode.RECONNECT) {
2018
- logger2.warn("offline: discord request reconnect");
2041
+ logger3.warn("offline: discord request reconnect");
2019
2042
  (_b = this.socket) == null ? void 0 : _b.close();
2020
2043
  }
2021
2044
  });
@@ -2037,7 +2060,7 @@ var WsClient = _WsClient;
2037
2060
 
2038
2061
  // satori/adapters/discord/src/bot.ts
2039
2062
  var import_package = require("../package.json");
2040
- var logger3 = new import_satori5.Logger("discord");
2063
+ var logger4 = new import_satori5.Logger("discord");
2041
2064
  var _DiscordBot = class _DiscordBot extends import_satori5.Bot {
2042
2065
  http;
2043
2066
  internal;
@@ -2199,7 +2222,7 @@ var _DiscordBot = class _DiscordBot extends import_satori5.Bot {
2199
2222
  this.commands = commands;
2200
2223
  const updates = commands.map(encodeCommand);
2201
2224
  if (updates.length) {
2202
- logger3.debug("update %d command(s)", updates.length);
2225
+ logger4.debug("update %d command(s)", updates.length);
2203
2226
  await this.internal.bulkOverwriteGlobalApplicationCommands(this.selfId, updates);
2204
2227
  }
2205
2228
  }