@satorijs/adapter-discord 4.0.0 → 4.0.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/bot.d.ts +9 -17
- package/lib/index.js +98 -96
- package/lib/index.js.map +3 -3
- package/lib/types/emoji.d.ts +20 -5
- package/lib/utils.d.ts +4 -4
- package/lib/ws.d.ts +1 -1
- package/package.json +2 -2
package/lib/bot.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Bot, Context, Fragment, Quester, Schema,
|
|
1
|
+
import { Bot, Context, Fragment, Quester, Schema, Universal } from '@satorijs/satori';
|
|
2
2
|
import * as Discord from './utils';
|
|
3
3
|
import { DiscordMessageEncoder } from './message';
|
|
4
4
|
import { Internal, Webhook } from './types';
|
|
@@ -11,10 +11,9 @@ export declare class DiscordBot extends Bot<DiscordBot.Config> {
|
|
|
11
11
|
webhookLock: Record<string, Promise<Webhook>>;
|
|
12
12
|
commands: Universal.Command[];
|
|
13
13
|
constructor(ctx: Context, config: DiscordBot.Config);
|
|
14
|
-
session(payload?: any, input?: any): import("@satorijs/core").Session;
|
|
15
14
|
private _ensureWebhook;
|
|
16
15
|
ensureWebhook(channelId: string): Promise<Discord.Webhook>;
|
|
17
|
-
|
|
16
|
+
getLogin(): Promise<Universal.Login>;
|
|
18
17
|
deleteMessage(channelId: string, messageId: string): Promise<void>;
|
|
19
18
|
editMessage(channelId: string, messageId: string, content: Fragment): Promise<void>;
|
|
20
19
|
getMessage(channelId: string, messageId: string): Promise<Universal.Message>;
|
|
@@ -24,18 +23,11 @@ export declare class DiscordBot extends Bot<DiscordBot.Config> {
|
|
|
24
23
|
}>;
|
|
25
24
|
getUser(userId: string): Promise<Universal.User>;
|
|
26
25
|
getGuildMemberList(guildId: string, after?: string): Promise<{
|
|
27
|
-
data: Universal.
|
|
26
|
+
data: Universal.GuildMember[];
|
|
28
27
|
next: string;
|
|
29
28
|
}>;
|
|
30
29
|
getChannel(channelId: string): Promise<Universal.Channel>;
|
|
31
|
-
getGuildMember(guildId: string, userId: string): Promise<
|
|
32
|
-
nickname: string;
|
|
33
|
-
userId: string;
|
|
34
|
-
username?: string;
|
|
35
|
-
avatar?: string;
|
|
36
|
-
discriminator?: string;
|
|
37
|
-
isBot?: boolean;
|
|
38
|
-
}>;
|
|
30
|
+
getGuildMember(guildId: string, userId: string): Promise<Universal.GuildMember>;
|
|
39
31
|
kickGuildMember(guildId: string, userId: string): Promise<void>;
|
|
40
32
|
getGuild(guildId: string): Promise<Universal.Guild>;
|
|
41
33
|
getGuildList(after?: string): Promise<{
|
|
@@ -55,16 +47,16 @@ export declare class DiscordBot extends Bot<DiscordBot.Config> {
|
|
|
55
47
|
setGuildMemberRole(guildId: string, userId: string, roleId: string): Promise<void>;
|
|
56
48
|
unsetGuildMemberRole(guildId: string, userId: string, roleId: string): Promise<void>;
|
|
57
49
|
getGuildRoleList(guildId: string): Promise<{
|
|
58
|
-
data: Universal.
|
|
50
|
+
data: Universal.GuildRole[];
|
|
59
51
|
}>;
|
|
60
|
-
createGuildRole(guildId: string, data: Partial<Universal.
|
|
61
|
-
|
|
52
|
+
createGuildRole(guildId: string, data: Partial<Universal.GuildRole>): Promise<Universal.GuildRole>;
|
|
53
|
+
updateGuildRole(guildId: string, roleId: string, data: Partial<Universal.GuildRole>): Promise<void>;
|
|
62
54
|
deleteGuildRole(guildId: string, roleId: string): Promise<void>;
|
|
63
|
-
|
|
55
|
+
createDirectChannel(userId: string): Promise<Universal.Channel>;
|
|
64
56
|
updateCommands(commands: Universal.Command[]): Promise<void>;
|
|
65
57
|
}
|
|
66
58
|
export declare namespace DiscordBot {
|
|
67
|
-
interface Config extends
|
|
59
|
+
interface Config extends Quester.Config, DiscordMessageEncoder.Config, WsClient.Config {
|
|
68
60
|
token: string;
|
|
69
61
|
slash?: boolean;
|
|
70
62
|
}
|
package/lib/index.js
CHANGED
|
@@ -84,9 +84,9 @@ __export(utils_exports, {
|
|
|
84
84
|
VisibilityType: () => VisibilityType,
|
|
85
85
|
Webhook: () => Webhook2,
|
|
86
86
|
adaptSession: () => adaptSession,
|
|
87
|
-
decodeAuthor: () => decodeAuthor,
|
|
88
87
|
decodeChannel: () => decodeChannel,
|
|
89
88
|
decodeGuild: () => decodeGuild,
|
|
89
|
+
decodeGuildMember: () => decodeGuildMember,
|
|
90
90
|
decodeMessage: () => decodeMessage,
|
|
91
91
|
decodeRole: () => decodeRole,
|
|
92
92
|
decodeUser: () => decodeUser,
|
|
@@ -1189,28 +1189,29 @@ var Locale3 = [
|
|
|
1189
1189
|
// satori/adapters/discord/src/utils.ts
|
|
1190
1190
|
var sanitize = /* @__PURE__ */ __name((val) => val.replace(/[\\*_`~|()\[\]]/g, "\\$&").replace(/@everyone/g, () => "\\@everyone").replace(/@here/g, () => "\\@here"), "sanitize");
|
|
1191
1191
|
var decodeUser = /* @__PURE__ */ __name((user) => ({
|
|
1192
|
+
id: user.id,
|
|
1193
|
+
name: user.username,
|
|
1192
1194
|
userId: user.id,
|
|
1193
|
-
avatar: `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png`,
|
|
1195
|
+
avatar: user.avatar && `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png`,
|
|
1194
1196
|
username: user.username,
|
|
1195
1197
|
discriminator: user.discriminator,
|
|
1196
1198
|
isBot: user.bot || false
|
|
1197
1199
|
}), "decodeUser");
|
|
1200
|
+
var decodeGuildMember = /* @__PURE__ */ __name((member) => ({
|
|
1201
|
+
user: member.user && decodeUser(member.user),
|
|
1202
|
+
name: member.nick,
|
|
1203
|
+
roles: member.roles,
|
|
1204
|
+
joinedAt: member.joined_at && new Date(member.joined_at).valueOf()
|
|
1205
|
+
}), "decodeGuildMember");
|
|
1198
1206
|
var decodeGuild = /* @__PURE__ */ __name((data) => ({
|
|
1199
1207
|
id: data.id,
|
|
1200
|
-
name: data.name
|
|
1201
|
-
guildId: data.id,
|
|
1202
|
-
guildName: data.name
|
|
1208
|
+
name: data.name
|
|
1203
1209
|
}), "decodeGuild");
|
|
1204
1210
|
var decodeChannel = /* @__PURE__ */ __name((data) => ({
|
|
1205
1211
|
id: data.id,
|
|
1206
1212
|
name: data.name,
|
|
1207
|
-
|
|
1208
|
-
channelName: data.name
|
|
1213
|
+
type: data.type === Channel2.Type.DM ? import_satori2.Universal.Channel.Type.DIRECT : import_satori2.Universal.Channel.Type.TEXT
|
|
1209
1214
|
}), "decodeChannel");
|
|
1210
|
-
var decodeAuthor = /* @__PURE__ */ __name((author) => ({
|
|
1211
|
-
...decodeUser(author),
|
|
1212
|
-
nickname: author.username
|
|
1213
|
-
}), "decodeAuthor");
|
|
1214
1215
|
var decodeRole = /* @__PURE__ */ __name((role) => ({
|
|
1215
1216
|
...role,
|
|
1216
1217
|
permissions: BigInt(role.permissions)
|
|
@@ -1219,27 +1220,18 @@ var encodeRole = /* @__PURE__ */ __name((role) => ({
|
|
|
1219
1220
|
...role,
|
|
1220
1221
|
permissions: role.permissions && "" + role.permissions
|
|
1221
1222
|
}), "encodeRole");
|
|
1222
|
-
async function decodeMessage(bot,
|
|
1223
|
-
var _a
|
|
1223
|
+
async function decodeMessage(bot, data, message, payload = message, details = true) {
|
|
1224
|
+
var _a;
|
|
1224
1225
|
const { platform } = bot;
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
session.author = decodeAuthor(meta.author);
|
|
1230
|
-
session.userId = meta.author.id;
|
|
1231
|
-
}
|
|
1232
|
-
if ((_a = meta.member) == null ? void 0 : _a.nick) {
|
|
1233
|
-
session.author.nickname = (_b = meta.member) == null ? void 0 : _b.nick;
|
|
1234
|
-
}
|
|
1235
|
-
session.content = "";
|
|
1236
|
-
if (meta.content) {
|
|
1237
|
-
session.content = meta.content.replace(/<@[!&]?(.+?)>/g, (_, id) => {
|
|
1226
|
+
message.id = message.messageId = data.id;
|
|
1227
|
+
message.content = "";
|
|
1228
|
+
if (data.content) {
|
|
1229
|
+
message.content = data.content.replace(/<@[!&]?(.+?)>/g, (_, id) => {
|
|
1238
1230
|
var _a2;
|
|
1239
|
-
if (
|
|
1231
|
+
if (data.mention_roles.includes(id)) {
|
|
1240
1232
|
return (0, import_satori2.h)("at", { role: id }).toString();
|
|
1241
1233
|
} else {
|
|
1242
|
-
const user = (_a2 =
|
|
1234
|
+
const user = (_a2 = data.mentions) == null ? void 0 : _a2.find((u) => u.id === id || `${u.username}#${u.discriminator}` === id);
|
|
1243
1235
|
return import_satori2.h.at(id, { name: user == null ? void 0 : user.username }).toString();
|
|
1244
1236
|
}
|
|
1245
1237
|
}).replace(/<a?:(.*):(.+?)>/g, (_, name, id) => {
|
|
@@ -1249,28 +1241,28 @@ async function decodeMessage(bot, meta, session = {}, reference = true) {
|
|
|
1249
1241
|
]).toString();
|
|
1250
1242
|
}).replace(/@everyone/g, () => (0, import_satori2.h)("at", { type: "all" }).toString()).replace(/@here/g, () => (0, import_satori2.h)("at", { type: "here" }).toString()).replace(/<#(.+?)>/g, (_, id) => {
|
|
1251
1243
|
var _a2;
|
|
1252
|
-
const channel = (_a2 =
|
|
1244
|
+
const channel = (_a2 = data.mention_channels) == null ? void 0 : _a2.find((c) => c.id === id);
|
|
1253
1245
|
return import_satori2.h.sharp(id, { name: channel == null ? void 0 : channel.name }).toString();
|
|
1254
1246
|
});
|
|
1255
1247
|
}
|
|
1256
|
-
if ((
|
|
1257
|
-
if (
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
var _a2,
|
|
1248
|
+
if ((_a = data.attachments) == null ? void 0 : _a.length) {
|
|
1249
|
+
if (message.content)
|
|
1250
|
+
message.content += " ";
|
|
1251
|
+
message.content += data.attachments.map((v) => {
|
|
1252
|
+
var _a2, _b, _c;
|
|
1261
1253
|
if (v.height && v.width && ((_a2 = v.content_type) == null ? void 0 : _a2.startsWith("image/"))) {
|
|
1262
1254
|
return (0, import_satori2.h)("image", {
|
|
1263
1255
|
url: v.url,
|
|
1264
1256
|
proxy_url: v.proxy_url,
|
|
1265
1257
|
file: v.filename
|
|
1266
1258
|
});
|
|
1267
|
-
} else if (v.height && v.width && ((
|
|
1259
|
+
} else if (v.height && v.width && ((_b = v.content_type) == null ? void 0 : _b.startsWith("video/"))) {
|
|
1268
1260
|
return (0, import_satori2.h)("video", {
|
|
1269
1261
|
url: v.url,
|
|
1270
1262
|
proxy_url: v.proxy_url,
|
|
1271
1263
|
file: v.filename
|
|
1272
1264
|
});
|
|
1273
|
-
} else if ((
|
|
1265
|
+
} else if ((_c = v.content_type) == null ? void 0 : _c.startsWith("audio/")) {
|
|
1274
1266
|
return (0, import_satori2.h)("record", {
|
|
1275
1267
|
url: v.url,
|
|
1276
1268
|
proxy_url: v.proxy_url,
|
|
@@ -1285,23 +1277,32 @@ async function decodeMessage(bot, meta, session = {}, reference = true) {
|
|
|
1285
1277
|
}
|
|
1286
1278
|
}).join("");
|
|
1287
1279
|
}
|
|
1288
|
-
for (const embed of
|
|
1280
|
+
for (const embed of data.embeds) {
|
|
1289
1281
|
if (embed.image) {
|
|
1290
|
-
|
|
1282
|
+
message.content += (0, import_satori2.h)("image", { url: embed.image.url, proxy_url: embed.image.proxy_url });
|
|
1291
1283
|
}
|
|
1292
1284
|
if (embed.thumbnail) {
|
|
1293
|
-
|
|
1285
|
+
message.content += (0, import_satori2.h)("image", { url: embed.thumbnail.url, proxy_url: embed.thumbnail.proxy_url });
|
|
1294
1286
|
}
|
|
1295
1287
|
if (embed.video) {
|
|
1296
|
-
|
|
1288
|
+
message.content += (0, import_satori2.h)("video", { url: embed.video.url, proxy_url: embed.video.proxy_url });
|
|
1297
1289
|
}
|
|
1298
1290
|
}
|
|
1299
|
-
|
|
1300
|
-
if (
|
|
1301
|
-
const { message_id, channel_id } =
|
|
1302
|
-
|
|
1303
|
-
}
|
|
1304
|
-
|
|
1291
|
+
message.elements = import_satori2.h.parse(message.content);
|
|
1292
|
+
if (details && data.message_reference) {
|
|
1293
|
+
const { message_id, channel_id } = data.message_reference;
|
|
1294
|
+
message.quote = await bot.getMessage(channel_id, message_id);
|
|
1295
|
+
}
|
|
1296
|
+
if (!payload)
|
|
1297
|
+
return message;
|
|
1298
|
+
payload.channel = {
|
|
1299
|
+
id: data.channel_id,
|
|
1300
|
+
type: data.member ? import_satori2.Universal.Channel.Type.TEXT : import_satori2.Universal.Channel.Type.DIRECT
|
|
1301
|
+
};
|
|
1302
|
+
payload.user = decodeUser(data.author);
|
|
1303
|
+
payload.member = data.member && decodeGuildMember(data.member);
|
|
1304
|
+
payload.timestamp = new Date(data.timestamp).valueOf() || Date.now();
|
|
1305
|
+
return message;
|
|
1305
1306
|
}
|
|
1306
1307
|
__name(decodeMessage, "decodeMessage");
|
|
1307
1308
|
function setupMessageGuildId(session, guildId) {
|
|
@@ -1324,7 +1325,8 @@ function setupReaction(session, data) {
|
|
|
1324
1325
|
}
|
|
1325
1326
|
__name(setupReaction, "setupReaction");
|
|
1326
1327
|
async function adaptSession(bot, input) {
|
|
1327
|
-
const session = bot.session(
|
|
1328
|
+
const session = bot.session();
|
|
1329
|
+
session.setInternal("discord", input);
|
|
1328
1330
|
if (input.t === "MESSAGE_CREATE") {
|
|
1329
1331
|
setupMessageGuildId(session, input.d.guild_id);
|
|
1330
1332
|
if (input.d.webhook_id && !session.isDirect) {
|
|
@@ -1336,11 +1338,11 @@ async function adaptSession(bot, input) {
|
|
|
1336
1338
|
}
|
|
1337
1339
|
}
|
|
1338
1340
|
session.type = "message";
|
|
1339
|
-
await decodeMessage(bot, input.d, session);
|
|
1341
|
+
await decodeMessage(bot, input.d, session.event.message = {}, session.event);
|
|
1340
1342
|
} else if (input.t === "MESSAGE_UPDATE") {
|
|
1341
1343
|
session.type = "message-updated";
|
|
1342
1344
|
const message = await bot.internal.getChannelMessage(input.d.channel_id, input.d.id);
|
|
1343
|
-
await decodeMessage(bot, message, session);
|
|
1345
|
+
await decodeMessage(bot, message, session.event.message = {}, session.event);
|
|
1344
1346
|
const channel = await bot.internal.getChannel(input.d.channel_id);
|
|
1345
1347
|
setupMessageGuildId(session, channel.guild_id);
|
|
1346
1348
|
} else if (input.t === "MESSAGE_DELETE") {
|
|
@@ -1367,12 +1369,12 @@ async function adaptSession(bot, input) {
|
|
|
1367
1369
|
session.type = "guild-role-added";
|
|
1368
1370
|
session.guildId = input.d.guild_id;
|
|
1369
1371
|
session.roleId = input.d.role.id;
|
|
1370
|
-
session.
|
|
1372
|
+
session.event.role = decodeRole(input.d.role);
|
|
1371
1373
|
} else if (input.t === "GUILD_ROLE_UPDATE") {
|
|
1372
1374
|
session.type = "guild-role-updated";
|
|
1373
1375
|
session.guildId = input.d.guild_id;
|
|
1374
1376
|
session.roleId = input.d.role.id;
|
|
1375
|
-
session.
|
|
1377
|
+
session.event.role = decodeRole(input.d.role);
|
|
1376
1378
|
} else if (input.t === "GUILD_ROLE_DELETE") {
|
|
1377
1379
|
session.type = "guild-role-added";
|
|
1378
1380
|
session.guildId = input.d.guild_id;
|
|
@@ -1393,7 +1395,7 @@ async function adaptSession(bot, input) {
|
|
|
1393
1395
|
session.userId = session.isDirect ? input.d.user.id : input.d.member.user.id;
|
|
1394
1396
|
session.messageId = input.d.id;
|
|
1395
1397
|
session.content = "";
|
|
1396
|
-
session.
|
|
1398
|
+
session.event.argv = decodeArgv(data, command);
|
|
1397
1399
|
} else if (input.t === "CHANNEL_UPDATE") {
|
|
1398
1400
|
session.type = "channel-updated";
|
|
1399
1401
|
session.guildId = input.d.guild_id;
|
|
@@ -1511,14 +1513,14 @@ var _DiscordMessageEncoder = class _DiscordMessageEncoder extends import_satori3
|
|
|
1511
1513
|
if ((input == null ? void 0 : input.t) === "INTERACTION_CREATE") {
|
|
1512
1514
|
return `/webhooks/${input.d.application_id}/${input.d.token}`;
|
|
1513
1515
|
} else if (this.stack[0].type === "forward" && ((_c = this.stack[0].channel) == null ? void 0 : _c.id)) {
|
|
1514
|
-
if (this.stack[1].author.
|
|
1516
|
+
if (this.stack[1].author.name || this.stack[1].author.avatar) {
|
|
1515
1517
|
const webhook = await this.ensureWebhook();
|
|
1516
1518
|
return `/webhooks/${webhook.id}/${webhook.token}?wait=true&thread_id=${(_d = this.stack[0].channel) == null ? void 0 : _d.id}`;
|
|
1517
1519
|
} else {
|
|
1518
1520
|
return `/channels/${this.stack[0].channel.id}/messages`;
|
|
1519
1521
|
}
|
|
1520
1522
|
} else {
|
|
1521
|
-
if (this.stack[0].author.
|
|
1523
|
+
if (this.stack[0].author.name || this.stack[0].author.avatar || this.stack[0].type === "forward" && !this.stack[0].threadCreated) {
|
|
1522
1524
|
const webhook = await this.ensureWebhook();
|
|
1523
1525
|
return `/webhooks/${webhook.id}/${webhook.token}?wait=true`;
|
|
1524
1526
|
} else {
|
|
@@ -1532,9 +1534,13 @@ var _DiscordMessageEncoder = class _DiscordMessageEncoder extends import_satori3
|
|
|
1532
1534
|
const url = await this.getUrl();
|
|
1533
1535
|
const result = await this.bot.http.post(url, data, { headers });
|
|
1534
1536
|
const session = this.bot.session();
|
|
1535
|
-
const message = await decodeMessage(this.bot, result, session);
|
|
1537
|
+
const message = await decodeMessage(this.bot, result, session.event.message = {}, session.event);
|
|
1536
1538
|
session.app.emit(session, "send", session);
|
|
1537
|
-
this.results.push(session);
|
|
1539
|
+
this.results.push(session.event.message);
|
|
1540
|
+
Object.defineProperty(session.event.message, "channel", {
|
|
1541
|
+
configurable: true,
|
|
1542
|
+
get: () => session.event.channel
|
|
1543
|
+
});
|
|
1538
1544
|
if (this.stack[0].type === "forward" && !this.stack[0].threadCreated) {
|
|
1539
1545
|
this.stack[0].threadCreated = true;
|
|
1540
1546
|
const thread = await this.bot.internal.startThreadFromMessage(this.channelId, result.id, {
|
|
@@ -1735,7 +1741,7 @@ var _DiscordMessageEncoder = class _DiscordMessageEncoder extends import_satori3
|
|
|
1735
1741
|
await this.flush();
|
|
1736
1742
|
const parse = /* @__PURE__ */ __name((val) => val.replace(/\\([\\*_`~|()\[\]])/g, "$1"), "parse");
|
|
1737
1743
|
const message = this.stack[this.stack[0].type === "forward" ? 1 : 0];
|
|
1738
|
-
if (!message.author.avatar && !message.author.
|
|
1744
|
+
if (!message.author.avatar && !message.author.name && this.stack[0].type !== "forward") {
|
|
1739
1745
|
await this.flush();
|
|
1740
1746
|
this.addition.message_reference = {
|
|
1741
1747
|
message_id: attrs.id
|
|
@@ -1743,18 +1749,18 @@ var _DiscordMessageEncoder = class _DiscordMessageEncoder extends import_satori3
|
|
|
1743
1749
|
} else {
|
|
1744
1750
|
let replyId = attrs.id, channelId = this.channelId;
|
|
1745
1751
|
if (this.stack[0].type === "forward" && ((_a = this.stack[0].fakeMessageMap[attrs.id]) == null ? void 0 : _a.length) >= 1) {
|
|
1746
|
-
replyId = this.stack[0].fakeMessageMap[attrs.id][0].
|
|
1747
|
-
channelId = this.stack[0].fakeMessageMap[attrs.id][0].
|
|
1752
|
+
replyId = this.stack[0].fakeMessageMap[attrs.id][0].id;
|
|
1753
|
+
channelId = this.stack[0].fakeMessageMap[attrs.id][0].channel.id;
|
|
1748
1754
|
}
|
|
1749
|
-
const
|
|
1755
|
+
const quote = await this.bot.getMessage(channelId, replyId);
|
|
1750
1756
|
this.addition.embeds = [{
|
|
1751
1757
|
description: [
|
|
1752
|
-
sanitize(parse(
|
|
1753
|
-
`<t:${Math.ceil(
|
|
1758
|
+
sanitize(parse(quote.elements.filter((v) => v.type === "text").join("")).slice(0, 30)),
|
|
1759
|
+
`<t:${Math.ceil(quote.timestamp / 1e3)}:R> [[ ↑ ]](https://discord.com/channels/${this.guildId}/${channelId}/${replyId})`
|
|
1754
1760
|
].join("\n\n"),
|
|
1755
1761
|
author: {
|
|
1756
|
-
name:
|
|
1757
|
-
icon_url:
|
|
1762
|
+
name: quote.user.name,
|
|
1763
|
+
icon_url: quote.user.avatar
|
|
1758
1764
|
}
|
|
1759
1765
|
}];
|
|
1760
1766
|
}
|
|
@@ -1837,13 +1843,13 @@ var _WsClient = class _WsClient extends import_satori4.Adapter.WsClient {
|
|
|
1837
1843
|
}
|
|
1838
1844
|
heartbeat() {
|
|
1839
1845
|
logger2.debug(`heartbeat d ${this._d}`);
|
|
1840
|
-
this.
|
|
1846
|
+
this.socket.send(JSON.stringify({
|
|
1841
1847
|
op: Gateway.Opcode.HEARTBEAT,
|
|
1842
1848
|
d: this._d
|
|
1843
1849
|
}));
|
|
1844
1850
|
}
|
|
1845
1851
|
accept() {
|
|
1846
|
-
this.
|
|
1852
|
+
this.socket.addEventListener("message", async ({ data }) => {
|
|
1847
1853
|
var _a, _b;
|
|
1848
1854
|
let parsed;
|
|
1849
1855
|
try {
|
|
@@ -1859,7 +1865,7 @@ var _WsClient = class _WsClient extends import_satori4.Adapter.WsClient {
|
|
|
1859
1865
|
this._ping = setInterval(() => this.heartbeat(), parsed.d.heartbeat_interval);
|
|
1860
1866
|
if (this._sessionId) {
|
|
1861
1867
|
logger2.debug("resuming");
|
|
1862
|
-
this.
|
|
1868
|
+
this.socket.send(JSON.stringify({
|
|
1863
1869
|
op: Gateway.Opcode.RESUME,
|
|
1864
1870
|
d: {
|
|
1865
1871
|
token: this.bot.config.token,
|
|
@@ -1868,7 +1874,7 @@ var _WsClient = class _WsClient extends import_satori4.Adapter.WsClient {
|
|
|
1868
1874
|
}
|
|
1869
1875
|
}));
|
|
1870
1876
|
} else {
|
|
1871
|
-
this.
|
|
1877
|
+
this.socket.send(JSON.stringify({
|
|
1872
1878
|
op: Gateway.Opcode.IDENTIFY,
|
|
1873
1879
|
d: {
|
|
1874
1880
|
token: this.bot.config.token,
|
|
@@ -1884,15 +1890,18 @@ var _WsClient = class _WsClient extends import_satori4.Adapter.WsClient {
|
|
|
1884
1890
|
return;
|
|
1885
1891
|
this._sessionId = "";
|
|
1886
1892
|
logger2.warn("offline: invalid session");
|
|
1887
|
-
(_a = this.
|
|
1893
|
+
(_a = this.socket) == null ? void 0 : _a.close();
|
|
1888
1894
|
}
|
|
1889
1895
|
if (parsed.op === Gateway.Opcode.DISPATCH) {
|
|
1890
|
-
this.bot.
|
|
1896
|
+
this.bot.dispatch(this.bot.session({
|
|
1897
|
+
type: "internal",
|
|
1898
|
+
_type: "discord/" + parsed.t.toLowerCase().replace(/_/g, "-"),
|
|
1899
|
+
_data: parsed
|
|
1900
|
+
}));
|
|
1891
1901
|
if (parsed.t === "READY") {
|
|
1892
1902
|
this._sessionId = parsed.d.session_id;
|
|
1893
1903
|
this._resumeUrl = parsed.d.resume_gateway_url;
|
|
1894
|
-
|
|
1895
|
-
Object.assign(this.bot, user);
|
|
1904
|
+
this.bot.user = decodeUser(parsed.d.user);
|
|
1896
1905
|
logger2.debug("session_id " + this._sessionId);
|
|
1897
1906
|
return this.bot.online();
|
|
1898
1907
|
}
|
|
@@ -1905,10 +1914,10 @@ var _WsClient = class _WsClient extends import_satori4.Adapter.WsClient {
|
|
|
1905
1914
|
}
|
|
1906
1915
|
if (parsed.op === Gateway.Opcode.RECONNECT) {
|
|
1907
1916
|
logger2.warn("offline: discord request reconnect");
|
|
1908
|
-
(_b = this.
|
|
1917
|
+
(_b = this.socket) == null ? void 0 : _b.close();
|
|
1909
1918
|
}
|
|
1910
1919
|
});
|
|
1911
|
-
this.
|
|
1920
|
+
this.socket.addEventListener("close", () => {
|
|
1912
1921
|
clearInterval(this._ping);
|
|
1913
1922
|
});
|
|
1914
1923
|
}
|
|
@@ -1920,7 +1929,7 @@ var WsClient = _WsClient;
|
|
|
1920
1929
|
import_satori4.Schema.object({
|
|
1921
1930
|
intents: import_satori4.Schema.bitset(Gateway.Intent).description("需要订阅的机器人事件。").default(0 | Gateway.Intent.GUILD_MESSAGES | Gateway.Intent.GUILD_MESSAGE_REACTIONS | Gateway.Intent.DIRECT_MESSAGES | Gateway.Intent.DIRECT_MESSAGE_REACTIONS | Gateway.Intent.MESSAGE_CONTENT)
|
|
1922
1931
|
}).description("推送设置"),
|
|
1923
|
-
import_satori4.Adapter.
|
|
1932
|
+
import_satori4.Adapter.WsClientConfig
|
|
1924
1933
|
]);
|
|
1925
1934
|
})(WsClient || (WsClient = {}));
|
|
1926
1935
|
|
|
@@ -1947,9 +1956,6 @@ var _DiscordBot = class _DiscordBot extends import_satori5.Bot {
|
|
|
1947
1956
|
this.internal = new Internal(this.http);
|
|
1948
1957
|
ctx.plugin(WsClient, this);
|
|
1949
1958
|
}
|
|
1950
|
-
session(payload, input) {
|
|
1951
|
-
return (0, import_satori5.defineProperty)(super.session(payload), "discord", Object.assign(Object.create(this.internal), input));
|
|
1952
|
-
}
|
|
1953
1959
|
async _ensureWebhook(channelId) {
|
|
1954
1960
|
let webhook;
|
|
1955
1961
|
const webhooks = await this.internal.getChannelWebhooks(channelId);
|
|
@@ -1975,9 +1981,10 @@ var _DiscordBot = class _DiscordBot extends import_satori5.Bot {
|
|
|
1975
1981
|
}
|
|
1976
1982
|
return (_a = this.webhookLock)[channelId] || (_a[channelId] = this._ensureWebhook(channelId));
|
|
1977
1983
|
}
|
|
1978
|
-
async
|
|
1984
|
+
async getLogin() {
|
|
1979
1985
|
const data = await this.internal.getCurrentUser();
|
|
1980
|
-
|
|
1986
|
+
this.user = decodeUser(data);
|
|
1987
|
+
return this.toJSON();
|
|
1981
1988
|
}
|
|
1982
1989
|
async deleteMessage(channelId, messageId) {
|
|
1983
1990
|
await this.internal.deleteMessage(channelId, messageId);
|
|
@@ -1995,13 +2002,13 @@ var _DiscordBot = class _DiscordBot extends import_satori5.Bot {
|
|
|
1995
2002
|
}
|
|
1996
2003
|
async getMessage(channelId, messageId) {
|
|
1997
2004
|
const data = await this.internal.getChannelMessage(channelId, messageId);
|
|
1998
|
-
return await decodeMessage(this, data);
|
|
2005
|
+
return await decodeMessage(this, data, {});
|
|
1999
2006
|
}
|
|
2000
2007
|
async getMessageList(channelId, before) {
|
|
2001
2008
|
var _a;
|
|
2002
2009
|
const messages = await this.internal.getChannelMessages(channelId, { before, limit: 100 });
|
|
2003
|
-
const data = await Promise.all(messages.reverse().map((data2) => decodeMessage(this, data2, {}, false)));
|
|
2004
|
-
return { data, next: (_a = data[0]) == null ? void 0 : _a.
|
|
2010
|
+
const data = await Promise.all(messages.reverse().map((data2) => decodeMessage(this, data2, {}, void 0, false)));
|
|
2011
|
+
return { data, next: (_a = data[0]) == null ? void 0 : _a.id };
|
|
2005
2012
|
}
|
|
2006
2013
|
async getUser(userId) {
|
|
2007
2014
|
const data = await this.internal.getUser(userId);
|
|
@@ -2010,8 +2017,8 @@ var _DiscordBot = class _DiscordBot extends import_satori5.Bot {
|
|
|
2010
2017
|
async getGuildMemberList(guildId, after) {
|
|
2011
2018
|
var _a;
|
|
2012
2019
|
const users = await this.internal.listGuildMembers(guildId, { after, limit: 1e3 });
|
|
2013
|
-
const data = users.map((v) =>
|
|
2014
|
-
return { data, next: (_a = data[999]) == null ? void 0 : _a.
|
|
2020
|
+
const data = users.map((v) => decodeGuildMember(v));
|
|
2021
|
+
return { data, next: (_a = data[999]) == null ? void 0 : _a.user.id };
|
|
2015
2022
|
}
|
|
2016
2023
|
async getChannel(channelId) {
|
|
2017
2024
|
const data = await this.internal.getChannel(channelId);
|
|
@@ -2019,10 +2026,7 @@ var _DiscordBot = class _DiscordBot extends import_satori5.Bot {
|
|
|
2019
2026
|
}
|
|
2020
2027
|
async getGuildMember(guildId, userId) {
|
|
2021
2028
|
const member = await this.internal.getGuildMember(guildId, userId);
|
|
2022
|
-
return
|
|
2023
|
-
...decodeUser(member.user),
|
|
2024
|
-
nickname: member.nick
|
|
2025
|
-
};
|
|
2029
|
+
return decodeGuildMember(member);
|
|
2026
2030
|
}
|
|
2027
2031
|
async kickGuildMember(guildId, userId) {
|
|
2028
2032
|
return this.internal.removeGuildMember(guildId, userId);
|
|
@@ -2075,19 +2079,17 @@ var _DiscordBot = class _DiscordBot extends import_satori5.Bot {
|
|
|
2075
2079
|
}
|
|
2076
2080
|
async createGuildRole(guildId, data) {
|
|
2077
2081
|
const role = await this.internal.createGuildRole(guildId, encodeRole(data));
|
|
2078
|
-
return role
|
|
2082
|
+
return decodeRole(role);
|
|
2079
2083
|
}
|
|
2080
|
-
async
|
|
2084
|
+
async updateGuildRole(guildId, roleId, data) {
|
|
2081
2085
|
await this.internal.modifyGuildRole(guildId, roleId, encodeRole(data));
|
|
2082
2086
|
}
|
|
2083
2087
|
deleteGuildRole(guildId, roleId) {
|
|
2084
2088
|
return this.internal.deleteGuildRole(guildId, roleId);
|
|
2085
2089
|
}
|
|
2086
|
-
async
|
|
2087
|
-
const channel = await this.internal.createDM({
|
|
2088
|
-
|
|
2089
|
-
});
|
|
2090
|
-
return this.sendMessage(channel.id, content, null, options);
|
|
2090
|
+
async createDirectChannel(userId) {
|
|
2091
|
+
const channel = await this.internal.createDM({ recipient_id: userId });
|
|
2092
|
+
return decodeChannel(channel);
|
|
2091
2093
|
}
|
|
2092
2094
|
async updateCommands(commands) {
|
|
2093
2095
|
if (!this.config.slash)
|