discord-self-lite 0.1.13 → 0.1.15
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/package.json +1 -1
- package/src/classes/Channel.js +4 -2
- package/src/classes/Client.js +4 -2
- package/src/classes/MessageAttachment.js +18 -0
- package/src/connection/rest/RestManager.js +10 -5
- package/src/connection/rest/methods/react.js +6 -2
- package/src/connection/rest/methods/sendMessage.js +69 -1
- package/src/connection/webhook/WebhookClient.js +73 -4
- package/src/index.js +8 -0
package/package.json
CHANGED
package/src/classes/Channel.js
CHANGED
|
@@ -74,10 +74,12 @@ class Channel {
|
|
|
74
74
|
* @param {number} [options.limit] - Number of messages to fetch
|
|
75
75
|
* @param {string} [options.before] - Fetch messages before this message ID
|
|
76
76
|
* @param {string} [options.after] - Fetch messages after this message ID
|
|
77
|
-
* @returns {Promise<
|
|
77
|
+
* @returns {Promise<Message[]>} Array of message instances
|
|
78
78
|
*/
|
|
79
79
|
async fetchMessages(options = {}) {
|
|
80
|
-
|
|
80
|
+
const data = await this.rest.fetchMessages(this.id, options);
|
|
81
|
+
if (!Array.isArray(data)) return data;
|
|
82
|
+
return data.map((msg) => new Message(this.client, msg));
|
|
81
83
|
}
|
|
82
84
|
|
|
83
85
|
/**
|
package/src/classes/Client.js
CHANGED
|
@@ -248,10 +248,12 @@ class Client extends EventEmitter {
|
|
|
248
248
|
* @param {number} [options.limit] - Number of messages to fetch
|
|
249
249
|
* @param {string} [options.before] - Fetch messages before this message ID
|
|
250
250
|
* @param {string} [options.after] - Fetch messages after this message ID
|
|
251
|
-
* @returns {Promise<
|
|
251
|
+
* @returns {Promise<Message[]>} Array of message instances
|
|
252
252
|
*/
|
|
253
253
|
async fetchMessages(channelId, options = {}) {
|
|
254
|
-
|
|
254
|
+
const data = await this.rest.fetchMessages(channelId, options);
|
|
255
|
+
if (!Array.isArray(data)) return data;
|
|
256
|
+
return data.map((msg) => new Message(this, msg));
|
|
255
257
|
}
|
|
256
258
|
|
|
257
259
|
/**
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents an attachment in a message
|
|
3
|
+
*/
|
|
4
|
+
class MessageAttachment {
|
|
5
|
+
/**
|
|
6
|
+
* Create a new MessageAttachment instance
|
|
7
|
+
* @param {Buffer|string|ArrayBuffer} data - File data buffer or string
|
|
8
|
+
* @param {string} name - Filename
|
|
9
|
+
* @param {string|null} [description=null] - Description of the attachment
|
|
10
|
+
*/
|
|
11
|
+
constructor(data, name, description = null) {
|
|
12
|
+
this.data = data;
|
|
13
|
+
this.name = name;
|
|
14
|
+
this.description = description;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = MessageAttachment;
|
|
@@ -231,13 +231,18 @@ class RestManager {
|
|
|
231
231
|
|
|
232
232
|
while (attempt < maxRetries) {
|
|
233
233
|
try {
|
|
234
|
+
const headers = {
|
|
235
|
+
Authorization: this.token,
|
|
236
|
+
...options.headers,
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
if (!(options.body instanceof FormData)) {
|
|
240
|
+
headers["Content-Type"] = "application/json";
|
|
241
|
+
}
|
|
242
|
+
|
|
234
243
|
const response = await fetch(url, {
|
|
235
244
|
...options,
|
|
236
|
-
headers
|
|
237
|
-
Authorization: this.token,
|
|
238
|
-
"Content-Type": "application/json",
|
|
239
|
-
...options.headers,
|
|
240
|
-
},
|
|
245
|
+
headers,
|
|
241
246
|
});
|
|
242
247
|
|
|
243
248
|
// Update rate limit info from headers
|
|
@@ -3,12 +3,16 @@
|
|
|
3
3
|
* @param {RestManager} rest - The REST manager instance
|
|
4
4
|
* @param {string} channelId - The channel ID
|
|
5
5
|
* @param {string} messageId - The message ID
|
|
6
|
-
* @param {string} emoji - The emoji to react with (unicode or
|
|
6
|
+
* @param {string|object} emoji - The emoji to react with (unicode, custom emoji string, or emoji object)
|
|
7
7
|
* @returns {Promise<void>}
|
|
8
8
|
*/
|
|
9
9
|
async function react(rest, channelId, messageId, emoji) {
|
|
10
|
+
let emojiStr = emoji;
|
|
11
|
+
if (emoji && typeof emoji === "object") {
|
|
12
|
+
emojiStr = emoji.id ? `${emoji.name}:${emoji.id}` : emoji.name;
|
|
13
|
+
}
|
|
10
14
|
// Encode emoji for URL (e.g., 😀 -> %F0%9F%98%80, or custom:123 -> custom%3A123)
|
|
11
|
-
const encodedEmoji = encodeURIComponent(
|
|
15
|
+
const encodedEmoji = encodeURIComponent(emojiStr);
|
|
12
16
|
return await rest.request(
|
|
13
17
|
`/channels/${channelId}/messages/${messageId}/reactions/${encodedEmoji}/@me`,
|
|
14
18
|
{
|
|
@@ -8,7 +8,75 @@
|
|
|
8
8
|
async function sendMessage(rest, channelId, payload) {
|
|
9
9
|
// If payload is a string, treat it as content
|
|
10
10
|
const messagePayload =
|
|
11
|
-
typeof payload === "string" ? { content: payload } : payload;
|
|
11
|
+
typeof payload === "string" ? { content: payload } : { ...payload };
|
|
12
|
+
|
|
13
|
+
const filesToUpload = [];
|
|
14
|
+
const keepAttachments = [];
|
|
15
|
+
|
|
16
|
+
const rawAttachments = messagePayload.attachments || [];
|
|
17
|
+
if (Array.isArray(rawAttachments)) {
|
|
18
|
+
for (const att of rawAttachments) {
|
|
19
|
+
if (
|
|
20
|
+
att &&
|
|
21
|
+
typeof att === "object" &&
|
|
22
|
+
(att.data !== undefined || att.attachment !== undefined)
|
|
23
|
+
) {
|
|
24
|
+
filesToUpload.push(att);
|
|
25
|
+
} else {
|
|
26
|
+
keepAttachments.push(att);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const rawFiles = messagePayload.files || [];
|
|
32
|
+
if (Array.isArray(rawFiles)) {
|
|
33
|
+
for (const file of rawFiles) {
|
|
34
|
+
if (
|
|
35
|
+
file &&
|
|
36
|
+
typeof file === "object" &&
|
|
37
|
+
(file.data !== undefined || file.attachment !== undefined)
|
|
38
|
+
) {
|
|
39
|
+
filesToUpload.push(file);
|
|
40
|
+
} else if (file) {
|
|
41
|
+
filesToUpload.push({ data: file });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (filesToUpload.length > 0) {
|
|
47
|
+
const formData = new FormData();
|
|
48
|
+
|
|
49
|
+
const uploadedAttachmentsMetadata = filesToUpload.map((file, index) => {
|
|
50
|
+
const filename = file.name || `file_${index}`;
|
|
51
|
+
const description = file.description || null;
|
|
52
|
+
return {
|
|
53
|
+
id: index,
|
|
54
|
+
filename,
|
|
55
|
+
description,
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
messagePayload.attachments = [
|
|
60
|
+
...keepAttachments,
|
|
61
|
+
...uploadedAttachmentsMetadata,
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
delete messagePayload.files;
|
|
65
|
+
|
|
66
|
+
formData.append("payload_json", JSON.stringify(messagePayload));
|
|
67
|
+
|
|
68
|
+
filesToUpload.forEach((file, index) => {
|
|
69
|
+
const fileData = file.data || file.attachment;
|
|
70
|
+
const filename = file.name || `file_${index}`;
|
|
71
|
+
const blob = new Blob([fileData]);
|
|
72
|
+
formData.append(`files[${index}]`, blob, filename);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return await rest.request(`/channels/${channelId}/messages`, {
|
|
76
|
+
method: "POST",
|
|
77
|
+
body: formData,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
12
80
|
|
|
13
81
|
return await rest.request(`/channels/${channelId}/messages`, {
|
|
14
82
|
method: "POST",
|
|
@@ -130,12 +130,81 @@ class WebhookClient {
|
|
|
130
130
|
payload = this._buildPayload("", options);
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
+
const filesToUpload = [];
|
|
134
|
+
const keepAttachments = [];
|
|
135
|
+
|
|
136
|
+
const rawAttachments = payload.attachments || [];
|
|
137
|
+
if (Array.isArray(rawAttachments)) {
|
|
138
|
+
for (const att of rawAttachments) {
|
|
139
|
+
if (
|
|
140
|
+
att &&
|
|
141
|
+
typeof att === "object" &&
|
|
142
|
+
(att.data !== undefined || att.attachment !== undefined)
|
|
143
|
+
) {
|
|
144
|
+
filesToUpload.push(att);
|
|
145
|
+
} else {
|
|
146
|
+
keepAttachments.push(att);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const rawFiles = payload.files || [];
|
|
152
|
+
if (Array.isArray(rawFiles)) {
|
|
153
|
+
for (const file of rawFiles) {
|
|
154
|
+
if (
|
|
155
|
+
file &&
|
|
156
|
+
typeof file === "object" &&
|
|
157
|
+
(file.data !== undefined || file.attachment !== undefined)
|
|
158
|
+
) {
|
|
159
|
+
filesToUpload.push(file);
|
|
160
|
+
} else if (file) {
|
|
161
|
+
filesToUpload.push({ data: file });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
let requestBody;
|
|
167
|
+
const headers = {};
|
|
168
|
+
|
|
169
|
+
if (filesToUpload.length > 0) {
|
|
170
|
+
const formData = new FormData();
|
|
171
|
+
|
|
172
|
+
const uploadedAttachmentsMetadata = filesToUpload.map((file, index) => {
|
|
173
|
+
const filename = file.name || `file_${index}`;
|
|
174
|
+
const description = file.description || null;
|
|
175
|
+
return {
|
|
176
|
+
id: index,
|
|
177
|
+
filename,
|
|
178
|
+
description,
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
payload.attachments = [
|
|
183
|
+
...keepAttachments,
|
|
184
|
+
...uploadedAttachmentsMetadata,
|
|
185
|
+
];
|
|
186
|
+
|
|
187
|
+
delete payload.files;
|
|
188
|
+
|
|
189
|
+
formData.append("payload_json", JSON.stringify(payload));
|
|
190
|
+
|
|
191
|
+
filesToUpload.forEach((file, index) => {
|
|
192
|
+
const fileData = file.data || file.attachment;
|
|
193
|
+
const filename = file.name || `file_${index}`;
|
|
194
|
+
const blob = new Blob([fileData]);
|
|
195
|
+
formData.append(`files[${index}]`, blob, filename);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
requestBody = formData;
|
|
199
|
+
} else {
|
|
200
|
+
headers["Content-Type"] = "application/json";
|
|
201
|
+
requestBody = JSON.stringify(payload);
|
|
202
|
+
}
|
|
203
|
+
|
|
133
204
|
const response = await fetch(this.url, {
|
|
134
205
|
method: "POST",
|
|
135
|
-
headers
|
|
136
|
-
|
|
137
|
-
},
|
|
138
|
-
body: JSON.stringify(payload),
|
|
206
|
+
headers,
|
|
207
|
+
body: requestBody,
|
|
139
208
|
});
|
|
140
209
|
|
|
141
210
|
if (!response.ok) {
|
package/src/index.js
CHANGED
|
@@ -21,6 +21,7 @@ const Client = require("./classes/Client");
|
|
|
21
21
|
const WebhookClient = require("./connection/webhook/WebhookClient");
|
|
22
22
|
const Permissions = require("./classes/Permissions");
|
|
23
23
|
const BitField = require("./classes/BitField");
|
|
24
|
+
const MessageAttachment = require("./classes/MessageAttachment");
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
27
|
* Main package exports
|
|
@@ -54,4 +55,11 @@ module.exports = {
|
|
|
54
55
|
* @see {@link BitField}
|
|
55
56
|
*/
|
|
56
57
|
BitField,
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Represents an attachment to be sent in a message
|
|
61
|
+
* @type {MessageAttachment}
|
|
62
|
+
* @see {@link MessageAttachment}
|
|
63
|
+
*/
|
|
64
|
+
MessageAttachment,
|
|
57
65
|
};
|