@satorijs/adapter-discord 3.8.1 → 3.8.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
@@ -41,7 +41,7 @@ export declare class DiscordBot extends Bot<DiscordBot.Config> {
41
41
  setGuildMemberRole(guildId: string, userId: string, roleId: string): Promise<void>;
42
42
  unsetGuildMemberRole(guildId: string, userId: string, roleId: string): Promise<void>;
43
43
  getGuildRoles(guildId: string): Promise<Universal.Role[]>;
44
- createGuildRole(guildId: string, data: Partial<Universal.Role>): Promise<Universal.Role>;
44
+ createGuildRole(guildId: string, data: Partial<Universal.Role>): Promise<string>;
45
45
  modifyGuildRole(guildId: string, roleId: string, data: Partial<Universal.Role>): Promise<void>;
46
46
  deleteGuildRole(guildId: string, roleId: string): Promise<void>;
47
47
  sendPrivateMessage(userId: string, content: Fragment, options?: SendOptions): Promise<string[]>;
package/lib/index.js CHANGED
@@ -89,13 +89,13 @@ __export(utils_exports, {
89
89
  encodeCommandOptions: () => encodeCommandOptions,
90
90
  encodeRole: () => encodeRole,
91
91
  sanitize: () => sanitize,
92
- setupMessage: () => setupMessage
92
+ setupMessageGuildId: () => setupMessageGuildId
93
93
  });
94
94
  var import_satori2 = require("@satorijs/satori");
95
95
 
96
96
  // satori/adapters/discord/src/types/internal.ts
97
97
  var import_satori = require("@satorijs/satori");
98
- var Internal = class {
98
+ var _Internal = class _Internal {
99
99
  constructor(http) {
100
100
  this.http = http;
101
101
  }
@@ -104,7 +104,7 @@ var Internal = class {
104
104
  for (const key in routes[path]) {
105
105
  const method = key;
106
106
  for (const name of (0, import_satori.makeArray)(routes[path][method])) {
107
- Internal.prototype[name] = async function(...args) {
107
+ _Internal.prototype[name] = async function(...args) {
108
108
  const raw = args.join(", ");
109
109
  const url = path.replace(/\{([^}]+)\}/g, () => {
110
110
  if (!args.length)
@@ -137,7 +137,8 @@ var Internal = class {
137
137
  }
138
138
  }
139
139
  };
140
- __name(Internal, "Internal");
140
+ __name(_Internal, "Internal");
141
+ var Internal = _Internal;
141
142
 
142
143
  // satori/adapters/discord/src/types/application.ts
143
144
  var ApplicationFlag = /* @__PURE__ */ ((ApplicationFlag2) => {
@@ -1212,8 +1213,8 @@ var encodeRole = /* @__PURE__ */ __name((role) => ({
1212
1213
  async function decodeMessage(bot, meta, session = {}) {
1213
1214
  var _a, _b, _c;
1214
1215
  const { platform } = bot;
1215
- setupMessage(session, meta);
1216
1216
  session.messageId = meta.id;
1217
+ session.channelId = meta.channel_id;
1217
1218
  session.timestamp = new Date(meta.timestamp).valueOf() || Date.now();
1218
1219
  if (meta.author) {
1219
1220
  session.author = decodeAuthor(meta.author);
@@ -1294,13 +1295,12 @@ async function decodeMessage(bot, meta, session = {}) {
1294
1295
  return session;
1295
1296
  }
1296
1297
  __name(decodeMessage, "decodeMessage");
1297
- function setupMessage(session, data) {
1298
- session.guildId = data.guild_id;
1299
- session.isDirect = !data.guild_id;
1300
- session.subtype = data.guild_id ? "group" : "private";
1301
- session.channelId = data.channel_id;
1298
+ function setupMessageGuildId(session, guildId) {
1299
+ session.guildId = guildId;
1300
+ session.isDirect = !guildId;
1301
+ session.subtype = guildId ? "group" : "private";
1302
1302
  }
1303
- __name(setupMessage, "setupMessage");
1303
+ __name(setupMessageGuildId, "setupMessageGuildId");
1304
1304
  function setupReaction(session, data) {
1305
1305
  session.userId = data.user_id;
1306
1306
  session.messageId = data.message_id;
@@ -1325,14 +1325,18 @@ async function adaptSession(bot, input) {
1325
1325
  }
1326
1326
  session.type = "message";
1327
1327
  await decodeMessage(bot, input.d, session);
1328
+ setupMessageGuildId(session, input.d.guild_id);
1328
1329
  } else if (input.t === "MESSAGE_UPDATE") {
1329
1330
  session.type = "message-updated";
1330
- const msg = await bot.internal.getChannelMessage(input.d.channel_id, input.d.id);
1331
- await decodeMessage(bot, msg, session);
1331
+ const message = await bot.internal.getChannelMessage(input.d.channel_id, input.d.id);
1332
+ await decodeMessage(bot, message, session);
1333
+ const channel = await bot.internal.getChannel(input.d.channel_id);
1334
+ setupMessageGuildId(session, channel.guild_id);
1332
1335
  } else if (input.t === "MESSAGE_DELETE") {
1333
1336
  session.type = "message-deleted";
1334
1337
  session.messageId = input.d.id;
1335
- setupMessage(session, input.d);
1338
+ session.channelId = input.d.channel_id;
1339
+ setupMessageGuildId(session, input.d.guild_id);
1336
1340
  } else if (input.t === "MESSAGE_REACTION_ADD") {
1337
1341
  session.type = "reaction-added";
1338
1342
  setupReaction(session, input.d);
@@ -1463,7 +1467,7 @@ __name(encodeCommandOptions, "encodeCommandOptions");
1463
1467
  var import_satori3 = require("@satorijs/satori");
1464
1468
  var import_form_data = __toESM(require("form-data"));
1465
1469
  var logger = new import_satori3.Logger("discord");
1466
- var State = class {
1470
+ var _State = class _State {
1467
1471
  // forward: send the first message and create a thread
1468
1472
  constructor(type) {
1469
1473
  this.type = type;
@@ -1475,8 +1479,9 @@ var State = class {
1475
1479
  this.threadCreated = false;
1476
1480
  }
1477
1481
  };
1478
- __name(State, "State");
1479
- var DiscordMessageEncoder = class extends import_satori3.MessageEncoder {
1482
+ __name(_State, "State");
1483
+ var State = _State;
1484
+ var _DiscordMessageEncoder = class _DiscordMessageEncoder extends import_satori3.MessageEncoder {
1480
1485
  constructor() {
1481
1486
  super(...arguments);
1482
1487
  this.stack = [new State("message")];
@@ -1525,12 +1530,16 @@ var DiscordMessageEncoder = class extends import_satori3.MessageEncoder {
1525
1530
  }
1526
1531
  return message;
1527
1532
  } catch (e) {
1528
- if (import_satori3.Quester.isAxiosError(e) && ((_a = e.response) == null ? void 0 : _a.data.code) === 10015) {
1529
- logger.debug("webhook has been deleted, recreating..., %o", e.response.data);
1530
- if (!this.bot.webhookLock[this.channelId])
1531
- this.bot.webhooks[this.channelId] = null;
1532
- await this.ensureWebhook();
1533
- return this.post(data, headers);
1533
+ if (import_satori3.Quester.isAxiosError(e) && e.response) {
1534
+ if (((_a = e.response.data) == null ? void 0 : _a.code) === 10015) {
1535
+ logger.debug("webhook has been deleted, recreating..., %o", e.response.data);
1536
+ if (!this.bot.webhookLock[this.channelId])
1537
+ this.bot.webhooks[this.channelId] = null;
1538
+ await this.ensureWebhook();
1539
+ return this.post(data, headers);
1540
+ } else {
1541
+ e = new Error(`[${e.response.status}] ${JSON.stringify(e.response.data)}`);
1542
+ }
1534
1543
  }
1535
1544
  this.errors.push(e);
1536
1545
  }
@@ -1664,6 +1673,45 @@ var DiscordMessageEncoder = class extends import_satori3.MessageEncoder {
1664
1673
  content: this.buffer.trim()
1665
1674
  });
1666
1675
  this.buffer = "";
1676
+ } else if (type === "author") {
1677
+ const { avatar, nickname } = attrs;
1678
+ if (avatar)
1679
+ this.addition.avatar_url = avatar;
1680
+ if (nickname)
1681
+ this.addition.username = nickname;
1682
+ if (this.stack[0].type === "message") {
1683
+ this.stack[0].author = attrs;
1684
+ }
1685
+ if (this.stack[0].type === "forward") {
1686
+ this.stack[1].author = attrs;
1687
+ }
1688
+ } else if (type === "quote") {
1689
+ await this.flush();
1690
+ const parse = /* @__PURE__ */ __name((val) => val.replace(/\\([\\*_`~|()\[\]])/g, "$1"), "parse");
1691
+ const message = this.stack[this.stack[0].type === "forward" ? 1 : 0];
1692
+ if (!message.author.avatar && !message.author.nickname && this.stack[0].type !== "forward") {
1693
+ await this.flush();
1694
+ this.addition.message_reference = {
1695
+ message_id: attrs.id
1696
+ };
1697
+ } else {
1698
+ let replyId = attrs.id, channelId = this.channelId;
1699
+ if (this.stack[0].type === "forward" && ((_a = this.stack[0].fakeMessageMap[attrs.id]) == null ? void 0 : _a.length) >= 1) {
1700
+ replyId = this.stack[0].fakeMessageMap[attrs.id][0].messageId;
1701
+ channelId = this.stack[0].fakeMessageMap[attrs.id][0].channelId;
1702
+ }
1703
+ const quoted = await this.bot.getMessage(channelId, replyId);
1704
+ this.addition.embeds = [{
1705
+ description: [
1706
+ sanitize(parse(quoted.elements.filter((v) => v.type === "text").join("")).slice(0, 30)),
1707
+ `<t:${Math.ceil(quoted.timestamp / 1e3)}:R> [[ ↑ ]](https://discord.com/channels/${this.guildId}/${channelId}/${replyId})`
1708
+ ].join("\n\n"),
1709
+ author: {
1710
+ name: quoted.author.nickname || quoted.author.username,
1711
+ icon_url: quoted.author.avatar
1712
+ }
1713
+ }];
1714
+ }
1667
1715
  } else if (type === "figure") {
1668
1716
  await this.flush();
1669
1717
  this.mode = "figure";
@@ -1681,48 +1729,6 @@ var DiscordMessageEncoder = class extends import_satori3.MessageEncoder {
1681
1729
  } else {
1682
1730
  const resultLength = +this.results.length;
1683
1731
  await this.flush();
1684
- const [author] = import_satori3.segment.select(children, "author");
1685
- if (author) {
1686
- const { avatar, nickname } = author.attrs;
1687
- if (avatar)
1688
- this.addition.avatar_url = avatar;
1689
- if (nickname)
1690
- this.addition.username = nickname;
1691
- if (this.stack[0].type === "message") {
1692
- this.stack[0].author = author.attrs;
1693
- }
1694
- if (this.stack[0].type === "forward") {
1695
- this.stack[1].author = author.attrs;
1696
- }
1697
- }
1698
- const [quote] = import_satori3.segment.select(children, "quote");
1699
- if (quote) {
1700
- const parse = /* @__PURE__ */ __name((val) => val.replace(/\\([\\*_`~|()\[\]])/g, "$1"), "parse");
1701
- const message = this.stack[this.stack[0].type === "forward" ? 1 : 0];
1702
- if (!message.author.avatar && !message.author.nickname && this.stack[0].type !== "forward") {
1703
- await this.flush();
1704
- this.addition.message_reference = {
1705
- message_id: quote.attrs.id
1706
- };
1707
- } else {
1708
- let replyId = quote.attrs.id, channelId = this.channelId;
1709
- if (this.stack[0].type === "forward" && ((_a = this.stack[0].fakeMessageMap[quote.attrs.id]) == null ? void 0 : _a.length) >= 1) {
1710
- replyId = this.stack[0].fakeMessageMap[quote.attrs.id][0].messageId;
1711
- channelId = this.stack[0].fakeMessageMap[quote.attrs.id][0].channelId;
1712
- }
1713
- const quoted = await this.bot.getMessage(channelId, replyId);
1714
- this.addition.embeds = [{
1715
- description: [
1716
- sanitize(parse(quoted.elements.filter((v) => v.type === "text").join("")).slice(0, 30)),
1717
- `<t:${Math.ceil(quoted.timestamp / 1e3)}:R> [[ ↑ ]](https://discord.com/channels/${this.guildId}/${channelId}/${replyId})`
1718
- ].join("\n\n"),
1719
- author: {
1720
- name: quoted.author.nickname || quoted.author.username,
1721
- icon_url: quoted.author.avatar
1722
- }
1723
- }];
1724
- }
1725
- }
1726
1732
  await this.render(children);
1727
1733
  await this.flush();
1728
1734
  const newLength = +this.results.length;
@@ -1751,7 +1757,8 @@ var DiscordMessageEncoder = class extends import_satori3.MessageEncoder {
1751
1757
  }
1752
1758
  }
1753
1759
  };
1754
- __name(DiscordMessageEncoder, "DiscordMessageEncoder");
1760
+ __name(_DiscordMessageEncoder, "DiscordMessageEncoder");
1761
+ var DiscordMessageEncoder = _DiscordMessageEncoder;
1755
1762
  ((DiscordMessageEncoder2) => {
1756
1763
  DiscordMessageEncoder2.Config = import_satori3.Schema.object({
1757
1764
  handleExternalAsset: import_satori3.Schema.union([
@@ -1770,7 +1777,7 @@ __name(DiscordMessageEncoder, "DiscordMessageEncoder");
1770
1777
  // satori/adapters/discord/src/ws.ts
1771
1778
  var import_satori4 = require("@satorijs/satori");
1772
1779
  var logger2 = new import_satori4.Logger("discord");
1773
- var WsClient = class extends import_satori4.Adapter.WsClient {
1780
+ var _WsClient = class _WsClient extends import_satori4.Adapter.WsClient {
1774
1781
  constructor() {
1775
1782
  super(...arguments);
1776
1783
  this._d = 0;
@@ -1865,7 +1872,8 @@ var WsClient = class extends import_satori4.Adapter.WsClient {
1865
1872
  });
1866
1873
  }
1867
1874
  };
1868
- __name(WsClient, "WsClient");
1875
+ __name(_WsClient, "WsClient");
1876
+ var WsClient = _WsClient;
1869
1877
  ((WsClient2) => {
1870
1878
  WsClient2.Config = import_satori4.Schema.intersect([
1871
1879
  import_satori4.Schema.object({
@@ -1878,7 +1886,7 @@ __name(WsClient, "WsClient");
1878
1886
  // satori/adapters/discord/src/bot.ts
1879
1887
  var import_package = require("../package.json");
1880
1888
  var logger3 = new import_satori5.Logger("discord");
1881
- var DiscordBot = class extends import_satori5.Bot {
1889
+ var _DiscordBot = class _DiscordBot extends import_satori5.Bot {
1882
1890
  constructor(ctx, config) {
1883
1891
  super(ctx, config);
1884
1892
  this.webhooks = {};
@@ -2016,7 +2024,7 @@ var DiscordBot = class extends import_satori5.Bot {
2016
2024
  }
2017
2025
  async createGuildRole(guildId, data) {
2018
2026
  const role = await this.internal.createGuildRole(guildId, encodeRole(data));
2019
- return decodeRole(role);
2027
+ return role.id;
2020
2028
  }
2021
2029
  async modifyGuildRole(guildId, roleId, data) {
2022
2030
  await this.internal.modifyGuildRole(guildId, roleId, encodeRole(data));
@@ -2052,8 +2060,9 @@ var DiscordBot = class extends import_satori5.Bot {
2052
2060
  }
2053
2061
  }
2054
2062
  };
2055
- __name(DiscordBot, "DiscordBot");
2056
- DiscordBot.MessageEncoder = DiscordMessageEncoder;
2063
+ __name(_DiscordBot, "DiscordBot");
2064
+ _DiscordBot.MessageEncoder = DiscordMessageEncoder;
2065
+ var DiscordBot = _DiscordBot;
2057
2066
  function shapeEqual(a, b, strict = false) {
2058
2067
  if (a === b)
2059
2068
  return true;