@satorijs/adapter-discord 3.2.0 → 3.2.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 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,69 @@ 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 { type, attrs, children } of elements) {
294
289
  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()
290
+ this.buffer += attrs.content;
291
+ } else if (type === "at") {
292
+ if (attrs.id) {
293
+ this.buffer += `<@${attrs.id}>`;
294
+ } else if (attrs.type === "all") {
295
+ this.buffer += `@everyone`;
296
+ } else if (attrs.type === "here") {
297
+ this.buffer += `@here`;
298
+ }
299
+ } else if (type === "sharp" && attrs.id) {
300
+ this.buffer += `<#${attrs.id}>`;
301
+ } else if (type === "face" && attrs.name && attrs.id) {
302
+ this.buffer += `<:${attrs.name}:${attrs.id}>`;
303
+ } else if ((type === "image" || type === "video") && attrs.url) {
304
+ await this.sendAsset(type, attrs, {
305
+ ...this.addition,
306
+ content: this.buffer.trim()
310
307
  });
311
- textBuffer = "";
308
+ this.buffer = "";
312
309
  } else if (type === "share") {
313
- await sendBuffer();
310
+ await this.sendBuffer();
314
311
  await this.post({
315
- ...addition,
316
- embeds: [{ ...data }]
312
+ ...this.addition,
313
+ embeds: [{ ...attrs }]
317
314
  });
318
315
  } else if (type === "record") {
319
- await this.sendAsset("file", data, {
320
- ...addition,
321
- content: textBuffer.trim()
316
+ await this.sendAsset("file", attrs, {
317
+ ...this.addition,
318
+ content: this.buffer.trim()
322
319
  });
323
- textBuffer = "";
320
+ this.buffer = "";
321
+ } else if (type === "quote") {
322
+ await this.sendBuffer();
323
+ this.addition.message_reference = {
324
+ message_id: attrs.id
325
+ };
326
+ } else if (type === "message") {
327
+ await this.sendBuffer();
328
+ if ("quote" in attrs) {
329
+ this.addition.message_reference = {
330
+ message_id: attrs.id
331
+ };
332
+ } else {
333
+ await this.sendMessage(children);
334
+ }
324
335
  }
325
336
  }
326
- await sendBuffer();
337
+ await this.sendBuffer();
338
+ }
339
+ async send(content) {
340
+ const elements = import_satori2.segment.parse(content);
341
+ await this.sendMessage(elements);
327
342
  if (!this.errors.length)
328
343
  return this.results;
329
344
  throw new import_es_aggregate_error.default(this.errors);
@@ -1471,11 +1486,6 @@ var DiscordBot = class extends import_satori4.Bot {
1471
1486
  const data = await this.internal.getCurrentUser();
1472
1487
  return adaptUser(data);
1473
1488
  }
1474
- parseQuote(chain) {
1475
- if (chain[0].type !== "quote")
1476
- return;
1477
- return chain.shift().attrs.id;
1478
- }
1479
1489
  async sendMessage(channelId, content, guildId) {
1480
1490
  const fragment = import_satori4.segment.normalize(content);
1481
1491
  const elements = fragment.children;
@@ -1491,15 +1501,10 @@ var DiscordBot = class extends import_satori4.Bot {
1491
1501
  });
1492
1502
  if (await this.context.serial(session, "before-send", session))
1493
1503
  return;
1494
- if (!(session == null ? void 0 : session.content))
1504
+ if (!session.content)
1495
1505
  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 });
1506
+ const sender = new Sender(this, `/channels/${channelId}/messages`);
1507
+ const results = await sender.send(session.content);
1503
1508
  for (const id of results) {
1504
1509
  session.messageId = id;
1505
1510
  this.context.emit(session, "send", session);