@satorijs/adapter-discord 4.1.1 → 4.1.3

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/bot.d.ts CHANGED
@@ -3,14 +3,14 @@ import * as Discord from './utils';
3
3
  import { DiscordMessageEncoder } from './message';
4
4
  import { Internal, Webhook } from './types';
5
5
  import { WsClient } from './ws';
6
- export declare class DiscordBot extends Bot<DiscordBot.Config> {
6
+ export declare class DiscordBot<C extends Context = Context> extends Bot<C, DiscordBot.Config> {
7
7
  static MessageEncoder: typeof DiscordMessageEncoder;
8
8
  http: Quester;
9
9
  internal: Internal;
10
10
  webhooks: Record<string, Webhook>;
11
11
  webhookLock: Record<string, Promise<Webhook>>;
12
12
  commands: Universal.Command[];
13
- constructor(ctx: Context, config: DiscordBot.Config);
13
+ constructor(ctx: C, config: DiscordBot.Config);
14
14
  private _ensureWebhook;
15
15
  ensureWebhook(channelId: string): Promise<Discord.Webhook>;
16
16
  getLogin(): Promise<Universal.Login>;
package/lib/index.js CHANGED
@@ -1201,7 +1201,7 @@ var decodeUser = /* @__PURE__ */ __name((user) => ({
1201
1201
  }), "decodeUser");
1202
1202
  var decodeGuildMember = /* @__PURE__ */ __name((member) => ({
1203
1203
  user: member.user && decodeUser(member.user),
1204
- name: member.nick,
1204
+ nick: member.nick,
1205
1205
  roles: member.roles,
1206
1206
  joinedAt: member.joined_at && new Date(member.joined_at).valueOf()
1207
1207
  }), "decodeGuildMember");
@@ -1400,11 +1400,11 @@ async function adaptSession(bot, input) {
1400
1400
  session.event.argv = decodeArgv(data, command);
1401
1401
  } else if (input.t === "INTERACTION_CREATE" && input.d.type === Interaction.Type.MODAL_SUBMIT) {
1402
1402
  const data = input.d.data;
1403
- if (!data.custom_id.startsWith("input:"))
1403
+ if (!data.custom_id.startsWith("input") && !data.custom_id.includes(":"))
1404
1404
  return;
1405
1405
  const user_input = data.components[0].components[0].value;
1406
1406
  await bot.internal.createInteractionResponse(input.d.id, input.d.token, {
1407
- type: Interaction.CallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
1407
+ type: Interaction.CallbackType.DEFERRED_UPDATE_MESSAGE
1408
1408
  });
1409
1409
  session.type = "interaction/command";
1410
1410
  session.isDirect = !input.d.guild_id;
@@ -1416,19 +1416,19 @@ async function adaptSession(bot, input) {
1416
1416
  session.content = user_input;
1417
1417
  } else if (input.t === "INTERACTION_CREATE" && input.d.type === Interaction.Type.MESSAGE_COMPONENT) {
1418
1418
  const id = input.d.data.custom_id;
1419
- if (id.startsWith("input:")) {
1419
+ if (id.startsWith("input") && id.includes(":")) {
1420
1420
  await bot.internal.createInteractionResponse(input.d.id, input.d.token, {
1421
1421
  type: Interaction.CallbackType.MODAL,
1422
1422
  data: {
1423
1423
  custom_id: id,
1424
- title: "title",
1424
+ title: "Input",
1425
1425
  components: [{
1426
1426
  type: 1 /* ACTION_ROW */,
1427
1427
  components: [{
1428
1428
  custom_id: id,
1429
1429
  type: 4 /* TEXT_INPUT */,
1430
- label: "auto complete",
1431
- value: id.slice("input:".length),
1430
+ label: "Command",
1431
+ value: id.slice(id.indexOf(":") + 1),
1432
1432
  style: 1
1433
1433
  }]
1434
1434
  }]
@@ -1436,7 +1436,7 @@ async function adaptSession(bot, input) {
1436
1436
  });
1437
1437
  } else {
1438
1438
  await bot.internal.createInteractionResponse(input.d.id, input.d.token, {
1439
- type: Interaction.CallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
1439
+ type: Interaction.CallbackType.DEFERRED_UPDATE_MESSAGE
1440
1440
  });
1441
1441
  }
1442
1442
  session.type = "interaction/button";
@@ -1561,7 +1561,6 @@ var _DiscordMessageEncoder = class _DiscordMessageEncoder extends import_satori3
1561
1561
  mode = "default";
1562
1562
  listType = null;
1563
1563
  rows = [];
1564
- buttonGroupState = false;
1565
1564
  async getUrl() {
1566
1565
  var _a, _b, _c, _d;
1567
1566
  const input = (_b = (_a = this.options) == null ? void 0 : _a.session) == null ? void 0 : _b.discord;
@@ -1671,7 +1670,8 @@ var _DiscordMessageEncoder = class _DiscordMessageEncoder extends import_satori3
1671
1670
  }
1672
1671
  async flush() {
1673
1672
  const content = this.buffer.trim();
1674
- if (!content)
1673
+ this.trimButtons();
1674
+ if (!content && !this.rows.length)
1675
1675
  return;
1676
1676
  this.addition.components = this.rows;
1677
1677
  await this.post({ ...this.addition, content });
@@ -1680,29 +1680,57 @@ var _DiscordMessageEncoder = class _DiscordMessageEncoder extends import_satori3
1680
1680
  this.rows = [];
1681
1681
  }
1682
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 */;
1683
1690
  if (attrs.type === "link") {
1684
1691
  return {
1685
1692
  type: 2 /* BUTTON */,
1686
1693
  url: attrs.href,
1687
1694
  label,
1688
- style: 5
1695
+ style: 5 /* LINK */
1689
1696
  };
1690
- } else if (attrs.type === "action") {
1697
+ } else if (attrs.type === "input") {
1691
1698
  return {
1692
1699
  type: 2 /* BUTTON */,
1693
- custom_id: attrs.id,
1700
+ custom_id: `input${attrs.id}:${attrs.text}`,
1694
1701
  label,
1695
- style: 1
1702
+ style
1696
1703
  };
1697
- } else if (attrs.type === "input") {
1704
+ } else {
1698
1705
  return {
1699
1706
  type: 2 /* BUTTON */,
1700
- custom_id: "input:" + attrs.text,
1707
+ custom_id: attrs.id,
1701
1708
  label,
1702
- style: 1
1709
+ style
1703
1710
  };
1704
1711
  }
1705
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
+ }
1706
1734
  async visit(element) {
1707
1735
  var _a;
1708
1736
  const { type, attrs, children } = element;
@@ -1877,28 +1905,21 @@ var _DiscordMessageEncoder = class _DiscordMessageEncoder extends import_satori3
1877
1905
  }
1878
1906
  }
1879
1907
  } else if (type === "button") {
1880
- if (this.buttonGroupState) {
1881
- const last = this.rows[this.rows.length - 1];
1882
- last.components.push(this.decodeButton(
1883
- attrs,
1884
- children.join("")
1885
- ));
1886
- } else {
1887
- this.rows.push({
1888
- type: 1 /* ACTION_ROW */,
1889
- components: [
1890
- this.decodeButton(attrs, children.join(""))
1891
- ]
1892
- });
1893
- }
1908
+ const last = this.lastRow();
1909
+ last.components.push(this.decodeButton(
1910
+ attrs,
1911
+ children.join("")
1912
+ ));
1894
1913
  } else if (type === "button-group") {
1895
- this.buttonGroupState = true;
1896
1914
  this.rows.push({
1897
1915
  type: 1 /* ACTION_ROW */,
1898
1916
  components: []
1899
1917
  });
1900
1918
  await this.render(children);
1901
- this.buttonGroupState = false;
1919
+ this.rows.push({
1920
+ type: 1 /* ACTION_ROW */,
1921
+ components: []
1922
+ });
1902
1923
  } else if (type === "message" && attrs.forward) {
1903
1924
  this.stack.unshift(new State("forward"));
1904
1925
  await this.render(children);
@@ -1961,7 +1982,7 @@ var _WsClient = class _WsClient extends import_satori4.Adapter.WsClient {
1961
1982
  } catch (error) {
1962
1983
  return logger3.warn("cannot parse message", data);
1963
1984
  }
1964
- logger3.debug(require("util").inspect(parsed, false, null, true));
1985
+ logger3.debug(parsed);
1965
1986
  if (parsed.s) {
1966
1987
  this._d = parsed.s;
1967
1988
  }