@fluxerjs/core 1.3.1 → 2.0.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/README.md +8 -1
- package/dist/client.d.mts +3 -3
- package/dist/client.d.ts +3 -3
- package/dist/client.js +4015 -2593
- package/dist/client.mjs +4009 -2584
- package/dist/cluster.d.mts +153 -0
- package/dist/cluster.d.ts +153 -0
- package/dist/cluster.js +5730 -0
- package/dist/cluster.mjs +5718 -0
- package/dist/index.d.mts +19 -36
- package/dist/index.d.ts +19 -36
- package/dist/index.js +4482 -2626
- package/dist/index.mjs +4445 -2612
- package/dist/message.d.mts +3 -3
- package/dist/message.d.ts +3 -3
- package/dist/message.js +388 -226
- package/dist/message.mjs +389 -224
- package/dist/subpath-message-Acbzr5HA.d.mts +2725 -0
- package/dist/subpath-message-Acbzr5HA.d.ts +2725 -0
- package/package.json +14 -9
- package/dist/subpath-message-CZC4oGko.d.mts +0 -2071
- package/dist/subpath-message-CZC4oGko.d.ts +0 -2071
package/dist/message.mjs
CHANGED
|
@@ -1,14 +1,25 @@
|
|
|
1
|
-
// src/structures/
|
|
2
|
-
var Base = class {
|
|
3
|
-
};
|
|
4
|
-
|
|
5
|
-
// src/structures/Message.ts
|
|
1
|
+
// src/structures/message/Message.ts
|
|
6
2
|
import { Collection as Collection2 } from "@fluxerjs/collection";
|
|
7
|
-
import { MessageType, MessageFlags, Routes } from "@fluxerjs/types";
|
|
8
3
|
import { EmbedBuilder as EmbedBuilder2 } from "@fluxerjs/builders";
|
|
4
|
+
import {
|
|
5
|
+
MessageType,
|
|
6
|
+
Routes as Routes3
|
|
7
|
+
} from "@fluxerjs/types";
|
|
9
8
|
|
|
10
|
-
// src/
|
|
11
|
-
|
|
9
|
+
// src/client/sdkOptions.ts
|
|
10
|
+
function toMessageAttachmentEditWire(attachments) {
|
|
11
|
+
return attachments.map((a) => {
|
|
12
|
+
const out = { id: a.id };
|
|
13
|
+
if (a.filename !== void 0) out.filename = a.filename;
|
|
14
|
+
if (a.uploadFilename !== void 0) out.upload_filename = a.uploadFilename;
|
|
15
|
+
if (a.fileSize !== void 0) out.file_size = a.fileSize;
|
|
16
|
+
if (a.contentType !== void 0) out.content_type = a.contentType;
|
|
17
|
+
if (a.title !== void 0) out.title = a.title;
|
|
18
|
+
if (a.description !== void 0) out.description = a.description;
|
|
19
|
+
if (a.flags !== void 0) out.flags = a.flags;
|
|
20
|
+
return out;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
12
23
|
|
|
13
24
|
// src/errors/ErrorCodes.ts
|
|
14
25
|
import { ErrorCodes } from "@fluxerjs/util";
|
|
@@ -16,40 +27,38 @@ import { ErrorCodes } from "@fluxerjs/util";
|
|
|
16
27
|
// src/errors/FluxerError.ts
|
|
17
28
|
import { FluxerError } from "@fluxerjs/util";
|
|
18
29
|
|
|
19
|
-
// src/util/messageUtils.ts
|
|
30
|
+
// src/util/messageUtils/files.ts
|
|
20
31
|
var FILE_FETCH_TIMEOUT_MS = 3e4;
|
|
32
|
+
async function fetchFileBuffer(url, index) {
|
|
33
|
+
if (!URL.canParse(url)) {
|
|
34
|
+
throw new FluxerError(`Invalid file URL at index ${index}: ${url}`, {
|
|
35
|
+
code: ErrorCodes.InvalidFileUrl
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
let res;
|
|
39
|
+
try {
|
|
40
|
+
res = await fetch(url, { signal: AbortSignal.timeout(FILE_FETCH_TIMEOUT_MS) });
|
|
41
|
+
} catch (err) {
|
|
42
|
+
throw new FluxerError(`Failed to fetch file from ${url}`, {
|
|
43
|
+
code: ErrorCodes.FileFetchFailed,
|
|
44
|
+
cause: err instanceof Error ? err : void 0
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
if (!res.ok) {
|
|
48
|
+
throw new FluxerError(`Failed to fetch file from ${url}: ${res.status} ${res.statusText}`, {
|
|
49
|
+
code: ErrorCodes.FileFetchFailed
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return res.arrayBuffer();
|
|
53
|
+
}
|
|
21
54
|
async function resolveMessageFiles(files) {
|
|
22
55
|
const result = [];
|
|
23
56
|
for (let i = 0; i < files.length; i++) {
|
|
24
57
|
const f = files[i];
|
|
58
|
+
if (!f) continue;
|
|
25
59
|
const filename = f.filename ?? f.name;
|
|
26
60
|
if ("url" in f && f.url) {
|
|
27
|
-
|
|
28
|
-
throw new FluxerError(`Invalid file URL at index ${i}: ${f.url}`, {
|
|
29
|
-
code: ErrorCodes.InvalidFileUrl
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
let res;
|
|
33
|
-
try {
|
|
34
|
-
res = await fetch(f.url, {
|
|
35
|
-
signal: AbortSignal.timeout(FILE_FETCH_TIMEOUT_MS)
|
|
36
|
-
});
|
|
37
|
-
} catch (err) {
|
|
38
|
-
throw new FluxerError(`Failed to fetch file from ${f.url}`, {
|
|
39
|
-
code: ErrorCodes.FileFetchFailed,
|
|
40
|
-
cause: err instanceof Error ? err : void 0
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
if (!res.ok) {
|
|
44
|
-
throw new FluxerError(
|
|
45
|
-
`Failed to fetch file from ${f.url}: ${res.status} ${res.statusText}`,
|
|
46
|
-
{
|
|
47
|
-
code: ErrorCodes.FileFetchFailed
|
|
48
|
-
}
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
const data = await res.arrayBuffer();
|
|
52
|
-
result.push({ name: f.name, data, filename });
|
|
61
|
+
result.push({ name: f.name, data: await fetchFileBuffer(f.url, i), filename });
|
|
53
62
|
} else if ("data" in f && f.data != null) {
|
|
54
63
|
result.push({ name: f.name, data: f.data, filename });
|
|
55
64
|
} else {
|
|
@@ -60,30 +69,142 @@ async function resolveMessageFiles(files) {
|
|
|
60
69
|
}
|
|
61
70
|
return result;
|
|
62
71
|
}
|
|
72
|
+
|
|
73
|
+
// src/util/messageUtils/body.ts
|
|
74
|
+
import { MessageReferenceType } from "@fluxerjs/types";
|
|
75
|
+
import { EmbedBuilder, MessagePayload } from "@fluxerjs/builders";
|
|
76
|
+
function toAPIAllowedMentions(options) {
|
|
77
|
+
const result = {};
|
|
78
|
+
if (options.parse !== void 0) result.parse = options.parse;
|
|
79
|
+
if (options.users?.length) result.users = options.users;
|
|
80
|
+
if (options.roles?.length) result.roles = options.roles;
|
|
81
|
+
if (options.repliedUser !== void 0) result.replied_user = options.repliedUser;
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
function applyReplyPingSuppression(body) {
|
|
85
|
+
body.allowed_mentions = {
|
|
86
|
+
...body.allowed_mentions ?? {},
|
|
87
|
+
replied_user: false
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function assignIfDefined(target, key, value) {
|
|
91
|
+
if (value !== void 0) target[key] = value;
|
|
92
|
+
}
|
|
63
93
|
function buildSendBody(options) {
|
|
64
94
|
const body = typeof options === "string" ? { content: options } : options;
|
|
65
95
|
const result = {};
|
|
66
|
-
|
|
96
|
+
assignIfDefined(result, "content", body.content);
|
|
67
97
|
if (body.embeds?.length) {
|
|
68
98
|
result.embeds = body.embeds.map((e) => e instanceof EmbedBuilder ? e.toJSON() : e);
|
|
69
99
|
}
|
|
70
|
-
if (body.
|
|
71
|
-
result.attachments = body.
|
|
100
|
+
if (body.uploadedAttachments?.length) {
|
|
101
|
+
result.attachments = body.uploadedAttachments;
|
|
102
|
+
} else if (body.files?.length) {
|
|
103
|
+
result.attachments = body.attachments?.length ? body.attachments.map((a) => ({
|
|
72
104
|
id: a.id,
|
|
73
105
|
filename: a.filename,
|
|
74
106
|
...a.title != null && { title: a.title },
|
|
75
107
|
...a.description != null && { description: a.description },
|
|
76
108
|
...a.flags != null && { flags: a.flags }
|
|
77
|
-
}));
|
|
78
|
-
} else if (body.files?.length) {
|
|
79
|
-
result.attachments = body.files.map((f, i) => ({
|
|
80
|
-
id: i,
|
|
81
|
-
filename: f.filename ?? f.name
|
|
82
|
-
}));
|
|
109
|
+
})) : body.files.map((f, i) => ({ id: i, filename: f.filename ?? f.name }));
|
|
83
110
|
}
|
|
84
|
-
if (body.
|
|
111
|
+
if (body.allowedMentions) result.allowed_mentions = toAPIAllowedMentions(body.allowedMentions);
|
|
112
|
+
if (body.stickerIds?.length) result.sticker_ids = body.stickerIds;
|
|
113
|
+
assignIfDefined(result, "nonce", body.nonce);
|
|
114
|
+
assignIfDefined(result, "favorite_meme_id", body.favoriteMemeId);
|
|
115
|
+
assignIfDefined(result, "tts", body.tts);
|
|
116
|
+
assignIfDefined(result, "flags", body.flags);
|
|
85
117
|
return result;
|
|
86
118
|
}
|
|
119
|
+
function toMessageReference(target, forceForward = false) {
|
|
120
|
+
const ref = {
|
|
121
|
+
channel_id: target.channelId,
|
|
122
|
+
message_id: target.messageId
|
|
123
|
+
};
|
|
124
|
+
if (target.guildId) ref.guild_id = target.guildId;
|
|
125
|
+
const type = forceForward ? MessageReferenceType.Forward : target.type ?? MessageReferenceType.Default;
|
|
126
|
+
if (type !== MessageReferenceType.Default) ref.type = type;
|
|
127
|
+
if (target.attachmentIds?.length) ref.attachment_ids = target.attachmentIds;
|
|
128
|
+
if (target.embedIndices?.length) ref.embed_indices = target.embedIndices;
|
|
129
|
+
return ref;
|
|
130
|
+
}
|
|
131
|
+
function shouldSuppressReplyPing(ping, allowedMentions, body) {
|
|
132
|
+
return ping === false || allowedMentions?.repliedUser === false || body.allowed_mentions?.replied_user === false;
|
|
133
|
+
}
|
|
134
|
+
function messagePayloadToSendOptions(payload) {
|
|
135
|
+
const json = payload.toJSON();
|
|
136
|
+
const options = {};
|
|
137
|
+
if (json.content != null) options.content = json.content;
|
|
138
|
+
if (json.embeds?.length) options.embeds = json.embeds;
|
|
139
|
+
if (json.tts !== void 0) options.tts = json.tts;
|
|
140
|
+
if (json.flags !== void 0) options.flags = json.flags;
|
|
141
|
+
if (json.allowed_mentions) {
|
|
142
|
+
options.allowedMentions = {
|
|
143
|
+
parse: json.allowed_mentions.parse,
|
|
144
|
+
users: json.allowed_mentions.users,
|
|
145
|
+
roles: json.allowed_mentions.roles,
|
|
146
|
+
repliedUser: json.allowed_mentions.replied_user
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
if (json.message_reference?.channel_id && json.message_reference.message_id) {
|
|
150
|
+
options.replyTo = {
|
|
151
|
+
channelId: json.message_reference.channel_id,
|
|
152
|
+
messageId: json.message_reference.message_id,
|
|
153
|
+
guildId: json.message_reference.guild_id
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
if (json.attachments?.length) {
|
|
157
|
+
options.attachments = json.attachments.map((a) => ({
|
|
158
|
+
id: a.id,
|
|
159
|
+
filename: a.filename,
|
|
160
|
+
description: a.description ?? void 0
|
|
161
|
+
}));
|
|
162
|
+
}
|
|
163
|
+
return options;
|
|
164
|
+
}
|
|
165
|
+
async function prepareMessagePostPayload(options, defaults) {
|
|
166
|
+
let normalized = options instanceof MessagePayload ? messagePayloadToSendOptions(options) : options;
|
|
167
|
+
if (typeof normalized === "string") {
|
|
168
|
+
if (normalized.length === 0) {
|
|
169
|
+
throw new FluxerError("Cannot send an empty message", { code: ErrorCodes.EmptyMessage });
|
|
170
|
+
}
|
|
171
|
+
normalized = { content: normalized };
|
|
172
|
+
} else {
|
|
173
|
+
normalized = { ...normalized };
|
|
174
|
+
if (!normalized.allowedMentions && defaults?.defaultAllowedMentions) {
|
|
175
|
+
normalized.allowedMentions = defaults.defaultAllowedMentions;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const { replyTo, forward, ping, files, allowedMentions, uploadedAttachments, ...sendFields } = normalized;
|
|
179
|
+
if (files?.length && uploadedAttachments?.length) {
|
|
180
|
+
throw new FluxerError("Cannot combine multipart files with uploadedAttachments", {
|
|
181
|
+
code: ErrorCodes.InvalidMessageOptions
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
if (replyTo && forward) {
|
|
185
|
+
throw new FluxerError("Cannot combine replyTo and forward", {
|
|
186
|
+
code: ErrorCodes.InvalidMessageOptions
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
const body = buildSendBody({ ...sendFields, files, allowedMentions, uploadedAttachments });
|
|
190
|
+
if (forward) {
|
|
191
|
+
if (!forward.channelId || !forward.messageId) {
|
|
192
|
+
throw new FluxerError("forward requires channelId and messageId", {
|
|
193
|
+
code: ErrorCodes.InvalidMessageOptions
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
body.message_reference = toMessageReference(forward, true);
|
|
197
|
+
} else if (replyTo) {
|
|
198
|
+
body.message_reference = toMessageReference(replyTo);
|
|
199
|
+
if (shouldSuppressReplyPing(ping, allowedMentions, body)) applyReplyPingSuppression(body);
|
|
200
|
+
} else if (allowedMentions?.repliedUser === false) {
|
|
201
|
+
applyReplyPingSuppression(body);
|
|
202
|
+
}
|
|
203
|
+
return {
|
|
204
|
+
body,
|
|
205
|
+
files: !uploadedAttachments?.length && files?.length ? await resolveMessageFiles(files) : void 0
|
|
206
|
+
};
|
|
207
|
+
}
|
|
87
208
|
|
|
88
209
|
// src/util/ReactionCollector.ts
|
|
89
210
|
import { EventEmitter } from "events";
|
|
@@ -97,18 +218,20 @@ var Events = {
|
|
|
97
218
|
MessageDelete: "messageDelete",
|
|
98
219
|
MessageDeleteBulk: "messageDeleteBulk",
|
|
99
220
|
MessageReactionAdd: "messageReactionAdd",
|
|
221
|
+
MessageReactionAddMany: "messageReactionAddMany",
|
|
100
222
|
MessageReactionRemove: "messageReactionRemove",
|
|
101
223
|
MessageReactionRemoveAll: "messageReactionRemoveAll",
|
|
102
224
|
MessageReactionRemoveEmoji: "messageReactionRemoveEmoji",
|
|
103
|
-
InteractionCreate: "interactionCreate",
|
|
104
225
|
GuildCreate: "guildCreate",
|
|
105
226
|
GuildUpdate: "guildUpdate",
|
|
106
227
|
GuildDelete: "guildDelete",
|
|
107
228
|
GuildBanAdd: "guildBanAdd",
|
|
108
229
|
GuildBanRemove: "guildBanRemove",
|
|
230
|
+
GuildAuditLogEntryCreate: "guildAuditLogEntryCreate",
|
|
109
231
|
GuildEmojisUpdate: "guildEmojisUpdate",
|
|
110
232
|
GuildStickersUpdate: "guildStickersUpdate",
|
|
111
|
-
|
|
233
|
+
GuildCountsUpdate: "guildCountsUpdate",
|
|
234
|
+
ChannelMemberCountsUpdate: "channelMemberCountsUpdate",
|
|
112
235
|
GuildMemberAdd: "guildMemberAdd",
|
|
113
236
|
GuildMemberUpdate: "guildMemberUpdate",
|
|
114
237
|
GuildMemberRemove: "guildMemberRemove",
|
|
@@ -116,9 +239,6 @@ var Events = {
|
|
|
116
239
|
GuildRoleCreate: "guildRoleCreate",
|
|
117
240
|
GuildRoleUpdate: "guildRoleUpdate",
|
|
118
241
|
GuildRoleDelete: "guildRoleDelete",
|
|
119
|
-
GuildScheduledEventCreate: "guildScheduledEventCreate",
|
|
120
|
-
GuildScheduledEventUpdate: "guildScheduledEventUpdate",
|
|
121
|
-
GuildScheduledEventDelete: "guildScheduledEventDelete",
|
|
122
242
|
ChannelCreate: "channelCreate",
|
|
123
243
|
ChannelUpdate: "channelUpdate",
|
|
124
244
|
ChannelDelete: "channelDelete",
|
|
@@ -127,9 +247,14 @@ var Events = {
|
|
|
127
247
|
InviteDelete: "inviteDelete",
|
|
128
248
|
TypingStart: "typingStart",
|
|
129
249
|
UserUpdate: "userUpdate",
|
|
250
|
+
UserConnectionsUpdate: "userConnectionsUpdate",
|
|
251
|
+
WebAuthnCredentialsUpdate: "webauthnCredentialsUpdate",
|
|
130
252
|
PresenceUpdate: "presenceUpdate",
|
|
253
|
+
PresenceUpdateBulk: "presenceUpdateBulk",
|
|
131
254
|
VoiceStateUpdate: "voiceStateUpdate",
|
|
255
|
+
VoiceStateAck: "voiceStateAck",
|
|
132
256
|
VoiceServerUpdate: "voiceServerUpdate",
|
|
257
|
+
EntranceSoundPlay: "entranceSoundPlay",
|
|
133
258
|
VoiceStatesSync: "voiceStatesSync",
|
|
134
259
|
WebhooksUpdate: "webhooksUpdate",
|
|
135
260
|
Resumed: "resumed",
|
|
@@ -157,12 +282,14 @@ var ReactionCollector = class extends EventEmitter {
|
|
|
157
282
|
time: options.time ?? 0,
|
|
158
283
|
max: options.max ?? 0
|
|
159
284
|
};
|
|
160
|
-
this._listener = (
|
|
161
|
-
if (this._ended || reaction.messageId !== this.messageId ||
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
this.
|
|
165
|
-
|
|
285
|
+
this._listener = (payload) => {
|
|
286
|
+
if (this._ended || payload.reaction.messageId !== this.messageId || payload.channelId !== this.channelId) {
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
if (!this.options.filter(payload.reaction, payload.user)) return;
|
|
290
|
+
const key = `${payload.userId}:${payload.reaction.emoji.id ?? payload.reaction.emoji.name}`;
|
|
291
|
+
this.collected.set(key, { reaction: payload.reaction, user: payload.user });
|
|
292
|
+
this.emit("collect", payload.reaction, payload.user);
|
|
166
293
|
if (this.options.max > 0 && this.collected.size >= this.options.max) {
|
|
167
294
|
this.stop("limit");
|
|
168
295
|
}
|
|
@@ -190,64 +317,199 @@ var ReactionCollector = class extends EventEmitter {
|
|
|
190
317
|
}
|
|
191
318
|
};
|
|
192
319
|
|
|
193
|
-
// src/structures/
|
|
320
|
+
// src/structures/Base.ts
|
|
321
|
+
var Base = class {
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
// src/structures/message/reactions.ts
|
|
325
|
+
import { Routes } from "@fluxerjs/types";
|
|
326
|
+
async function emojiPath(message, emoji) {
|
|
327
|
+
return message.client.resolveEmoji(emoji, message.guildId);
|
|
328
|
+
}
|
|
329
|
+
async function reactToMessage(message, emoji) {
|
|
330
|
+
const e = await emojiPath(message, emoji);
|
|
331
|
+
await message.client.rest.put(Routes.channelMessageReactionMe(message.channelId, message.id, e));
|
|
332
|
+
}
|
|
333
|
+
async function removeMessageReaction(message, emoji, userId) {
|
|
334
|
+
const e = await emojiPath(message, emoji);
|
|
335
|
+
const route = userId ? Routes.channelMessageReactionUser(message.channelId, message.id, e, userId) : Routes.channelMessageReactionMe(message.channelId, message.id, e);
|
|
336
|
+
await message.client.rest.delete(route);
|
|
337
|
+
}
|
|
338
|
+
async function removeAllMessageReactions(message) {
|
|
339
|
+
await message.client.rest.delete(Routes.channelMessageReactions(message.channelId, message.id));
|
|
340
|
+
}
|
|
341
|
+
async function removeMessageReactionEmoji(message, emoji) {
|
|
342
|
+
const e = await emojiPath(message, emoji);
|
|
343
|
+
await message.client.rest.delete(Routes.channelMessageReaction(message.channelId, message.id, e));
|
|
344
|
+
}
|
|
345
|
+
async function fetchMessageReactionUsers(message, emoji, options) {
|
|
346
|
+
return (await fetchMessageReactionUsersPage(message, emoji, options)).users;
|
|
347
|
+
}
|
|
348
|
+
async function fetchMessageReactionUsersPage(message, emoji, options) {
|
|
349
|
+
const e = await emojiPath(message, emoji);
|
|
350
|
+
const params = new URLSearchParams();
|
|
351
|
+
if (options?.limit != null) params.set("limit", String(options.limit));
|
|
352
|
+
if (options?.after) params.set("after", options.after);
|
|
353
|
+
const qs = params.toString();
|
|
354
|
+
const route = Routes.channelMessageReactionUsers(message.channelId, message.id, e) + (qs ? `?${qs}` : "");
|
|
355
|
+
const data = await message.client.rest.get(route);
|
|
356
|
+
if (Array.isArray(data)) {
|
|
357
|
+
return {
|
|
358
|
+
users: data.map((u) => message.client.getOrCreateUser(u)),
|
|
359
|
+
hasMore: false,
|
|
360
|
+
nextAfter: null
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
const list = data.items ?? data.users ?? [];
|
|
364
|
+
return {
|
|
365
|
+
users: list.map((u) => message.client.getOrCreateUser(u)),
|
|
366
|
+
hasMore: data.has_more ?? false,
|
|
367
|
+
nextAfter: data.next_after ?? null
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// src/structures/message/send.ts
|
|
372
|
+
import { MessagePayload as MessagePayloadBuilder } from "@fluxerjs/builders";
|
|
373
|
+
import { Routes as Routes2 } from "@fluxerjs/types";
|
|
374
|
+
function toReplyTarget(ref) {
|
|
375
|
+
return {
|
|
376
|
+
channelId: ref.channel_id,
|
|
377
|
+
messageId: ref.message_id,
|
|
378
|
+
...ref.guild_id != null ? { guildId: ref.guild_id } : {}
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
function toSendOptions(content) {
|
|
382
|
+
if (typeof content === "string") return { content };
|
|
383
|
+
if (content instanceof MessagePayloadBuilder) return messagePayloadToSendOptions(content);
|
|
384
|
+
return { ...content };
|
|
385
|
+
}
|
|
386
|
+
async function createMessageBody(content, referenced_message, ping) {
|
|
387
|
+
if (typeof content === "string" && content.length === 0) {
|
|
388
|
+
throw new FluxerError("Cannot send an empty message", { code: ErrorCodes.EmptyMessage });
|
|
389
|
+
}
|
|
390
|
+
const opts = toSendOptions(content);
|
|
391
|
+
if (referenced_message) {
|
|
392
|
+
opts.replyTo = toReplyTarget(referenced_message);
|
|
393
|
+
if (ping !== void 0) opts.ping = ping;
|
|
394
|
+
}
|
|
395
|
+
return prepareMessagePostPayload(opts);
|
|
396
|
+
}
|
|
397
|
+
function asReplyTarget(value) {
|
|
398
|
+
if ("messageId" in value) return value;
|
|
399
|
+
return {
|
|
400
|
+
channelId: value.channelId,
|
|
401
|
+
messageId: value.id,
|
|
402
|
+
guildId: value.guildId
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
async function sendPrepared(message, payload) {
|
|
406
|
+
const data = await message.client.rest.post(
|
|
407
|
+
Routes2.channelMessages(message.channelId),
|
|
408
|
+
payload
|
|
409
|
+
);
|
|
410
|
+
message.client._addMessageToCache(message.channelId, data);
|
|
411
|
+
return new message.constructor(message.client, data);
|
|
412
|
+
}
|
|
413
|
+
async function sendMessage(message, options) {
|
|
414
|
+
return message._send(
|
|
415
|
+
await prepareMessagePostPayload(options, {
|
|
416
|
+
defaultAllowedMentions: message.client.options.defaultAllowedMentions
|
|
417
|
+
})
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
async function sendMessageTo(message, channelId, options) {
|
|
421
|
+
return message.client.channels.send(channelId, options);
|
|
422
|
+
}
|
|
423
|
+
async function replyToMessage(message, options, replyOptions) {
|
|
424
|
+
const opts = typeof options === "string" ? { content: options } : { ...options };
|
|
425
|
+
const { replyTo: inlineReplyTo, ping: inlinePing, ...sendOpts } = opts;
|
|
426
|
+
const ping = replyOptions?.ping ?? inlinePing ?? message.client.options.defaultReplyPing;
|
|
427
|
+
const replyTo = asReplyTarget(replyOptions?.replyTo ?? inlineReplyTo ?? message);
|
|
428
|
+
return message._send(
|
|
429
|
+
await prepareMessagePostPayload(
|
|
430
|
+
{ ...sendOpts, replyTo, ping },
|
|
431
|
+
{ defaultAllowedMentions: message.client.options.defaultAllowedMentions }
|
|
432
|
+
)
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// src/structures/message/Message.ts
|
|
194
437
|
var Message = class _Message extends Base {
|
|
438
|
+
/** The {@link Client} that instantiated this message. */
|
|
195
439
|
client;
|
|
440
|
+
/** Snowflake ID of this message. */
|
|
196
441
|
id;
|
|
442
|
+
/** Channel ID where this message was sent. */
|
|
197
443
|
channelId;
|
|
444
|
+
/** Guild ID if in a guild channel, null for DMs. */
|
|
198
445
|
guildId;
|
|
446
|
+
/** The user who sent this message. */
|
|
199
447
|
author;
|
|
448
|
+
/** Text content of the message. */
|
|
200
449
|
content;
|
|
450
|
+
/** When the message was created. */
|
|
201
451
|
createdAt;
|
|
452
|
+
/** When the message was last edited, or null if never edited. */
|
|
202
453
|
editedAt;
|
|
454
|
+
/** Whether the message is pinned. */
|
|
203
455
|
pinned;
|
|
456
|
+
/** Attached files (images, videos, etc.). */
|
|
204
457
|
attachments;
|
|
458
|
+
/** Message type (Default, Reply, etc.). */
|
|
205
459
|
type;
|
|
460
|
+
/** Message flags bitfield. */
|
|
206
461
|
flags;
|
|
462
|
+
/** Whether `@everyone` or `@here` was mentioned. */
|
|
207
463
|
mentionEveryone;
|
|
464
|
+
/** Whether this is a text-to-speech message. */
|
|
208
465
|
tts;
|
|
466
|
+
/** Embedded content (rich previews, etc.). */
|
|
209
467
|
embeds;
|
|
468
|
+
/** Stickers sent with the message. */
|
|
210
469
|
stickers;
|
|
470
|
+
/** Reactions on the message. */
|
|
211
471
|
reactions;
|
|
472
|
+
/** Reference to a replied-to message, if any. */
|
|
212
473
|
messageReference;
|
|
474
|
+
/** Message snapshots for forwarded messages. */
|
|
213
475
|
messageSnapshots;
|
|
476
|
+
/** Call data if this message represents a call event. */
|
|
214
477
|
call;
|
|
478
|
+
/** The full referenced (replied-to) message, or null. */
|
|
215
479
|
referencedMessage;
|
|
216
|
-
/** Webhook ID if
|
|
480
|
+
/** Webhook ID if sent by a webhook. */
|
|
217
481
|
webhookId;
|
|
218
|
-
/** Users mentioned in
|
|
482
|
+
/** Users mentioned in the message. */
|
|
219
483
|
mentions;
|
|
220
|
-
/** Role IDs mentioned in
|
|
484
|
+
/** Role IDs mentioned in the message. */
|
|
221
485
|
mentionRoles;
|
|
222
|
-
/** Client-side nonce for
|
|
486
|
+
/** Client-side nonce for deduplication. */
|
|
223
487
|
nonce;
|
|
224
|
-
/**
|
|
225
|
-
* Channel where this message was sent. Resolved from cache; null if not cached.
|
|
226
|
-
* Messages can only exist in text-based channels (text, DM, announcement), so this always has send() when non-null.
|
|
227
|
-
*/
|
|
488
|
+
/** Cached text/DM/guild channel, or null if uncached. */
|
|
228
489
|
get channel() {
|
|
229
490
|
return this.client.channels.get(this.channelId) ?? null;
|
|
230
491
|
}
|
|
231
|
-
/**
|
|
492
|
+
/** Cached guild, or null for DMs / uncached. */
|
|
232
493
|
get guild() {
|
|
233
494
|
return this.guildId ? this.client.guilds.get(this.guildId) ?? null : null;
|
|
234
495
|
}
|
|
235
|
-
/**
|
|
236
|
-
* Resolve the channel (from cache or API). Use when you need the channel and it may not be cached.
|
|
237
|
-
* @returns The channel
|
|
238
|
-
* @throws FluxerError with CHANNEL_NOT_FOUND if the channel does not exist
|
|
239
|
-
*/
|
|
496
|
+
/** Fetch and resolve the channel this message is in. */
|
|
240
497
|
async resolveChannel() {
|
|
241
498
|
return this.client.channels.resolve(this.channelId);
|
|
242
499
|
}
|
|
243
|
-
/**
|
|
244
|
-
* Resolve the guild (from cache or API). Returns null for DMs.
|
|
245
|
-
* @returns The guild, or null if this is a DM or guild not found
|
|
246
|
-
*/
|
|
500
|
+
/** Fetch and resolve the guild, or null if a DM. */
|
|
247
501
|
async resolveGuild() {
|
|
248
|
-
|
|
502
|
+
if (!this.guildId) return null;
|
|
503
|
+
try {
|
|
504
|
+
return await this.client.guilds.resolve(this.guildId);
|
|
505
|
+
} catch {
|
|
506
|
+
return null;
|
|
507
|
+
}
|
|
249
508
|
}
|
|
250
|
-
/**
|
|
509
|
+
/**
|
|
510
|
+
* Construct a message from API data.
|
|
511
|
+
* @param data - API message from REST or MESSAGE_CREATE
|
|
512
|
+
*/
|
|
251
513
|
constructor(client, data) {
|
|
252
514
|
super();
|
|
253
515
|
this.client = client;
|
|
@@ -277,206 +539,109 @@ var Message = class _Message extends Base {
|
|
|
277
539
|
this.mentionRoles = data.mention_roles ?? [];
|
|
278
540
|
this.nonce = data.nonce ?? null;
|
|
279
541
|
}
|
|
280
|
-
/**
|
|
281
|
-
* Send a message to this channel without replying. Use when you want a standalone message.
|
|
282
|
-
* @param options - Text content or object with content, embeds, and/or files
|
|
283
|
-
* @example
|
|
284
|
-
* await message.send('Pong!');
|
|
285
|
-
* await message.send({ embeds: [embed] }); // EmbedBuilder auto-converted
|
|
286
|
-
* await message.send({ content: 'File', files: [{ name: 'data.txt', data }] });
|
|
287
|
-
*/
|
|
542
|
+
/** Send a message in this channel without replying. */
|
|
288
543
|
async send(options) {
|
|
289
|
-
|
|
290
|
-
return this._send(payload);
|
|
544
|
+
return sendMessage(this, options);
|
|
291
545
|
}
|
|
292
|
-
/**
|
|
293
|
-
* Send a message to a specific channel. Use for logging, forwarding, or sending to another channel in the guild.
|
|
294
|
-
* @param channelId - Snowflake of the target channel (e.g. log channel ID)
|
|
295
|
-
* @param options - Text content or object with content and/or embeds
|
|
296
|
-
* @example
|
|
297
|
-
* await message.sendTo(logChannelId, 'User ' + message.author.username + ' said: ' + message.content);
|
|
298
|
-
* await message.sendTo(announceChannelId, { embeds: [embed] });
|
|
299
|
-
*/
|
|
546
|
+
/** Send a message to another channel by ID. */
|
|
300
547
|
async sendTo(channelId, options) {
|
|
301
|
-
return this
|
|
548
|
+
return sendMessageTo(this, channelId, options);
|
|
302
549
|
}
|
|
303
550
|
/**
|
|
304
|
-
* Reply to this message
|
|
305
|
-
* @param options - Text content or object with content, embeds, and/or reply options (ping, replyTo)
|
|
551
|
+
* Reply to this message.
|
|
306
552
|
* @example
|
|
307
553
|
* await message.reply('Pong!');
|
|
308
|
-
* await message.reply({ embeds: [embed] });
|
|
309
554
|
* await message.reply('No ping!', { ping: false });
|
|
310
|
-
* await message.reply({ content: '
|
|
555
|
+
* await message.reply({ content: 'Silent', allowedMentions: AllowedMentions.suppressReplyPing });
|
|
311
556
|
*/
|
|
312
557
|
async reply(options, replyOptions) {
|
|
313
|
-
|
|
314
|
-
const mergedReply = replyOptions ?? (opts.ping !== void 0 || opts.replyTo !== void 0) ? { ping: opts.ping, replyTo: opts.replyTo } : void 0;
|
|
315
|
-
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);
|
|
326
|
-
return this._send(payload);
|
|
558
|
+
return replyToMessage(this, options, replyOptions);
|
|
327
559
|
}
|
|
328
|
-
/**
|
|
560
|
+
/** Test helper — prefer {@link reply} / `prepareMessagePostPayload()`. */
|
|
329
561
|
static async _createMessageBody(content, referenced_message, ping) {
|
|
330
|
-
|
|
331
|
-
if (content.length === 0) {
|
|
332
|
-
throw new RangeError("Cannot send an empty message");
|
|
333
|
-
}
|
|
334
|
-
content = { content };
|
|
335
|
-
}
|
|
336
|
-
const base = buildSendBody(content);
|
|
337
|
-
const files = content.files?.length ? await resolveMessageFiles(content.files) : void 0;
|
|
338
|
-
const body = { ...base };
|
|
339
|
-
if (referenced_message) {
|
|
340
|
-
body.message_reference = referenced_message;
|
|
341
|
-
if (ping === false) {
|
|
342
|
-
body.flags = (body.flags ?? 0) | MessageFlags.SuppressNotifications;
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
return { files, body };
|
|
562
|
+
return createMessageBody(content, referenced_message, ping);
|
|
346
563
|
}
|
|
564
|
+
/** Send a prepared payload (internal helper). */
|
|
347
565
|
async _send(payload) {
|
|
348
|
-
|
|
349
|
-
Routes.channelMessages(this.channelId),
|
|
350
|
-
payload
|
|
351
|
-
);
|
|
352
|
-
this.client._addMessageToCache(this.channelId, data);
|
|
353
|
-
return new _Message(this.client, data);
|
|
566
|
+
return sendPrepared(this, payload);
|
|
354
567
|
}
|
|
355
|
-
/**
|
|
356
|
-
* Edit this message. Only the author (or admins) can edit.
|
|
357
|
-
* @param options - New content and/or embeds
|
|
358
|
-
*/
|
|
568
|
+
/** Edit this message (requires author or admin permissions). */
|
|
359
569
|
async edit(options) {
|
|
360
570
|
const body = {};
|
|
361
571
|
if (options.content !== void 0) body.content = options.content;
|
|
362
572
|
if (options.embeds?.length) {
|
|
363
|
-
body.embeds = options.embeds.map(
|
|
573
|
+
body.embeds = options.embeds.map(
|
|
574
|
+
(e) => e instanceof EmbedBuilder2 ? e.toJSON() : e
|
|
575
|
+
);
|
|
364
576
|
}
|
|
365
|
-
|
|
577
|
+
if (options.allowedMentions) {
|
|
578
|
+
body.allowed_mentions = toAPIAllowedMentions(options.allowedMentions);
|
|
579
|
+
}
|
|
580
|
+
if (options.flags !== void 0) body.flags = options.flags;
|
|
581
|
+
if (options.attachments !== void 0) {
|
|
582
|
+
body.attachments = toMessageAttachmentEditWire(options.attachments);
|
|
583
|
+
}
|
|
584
|
+
const data = await this.client.rest.patch(Routes3.channelMessage(this.channelId, this.id), {
|
|
366
585
|
body
|
|
367
586
|
});
|
|
368
|
-
|
|
587
|
+
const updated = new _Message(this.client, data);
|
|
588
|
+
this.client._addMessageToCache(this.channelId, data);
|
|
589
|
+
return updated;
|
|
369
590
|
}
|
|
370
|
-
/**
|
|
371
|
-
* Create a reaction collector for this message.
|
|
372
|
-
* Collects reactions matching the filter until time expires or max is reached.
|
|
373
|
-
* @param options - Filter, time (ms), and max count
|
|
374
|
-
* @example
|
|
375
|
-
* const collector = message.createReactionCollector({ filter: (r, u) => u.id === userId, time: 10000 });
|
|
376
|
-
* collector.on('collect', (reaction, user) => console.log(user.username, 'reacted with', reaction.emoji.name));
|
|
377
|
-
* collector.on('end', (collected, reason) => { ... });
|
|
378
|
-
*/
|
|
591
|
+
/** Create a {@link ReactionCollector} for this message. */
|
|
379
592
|
createReactionCollector(options) {
|
|
380
593
|
return new ReactionCollector(this.client, this.id, this.channelId, options);
|
|
381
594
|
}
|
|
382
|
-
/**
|
|
383
|
-
* Re-fetch this message from the API to get the latest content, embeds, reactions, etc.
|
|
384
|
-
* Use when you have a stale Message (e.g. from an old event or cache) and need fresh data.
|
|
385
|
-
* @returns The updated message
|
|
386
|
-
* @throws FluxerError with MESSAGE_NOT_FOUND if the message was deleted or does not exist
|
|
387
|
-
* @example
|
|
388
|
-
* const updated = await message.fetch();
|
|
389
|
-
* console.log('Latest content:', updated.content);
|
|
390
|
-
*/
|
|
595
|
+
/** Fetch the latest version of this message from the API. */
|
|
391
596
|
async fetch() {
|
|
392
597
|
return this.client.channels.fetchMessage(this.channelId, this.id);
|
|
393
598
|
}
|
|
394
599
|
/** Delete this message. */
|
|
395
600
|
async delete() {
|
|
396
|
-
await this.client.rest.delete(
|
|
601
|
+
await this.client.rest.delete(Routes3.channelMessage(this.channelId, this.id));
|
|
602
|
+
this.client._removeMessageFromCache(this.channelId, this.id);
|
|
397
603
|
}
|
|
398
|
-
/**
|
|
399
|
-
* Delete a specific attachment from this message.
|
|
400
|
-
* DELETE /channels/{id}/messages/{id}/attachments/{attachmentId}.
|
|
401
|
-
*/
|
|
604
|
+
/** Delete a specific attachment from this message. */
|
|
402
605
|
async deleteAttachment(attachmentId) {
|
|
403
606
|
await this.client.rest.delete(
|
|
404
|
-
|
|
607
|
+
Routes3.channelMessageAttachment(this.channelId, this.id, attachmentId),
|
|
405
608
|
{ auth: true }
|
|
406
609
|
);
|
|
407
610
|
this.attachments.delete(attachmentId);
|
|
408
611
|
}
|
|
409
|
-
/** Pin this message
|
|
612
|
+
/** Pin this message in the channel. */
|
|
410
613
|
async pin() {
|
|
411
|
-
await this.client.rest.put(
|
|
614
|
+
await this.client.rest.put(Routes3.channelPinMessage(this.channelId, this.id));
|
|
412
615
|
this.pinned = true;
|
|
413
616
|
}
|
|
414
|
-
/** Unpin this message from the channel.
|
|
617
|
+
/** Unpin this message from the channel. */
|
|
415
618
|
async unpin() {
|
|
416
|
-
await this.client.rest.delete(
|
|
619
|
+
await this.client.rest.delete(Routes3.channelPinMessage(this.channelId, this.id));
|
|
417
620
|
this.pinned = false;
|
|
418
621
|
}
|
|
419
|
-
/**
|
|
420
|
-
* Format emoji for reaction API: unicode string or "name:id" for custom.
|
|
421
|
-
* For string resolution (e.g. :name:), use client.resolveEmoji; Message methods resolve automatically when guildId is available.
|
|
422
|
-
*/
|
|
423
|
-
static formatEmoji(emoji) {
|
|
424
|
-
if (typeof emoji === "string") return emoji;
|
|
425
|
-
return `${emoji.name}:${emoji.id}`;
|
|
426
|
-
}
|
|
427
|
-
resolveEmojiForReaction(emoji) {
|
|
428
|
-
return this.client.resolveEmoji(emoji, this.guildId);
|
|
429
|
-
}
|
|
430
|
-
/**
|
|
431
|
-
* Add a reaction to this message (as the bot).
|
|
432
|
-
* @param emoji - Unicode emoji, custom `{ name, id }`, `:name:`, `name:id`, or `<:name:id>`
|
|
433
|
-
*/
|
|
622
|
+
/** React to this message with an emoji. */
|
|
434
623
|
async react(emoji) {
|
|
435
|
-
|
|
436
|
-
const route = `${Routes.channelMessageReaction(this.channelId, this.id, emojiStr)}/@me`;
|
|
437
|
-
await this.client.rest.put(route);
|
|
624
|
+
return reactToMessage(this, emoji);
|
|
438
625
|
}
|
|
439
|
-
/**
|
|
440
|
-
* Remove the bot's reaction, or a specific user's reaction if userId is provided.
|
|
441
|
-
* @param emoji - Unicode emoji, custom `{ name, id }`, `:name:`, `name:id`, or `<:name:id>`
|
|
442
|
-
* @param userId - If provided, removes that user's reaction (requires moderator permissions)
|
|
443
|
-
*/
|
|
626
|
+
/** Remove a reaction (bot's own or a user's if ID is provided). */
|
|
444
627
|
async removeReaction(emoji, userId) {
|
|
445
|
-
|
|
446
|
-
const route = `${Routes.channelMessageReaction(this.channelId, this.id, emojiStr)}/${userId ?? "@me"}`;
|
|
447
|
-
await this.client.rest.delete(route);
|
|
628
|
+
return removeMessageReaction(this, emoji, userId);
|
|
448
629
|
}
|
|
449
|
-
/**
|
|
450
|
-
* Remove all reactions from this message.
|
|
451
|
-
* Requires moderator permissions.
|
|
452
|
-
*/
|
|
630
|
+
/** Remove all reactions from this message. */
|
|
453
631
|
async removeAllReactions() {
|
|
454
|
-
|
|
632
|
+
return removeAllMessageReactions(this);
|
|
455
633
|
}
|
|
456
|
-
/**
|
|
457
|
-
* Remove all reactions of a specific emoji from this message.
|
|
458
|
-
* @param emoji - Unicode emoji, custom `{ name, id }`, `:name:`, `name:id`, or `<:name:id>`. Requires moderator permissions.
|
|
459
|
-
*/
|
|
634
|
+
/** Remove all reactions of a specific emoji. */
|
|
460
635
|
async removeReactionEmoji(emoji) {
|
|
461
|
-
|
|
462
|
-
await this.client.rest.delete(Routes.channelMessageReaction(this.channelId, this.id, emojiStr));
|
|
636
|
+
return removeMessageReactionEmoji(this, emoji);
|
|
463
637
|
}
|
|
464
|
-
/**
|
|
465
|
-
* Fetch users who reacted with the given emoji.
|
|
466
|
-
* @param emoji - Unicode emoji or custom `{ name, id }`
|
|
467
|
-
* @param options - limit (1–100), after (user ID for pagination)
|
|
468
|
-
* @returns Array of User objects
|
|
469
|
-
*/
|
|
638
|
+
/** Fetch users who reacted with a specific emoji. */
|
|
470
639
|
async fetchReactionUsers(emoji, options) {
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
const route = Routes.channelMessageReaction(this.channelId, this.id, emojiStr) + (qs ? `?${qs}` : "");
|
|
477
|
-
const data = await this.client.rest.get(route);
|
|
478
|
-
const list = Array.isArray(data) ? data : data?.users ?? [];
|
|
479
|
-
return list.map((u) => this.client.getOrCreateUser(u));
|
|
640
|
+
return fetchMessageReactionUsers(this, emoji, options);
|
|
641
|
+
}
|
|
642
|
+
/** Fetch reaction users with pagination metadata. */
|
|
643
|
+
async fetchReactionUsersPage(emoji, options) {
|
|
644
|
+
return fetchMessageReactionUsersPage(this, emoji, options);
|
|
480
645
|
}
|
|
481
646
|
};
|
|
482
647
|
export {
|