Skip to content

Commit

Permalink
Merge pull request #30 from 44xo/patch-1
Browse files Browse the repository at this point in the history
Add documentation to Attachment.js && improve availability
  • Loading branch information
9xN authored Apr 30, 2024
2 parents fc7001e + 41b7cca commit 2b439fb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/structures/Attachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,27 @@ const fs = require("fs").promises;
const Jimp = require("jimp");

class Attachment {
/**
* @param {Buffer|string} data The data for the attachment
*/
constructor(data) {
/**
* The data for the attachment
* @type {Buffer|string}
*/
this.data = data;
/**
* The processed file
* @type {Buffer}
*/
this.file = null;
}

/**
* Verifies and processes the attachment data
* @returns {Promise<void>}
* @throws {Error} If the attachment data is empty or unsupported
*/
async _verify() {
if (!this.data) {
throw new Error("Can not create empty attachment!");
Expand All @@ -24,6 +40,12 @@ class Attachment {
}
}

/**
* Handles a file attachment
* @param {string} file The file path
* @returns {Promise<void>}
* @throws {Error} If the file cannot be resolved
*/
async _handleFile(file) {
try {
const fileStream = await fs.readFile(file);
Expand All @@ -37,11 +59,22 @@ class Attachment {
}
}

/**
* Handles an attachment buffer
* @param {Buffer} data The attachment buffer
* @returns {Promise<void>}
*/
async _handleBuffer(data) {
const image = await Jimp.read(data);
this.file = await image.getBufferAsync(Jimp.MIME_JPEG);
}

/**
* Handles an attachment URL
* @param {string} link The URL link
* @returns {Promise<void>}
* @throws {Error} If unable to fetch image from URL
*/
async _handleURL(link) {
try {
const res = await fetch(link);
Expand Down
1 change: 1 addition & 0 deletions src/structures/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ class Client extends EventEmitter {
error: null,
};
} catch (err) {
console.error(err?.message);
return {
success: false,
error: err?.message,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Util {
* @return {boolean}
*/
static isMessageValid(message) {
return message.timestamp / 1000 + 10000 > Date.now();
return ((message.timestamp / 1000 + 10000 > Date.now()) && (message.type)) ? true : false;
}

/**
Expand Down

0 comments on commit 2b439fb

Please sign in to comment.