discord-self-lite 0.1.12 → 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "discord-self-lite",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "description": "Lightweight Discord selfbot library",
5
5
  "keywords": [
6
6
  "discord",
@@ -53,6 +53,8 @@ class Message {
53
53
  this.mentions = this.mentions || [];
54
54
  this.mentionRoles = this.mentionRoles || [];
55
55
  this.reactions = this.reactions || [];
56
+ this.guildId =
57
+ this.guildId || this.client.getChannel(this.channelId)?.guildId || null;
56
58
  this.url = `https://discord.com/channels/${this.guildId ? this.guildId : "@me"}/${this.channelId}/${this.id}`;
57
59
  }
58
60
 
@@ -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",
@@ -22,18 +22,79 @@ function resolveColor(color) {
22
22
  class WebhookClient {
23
23
  /**
24
24
  * Create a new WebhookClient
25
- * @param {string} url - The webhook URL
26
- * @param {object} [options={}] - Default options for the webhook
27
- * @param {string} [options.username] - Default username for the webhook
28
- * @param {string} [options.avatarURL] - Default avatar URL for the webhook
25
+ * @param {string|object} urlOrId - The webhook URL, an object containing `id` and `token` (or `url`), or just the webhook ID
26
+ * @param {string|object} [tokenOrOptions={}] - The webhook token (if first arg is ID) or default options
27
+ * @param {object} [options={}] - Default options for the webhook (if first two args are ID and token)
29
28
  */
30
- constructor(url, options = {}) {
31
- this.url = url;
32
- this.options = {
33
- username: options.username || null,
34
- avatarURL: options.avatarURL || null,
35
- ...options,
36
- };
29
+ constructor(urlOrId, tokenOrOptions = {}, options = {}) {
30
+ if (typeof urlOrId === "object" && urlOrId !== null) {
31
+ // constructor({ id, token, url, ...options }, options)
32
+ const data = urlOrId;
33
+ const opts = tokenOrOptions || {};
34
+
35
+ if (data.url) {
36
+ this.url = data.url;
37
+ try {
38
+ const parsed = WebhookClient.parseURL(data.url);
39
+ this.id = parsed.id;
40
+ this.token = parsed.token;
41
+ } catch {
42
+ // Ignore parse errors for custom/mock URLs
43
+ }
44
+ } else if (data.id && data.token) {
45
+ this.id = data.id.toString();
46
+ this.token = data.token;
47
+ this.url = `https://discord.com/api/webhooks/${this.id}/${this.token}`;
48
+ } else {
49
+ throw new Error(
50
+ "WebhookClient requires either a URL or an ID and Token",
51
+ );
52
+ }
53
+
54
+ this.options = {
55
+ username: data.username || opts.username || null,
56
+ avatarURL: data.avatarURL || opts.avatarURL || null,
57
+ ...data,
58
+ ...opts,
59
+ };
60
+
61
+ // Clean up internal keys from options
62
+ delete this.options.id;
63
+ delete this.options.token;
64
+ delete this.options.url;
65
+ } else if (
66
+ typeof urlOrId === "string" &&
67
+ typeof tokenOrOptions === "string"
68
+ ) {
69
+ // constructor(id, token, options)
70
+ this.id = urlOrId;
71
+ this.token = tokenOrOptions;
72
+ this.url = `https://discord.com/api/webhooks/${this.id}/${this.token}`;
73
+ this.options = {
74
+ username: options.username || null,
75
+ avatarURL: options.avatarURL || null,
76
+ ...options,
77
+ };
78
+ } else if (typeof urlOrId === "string") {
79
+ // constructor(url, options)
80
+ this.url = urlOrId;
81
+ try {
82
+ const parsed = WebhookClient.parseURL(urlOrId);
83
+ this.id = parsed.id;
84
+ this.token = parsed.token;
85
+ } catch {
86
+ // Ignore parse errors for custom/mock URLs
87
+ }
88
+ this.options = {
89
+ username: tokenOrOptions.username || null,
90
+ avatarURL: tokenOrOptions.avatarURL || null,
91
+ ...tokenOrOptions,
92
+ };
93
+ } else {
94
+ throw new Error(
95
+ "Invalid parameters provided to WebhookClient constructor",
96
+ );
97
+ }
37
98
  }
38
99
 
39
100
  /**
@@ -69,12 +130,81 @@ class WebhookClient {
69
130
  payload = this._buildPayload("", options);
70
131
  }
71
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
+
72
204
  const response = await fetch(this.url, {
73
205
  method: "POST",
74
- headers: {
75
- "Content-Type": "application/json",
76
- },
77
- body: JSON.stringify(payload),
206
+ headers,
207
+ body: requestBody,
78
208
  });
79
209
 
80
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
  };