@fluxerjs/core 1.3.1 → 1.4.0

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/dist/message.js CHANGED
@@ -30,10 +30,11 @@ var Base = class {
30
30
 
31
31
  // src/structures/Message.ts
32
32
  var import_collection2 = require("@fluxerjs/collection");
33
- var import_types = require("@fluxerjs/types");
33
+ var import_types2 = require("@fluxerjs/types");
34
34
  var import_builders2 = require("@fluxerjs/builders");
35
35
 
36
36
  // src/util/messageUtils.ts
37
+ var import_types = require("@fluxerjs/types");
37
38
  var import_builders = require("@fluxerjs/builders");
38
39
 
39
40
  // src/errors/ErrorCodes.ts
@@ -48,6 +49,7 @@ async function resolveMessageFiles(files) {
48
49
  const result = [];
49
50
  for (let i = 0; i < files.length; i++) {
50
51
  const f = files[i];
52
+ if (!f) continue;
51
53
  const filename = f.filename ?? f.name;
52
54
  if ("url" in f && f.url) {
53
55
  if (!URL.canParse(f.url)) {
@@ -86,6 +88,21 @@ async function resolveMessageFiles(files) {
86
88
  }
87
89
  return result;
88
90
  }
91
+ function toAPIAllowedMentions(options) {
92
+ const result = {};
93
+ if (options.parse?.length) result.parse = options.parse;
94
+ if (options.users?.length) result.users = options.users;
95
+ if (options.roles?.length) result.roles = options.roles;
96
+ if (options.repliedUser !== void 0) result.replied_user = options.repliedUser;
97
+ return result;
98
+ }
99
+ function applyReplyPingSuppression(body) {
100
+ body.flags = (body.flags ?? 0) | import_types.MessageFlags.SuppressNotifications;
101
+ body.allowed_mentions = {
102
+ ...body.allowed_mentions ?? {},
103
+ replied_user: false
104
+ };
105
+ }
89
106
  function buildSendBody(options) {
90
107
  const body = typeof options === "string" ? { content: options } : options;
91
108
  const result = {};
@@ -107,9 +124,39 @@ function buildSendBody(options) {
107
124
  filename: f.filename ?? f.name
108
125
  }));
109
126
  }
127
+ if (body.allowedMentions) result.allowed_mentions = toAPIAllowedMentions(body.allowedMentions);
128
+ if (body.stickerIds?.length) result.sticker_ids = body.stickerIds;
129
+ if (body.nonce !== void 0) result.nonce = body.nonce;
130
+ if (body.favoriteMemeId !== void 0) result.favorite_meme_id = body.favoriteMemeId;
131
+ if (body.tts !== void 0) result.tts = body.tts;
110
132
  if (body.flags !== void 0) result.flags = body.flags;
111
133
  return result;
112
134
  }
135
+ function toMessageReference(target) {
136
+ const ref = {
137
+ channel_id: target.channelId,
138
+ message_id: target.messageId
139
+ };
140
+ if (target.guildId != null && target.guildId !== "") ref.guild_id = target.guildId;
141
+ return ref;
142
+ }
143
+ async function prepareMessagePostPayload(options) {
144
+ if (typeof options === "string") {
145
+ if (options.length === 0) throw new RangeError("Cannot send an empty message");
146
+ options = { content: options };
147
+ }
148
+ const { replyTo, ping, files, allowedMentions, ...sendFields } = options;
149
+ const body = buildSendBody({ ...sendFields, files, allowedMentions });
150
+ if (replyTo) {
151
+ body.message_reference = toMessageReference(replyTo);
152
+ const suppressPing = ping === false || allowedMentions?.repliedUser === false || body.allowed_mentions?.replied_user === false;
153
+ if (suppressPing) applyReplyPingSuppression(body);
154
+ } else if (allowedMentions?.repliedUser === false) {
155
+ applyReplyPingSuppression(body);
156
+ }
157
+ const resolvedFiles = files?.length ? await resolveMessageFiles(files) : void 0;
158
+ return { body, files: resolvedFiles };
159
+ }
113
160
 
114
161
  // src/util/ReactionCollector.ts
115
162
  var import_events = require("events");
@@ -287,7 +334,7 @@ var Message = class _Message extends Base {
287
334
  this.pinned = data.pinned;
288
335
  this.attachments = new import_collection2.Collection();
289
336
  for (const a of data.attachments ?? []) this.attachments.set(a.id, a);
290
- this.type = data.type ?? import_types.MessageType.Default;
337
+ this.type = data.type ?? import_types2.MessageType.Default;
291
338
  this.flags = data.flags ?? 0;
292
339
  this.mentionEveryone = data.mention_everyone ?? false;
293
340
  this.tts = data.tts ?? false;
@@ -312,7 +359,7 @@ var Message = class _Message extends Base {
312
359
  * await message.send({ content: 'File', files: [{ name: 'data.txt', data }] });
313
360
  */
314
361
  async send(options) {
315
- const payload = await _Message._createMessageBody(options);
362
+ const payload = await prepareMessagePostPayload(options);
316
363
  return this._send(payload);
317
364
  }
318
365
  /**
@@ -333,46 +380,57 @@ var Message = class _Message extends Base {
333
380
  * await message.reply('Pong!');
334
381
  * await message.reply({ embeds: [embed] });
335
382
  * await message.reply('No ping!', { ping: false });
383
+ * await message.reply({ content: 'Silent reply', allowedMentions: AllowedMentions.suppressReplyPing });
336
384
  * await message.reply({ content: 'Reply to other', replyTo: otherMessage });
337
385
  */
338
386
  async reply(options, replyOptions) {
339
387
  const opts = typeof options === "string" ? { content: options } : options;
340
388
  const mergedReply = replyOptions ?? (opts.ping !== void 0 || opts.replyTo !== void 0) ? { ping: opts.ping, replyTo: opts.replyTo } : void 0;
341
389
  const refMessage = mergedReply?.replyTo ?? this;
342
- const ref = refMessage instanceof _Message ? {
343
- channel_id: refMessage.channelId,
344
- message_id: refMessage.id,
345
- guild_id: refMessage.guildId ?? void 0
346
- } : {
347
- channel_id: refMessage.channelId,
348
- message_id: refMessage.messageId,
349
- guild_id: void 0
350
- };
351
- const payload = await _Message._createMessageBody(opts, ref, mergedReply?.ping !== false);
390
+ const replyTo = refMessage instanceof _Message ? {
391
+ channelId: refMessage.channelId,
392
+ messageId: refMessage.id,
393
+ guildId: refMessage.guildId
394
+ } : refMessage;
395
+ const { replyTo: _ignored, ping: _ping, ...sendOpts } = opts;
396
+ const payload = await prepareMessagePostPayload({
397
+ ...sendOpts,
398
+ replyTo,
399
+ ping: mergedReply?.ping
400
+ });
352
401
  return this._send(payload);
353
402
  }
354
- /** Exposed for testing purposes, use Message.reply() or send() for normal use */
403
+ /** Exposed for testing — prefer `message.reply()` / `prepareMessagePostPayload()`. */
355
404
  static async _createMessageBody(content, referenced_message, ping) {
405
+ if (typeof content === "string" && content.length === 0) {
406
+ throw new RangeError("Cannot send an empty message");
407
+ }
356
408
  if (typeof content === "string") {
357
- if (content.length === 0) {
358
- throw new RangeError("Cannot send an empty message");
409
+ const opts2 = { content };
410
+ if (referenced_message) {
411
+ opts2.replyTo = {
412
+ channelId: referenced_message.channel_id,
413
+ messageId: referenced_message.message_id,
414
+ ...referenced_message.guild_id != null ? { guildId: referenced_message.guild_id } : {}
415
+ };
416
+ if (ping !== void 0) opts2.ping = ping;
359
417
  }
360
- content = { content };
418
+ return prepareMessagePostPayload(opts2);
361
419
  }
362
- const base = buildSendBody(content);
363
- const files = content.files?.length ? await resolveMessageFiles(content.files) : void 0;
364
- const body = { ...base };
420
+ const opts = { ...content };
365
421
  if (referenced_message) {
366
- body.message_reference = referenced_message;
367
- if (ping === false) {
368
- body.flags = (body.flags ?? 0) | import_types.MessageFlags.SuppressNotifications;
369
- }
422
+ opts.replyTo = {
423
+ channelId: referenced_message.channel_id,
424
+ messageId: referenced_message.message_id,
425
+ ...referenced_message.guild_id != null ? { guildId: referenced_message.guild_id } : {}
426
+ };
427
+ if (ping !== void 0) opts.ping = ping;
370
428
  }
371
- return { files, body };
429
+ return prepareMessagePostPayload(opts);
372
430
  }
373
431
  async _send(payload) {
374
432
  const data = await this.client.rest.post(
375
- import_types.Routes.channelMessages(this.channelId),
433
+ import_types2.Routes.channelMessages(this.channelId),
376
434
  payload
377
435
  );
378
436
  this.client._addMessageToCache(this.channelId, data);
@@ -388,7 +446,10 @@ var Message = class _Message extends Base {
388
446
  if (options.embeds?.length) {
389
447
  body.embeds = options.embeds.map((e) => e instanceof import_builders2.EmbedBuilder ? e.toJSON() : e);
390
448
  }
391
- const data = await this.client.rest.patch(import_types.Routes.channelMessage(this.channelId, this.id), {
449
+ if (options.allowedMentions) {
450
+ body.allowed_mentions = toAPIAllowedMentions(options.allowedMentions);
451
+ }
452
+ const data = await this.client.rest.patch(import_types2.Routes.channelMessage(this.channelId, this.id), {
392
453
  body
393
454
  });
394
455
  return new _Message(this.client, data);
@@ -419,7 +480,7 @@ var Message = class _Message extends Base {
419
480
  }
420
481
  /** Delete this message. */
421
482
  async delete() {
422
- await this.client.rest.delete(import_types.Routes.channelMessage(this.channelId, this.id));
483
+ await this.client.rest.delete(import_types2.Routes.channelMessage(this.channelId, this.id));
423
484
  }
424
485
  /**
425
486
  * Delete a specific attachment from this message.
@@ -427,19 +488,19 @@ var Message = class _Message extends Base {
427
488
  */
428
489
  async deleteAttachment(attachmentId) {
429
490
  await this.client.rest.delete(
430
- import_types.Routes.channelMessageAttachment(this.channelId, this.id, attachmentId),
491
+ import_types2.Routes.channelMessageAttachment(this.channelId, this.id, attachmentId),
431
492
  { auth: true }
432
493
  );
433
494
  this.attachments.delete(attachmentId);
434
495
  }
435
496
  /** Pin this message to the channel. Requires Manage Messages permission. */
436
497
  async pin() {
437
- await this.client.rest.put(import_types.Routes.channelPinMessage(this.channelId, this.id));
498
+ await this.client.rest.put(import_types2.Routes.channelPinMessage(this.channelId, this.id));
438
499
  this.pinned = true;
439
500
  }
440
501
  /** Unpin this message from the channel. Requires Manage Messages permission. */
441
502
  async unpin() {
442
- await this.client.rest.delete(import_types.Routes.channelPinMessage(this.channelId, this.id));
503
+ await this.client.rest.delete(import_types2.Routes.channelPinMessage(this.channelId, this.id));
443
504
  this.pinned = false;
444
505
  }
445
506
  /**
@@ -459,7 +520,7 @@ var Message = class _Message extends Base {
459
520
  */
460
521
  async react(emoji) {
461
522
  const emojiStr = await this.resolveEmojiForReaction(emoji);
462
- const route = `${import_types.Routes.channelMessageReaction(this.channelId, this.id, emojiStr)}/@me`;
523
+ const route = `${import_types2.Routes.channelMessageReaction(this.channelId, this.id, emojiStr)}/@me`;
463
524
  await this.client.rest.put(route);
464
525
  }
465
526
  /**
@@ -469,7 +530,7 @@ var Message = class _Message extends Base {
469
530
  */
470
531
  async removeReaction(emoji, userId) {
471
532
  const emojiStr = await this.resolveEmojiForReaction(emoji);
472
- const route = `${import_types.Routes.channelMessageReaction(this.channelId, this.id, emojiStr)}/${userId ?? "@me"}`;
533
+ const route = `${import_types2.Routes.channelMessageReaction(this.channelId, this.id, emojiStr)}/${userId ?? "@me"}`;
473
534
  await this.client.rest.delete(route);
474
535
  }
475
536
  /**
@@ -477,7 +538,7 @@ var Message = class _Message extends Base {
477
538
  * Requires moderator permissions.
478
539
  */
479
540
  async removeAllReactions() {
480
- await this.client.rest.delete(import_types.Routes.channelMessageReactions(this.channelId, this.id));
541
+ await this.client.rest.delete(import_types2.Routes.channelMessageReactions(this.channelId, this.id));
481
542
  }
482
543
  /**
483
544
  * Remove all reactions of a specific emoji from this message.
@@ -485,7 +546,7 @@ var Message = class _Message extends Base {
485
546
  */
486
547
  async removeReactionEmoji(emoji) {
487
548
  const emojiStr = await this.resolveEmojiForReaction(emoji);
488
- await this.client.rest.delete(import_types.Routes.channelMessageReaction(this.channelId, this.id, emojiStr));
549
+ await this.client.rest.delete(import_types2.Routes.channelMessageReaction(this.channelId, this.id, emojiStr));
489
550
  }
490
551
  /**
491
552
  * Fetch users who reacted with the given emoji.
@@ -499,7 +560,7 @@ var Message = class _Message extends Base {
499
560
  if (options?.limit != null) params.set("limit", String(options.limit));
500
561
  if (options?.after) params.set("after", options.after);
501
562
  const qs = params.toString();
502
- const route = import_types.Routes.channelMessageReaction(this.channelId, this.id, emojiStr) + (qs ? `?${qs}` : "");
563
+ const route = import_types2.Routes.channelMessageReaction(this.channelId, this.id, emojiStr) + (qs ? `?${qs}` : "");
503
564
  const data = await this.client.rest.get(route);
504
565
  const list = Array.isArray(data) ? data : data?.users ?? [];
505
566
  return list.map((u) => this.client.getOrCreateUser(u));
package/dist/message.mjs CHANGED
@@ -4,10 +4,11 @@ var Base = class {
4
4
 
5
5
  // src/structures/Message.ts
6
6
  import { Collection as Collection2 } from "@fluxerjs/collection";
7
- import { MessageType, MessageFlags, Routes } from "@fluxerjs/types";
7
+ import { MessageType, Routes } from "@fluxerjs/types";
8
8
  import { EmbedBuilder as EmbedBuilder2 } from "@fluxerjs/builders";
9
9
 
10
10
  // src/util/messageUtils.ts
11
+ import { MessageFlags } from "@fluxerjs/types";
11
12
  import { EmbedBuilder } from "@fluxerjs/builders";
12
13
 
13
14
  // src/errors/ErrorCodes.ts
@@ -22,6 +23,7 @@ async function resolveMessageFiles(files) {
22
23
  const result = [];
23
24
  for (let i = 0; i < files.length; i++) {
24
25
  const f = files[i];
26
+ if (!f) continue;
25
27
  const filename = f.filename ?? f.name;
26
28
  if ("url" in f && f.url) {
27
29
  if (!URL.canParse(f.url)) {
@@ -60,6 +62,21 @@ async function resolveMessageFiles(files) {
60
62
  }
61
63
  return result;
62
64
  }
65
+ function toAPIAllowedMentions(options) {
66
+ const result = {};
67
+ if (options.parse?.length) result.parse = options.parse;
68
+ if (options.users?.length) result.users = options.users;
69
+ if (options.roles?.length) result.roles = options.roles;
70
+ if (options.repliedUser !== void 0) result.replied_user = options.repliedUser;
71
+ return result;
72
+ }
73
+ function applyReplyPingSuppression(body) {
74
+ body.flags = (body.flags ?? 0) | MessageFlags.SuppressNotifications;
75
+ body.allowed_mentions = {
76
+ ...body.allowed_mentions ?? {},
77
+ replied_user: false
78
+ };
79
+ }
63
80
  function buildSendBody(options) {
64
81
  const body = typeof options === "string" ? { content: options } : options;
65
82
  const result = {};
@@ -81,9 +98,39 @@ function buildSendBody(options) {
81
98
  filename: f.filename ?? f.name
82
99
  }));
83
100
  }
101
+ if (body.allowedMentions) result.allowed_mentions = toAPIAllowedMentions(body.allowedMentions);
102
+ if (body.stickerIds?.length) result.sticker_ids = body.stickerIds;
103
+ if (body.nonce !== void 0) result.nonce = body.nonce;
104
+ if (body.favoriteMemeId !== void 0) result.favorite_meme_id = body.favoriteMemeId;
105
+ if (body.tts !== void 0) result.tts = body.tts;
84
106
  if (body.flags !== void 0) result.flags = body.flags;
85
107
  return result;
86
108
  }
109
+ function toMessageReference(target) {
110
+ const ref = {
111
+ channel_id: target.channelId,
112
+ message_id: target.messageId
113
+ };
114
+ if (target.guildId != null && target.guildId !== "") ref.guild_id = target.guildId;
115
+ return ref;
116
+ }
117
+ async function prepareMessagePostPayload(options) {
118
+ if (typeof options === "string") {
119
+ if (options.length === 0) throw new RangeError("Cannot send an empty message");
120
+ options = { content: options };
121
+ }
122
+ const { replyTo, ping, files, allowedMentions, ...sendFields } = options;
123
+ const body = buildSendBody({ ...sendFields, files, allowedMentions });
124
+ if (replyTo) {
125
+ body.message_reference = toMessageReference(replyTo);
126
+ const suppressPing = ping === false || allowedMentions?.repliedUser === false || body.allowed_mentions?.replied_user === false;
127
+ if (suppressPing) applyReplyPingSuppression(body);
128
+ } else if (allowedMentions?.repliedUser === false) {
129
+ applyReplyPingSuppression(body);
130
+ }
131
+ const resolvedFiles = files?.length ? await resolveMessageFiles(files) : void 0;
132
+ return { body, files: resolvedFiles };
133
+ }
87
134
 
88
135
  // src/util/ReactionCollector.ts
89
136
  import { EventEmitter } from "events";
@@ -286,7 +333,7 @@ var Message = class _Message extends Base {
286
333
  * await message.send({ content: 'File', files: [{ name: 'data.txt', data }] });
287
334
  */
288
335
  async send(options) {
289
- const payload = await _Message._createMessageBody(options);
336
+ const payload = await prepareMessagePostPayload(options);
290
337
  return this._send(payload);
291
338
  }
292
339
  /**
@@ -307,42 +354,53 @@ var Message = class _Message extends Base {
307
354
  * await message.reply('Pong!');
308
355
  * await message.reply({ embeds: [embed] });
309
356
  * await message.reply('No ping!', { ping: false });
357
+ * await message.reply({ content: 'Silent reply', allowedMentions: AllowedMentions.suppressReplyPing });
310
358
  * await message.reply({ content: 'Reply to other', replyTo: otherMessage });
311
359
  */
312
360
  async reply(options, replyOptions) {
313
361
  const opts = typeof options === "string" ? { content: options } : options;
314
362
  const mergedReply = replyOptions ?? (opts.ping !== void 0 || opts.replyTo !== void 0) ? { ping: opts.ping, replyTo: opts.replyTo } : void 0;
315
363
  const refMessage = mergedReply?.replyTo ?? this;
316
- const ref = refMessage instanceof _Message ? {
317
- channel_id: refMessage.channelId,
318
- message_id: refMessage.id,
319
- guild_id: refMessage.guildId ?? void 0
320
- } : {
321
- channel_id: refMessage.channelId,
322
- message_id: refMessage.messageId,
323
- guild_id: void 0
324
- };
325
- const payload = await _Message._createMessageBody(opts, ref, mergedReply?.ping !== false);
364
+ const replyTo = refMessage instanceof _Message ? {
365
+ channelId: refMessage.channelId,
366
+ messageId: refMessage.id,
367
+ guildId: refMessage.guildId
368
+ } : refMessage;
369
+ const { replyTo: _ignored, ping: _ping, ...sendOpts } = opts;
370
+ const payload = await prepareMessagePostPayload({
371
+ ...sendOpts,
372
+ replyTo,
373
+ ping: mergedReply?.ping
374
+ });
326
375
  return this._send(payload);
327
376
  }
328
- /** Exposed for testing purposes, use Message.reply() or send() for normal use */
377
+ /** Exposed for testing — prefer `message.reply()` / `prepareMessagePostPayload()`. */
329
378
  static async _createMessageBody(content, referenced_message, ping) {
379
+ if (typeof content === "string" && content.length === 0) {
380
+ throw new RangeError("Cannot send an empty message");
381
+ }
330
382
  if (typeof content === "string") {
331
- if (content.length === 0) {
332
- throw new RangeError("Cannot send an empty message");
383
+ const opts2 = { content };
384
+ if (referenced_message) {
385
+ opts2.replyTo = {
386
+ channelId: referenced_message.channel_id,
387
+ messageId: referenced_message.message_id,
388
+ ...referenced_message.guild_id != null ? { guildId: referenced_message.guild_id } : {}
389
+ };
390
+ if (ping !== void 0) opts2.ping = ping;
333
391
  }
334
- content = { content };
392
+ return prepareMessagePostPayload(opts2);
335
393
  }
336
- const base = buildSendBody(content);
337
- const files = content.files?.length ? await resolveMessageFiles(content.files) : void 0;
338
- const body = { ...base };
394
+ const opts = { ...content };
339
395
  if (referenced_message) {
340
- body.message_reference = referenced_message;
341
- if (ping === false) {
342
- body.flags = (body.flags ?? 0) | MessageFlags.SuppressNotifications;
343
- }
396
+ opts.replyTo = {
397
+ channelId: referenced_message.channel_id,
398
+ messageId: referenced_message.message_id,
399
+ ...referenced_message.guild_id != null ? { guildId: referenced_message.guild_id } : {}
400
+ };
401
+ if (ping !== void 0) opts.ping = ping;
344
402
  }
345
- return { files, body };
403
+ return prepareMessagePostPayload(opts);
346
404
  }
347
405
  async _send(payload) {
348
406
  const data = await this.client.rest.post(
@@ -362,6 +420,9 @@ var Message = class _Message extends Base {
362
420
  if (options.embeds?.length) {
363
421
  body.embeds = options.embeds.map((e) => e instanceof EmbedBuilder2 ? e.toJSON() : e);
364
422
  }
423
+ if (options.allowedMentions) {
424
+ body.allowed_mentions = toAPIAllowedMentions(options.allowedMentions);
425
+ }
365
426
  const data = await this.client.rest.patch(Routes.channelMessage(this.channelId, this.id), {
366
427
  body
367
428
  });