@satorijs/adapter-discord 3.2.0 → 3.2.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
@@ -7,13 +7,12 @@ export declare class DiscordBot<C extends Context = Context> extends Bot<C, Disc
7
7
  internal: Internal;
8
8
  constructor(ctx: C, config: DiscordBot.Config);
9
9
  getSelf(): Promise<import("@satorijs/core").User>;
10
- private parseQuote;
11
- sendMessage(channelId: string, content: string | segment, guildId?: string): Promise<string[]>;
12
- sendPrivateMessage(channelId: string, content: string | segment): Promise<string[]>;
10
+ sendMessage(channelId: string, content: string | segment, guildId?: string): any;
11
+ sendPrivateMessage(channelId: string, content: string | segment): any;
13
12
  deleteMessage(channelId: string, messageId: string): Promise<void>;
14
13
  editMessage(channelId: string, messageId: string, content: string | segment): Promise<void>;
15
14
  getMessage(channelId: string, messageId: string): Promise<Message>;
16
- getMessageList(channelId: string, before?: string): any;
15
+ getMessageList(channelId: string, before?: string): Promise<Message[]>;
17
16
  getUser(userId: string): Promise<import("@satorijs/core").User>;
18
17
  getGuildMemberList(guildId: string): Promise<import("@satorijs/core").User[]>;
19
18
  getChannel(channelId: string): Promise<import("@satorijs/core").Channel>;
package/lib/index.js CHANGED
@@ -213,10 +213,8 @@ var Sender = class {
213
213
  this.url = url;
214
214
  this.results = [];
215
215
  this.errors = [];
216
- }
217
- static from(bot, url) {
218
- const sender = new Sender(bot, url);
219
- return sender.sendMessage.bind(sender);
216
+ this.buffer = "";
217
+ this.addition = {};
220
218
  }
221
219
  async post(data, headers) {
222
220
  try {
@@ -278,52 +276,70 @@ var Sender = class {
278
276
  }
279
277
  }, sendDownload);
280
278
  }
281
- async sendMessage(content, addition = {}) {
282
- const chain = import_satori2.segment.parse(content);
283
- let textBuffer = "";
284
- delete addition.content;
285
- const sendBuffer = /* @__PURE__ */ __name(async () => {
286
- const content2 = textBuffer.trim();
287
- if (!content2)
288
- return;
289
- await this.post({ ...addition, content: content2 });
290
- textBuffer = "";
291
- }, "sendBuffer");
292
- for (const code of chain) {
293
- const { type, data } = code;
279
+ async sendBuffer() {
280
+ const content = this.buffer.trim();
281
+ if (!content)
282
+ return;
283
+ await this.post({ ...this.addition, content });
284
+ this.buffer = "";
285
+ this.addition = {};
286
+ }
287
+ async sendMessage(elements) {
288
+ for (const code of elements) {
289
+ const { type, attrs, children } = code;
294
290
  if (type === "text") {
295
- textBuffer += data.content.trim();
296
- } else if (type === "at" && data.id) {
297
- textBuffer += `<@${data.id}>`;
298
- } else if (type === "at" && data.type === "all") {
299
- textBuffer += `@everyone`;
300
- } else if (type === "at" && data.type === "here") {
301
- textBuffer += `@here`;
302
- } else if (type === "sharp" && data.id) {
303
- textBuffer += `<#${data.id}>`;
304
- } else if (type === "face" && data.name && data.id) {
305
- textBuffer += `<:${data.name}:${data.id}>`;
306
- } else if ((type === "image" || type === "video") && data.url) {
307
- await this.sendAsset(type, data, {
308
- ...addition,
309
- content: textBuffer.trim()
291
+ this.buffer += attrs.content.trim();
292
+ } else if (type === "at") {
293
+ if (attrs.id) {
294
+ this.buffer += `<@${attrs.id}>`;
295
+ } else if (attrs.type === "all") {
296
+ this.buffer += `@everyone`;
297
+ } else if (attrs.type === "here") {
298
+ this.buffer += `@here`;
299
+ }
300
+ } else if (type === "sharp" && attrs.id) {
301
+ this.buffer += `<#${attrs.id}>`;
302
+ } else if (type === "face" && attrs.name && attrs.id) {
303
+ this.buffer += `<:${attrs.name}:${attrs.id}>`;
304
+ } else if ((type === "image" || type === "video") && attrs.url) {
305
+ await this.sendAsset(type, attrs, {
306
+ ...this.addition,
307
+ content: this.buffer.trim()
310
308
  });
311
- textBuffer = "";
309
+ this.buffer = "";
312
310
  } else if (type === "share") {
313
- await sendBuffer();
311
+ await this.sendBuffer();
314
312
  await this.post({
315
- ...addition,
316
- embeds: [{ ...data }]
313
+ ...this.addition,
314
+ embeds: [{ ...attrs }]
317
315
  });
318
316
  } else if (type === "record") {
319
- await this.sendAsset("file", data, {
320
- ...addition,
321
- content: textBuffer.trim()
317
+ await this.sendAsset("file", attrs, {
318
+ ...this.addition,
319
+ content: this.buffer.trim()
322
320
  });
323
- textBuffer = "";
321
+ this.buffer = "";
322
+ } else if (type === "quote") {
323
+ await this.sendBuffer();
324
+ this.addition.message_reference = {
325
+ message_id: attrs.id
326
+ };
327
+ } else if (type === "message") {
328
+ await this.sendBuffer();
329
+ if ("quote" in attrs) {
330
+ this.addition.message_reference = {
331
+ message_id: attrs.id
332
+ };
333
+ } else {
334
+ await this.sendMessage(children);
335
+ }
324
336
  }
325
337
  }
326
- await sendBuffer();
338
+ await this.sendBuffer();
339
+ }
340
+ async send(content) {
341
+ const elements = import_satori2.segment.parse(content);
342
+ await this.sendMessage(elements);
327
343
  if (!this.errors.length)
328
344
  return this.results;
329
345
  throw new import_es_aggregate_error.default(this.errors);
@@ -1471,11 +1487,6 @@ var DiscordBot = class extends import_satori4.Bot {
1471
1487
  const data = await this.internal.getCurrentUser();
1472
1488
  return adaptUser(data);
1473
1489
  }
1474
- parseQuote(chain) {
1475
- if (chain[0].type !== "quote")
1476
- return;
1477
- return chain.shift().attrs.id;
1478
- }
1479
1490
  async sendMessage(channelId, content, guildId) {
1480
1491
  const fragment = import_satori4.segment.normalize(content);
1481
1492
  const elements = fragment.children;
@@ -1493,13 +1504,8 @@ var DiscordBot = class extends import_satori4.Bot {
1493
1504
  return;
1494
1505
  if (!(session == null ? void 0 : session.content))
1495
1506
  return [];
1496
- const chain = import_satori4.segment.parse(session.content);
1497
- const quote = this.parseQuote(chain);
1498
- const message_reference = quote ? {
1499
- message_id: quote
1500
- } : void 0;
1501
- const send = Sender.from(this, `/channels/${channelId}/messages`);
1502
- const results = await send(session.content, { message_reference });
1507
+ const sender = new Sender(this, `/channels/${channelId}/messages`);
1508
+ const results = await sender.send(session.content);
1503
1509
  for (const id of results) {
1504
1510
  session.messageId = id;
1505
1511
  this.context.emit(session, "send", session);