woomy-v2/bot/util/embed.js

161 lines
6.1 KiB
JavaScript
Raw Normal View History

2020-10-24 04:02:25 +00:00
const colours = require('../constants/colours.json');
const HEX_REGEX = /^#?([a-fA-F0-9]{6})$/;
const URL_REGEX = /^http(s)?:\/\/[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=.]+$/;
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}'`);
if (!URL_REGEX.test(url)) throw new Error('Not a well formed 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-24 04:02:25 +00:00
setColor (color) {
const resolvedColor = colours[color.toUpperCase()];
if (resolvedColor) color = resolvedColor;
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}'`);
if (!iconURL.startsWith('attachment://') && !URL_REGEX.test(iconURL)) throw new Error('Not a well formed URL');
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}`);
if (!thumbnailURL.startsWith('attachment://') && !URL_REGEX.test(thumbnailURL)) throw new Error('Not a well formed URL');
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}'`);
if (!URL_REGEX.test(url)) throw new Error('Not a well formed URL');
this.author.url = url;
}
if (iconURL !== undefined) {
if (typeof iconURL !== 'string') throw new TypeError(`Expected type 'string', received type '${typeof iconURL}'`);
if (!iconURL.startsWith('attachment://') && !URL_REGEX.test(iconURL)) throw new Error('Not a well formed URL');
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');
if (typeof name !== 'string') throw new TypeError(`Expected type 'string', received type ${typeof name}`);
if (typeof value !== 'string') throw new TypeError(`Expected type 'string', received type ${typeof value}`);
if (typeof inline !== 'boolean') throw new TypeError(`Expected type 'boolean', received type ${typeof inline}`);
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;