Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove RichEmbed in favour of MessageEmbed #1584

Merged
merged 3 commits into from
Jul 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ module.exports = {
Presence: require('./structures/Presence').Presence,
ReactionEmoji: require('./structures/ReactionEmoji'),
ReactionCollector: require('./structures/ReactionCollector'),
RichEmbed: require('./structures/RichEmbed'),
Role: require('./structures/Role'),
TextChannel: require('./structures/TextChannel'),
User: require('./structures/User'),
Expand Down
4 changes: 3 additions & 1 deletion src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,9 @@ class Message {

if (typeof content !== 'undefined') content = Util.resolveString(content);

const { embed, code, reply } = options;
let { embed, code, reply } = options;

if (embed) embed = new Embed(embed)._apiTransform();

// Wrap everything in a code block
if (typeof code !== 'undefined' && (typeof code !== 'boolean' || code === true)) {
Expand Down
81 changes: 52 additions & 29 deletions src/structures/MessageEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { RangeError } = require('../errors');
* Represents an embed in a message (image/video preview, rich embed, etc.)
*/
class MessageEmbed {
constructor(data) {
constructor(data = {}) {
this.setup(data);
}

Expand All @@ -20,31 +20,31 @@ class MessageEmbed {
* The title of this embed
* @type {?string}
*/
this.title = data.title || null;
this.title = data.title;

/**
* The description of this embed
* @type {?string}
*/
this.description = data.description || null;
this.description = data.description;

/**
* The URL of this embed
* @type {?string}
*/
this.url = data.url || null;
this.url = data.url;

/**
* The color of the embed
* @type {?number}
*/
this.color = data.color || null;
this.color = data.color;

/**
* The timestamp of this embed
* @type {?number}
*/
this.timestamp = new Date(data.timestamp) || null;
this.timestamp = new Date(data.timestamp);

/**
* The fields of this embed
Expand All @@ -53,7 +53,7 @@ class MessageEmbed {
* @property {string} value The value of this field
* @property {boolean} inline If this field will be displayed inline
*/
this.fields = data.fields || null;
this.fields = data.fields || [];

/**
* The thumbnail of this embed, if there is one
Expand All @@ -64,7 +64,7 @@ class MessageEmbed {
* @property {number} width Width of this thumbnail
*/
this.thumbnail = data.thumbnail ? {
url: data.thumbnail.url || null,
url: data.thumbnail.url,
proxyURL: data.thumbnail.proxy_url,
height: data.height,
width: data.width,
Expand All @@ -79,7 +79,7 @@ class MessageEmbed {
* @property {number} width Width of this image
*/
this.image = data.image ? {
url: data.image.url || null,
url: data.image.url,
proxyURL: data.image.proxy_url,
height: data.height,
width: data.width,
Expand All @@ -92,11 +92,7 @@ class MessageEmbed {
* @property {number} height Height of this video
* @property {number} width Width of this video
*/
this.video = data.video ? {
url: data.video.url || null,
height: data.video.height,
width: data.video.width,
} : null;
this.video = data.video;

/**
* The author of this embed, if there is one
Expand All @@ -107,10 +103,10 @@ class MessageEmbed {
* @property {string} proxyIconURL Proxied URL of the icon for this author
*/
this.author = data.author ? {
name: data.author.name || null,
url: data.author.url || null,
iconURL: data.author.iconURL || data.author.icon_url || null,
proxyIconURL: data.author.proxyIconUrl || data.author.proxy_icon_url || null,
name: data.author.name,
url: data.author.url,
iconURL: data.author.iconURL || data.author.icon_url,
proxyIconURL: data.author.proxyIconUrl || data.author.proxy_icon_url,
} : null;

/**
Expand All @@ -119,10 +115,7 @@ class MessageEmbed {
* @property {string} name The name of this provider
* @property {string} url URL of this provider
*/
this.provider = data.provider ? {
name: data.provider.name,
url: data.provider.url,
} : null;
this.provider = data.provider;

/**
* The footer of this embed
Expand All @@ -132,9 +125,9 @@ class MessageEmbed {
* @property {string} proxyIconURL Proxied URL of the icon for this footer
*/
this.footer = data.footer ? {
text: data.footer.text || null,
iconURL: data.footer.iconURL || data.footer.icon_url || null,
proxyIconURL: data.footer.proxyIconURL || data.footer.proxy_icon_url || null,
text: data.footer.text,
iconURL: data.footer.iconURL || data.footer.icon_url,
proxyIconURL: data.footer.proxyIconURL || data.footer.proxy_icon_url,
} : null;
}

Expand All @@ -144,7 +137,7 @@ class MessageEmbed {
* @readonly
*/
get createdAt() {
return new Date(this.timestamp);
return !isNaN(this.timestamp) ? this.timestamp : null;
}

/**
Expand All @@ -153,9 +146,7 @@ class MessageEmbed {
* @readonly
*/
get hexColor() {
let col = this.color.toString(16);
while (col.length < 6) col = `0${col}`;
return `#${col}`;
return this.color ? `#${this.color.toString(16).padStart(6, '0')}` : null;
}

/**
Expand All @@ -175,6 +166,15 @@ class MessageEmbed {
return this;
}

/**
* Convenience function for `<MessageEmbed>.addField('\u200B', '\u200B', inline)`.
* @param {boolean} [inline=false] Set the field to display inline
* @returns {MessageEmbed} This embed
*/
addBlankField(inline = false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delet

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? Its such a useful small thing! Ain't nobody got time to get the unicode code for the 0-width space

return this.addField('\u200B', '\u200B', inline);
}

/**
* Sets the file to upload alongside the embed. This file can be accessed via `attachment://fileName.extension` when
* setting an embed image or author/footer icons. Only one file may be attached.
Expand Down Expand Up @@ -286,6 +286,29 @@ class MessageEmbed {
return this;
}

_apiTransform() {
return {
title: this.title,
type: 'rich',
description: this.description,
url: this.url,
timestamp: this.timestamp,
color: this.color,
fields: this.fields,
file: this.file,
thumbnail: this.thumbnail,
image: this.image,
author: this.author ? {
name: this.author.name,
url: this.author.url,
icon_url: this.author.iconURL,
} : null,
footer: this.footer ? {
text: this.footer.text,
icon_url: this.footer.iconURL,
} : null,
};
}
}

module.exports = MessageEmbed;
Loading