2020-11-10 03:31:49 +00:00
|
|
|
const colours = require('../assets/colours.json');
|
2020-10-28 02:03:09 +00:00
|
|
|
const HEX_REGEX = /#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
|
2020-10-20 05:47:05 +00:00
|
|
|
|
2020-10-24 04:02:25 +00:00
|
|
|
class RichEmbed {
|
2020-10-20 05:47:05 +00:00
|
|
|
/**
|
2020-10-24 04:02:25 +00:00
|
|
|
* @param {EmbedData} data
|
2020-10-20 05:47:05 +00:00
|
|
|
*/
|
2020-10-24 04:02:25 +00:00
|
|
|
constructor (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.image) this.image = data.image;
|
|
|
|
if (data.thumbnail) this.thumbnail = data.thumbnail;
|
|
|
|
if (data.author) this.author = data.author;
|
|
|
|
this.fields = data.fields || [];
|
2020-10-20 05:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-24 04:02:25 +00:00
|
|
|
* @param {String} title
|
2020-10-20 05:47:05 +00:00
|
|
|
*/
|
2020-10-24 04:02:25 +00:00
|
|
|
setTitle (title) {
|
|
|
|
if (typeof title !== 'string') throw new TypeError(`Expected type 'string', received type '${typeof title}'`);
|
|
|
|
if (title.length > 256) throw new RangeError('Embed titles cannot exceed 256 characters');
|
|
|
|
this.title = title;
|
2020-10-20 05:47:05 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-24 04:02:25 +00:00
|
|
|
* @param {String} description
|
2020-10-20 05:47:05 +00:00
|
|
|
*/
|
2020-10-24 04:02:25 +00:00
|
|
|
setDescription (description) {
|
|
|
|
if (typeof description !== 'string') throw new TypeError(`Expected type 'string', received type '${typeof description}'`);
|
|
|
|
if (description.length > 2048) throw new RangeError('Embed titles cannot exceed 2048 characters');
|
|
|
|
this.description = description;
|
2020-10-20 05:47:05 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-24 04:02:25 +00:00
|
|
|
* @param {String} url
|
2020-10-20 05:47:05 +00:00
|
|
|
*/
|
2020-10-24 04:02:25 +00:00
|
|
|
setURL (url) {
|
|
|
|
if (typeof url !== 'string') throw new TypeError(`Expected type 'string', received type '${typeof url}'`);
|
|
|
|
this.url = url;
|
2020-10-20 05:47:05 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-24 04:02:25 +00:00
|
|
|
* @param {DateConstructor} [timestamp]
|
2020-10-20 05:47:05 +00:00
|
|
|
*/
|
2020-10-24 04:02:25 +00:00
|
|
|
setTimestamp (timestamp = new Date()) {
|
|
|
|
if (Number.isNaN(new Date(timestamp).getTime())) throw new Error('Invalid Date');
|
|
|
|
this.timestamp = new Date(timestamp);
|
2020-10-20 05:47:05 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-24 04:02:25 +00:00
|
|
|
* @param {String|Number} color
|
2020-10-20 05:47:05 +00:00
|
|
|
*/
|
2020-10-28 04:48:21 +00:00
|
|
|
setColour (color) {
|
|
|
|
const resolvedColour = colours[color.toUpperCase()];
|
|
|
|
if (resolvedColour) color = resolvedColour;
|
2020-10-24 04:02:25 +00:00
|
|
|
|
|
|
|
if (typeof color !== 'string' && typeof color !== 'number') throw new TypeError(`Expected types 'string' or 'number', received type ${typeof color} instead`);
|
|
|
|
if (typeof color === 'number') {
|
|
|
|
if (color > 16777215 || color < 0) throw new RangeError('Invalid color');
|
|
|
|
this.color = color;
|
|
|
|
} else {
|
|
|
|
const match = color.match(HEX_REGEX);
|
|
|
|
if (!match) throw new Error('Invalid color');
|
|
|
|
this.color = parseInt(match[1], 16);
|
|
|
|
}
|
2020-10-20 05:47:05 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-24 04:02:25 +00:00
|
|
|
* @param {String} text
|
|
|
|
* @param {String} [iconURL]
|
2020-10-20 05:47:05 +00:00
|
|
|
*/
|
2020-10-24 04:02:25 +00:00
|
|
|
setFooter (text, iconURL) {
|
|
|
|
if (typeof text !== 'string') throw new TypeError(`Expected type 'string', received type ${typeof text}`);
|
|
|
|
if (text.length > 2048) throw new RangeError('Embed footer texts cannot exceed 2048 characters');
|
|
|
|
this.footer = { text };
|
|
|
|
|
|
|
|
if (iconURL !== undefined) {
|
|
|
|
if (typeof iconURL !== 'string') throw new TypeError(`Expected type 'string', received type '${typeof iconURL}'`);
|
|
|
|
this.footer.icon_url = iconURL;
|
|
|
|
}
|
2020-10-20 05:47:05 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-24 04:02:25 +00:00
|
|
|
* @param {String} imageURL
|
2020-10-20 05:47:05 +00:00
|
|
|
*/
|
2020-10-24 04:02:25 +00:00
|
|
|
setImage (imageURL) {
|
|
|
|
if (typeof imageURL !== 'string') throw new TypeError(`Expected type 'string', received type ${typeof imageURL}`);
|
|
|
|
this.image = { url: imageURL };
|
2020-10-20 05:47:05 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-24 04:02:25 +00:00
|
|
|
* @param {String} thumbnailURL
|
2020-10-20 05:47:05 +00:00
|
|
|
*/
|
2020-10-24 04:02:25 +00:00
|
|
|
setThumbnail (thumbnailURL) {
|
|
|
|
if (typeof thumbnailURL !== 'string') throw new TypeError(`Expected type 'string', received type ${typeof thumbnailURL}`);
|
|
|
|
this.thumbnail = { url: thumbnailURL };
|
2020-10-20 05:47:05 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-24 04:02:25 +00:00
|
|
|
* @param {String} name
|
|
|
|
* @param {String} [url]
|
|
|
|
* @param {String} [iconURL]
|
2020-10-20 05:47:05 +00:00
|
|
|
*/
|
2020-10-24 04:02:25 +00:00
|
|
|
setAuthor (name, url, iconURL) {
|
|
|
|
if (typeof name !== 'string') throw new TypeError(`Expected type 'string', received type ${typeof name}`);
|
|
|
|
if (name.length > 256) throw new RangeError('Embed footer texts cannot exceed 2048 characters');
|
|
|
|
this.author = { name };
|
|
|
|
|
|
|
|
if (url !== undefined) {
|
|
|
|
if (typeof url !== 'string') throw new TypeError(`Expected type 'string', received type '${typeof url}'`);
|
|
|
|
this.author.url = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iconURL !== undefined) {
|
|
|
|
if (typeof iconURL !== 'string') throw new TypeError(`Expected type 'string', received type '${typeof iconURL}'`);
|
|
|
|
this.author.icon_url = iconURL;
|
|
|
|
}
|
2020-10-20 05:47:05 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-24 04:02:25 +00:00
|
|
|
* @param {String} name
|
|
|
|
* @param {String} value
|
|
|
|
* @param {Boolean} [inline]
|
2020-10-20 05:47:05 +00:00
|
|
|
*/
|
2020-10-24 04:02:25 +00:00
|
|
|
addField (name, value, inline = false) {
|
|
|
|
if (this.fields.length >= 25) throw new RangeError('Embeds cannot contain more than 25 fields');
|
2020-10-29 08:20:33 +00:00
|
|
|
if (typeof name !== 'string') throw new TypeError(`Name field expected type 'string', received type ${typeof name}`);
|
|
|
|
if (typeof value !== 'string') throw new TypeError(`Value field expected type 'string', received type ${typeof value}`);
|
|
|
|
if (typeof inline !== 'boolean') throw new TypeError(`Inline field expected type 'boolean', received type ${typeof inline}`);
|
2020-10-24 04:02:25 +00:00
|
|
|
if (name.length > 256) throw new RangeError('Embed field names cannot exceed 256 characters');
|
|
|
|
if (value.length > 1024) throw new RangeError('Embed field names cannot exceed 256 characters');
|
|
|
|
|
|
|
|
this.fields.push({ name, value, inline });
|
2020-10-20 05:47:05 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 04:02:25 +00:00
|
|
|
module.exports = RichEmbed;
|