@satorijs/adapter-discord 3.8.0 → 3.8.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 +67 -63
- package/lib/index.js.map +2 -2
- package/lib/types/message.d.ts +13 -5
- package/lib/utils.d.ts +1 -1
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -89,7 +89,7 @@ __export(utils_exports, {
|
|
|
89
89
|
encodeCommandOptions: () => encodeCommandOptions,
|
|
90
90
|
encodeRole: () => encodeRole,
|
|
91
91
|
sanitize: () => sanitize,
|
|
92
|
-
|
|
92
|
+
setupMessageGuildId: () => setupMessageGuildId
|
|
93
93
|
});
|
|
94
94
|
var import_satori2 = require("@satorijs/satori");
|
|
95
95
|
|
|
@@ -1212,8 +1212,8 @@ var encodeRole = /* @__PURE__ */ __name((role) => ({
|
|
|
1212
1212
|
async function decodeMessage(bot, meta, session = {}) {
|
|
1213
1213
|
var _a, _b, _c;
|
|
1214
1214
|
const { platform } = bot;
|
|
1215
|
-
setupMessage(session, meta);
|
|
1216
1215
|
session.messageId = meta.id;
|
|
1216
|
+
session.channelId = meta.channel_id;
|
|
1217
1217
|
session.timestamp = new Date(meta.timestamp).valueOf() || Date.now();
|
|
1218
1218
|
if (meta.author) {
|
|
1219
1219
|
session.author = decodeAuthor(meta.author);
|
|
@@ -1294,13 +1294,12 @@ async function decodeMessage(bot, meta, session = {}) {
|
|
|
1294
1294
|
return session;
|
|
1295
1295
|
}
|
|
1296
1296
|
__name(decodeMessage, "decodeMessage");
|
|
1297
|
-
function
|
|
1298
|
-
session.guildId =
|
|
1299
|
-
session.isDirect = !
|
|
1300
|
-
session.subtype =
|
|
1301
|
-
session.channelId = data.channel_id;
|
|
1297
|
+
function setupMessageGuildId(session, guildId) {
|
|
1298
|
+
session.guildId = guildId;
|
|
1299
|
+
session.isDirect = !guildId;
|
|
1300
|
+
session.subtype = guildId ? "group" : "private";
|
|
1302
1301
|
}
|
|
1303
|
-
__name(
|
|
1302
|
+
__name(setupMessageGuildId, "setupMessageGuildId");
|
|
1304
1303
|
function setupReaction(session, data) {
|
|
1305
1304
|
session.userId = data.user_id;
|
|
1306
1305
|
session.messageId = data.message_id;
|
|
@@ -1325,14 +1324,18 @@ async function adaptSession(bot, input) {
|
|
|
1325
1324
|
}
|
|
1326
1325
|
session.type = "message";
|
|
1327
1326
|
await decodeMessage(bot, input.d, session);
|
|
1327
|
+
setupMessageGuildId(session, input.d.guild_id);
|
|
1328
1328
|
} else if (input.t === "MESSAGE_UPDATE") {
|
|
1329
1329
|
session.type = "message-updated";
|
|
1330
|
-
const
|
|
1331
|
-
await decodeMessage(bot,
|
|
1330
|
+
const message = await bot.internal.getChannelMessage(input.d.channel_id, input.d.id);
|
|
1331
|
+
await decodeMessage(bot, message, session);
|
|
1332
|
+
const channel = await bot.internal.getChannel(input.d.channel_id);
|
|
1333
|
+
setupMessageGuildId(session, channel.guild_id);
|
|
1332
1334
|
} else if (input.t === "MESSAGE_DELETE") {
|
|
1333
1335
|
session.type = "message-deleted";
|
|
1334
1336
|
session.messageId = input.d.id;
|
|
1335
|
-
|
|
1337
|
+
session.channelId = input.d.channel_id;
|
|
1338
|
+
setupMessageGuildId(session, input.d.guild_id);
|
|
1336
1339
|
} else if (input.t === "MESSAGE_REACTION_ADD") {
|
|
1337
1340
|
session.type = "reaction-added";
|
|
1338
1341
|
setupReaction(session, input.d);
|
|
@@ -1486,14 +1489,14 @@ var DiscordMessageEncoder = class extends import_satori3.MessageEncoder {
|
|
|
1486
1489
|
this.mode = "default";
|
|
1487
1490
|
}
|
|
1488
1491
|
async getUrl() {
|
|
1489
|
-
var _a, _b;
|
|
1490
|
-
const input = this.options.session.discord;
|
|
1492
|
+
var _a, _b, _c, _d;
|
|
1493
|
+
const input = (_b = (_a = this.options) == null ? void 0 : _a.session) == null ? void 0 : _b.discord;
|
|
1491
1494
|
if ((input == null ? void 0 : input.t) === "INTERACTION_CREATE") {
|
|
1492
1495
|
return `/webhooks/${input.d.application_id}/${input.d.token}`;
|
|
1493
|
-
} else if (this.stack[0].type === "forward" && ((
|
|
1496
|
+
} else if (this.stack[0].type === "forward" && ((_c = this.stack[0].channel) == null ? void 0 : _c.id)) {
|
|
1494
1497
|
if (this.stack[1].author.nickname || this.stack[1].author.avatar) {
|
|
1495
1498
|
const webhook = await this.ensureWebhook();
|
|
1496
|
-
return `/webhooks/${webhook.id}/${webhook.token}?wait=true&thread_id=${(
|
|
1499
|
+
return `/webhooks/${webhook.id}/${webhook.token}?wait=true&thread_id=${(_d = this.stack[0].channel) == null ? void 0 : _d.id}`;
|
|
1497
1500
|
} else {
|
|
1498
1501
|
return `/channels/${this.stack[0].channel.id}/messages`;
|
|
1499
1502
|
}
|
|
@@ -1525,12 +1528,16 @@ var DiscordMessageEncoder = class extends import_satori3.MessageEncoder {
|
|
|
1525
1528
|
}
|
|
1526
1529
|
return message;
|
|
1527
1530
|
} catch (e) {
|
|
1528
|
-
if (import_satori3.Quester.isAxiosError(e) &&
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
this.bot.
|
|
1532
|
-
|
|
1533
|
-
|
|
1531
|
+
if (import_satori3.Quester.isAxiosError(e) && e.response) {
|
|
1532
|
+
if (((_a = e.response.data) == null ? void 0 : _a.code) === 10015) {
|
|
1533
|
+
logger.debug("webhook has been deleted, recreating..., %o", e.response.data);
|
|
1534
|
+
if (!this.bot.webhookLock[this.channelId])
|
|
1535
|
+
this.bot.webhooks[this.channelId] = null;
|
|
1536
|
+
await this.ensureWebhook();
|
|
1537
|
+
return this.post(data, headers);
|
|
1538
|
+
} else {
|
|
1539
|
+
e = new Error(`[${e.response.status}] ${JSON.stringify(e.response.data)}`);
|
|
1540
|
+
}
|
|
1534
1541
|
}
|
|
1535
1542
|
this.errors.push(e);
|
|
1536
1543
|
}
|
|
@@ -1664,6 +1671,45 @@ var DiscordMessageEncoder = class extends import_satori3.MessageEncoder {
|
|
|
1664
1671
|
content: this.buffer.trim()
|
|
1665
1672
|
});
|
|
1666
1673
|
this.buffer = "";
|
|
1674
|
+
} else if (type === "author") {
|
|
1675
|
+
const { avatar, nickname } = attrs;
|
|
1676
|
+
if (avatar)
|
|
1677
|
+
this.addition.avatar_url = avatar;
|
|
1678
|
+
if (nickname)
|
|
1679
|
+
this.addition.username = nickname;
|
|
1680
|
+
if (this.stack[0].type === "message") {
|
|
1681
|
+
this.stack[0].author = attrs;
|
|
1682
|
+
}
|
|
1683
|
+
if (this.stack[0].type === "forward") {
|
|
1684
|
+
this.stack[1].author = attrs;
|
|
1685
|
+
}
|
|
1686
|
+
} else if (type === "quote") {
|
|
1687
|
+
await this.flush();
|
|
1688
|
+
const parse = /* @__PURE__ */ __name((val) => val.replace(/\\([\\*_`~|()\[\]])/g, "$1"), "parse");
|
|
1689
|
+
const message = this.stack[this.stack[0].type === "forward" ? 1 : 0];
|
|
1690
|
+
if (!message.author.avatar && !message.author.nickname && this.stack[0].type !== "forward") {
|
|
1691
|
+
await this.flush();
|
|
1692
|
+
this.addition.message_reference = {
|
|
1693
|
+
message_id: attrs.id
|
|
1694
|
+
};
|
|
1695
|
+
} else {
|
|
1696
|
+
let replyId = attrs.id, channelId = this.channelId;
|
|
1697
|
+
if (this.stack[0].type === "forward" && ((_a = this.stack[0].fakeMessageMap[attrs.id]) == null ? void 0 : _a.length) >= 1) {
|
|
1698
|
+
replyId = this.stack[0].fakeMessageMap[attrs.id][0].messageId;
|
|
1699
|
+
channelId = this.stack[0].fakeMessageMap[attrs.id][0].channelId;
|
|
1700
|
+
}
|
|
1701
|
+
const quoted = await this.bot.getMessage(channelId, replyId);
|
|
1702
|
+
this.addition.embeds = [{
|
|
1703
|
+
description: [
|
|
1704
|
+
sanitize(parse(quoted.elements.filter((v) => v.type === "text").join("")).slice(0, 30)),
|
|
1705
|
+
`<t:${Math.ceil(quoted.timestamp / 1e3)}:R> [[ ↑ ]](https://discord.com/channels/${this.guildId}/${channelId}/${replyId})`
|
|
1706
|
+
].join("\n\n"),
|
|
1707
|
+
author: {
|
|
1708
|
+
name: quoted.author.nickname || quoted.author.username,
|
|
1709
|
+
icon_url: quoted.author.avatar
|
|
1710
|
+
}
|
|
1711
|
+
}];
|
|
1712
|
+
}
|
|
1667
1713
|
} else if (type === "figure") {
|
|
1668
1714
|
await this.flush();
|
|
1669
1715
|
this.mode = "figure";
|
|
@@ -1681,48 +1727,6 @@ var DiscordMessageEncoder = class extends import_satori3.MessageEncoder {
|
|
|
1681
1727
|
} else {
|
|
1682
1728
|
const resultLength = +this.results.length;
|
|
1683
1729
|
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
1730
|
await this.render(children);
|
|
1727
1731
|
await this.flush();
|
|
1728
1732
|
const newLength = +this.results.length;
|