@satorijs/adapter-discord 3.2.4 → 3.2.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 CHANGED
@@ -202,8 +202,6 @@ __name(adaptSession, "adaptSession");
202
202
 
203
203
  // satori/adapters/discord/src/sender.ts
204
204
  var import_satori2 = require("@satorijs/satori");
205
- var import_fs = require("fs");
206
- var import_path = require("path");
207
205
  var import_file_type = require("file-type");
208
206
  var import_form_data = __toESM(require("form-data"));
209
207
  var AggregateError = class extends Error {
@@ -221,6 +219,8 @@ var Sender = class {
221
219
  this.errors = [];
222
220
  this.buffer = "";
223
221
  this.addition = {};
222
+ this.figure = null;
223
+ this.mode = "default";
224
224
  }
225
225
  async post(data, headers) {
226
226
  try {
@@ -233,7 +233,7 @@ var Sender = class {
233
233
  async sendEmbed(fileBuffer, payload_json, filename) {
234
234
  const fd = new import_form_data.default();
235
235
  filename || (filename = "file." + (await (0, import_file_type.fromBuffer)(fileBuffer)).ext);
236
- fd.append("file", fileBuffer, filename);
236
+ fd.append("file", Buffer.from(fileBuffer), filename);
237
237
  fd.append("payload_json", JSON.stringify(payload_json));
238
238
  return this.post(fd, fd.getHeaders());
239
239
  }
@@ -246,12 +246,9 @@ var Sender = class {
246
246
  await this.post(addition);
247
247
  addition.content = "";
248
248
  }
249
- if (data.url.startsWith("file://")) {
250
- const filename = (0, import_path.basename)(data.url.slice(7));
251
- return await this.sendEmbed((0, import_fs.readFileSync)(data.url.slice(7)), addition, data.file || filename);
252
- } else if (data.url.startsWith("base64://")) {
253
- const a = Buffer.from(data.url.slice(9), "base64");
254
- return await this.sendEmbed(a, addition, data.file);
249
+ if (["file:", "data:", "base64:"].some((prefix) => data.url.startsWith(prefix))) {
250
+ const result = await this.bot.ctx.http.file(data.url);
251
+ return await this.sendEmbed(result.data, addition, data.file || result.filename);
255
252
  }
256
253
  const sendDirect = /* @__PURE__ */ __name(async () => {
257
254
  if (addition.content) {
@@ -291,9 +288,13 @@ var Sender = class {
291
288
  this.addition = {};
292
289
  }
293
290
  async render(elements) {
294
- for (const { type, attrs, children } of elements) {
291
+ for (const element of elements) {
292
+ const { type, attrs, children } = element;
295
293
  if (type === "text") {
296
294
  this.buffer += attrs.content;
295
+ } else if (type === "p") {
296
+ await this.render(children);
297
+ this.buffer += "\n";
297
298
  } else if (type === "at") {
298
299
  if (attrs.id) {
299
300
  this.buffer += `<@${attrs.id}>`;
@@ -307,11 +308,15 @@ var Sender = class {
307
308
  } else if (type === "face" && attrs.name && attrs.id) {
308
309
  this.buffer += `<:${attrs.name}:${attrs.id}>`;
309
310
  } else if ((type === "image" || type === "video") && attrs.url) {
310
- await this.sendAsset(type, attrs, {
311
- ...this.addition,
312
- content: this.buffer.trim()
313
- });
314
- this.buffer = "";
311
+ if (this.mode === "figure") {
312
+ this.figure = element;
313
+ } else {
314
+ await this.sendAsset(type, attrs, {
315
+ ...this.addition,
316
+ content: this.buffer.trim()
317
+ });
318
+ this.buffer = "";
319
+ }
315
320
  } else if (type === "share") {
316
321
  await this.sendBuffer();
317
322
  await this.post({
@@ -324,19 +329,34 @@ var Sender = class {
324
329
  content: this.buffer.trim()
325
330
  });
326
331
  this.buffer = "";
332
+ } else if (type === "figure") {
333
+ await this.sendBuffer();
334
+ this.mode = "figure";
335
+ await this.render(children);
336
+ await this.sendAsset(this.figure.type, this.figure.attrs, {
337
+ ...this.addition,
338
+ content: this.buffer.trim()
339
+ });
340
+ this.buffer = "";
341
+ this.mode = "default";
327
342
  } else if (type === "quote") {
328
343
  await this.sendBuffer();
329
344
  this.addition.message_reference = {
330
345
  message_id: attrs.id
331
346
  };
332
347
  } else if (type === "message") {
333
- await this.sendBuffer();
334
- if ("quote" in attrs) {
335
- this.addition.message_reference = {
336
- message_id: attrs.id
337
- };
348
+ if (this.mode === "figure") {
349
+ await this.render(children);
350
+ this.buffer += "\n";
338
351
  } else {
339
- await this.sendMessage(children);
352
+ await this.sendBuffer();
353
+ if ("quote" in attrs) {
354
+ this.addition.message_reference = {
355
+ message_id: attrs.id
356
+ };
357
+ } else {
358
+ await this.sendMessage(children);
359
+ }
340
360
  }
341
361
  } else {
342
362
  await this.render(children);