discord-self-lite 0.1.13 → 0.1.14
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
CHANGED
|
@@ -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
|
|
@@ -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
|
};
|