@satorijs/adapter-discord 3.8.4 → 3.8.6
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 +56 -47
- package/lib/index.js.map +2 -2
- package/lib/types/command.d.ts +7 -2
- package/package.json +2 -2
- package/readme.md +5 -0
package/lib/index.js
CHANGED
|
@@ -4,6 +4,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __getProtoOf = Object.getPrototypeOf;
|
|
6
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
8
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
9
|
var __export = (target, all) => {
|
|
9
10
|
for (var name in all)
|
|
@@ -26,6 +27,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
26
27
|
mod
|
|
27
28
|
));
|
|
28
29
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
var __publicField = (obj, key, value) => {
|
|
31
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
32
|
+
return value;
|
|
33
|
+
};
|
|
29
34
|
|
|
30
35
|
// satori/adapters/discord/src/index.ts
|
|
31
36
|
var src_exports = {};
|
|
@@ -1317,15 +1322,17 @@ __name(setupReaction, "setupReaction");
|
|
|
1317
1322
|
async function adaptSession(bot, input) {
|
|
1318
1323
|
const session = bot.session({}, input);
|
|
1319
1324
|
if (input.t === "MESSAGE_CREATE") {
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
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) {
|
|
1324
1332
|
}
|
|
1325
1333
|
}
|
|
1326
1334
|
session.type = "message";
|
|
1327
1335
|
await decodeMessage(bot, input.d, session);
|
|
1328
|
-
setupMessageGuildId(session, input.d.guild_id);
|
|
1329
1336
|
} else if (input.t === "MESSAGE_UPDATE") {
|
|
1330
1337
|
session.type = "message-updated";
|
|
1331
1338
|
const message = await bot.internal.getChannelMessage(input.d.channel_id, input.d.id);
|
|
@@ -1379,7 +1386,7 @@ async function adaptSession(bot, input) {
|
|
|
1379
1386
|
session.subtype = input.d.guild_id ? "group" : "private";
|
|
1380
1387
|
session.channelId = input.d.channel_id;
|
|
1381
1388
|
session.guildId = input.d.guild_id;
|
|
1382
|
-
session.userId = input.d.member.user.id;
|
|
1389
|
+
session.userId = session.isDirect ? input.d.user.id : input.d.member.user.id;
|
|
1383
1390
|
session.messageId = input.d.id;
|
|
1384
1391
|
session.content = "";
|
|
1385
1392
|
session.data.argv = decodeArgv(data, command);
|
|
@@ -1428,7 +1435,7 @@ var decodeArgv = /* @__PURE__ */ __name((data, command) => {
|
|
|
1428
1435
|
return result;
|
|
1429
1436
|
}, "decodeArgv");
|
|
1430
1437
|
function encodeCommandOptions(cmd) {
|
|
1431
|
-
var _a, _b
|
|
1438
|
+
var _a, _b;
|
|
1432
1439
|
const result = [];
|
|
1433
1440
|
if (cmd.children.length) {
|
|
1434
1441
|
result.push(...cmd.children.map((child) => ({
|
|
@@ -1444,8 +1451,8 @@ function encodeCommandOptions(cmd) {
|
|
|
1444
1451
|
name: arg.name.toLowerCase().replace(/[^a-z0-9]/g, ""),
|
|
1445
1452
|
description: arg.description[""] || arg.name,
|
|
1446
1453
|
description_localizations: (0, import_satori2.pick)(arg.description, Locale3),
|
|
1447
|
-
type: (_a = types[arg.type]) != null ? _a : types.text
|
|
1448
|
-
required:
|
|
1454
|
+
type: (_a = types[arg.type]) != null ? _a : types.text
|
|
1455
|
+
// required: arg.required ?? false,
|
|
1449
1456
|
});
|
|
1450
1457
|
}
|
|
1451
1458
|
for (const option of cmd.options) {
|
|
@@ -1453,8 +1460,8 @@ function encodeCommandOptions(cmd) {
|
|
|
1453
1460
|
name: option.name.toLowerCase(),
|
|
1454
1461
|
description: option.description[""] || option.name,
|
|
1455
1462
|
description_localizations: (0, import_satori2.pick)(option.description, Locale3),
|
|
1456
|
-
type: (
|
|
1457
|
-
required:
|
|
1463
|
+
type: (_b = types[option.type]) != null ? _b : types.text,
|
|
1464
|
+
// required: option.required ?? false,
|
|
1458
1465
|
min_value: option.type === "posint" ? 1 : void 0
|
|
1459
1466
|
});
|
|
1460
1467
|
}
|
|
@@ -1471,26 +1478,23 @@ var _State = class _State {
|
|
|
1471
1478
|
// forward: send the first message and create a thread
|
|
1472
1479
|
constructor(type) {
|
|
1473
1480
|
this.type = type;
|
|
1474
|
-
this.author = {};
|
|
1475
|
-
this.quote = {};
|
|
1476
|
-
this.channel = {};
|
|
1477
|
-
this.fakeMessageMap = {};
|
|
1478
|
-
// [userInput] = discord messages
|
|
1479
|
-
this.threadCreated = false;
|
|
1480
1481
|
}
|
|
1482
|
+
author = {};
|
|
1483
|
+
quote = {};
|
|
1484
|
+
channel = {};
|
|
1485
|
+
fakeMessageMap = {};
|
|
1486
|
+
// [userInput] = discord messages
|
|
1487
|
+
threadCreated = false;
|
|
1481
1488
|
};
|
|
1482
1489
|
__name(_State, "State");
|
|
1483
1490
|
var State = _State;
|
|
1484
1491
|
var _DiscordMessageEncoder = class _DiscordMessageEncoder extends import_satori3.MessageEncoder {
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
this.mode = "default";
|
|
1492
|
-
this.listType = null;
|
|
1493
|
-
}
|
|
1492
|
+
stack = [new State("message")];
|
|
1493
|
+
buffer = "";
|
|
1494
|
+
addition = {};
|
|
1495
|
+
figure = null;
|
|
1496
|
+
mode = "default";
|
|
1497
|
+
listType = null;
|
|
1494
1498
|
async getUrl() {
|
|
1495
1499
|
var _a, _b, _c, _d;
|
|
1496
1500
|
const input = (_b = (_a = this.options) == null ? void 0 : _a.session) == null ? void 0 : _b.discord;
|
|
@@ -1801,11 +1805,10 @@ var DiscordMessageEncoder = _DiscordMessageEncoder;
|
|
|
1801
1805
|
var import_satori4 = require("@satorijs/satori");
|
|
1802
1806
|
var logger2 = new import_satori4.Logger("discord");
|
|
1803
1807
|
var _WsClient = class _WsClient extends import_satori4.Adapter.WsClient {
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
}
|
|
1808
|
+
_d = 0;
|
|
1809
|
+
_ping;
|
|
1810
|
+
_sessionId = "";
|
|
1811
|
+
_resumeUrl;
|
|
1809
1812
|
async prepare() {
|
|
1810
1813
|
if (this._resumeUrl) {
|
|
1811
1814
|
return this.bot.http.ws(this._resumeUrl + "/?v=10&encoding=json");
|
|
@@ -1870,10 +1873,8 @@ var _WsClient = class _WsClient extends import_satori4.Adapter.WsClient {
|
|
|
1870
1873
|
if (parsed.t === "READY") {
|
|
1871
1874
|
this._sessionId = parsed.d.session_id;
|
|
1872
1875
|
this._resumeUrl = parsed.d.resume_gateway_url;
|
|
1873
|
-
const
|
|
1874
|
-
|
|
1875
|
-
delete self.userId;
|
|
1876
|
-
Object.assign(this.bot, self);
|
|
1876
|
+
const user = decodeUser(parsed.d.user);
|
|
1877
|
+
Object.assign(this.bot, user);
|
|
1877
1878
|
logger2.debug("session_id " + this._sessionId);
|
|
1878
1879
|
return this.bot.online();
|
|
1879
1880
|
}
|
|
@@ -1910,11 +1911,14 @@ var WsClient = _WsClient;
|
|
|
1910
1911
|
var import_package = require("../package.json");
|
|
1911
1912
|
var logger3 = new import_satori5.Logger("discord");
|
|
1912
1913
|
var _DiscordBot = class _DiscordBot extends import_satori5.Bot {
|
|
1914
|
+
http;
|
|
1915
|
+
internal;
|
|
1916
|
+
webhooks = {};
|
|
1917
|
+
webhookLock = {};
|
|
1918
|
+
commands = [];
|
|
1913
1919
|
constructor(ctx, config) {
|
|
1914
1920
|
super(ctx, config);
|
|
1915
|
-
this.
|
|
1916
|
-
this.webhookLock = {};
|
|
1917
|
-
this.commands = [];
|
|
1921
|
+
this.platform = "discord";
|
|
1918
1922
|
this.http = ctx.http.extend({
|
|
1919
1923
|
...config,
|
|
1920
1924
|
headers: {
|
|
@@ -2064,7 +2068,8 @@ var _DiscordBot = class _DiscordBot extends import_satori5.Bot {
|
|
|
2064
2068
|
async updateCommands(commands) {
|
|
2065
2069
|
this.commands = commands;
|
|
2066
2070
|
const local = Object.fromEntries(commands.map((cmd) => [cmd.name, cmd]));
|
|
2067
|
-
const remote = Object.fromEntries((await this.internal.getGlobalApplicationCommands(this.selfId)).filter((cmd) => cmd.type === ApplicationCommand2.Type.CHAT_INPUT).map((cmd) => [cmd.name, cmd]));
|
|
2071
|
+
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]));
|
|
2072
|
+
const updates = [];
|
|
2068
2073
|
for (const key in { ...local, ...remote }) {
|
|
2069
2074
|
if (!local[key]) {
|
|
2070
2075
|
logger3.debug("delete command %s", key);
|
|
@@ -2075,26 +2080,31 @@ var _DiscordBot = class _DiscordBot extends import_satori5.Bot {
|
|
|
2075
2080
|
logger3.debug(data, remote[key]);
|
|
2076
2081
|
if (!remote[key]) {
|
|
2077
2082
|
logger3.debug("create command: %s", local[key].name);
|
|
2078
|
-
|
|
2079
|
-
} else if (shapeEqual(data, remote[key])) {
|
|
2083
|
+
updates.push(data);
|
|
2084
|
+
} else if (!shapeEqual(data, remote[key])) {
|
|
2080
2085
|
logger3.debug("edit command: %s", local[key].name);
|
|
2081
|
-
|
|
2086
|
+
updates.push(data);
|
|
2082
2087
|
}
|
|
2083
2088
|
}
|
|
2089
|
+
if (updates.length) {
|
|
2090
|
+
await this.internal.bulkOverwriteGlobalApplicationCommands(this.selfId, updates);
|
|
2091
|
+
}
|
|
2084
2092
|
}
|
|
2085
2093
|
};
|
|
2086
2094
|
__name(_DiscordBot, "DiscordBot");
|
|
2087
|
-
_DiscordBot
|
|
2095
|
+
__publicField(_DiscordBot, "MessageEncoder", DiscordMessageEncoder);
|
|
2088
2096
|
var DiscordBot = _DiscordBot;
|
|
2089
|
-
function shapeEqual(a, b
|
|
2097
|
+
function shapeEqual(a, b) {
|
|
2090
2098
|
if (a === b)
|
|
2091
2099
|
return true;
|
|
2092
|
-
if (
|
|
2100
|
+
if ((0, import_satori5.isNullable)(a) && (0, import_satori5.isNullable)(b))
|
|
2093
2101
|
return true;
|
|
2094
2102
|
if (typeof a !== typeof b)
|
|
2095
2103
|
return false;
|
|
2096
2104
|
if (typeof a !== "object")
|
|
2097
2105
|
return false;
|
|
2106
|
+
if (Object.values(a).every(import_satori5.isNullable) && (0, import_satori5.isNullable)(b))
|
|
2107
|
+
return true;
|
|
2098
2108
|
if (!a || !b)
|
|
2099
2109
|
return false;
|
|
2100
2110
|
if (Array.isArray(a)) {
|
|
@@ -2104,7 +2114,7 @@ function shapeEqual(a, b, strict = false) {
|
|
|
2104
2114
|
} else if (Array.isArray(b)) {
|
|
2105
2115
|
return false;
|
|
2106
2116
|
}
|
|
2107
|
-
return Object.keys(a).every((key) => shapeEqual(a[key], b[key]
|
|
2117
|
+
return Object.keys(a).every((key) => shapeEqual(a[key], b[key]));
|
|
2108
2118
|
}
|
|
2109
2119
|
__name(shapeEqual, "shapeEqual");
|
|
2110
2120
|
((DiscordBot2) => {
|
|
@@ -2117,7 +2127,6 @@ __name(shapeEqual, "shapeEqual");
|
|
|
2117
2127
|
import_satori5.Quester.createConfig("https://discord.com/api/v10")
|
|
2118
2128
|
]);
|
|
2119
2129
|
})(DiscordBot || (DiscordBot = {}));
|
|
2120
|
-
DiscordBot.prototype.platform = "discord";
|
|
2121
2130
|
|
|
2122
2131
|
// satori/adapters/discord/src/index.ts
|
|
2123
2132
|
var src_default = DiscordBot;
|