@satorijs/adapter-discord 3.0.2 → 3.1.1

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
@@ -1,4 +1,4 @@
1
- import { Bot, Context, Message, Quester, Schema } from '@satorijs/satori';
1
+ import { Bot, Context, Message, Quester, Schema, segment } from '@satorijs/satori';
2
2
  import { Sender } from './sender';
3
3
  import { Internal } from './types';
4
4
  import { WsClient } from './ws';
@@ -8,11 +8,12 @@ export declare class DiscordBot<C extends Context = Context> extends Bot<C, Disc
8
8
  constructor(ctx: C, config: DiscordBot.Config);
9
9
  getSelf(): Promise<import("@satorijs/core").User>;
10
10
  private parseQuote;
11
- sendMessage(channelId: string, content: string, guildId?: string): Promise<string[]>;
12
- sendPrivateMessage(channelId: string, content: string): Promise<string[]>;
11
+ sendMessage(channelId: string, content: string | segment, guildId?: string): Promise<string[]>;
12
+ sendPrivateMessage(channelId: string, content: string | segment): Promise<string[]>;
13
13
  deleteMessage(channelId: string, messageId: string): Promise<void>;
14
- editMessage(channelId: string, messageId: string, content: string): Promise<void>;
14
+ editMessage(channelId: string, messageId: string, content: string | segment): Promise<void>;
15
15
  getMessage(channelId: string, messageId: string): Promise<Message>;
16
+ getMessageList(channelId: string, before?: string): any;
16
17
  getUser(userId: string): Promise<import("@satorijs/core").User>;
17
18
  getGuildMemberList(guildId: string): Promise<import("@satorijs/core").User[]>;
18
19
  getChannel(channelId: string): Promise<import("@satorijs/core").Channel>;
@@ -28,7 +29,6 @@ export declare class DiscordBot<C extends Context = Context> extends Bot<C, Disc
28
29
  getGuild(guildId: string): Promise<import("@satorijs/core").Guild>;
29
30
  getGuildList(): Promise<import("@satorijs/core").Guild[]>;
30
31
  getChannelList(guildId: string): Promise<import("@satorijs/core").Channel[]>;
31
- getMessageList(channelId: string, before?: string): Promise<Message[]>;
32
32
  }
