Skip to content

Commit

Permalink
modifications to embed builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Starman3787 committed Aug 1, 2024
1 parent c741b40 commit 4be23bc
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 11 deletions.
72 changes: 69 additions & 3 deletions src/util/builder/embedBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,31 @@ class Embed {
/**
* Creates an embed structure.
*/
constructor() {
this.type = "rich";
constructor(data) {
if (data) {
if (data.title) this.title = data.title;
if (data.description) this.description = data.description;
if (data.url) this.url = data.url;
if (data.timestamp) this.timestamp = data.timestamp;
if (data.color) this.color = data.color;
if (data.footer) this.footer = data.footer;
if (data.author) this.author = data.author;
if (data.fields) this.fields = data.fields;
if (data.image) this.image = data.image;
if (data.thumbnail) this.thumbnail = data.thumbnail;
if (data.video) this.video = data.video;
}

this.fields = [];
}

/**
* Sets the title of the embed.
* @param {String} title The title of the embed.
* @returns {Embed}
* @throws {TypeError}
* @method
* @public
*/
setTitle(title) {
if (!title) throw new TypeError("GLUON: Embed title must be provided.");
Expand All @@ -34,6 +50,9 @@ class Embed {
* Sets the embed description.
* @param {String} text The description.
* @returns {Embed}
* @throws {TypeError}
* @method
* @public
*/
setDescription(text) {
if (!text)
Expand All @@ -51,6 +70,9 @@ class Embed {
* Sets the url of the embed.
* @param {String} url The url.
* @returns {Embed}
* @throws {TypeError}
* @method
* @public
*/
setURL(url) {
if (!url) throw new TypeError("GLUON: Embed url must be provided.");
Expand All @@ -64,6 +86,8 @@ class Embed {
* Sets the timestamp displayed on the embed.
* @param {Number?} timestamp The UNIX timestamp.
* @returns {Embed}
* @method
* @public
*/
setTimestamp(timestamp) {
if (timestamp) this.timestamp = new Date(timestamp * 1000).toISOString();
Expand All @@ -76,6 +100,9 @@ class Embed {
* Sets the color of the embed.
* @param {String | Number} color The color.
* @returns {Embed}
* @throws {TypeError}
* @method
* @public
*/
setColor(color) {
if (!color) throw new TypeError("GLUON: Embed color must be provided.");
Expand All @@ -93,6 +120,9 @@ class Embed {
* Sets the embed thumbnail image.
* @param {String} url The url of the thumbnail.
* @returns {Embed}
* @throws {TypeError}
* @method
* @public
*/
setThumbnail(url) {
if (!url)
Expand All @@ -110,6 +140,9 @@ class Embed {
* @param {String} text The footer text.
* @param {String?} icon The url of the footer icon.
* @returns {Embed}
* @throws {TypeError}
* @method
* @public
*/
setFooter(text, icon) {
if (!text)
Expand All @@ -132,6 +165,9 @@ class Embed {
* @param {String?} url The url.
* @param {String?} icon_url The embed author image url.
* @returns {Embed}
* @throws {TypeError}
* @method
* @public
*/
setAuthor(name, url, icon_url) {
if (!name)
Expand All @@ -156,6 +192,9 @@ class Embed {
* @param {String} value Sets the embed field value.
* @param {Boolean?} inline Whether this field should be displayed inline.
* @returns {Embed}
* @throws {RangeError | TypeError}
* @method
* @public
*/
addField(name, value, inline = false) {
if (this.fields.length >= LIMITS.MAX_EMBED_FIELDS)
Expand Down Expand Up @@ -187,18 +226,43 @@ class Embed {
* Sets the embed image url.
* @param {String} url The image url.
* @returns {Embed}
* @method
* @public
*/
setImage(url) {
if (typeof url !== "string")
throw new TypeError("GLUON: Embed image url must be a string.");

this.image = {
url,
};

return this;
}

/**
* Sets the embed video url.
* @param {String} url The video url.
* @returns {Embed}
* @method
* @public
*/
setVideo(url) {
if (typeof url !== "string")
throw new TypeError("GLUON: Embed video url must be a string.");

this.video = {
url,
};

return this;
}

/**
* Converts the embed into string form.
* @returns {String}
* @method
* @public
*/
toString() {
let string = "";
Expand All @@ -221,11 +285,12 @@ class Embed {
/**
* Returns the correct Discord format for an embed.
* @returns {Object}
* @method
* @public
*/
toJSON() {
return {
title: this.title,
type: this.type,
description: this.description,
url: this.url,
timestamp: this.timestamp,
Expand All @@ -235,6 +300,7 @@ class Embed {
fields: this.fields,
image: this.image,
thumbnail: this.thumbnail,
video: this.video,
};
}

Expand Down
36 changes: 28 additions & 8 deletions test/util/builder/embedBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ describe("Embed", function () {
});
});

context("check type", function () {
it("should have the correct type", function () {
const embed = new Embed();
expect(embed.type).to.equal("rich");
});
});

context("check setTitle", function () {
it("should have method setTitle", function () {
const embed = new Embed();
Expand Down Expand Up @@ -146,13 +139,37 @@ describe("Embed", function () {
const embed = new Embed();
expect(embed).to.respondTo("setImage");
});
it("should throw an error if the url is empty", function () {
const embed = new Embed();
expect(() => {
embed.setImage(undefined);
}).to.throw(TypeError, "GLUON: Embed image url must be a string.");
});
it("should set the image of the embed", function () {
const embed = new Embed();
embed.setImage("https://example.com");
expect(embed.image.url).to.equal("https://example.com");
});
});

context("check setVideo", function () {
it("should have method setVideo", function () {
const embed = new Embed();
expect(embed).to.respondTo("setVideo");
});
it("should throw an error if the url is empty", function () {
const embed = new Embed();
expect(() => {
embed.setVideo(undefined);
}).to.throw(TypeError, "GLUON: Embed video url must be a string.");
});
it("should set the video of the embed", function () {
const embed = new Embed();
embed.setVideo("https://example.com");
expect(embed.video.url).to.equal("https://example.com");
});
});

context("check setThumbnail", function () {
it("should have method setThumbnail", function () {
const embed = new Embed();
Expand Down Expand Up @@ -317,11 +334,11 @@ describe("Embed", function () {
embed.setTimestamp(123456);
embed.setFooter("footer");
embed.setImage("https://example.com");
embed.setVideo("https://example.com");
embed.setThumbnail("https://example.com");
embed.setAuthor("author");
embed.addField("field", "fieldValue");
expect(embed.toJSON()).to.deep.equal({
type: "rich",
title: "title",
description: "description",
url: "https://example.com",
Expand All @@ -333,6 +350,9 @@ describe("Embed", function () {
image: {
url: "https://example.com",
},
video: {
url: "https://example.com",
},
thumbnail: {
url: "https://example.com",
},
Expand Down

0 comments on commit 4be23bc

Please sign in to comment.