33
33
  export declare namespace DiscordBot {
34
34
  interface Config extends Bot.Config, Quester.Config, Sender.Config, WsClient.Config {
package/lib/index.js CHANGED
@@ -17,7 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  }
18
18
  return to;
19
19
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
21
24
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
25
 
23
26
  // adapters/discord/src/index.ts
@@ -31,11 +34,10 @@ __export(src_exports, {
31
34
  adaptChannel: () => adaptChannel,
32
35
  adaptGuild: () => adaptGuild,
33
36
  adaptMessage: () => adaptMessage,
34
- adaptMessageSession: () => adaptMessageSession,
35
37
  adaptSession: () => adaptSession,
36
38
  adaptUser: () => adaptUser,
37
39
  default: () => src_default,
38
- prepareMessageSession: () => prepareMessageSession
40
+ prepareMessage: () => prepareMessage
39
41
  });
40
42
  module.exports = __toCommonJS(src_exports);
41
43
 
@@ -63,8 +65,11 @@ var adaptAuthor = /* @__PURE__ */ __name((author) => ({
63
65
  ...adaptUser(author),
64
66
  nickname: author.username
65
67
  }), "adaptAuthor");
66
- function adaptMessage(meta, session = {}) {
68
+ async function adaptMessage(bot, meta, session = {}) {
67
69
  var _a, _b, _c;
70
+ prepareMessage(session, meta);
71
+ session.messageId = meta.id;
72
+ session.timestamp = new Date(meta.timestamp).valueOf() || Date.now();
68
73
  if (meta.author) {
69
74
  session.author = adaptAuthor(meta.author);
70
75
  session.userId = meta.author.id;
@@ -77,15 +82,15 @@ function adaptMessage(meta, session = {}) {
77
82
  session.content = meta.content.replace(/<@[!&]?(.+?)>/g, (_, id) => {
78
83
  var _a2;
79
84
  if (meta.mention_roles.includes(id)) {
80
- return (0, import_satori.segment)("at", { role: id });
85
+ return (0, import_satori.segment)("at", { role: id }).toString();
81
86
  } else {
82
87
  const user = (_a2 = meta.mentions) == null ? void 0 : _a2.find((u) => u.id === id || `${u.username}#${u.discriminator}` === id);
83
- return import_satori.segment.at(id, { name: user == null ? void 0 : user.username });
88
+ return import_satori.segment.at(id, { name: user == null ? void 0 : user.username }).toString();
84
89
  }
85
- }).replace(/<:(.*):(.+?)>/g, (_, name, id) => (0, import_satori.segment)("face", { id, name })).replace(/<a:(.*):(.+?)>/g, (_, name, id) => (0, import_satori.segment)("face", { id, name, animated: true })).replace(/@everyone/g, () => (0, import_satori.segment)("at", { type: "all" })).replace(/@here/g, () => (0, import_satori.segment)("at", { type: "here" })).replace(/<#(.+?)>/g, (_, id) => {
90
+ }).replace(/<:(.*):(.+?)>/g, (_, name, id) => (0, import_satori.segment)("face", { id, name }).toString()).replace(/<a:(.*):(.+?)>/g, (_, name, id) => (0, import_satori.segment)("face", { id, name, animated: true }).toString()).replace(/@everyone/g, () => (0, import_satori.segment)("at", { type: "all" }).toString()).replace(/@here/g, () => (0, import_satori.segment)("at", { type: "here" }).toString()).replace(/<#(.+?)>/g, (_, id) => {
86
91
  var _a2;
87
92
  const channel = (_a2 = meta.mention_channels) == null ? void 0 : _a2.find((c) => c.id === id);
88
- return import_satori.segment.sharp(id, { name: channel == null ? void 0 : channel.name });
93
+ return import_satori.segment.sharp(id, { name: channel == null ? void 0 : channel.name }).toString();
89
94
  });
90
95
  }
91
96
  if ((_c = meta.attachments) == null ? void 0 : _c.length) {
@@ -129,26 +134,20 @@ function adaptMessage(meta, session = {}) {
129
134
  session.content += (0, import_satori.segment)("video", { url: embed.video.url, proxy_url: embed.video.proxy_url });
130
135
  }
131
136
  }
132
- return session;
133
- }
134
- __name(adaptMessage, "adaptMessage");
135
- function adaptMessageSession(meta, session = {}) {
136
- adaptMessage(meta, session);
137
- session.messageId = meta.id;
138
- session.timestamp = new Date(meta.timestamp).valueOf() || Date.now();
137
+ session.elements = import_satori.segment.parse(session.content);
139
138
  if (meta.message_reference) {
140
139
  const { message_id, channel_id } = meta.message_reference;
141
- session.content = (0, import_satori.segment)("quote", { id: message_id, channelId: channel_id }) + session.content;
140
+ session.quote = await bot.getMessage(channel_id, message_id);
142
141
  }
143
142
  return session;
144
143
  }
145
- __name(adaptMessageSession, "adaptMessageSession");
146
- function prepareMessageSession(session, data) {
144
+ __name(adaptMessage, "adaptMessage");
145
+ function prepareMessage(session, data) {
147
146
  session.guildId = data.guild_id;
148
147
  session.subtype = data.guild_id ? "group" : "private";
149
148
  session.channelId = data.channel_id;
150
149
  }
151
- __name(prepareMessageSession, "prepareMessageSession");
150
+ __name(prepareMessage, "prepareMessage");
152
151
  function prepareReactionSession(session, data) {
153
152
  session.userId = data.user_id;
154
153
  session.messageId = data.message_id;
@@ -165,17 +164,15 @@ async function adaptSession(bot, input) {
165
164
  const session = bot.session();
166
165
  if (input.t === "MESSAGE_CREATE") {
167
166
  session.type = "message";
168
- prepareMessageSession(session, input.d);
169
- adaptMessageSession(input.d, session);
167
+ await adaptMessage(bot, input.d, session);
170
168
  } else if (input.t === "MESSAGE_UPDATE") {
171
169
  session.type = "message-updated";
172
- prepareMessageSession(session, input.d);
173
170
  const msg = await bot.internal.getChannelMessage(input.d.channel_id, input.d.id);
174
- adaptMessageSession(msg, session);
171
+ await adaptMessage(bot, msg, session);
175
172
  } else if (input.t === "MESSAGE_DELETE") {
176
173
  session.type = "message-deleted";
177
174
  session.messageId = input.d.id;
178
- prepareMessageSession(session, input.d);
175
+ prepareMessage(session, input.d);
179
176
  } else if (input.t === "MESSAGE_REACTION_ADD") {
180
177
  session.type = "reaction-added";
181
178
  prepareReactionSession(session, input.d);
@@ -339,12 +336,12 @@ __name(Sender, "Sender");
339
336
  import_satori2.Schema.const("download").description("先下载后发送"),
340
337
  import_satori2.Schema.const("direct").description("直接发送链接"),
341
338
  import_satori2.Schema.const("auto").description("发送一个 HEAD 请求,根据返回的 Content-Type 决定发送方式")
342
- ]).description("发送外链资源时采用的方式。").default("auto"),
339
+ ]).role("radio").description("发送外链资源时采用的方式。").default("auto"),
343
340
  handleMixedContent: import_satori2.Schema.union([
344
341
  import_satori2.Schema.const("separate").description("将每个不同形式的内容分开发送"),
345
342
  import_satori2.Schema.const("attach").description("图片前如果有文本内容,则将文本作为图片的附带信息进行发送"),
346
343
  import_satori2.Schema.const("auto").description("如果图片本身采用直接发送则与前面的文本分开,否则将文本作为图片的附带信息发送")
347
- ]).description("发送图文等混合内容时采用的方式。").default("auto")
344
+ ]).role("radio").description("发送图文等混合内容时采用的方式。").default("auto")
348
345
  }).description("发送设置");
349
346
  })(Sender || (Sender = {}));
350
347
 
@@ -1377,7 +1374,7 @@ __name(WsClient, "WsClient");
1377
1374
  ((WsClient2) => {
1378
1375
  WsClient2.Config = import_satori3.Schema.intersect([
1379
1376
  import_satori3.Schema.object({
1380
- gateway: import_satori3.Schema.string().role("url").default("wss://gateway.discord.gg/?v=8&encoding=json").description("要连接的 WebSocket 网关。"),
1377
+ gateway: import_satori3.Schema.string().default("wss://gateway.discord.gg/?v=8&encoding=json").description("要连接的 WebSocket 网关。"),
1381
1378
  intents: import_satori3.Schema.bitset(GatewayIntent).description("需要订阅的机器人事件。").default(0 | 512 /* GUILD_MESSAGES */ | 1024 /* GUILD_MESSAGE_REACTIONS */ | 4096 /* DIRECT_MESSAGES */ | 8192 /* DIRECT_MESSAGE_REACTIONS */)
1382
1379
  }).description("推送设置"),
1383
1380
  import_satori3.Adapter.WsClient.Config
@@ -1405,13 +1402,17 @@ var DiscordBot = class extends import_satori4.Bot {
1405
1402
  parseQuote(chain) {
1406
1403
  if (chain[0].type !== "quote")
1407
1404
  return;
1408
- return chain.shift().data.id;
1405
+ return chain.shift().attrs.id;
1409
1406
  }
1410
1407
  async sendMessage(channelId, content, guildId) {
1408
+ const fragment = import_satori4.segment.normalize(content);
1409
+ const elements = fragment.children;
1410
+ content = fragment.toString();
1411
1411
  const session = this.session({
1412
1412
  type: "send",
1413
1413
  author: this,
1414
1414
  channelId,
1415
+ elements,
1415
1416
  content,
1416
1417
  guildId,
1417
1418
  subtype: guildId ? "group" : "private"
@@ -1440,7 +1441,9 @@ var DiscordBot = class extends import_satori4.Bot {
1440
1441
  await this.internal.deleteMessage(channelId, messageId);
1441
1442
  }
1442
1443
  async editMessage(channelId, messageId, content) {
1443
- const chain = import_satori4.segment.parse(content);
1444
+ const fragment = import_satori4.segment.normalize(content);
1445
+ content = fragment.toString();
1446
+ const chain = fragment.children;
1444
1447
  const image = chain.find((v) => v.type === "image");
1445
1448
  if (image) {
1446
1449
  throw new Error("You can't include embed object(s) while editing message.");
@@ -1450,14 +1453,12 @@ var DiscordBot = class extends import_satori4.Bot {
1450
1453
  });
1451
1454
  }
1452
1455
  async getMessage(channelId, messageId) {
1453
- const original = await this.internal.getChannelMessage(channelId, messageId);
1454
- const result = adaptMessage(original);
1455
- const reference = original.message_reference;
1456
- if (reference) {
1457
- const quoteMsg = await this.internal.getChannelMessage(reference.channel_id, reference.message_id);
1458
- result.quote = adaptMessage(quoteMsg);
1459
- }
1460
- return result;
1456
+ const data = await this.internal.getChannelMessage(channelId, messageId);
1457
+ return await adaptMessage(this, data);
1458
+ }
1459
+ async getMessageList(channelId, before) {
1460
+ const data = (await this.internal.getChannelMessages(channelId, { before, limit: 50 })).reverse();
1461
+ return await Promise.all(data.map((data2) => adaptMessage(this, data2)));
1461
1462
  }
1462
1463
  async getUser(userId) {
1463
1464
  const data = await this.internal.getUser(userId);
@@ -1493,18 +1494,6 @@ var DiscordBot = class extends import_satori4.Bot {
1493
1494
  const data = await this.internal.getGuildChannels(guildId);
1494
1495
  return data.map((v) => adaptChannel(v));
1495
1496
  }
1496
- async getMessageList(channelId, before) {
1497
- const data = (await this.internal.getChannelMessages(channelId, {
1498
- before,
1499
- limit: 50
1500
- })).reverse();
1501
- return data.map((v) => {
1502
- const session = {};
1503
- prepareMessageSession(session, v);
1504
- adaptMessageSession(v, session);
1505
- return session;
1506
- });
1507
- }
1508
1497
  };
1509
1498
  __name(DiscordBot, "DiscordBot");
1510
1499
  ((DiscordBot2) => {
@@ -1514,12 +1503,7 @@ __name(DiscordBot, "DiscordBot");
1514
1503
  }),
1515
1504
  WsClient.Config,
1516
1505
  Sender.Config,
1517
- import_satori4.Schema.object({
1518
- endpoint: import_satori4.Schema.string().role("url").description("要连接的服务器地址。").default("https://discord.com/api/v8"),
1519
- proxyAgent: import_satori4.Schema.string().role("url").description("使用的代理服务器地址。"),
1520
- headers: import_satori4.Schema.dict(String).description("要附加的额外请求头。"),
1521
- timeout: import_satori4.Schema.natural().role("ms").description("等待连接建立的最长时间。")
1522
- }).description("请求设置")
1506
+ import_satori4.Quester.createConfig("https://discord.com/api/v8")
1523
1507
  ]);
1524
1508
  })(DiscordBot || (DiscordBot = {}));
1525
1509
  DiscordBot.prototype.platform = "discord";
@@ -1536,9 +1520,8 @@ var src_default = DiscordBot;
1536
1520
  adaptChannel,
1537
1521
  adaptGuild,
1538
1522
  adaptMessage,
1539
- adaptMessageSession,
1540
1523
  adaptSession,
1541
1524
  adaptUser,
1542
- prepareMessageSession
1525
+ prepareMessage
1543
1526
  });
1544
1527
  //# sourceMappingURL=index.js.map