detritus-client
Version:
A Typescript NodeJS library to interact with Discord's API, both Rest and Gateway.
338 lines (337 loc) • 9.74 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmbedVideo = exports.EmbedThumbnail = exports.EmbedProvider = exports.EmbedImage = exports.EmbedFooter = exports.EmbedField = exports.EmbedAuthor = exports.Embed = void 0;
const basecollection_1 = require("../collections/basecollection");
const baseset_1 = require("../collections/baseset");
const constants_1 = require("../constants");
const basestructure_1 = require("../structures/basestructure");
const keysEmbed = new baseset_1.BaseSet([
constants_1.DiscordKeys.AUTHOR,
constants_1.DiscordKeys.COLOR,
constants_1.DiscordKeys.DESCRIPTION,
constants_1.DiscordKeys.FIELDS,
constants_1.DiscordKeys.FOOTER,
constants_1.DiscordKeys.IMAGE,
constants_1.DiscordKeys.PROVIDER,
constants_1.DiscordKeys.REFERENCE_ID,
constants_1.DiscordKeys.THUMBNAIL,
constants_1.DiscordKeys.TIMESTAMP,
constants_1.DiscordKeys.TITLE,
constants_1.DiscordKeys.TYPE,
constants_1.DiscordKeys.URL,
constants_1.DiscordKeys.VIDEO,
]);
/**
* Utils Embed Structure
* @category Utils
*/
class Embed extends basestructure_1.Structure {
constructor(data = {}) {
super();
this._keys = keysEmbed;
this.type = constants_1.MessageEmbedTypes.RICH;
this.merge(data);
}
get length() {
return this.size;
}
get size() {
let size = 0;
if (this.author) {
size += (this.author.name || '').length;
}
if (this.title) {
size += (this.title || '').length;
}
if (this.description) {
size += (this.description || '').length;
}
if (this.fields) {
size += this.fields.reduce((s, field) => s + (field.name || '').length + (field.value || '').length, 0);
}
if (this.footer) {
size += (this.footer.text || '').length;
}
return size;
}
addField(name, value, inline) {
if (!this.fields) {
this.fields = new basecollection_1.BaseCollection();
}
const field = new EmbedField({ inline, name, value });
this.fields.set(this.fields.length, field);
return this;
}
setAuthor(name, iconUrl, url) {
this.merge({
author: {
icon_url: iconUrl,
name,
url,
},
});
return this;
}
setColor(color) {
this.merge({ color });
return this;
}
setDescription(description) {
this.merge({ description });
return this;
}
setFooter(text, iconUrl) {
this.merge({
footer: {
icon_url: iconUrl,
text,
},
});
return this;
}
setImage(url) {
this.merge({
image: { url },
});
return this;
}
setThumbnail(url) {
this.merge({
thumbnail: { url },
});
return this;
}
setTimestamp(timestamp = Date.now()) {
if (typeof (timestamp) === 'number') {
timestamp = new Date(timestamp);
}
if (timestamp instanceof Date) {
timestamp = String(timestamp);
}
this.merge({ timestamp });
return this;
}
setTitle(title) {
this.merge({ title });
return this;
}
setUrl(url) {
this.merge({ url });
return this;
}
mergeValue(key, value) {
switch (key) {
case constants_1.DiscordKeys.AUTHOR:
{
let author;
if (this.author) {
author = this.author;
author.merge(value);
}
else {
author = new EmbedAuthor(value);
}
value = author;
}
;
break;
case constants_1.DiscordKeys.FIELDS:
{
if (!this.fields) {
this.fields = new basecollection_1.BaseCollection();
}
this.fields.clear();
for (let i = 0; i < value.length; i++) {
this.fields.set(i, value[i]);
}
}
;
return;
case constants_1.DiscordKeys.FOOTER:
{
let footer;
if (this.footer) {
footer = this.footer;
footer.merge(value);
}
else {
footer = new EmbedFooter(value);
}
value = footer;
}
;
break;
case constants_1.DiscordKeys.PROVIDER:
{
let provider;
if (this.provider) {
provider = this.provider;
provider.merge(value);
}
else {
provider = new EmbedProvider(value);
}
value = provider;
}
;
break;
case constants_1.DiscordKeys.IMAGE:
{
let image;
if (this.image) {
image = this.image;
image.merge(value);
}
else {
image = new EmbedImage(value);
}
value = image;
}
;
break;
case constants_1.DiscordKeys.TIMESTAMP:
{
value = new Date(value);
}
;
break;
case constants_1.DiscordKeys.THUMBNAIL:
{
let thumbnail;
if (this.thumbnail) {
thumbnail = this.thumbnail;
thumbnail.merge(value);
}
else {
thumbnail = new EmbedThumbnail(value);
}
value = thumbnail;
}
;
break;
case constants_1.DiscordKeys.VIDEO:
{
let video;
if (this.video) {
video = this.video;
video.merge(value);
}
else {
video = new EmbedVideo(value);
}
value = video;
}
;
break;
}
return super.mergeValue(key, value);
}
toJSON() {
return super.toJSON();
}
}
exports.Embed = Embed;
const keysEmbedAuthor = new baseset_1.BaseSet([
constants_1.DiscordKeys.ICON_URL,
constants_1.DiscordKeys.NAME,
constants_1.DiscordKeys.PROXY_ICON_URL,
constants_1.DiscordKeys.URL,
]);
/**
* Utils Embed Author Structure, used for [Embed] Structures
* @category Utils
*/
class EmbedAuthor extends basestructure_1.Structure {
constructor(data) {
super();
this._keys = keysEmbedAuthor;
this.merge(data);
}
}
exports.EmbedAuthor = EmbedAuthor;
const keysEmbedField = new baseset_1.BaseSet([
constants_1.DiscordKeys.INLINE,
constants_1.DiscordKeys.NAME,
constants_1.DiscordKeys.VALUE,
]);
/**
* Utils Embed Field Structure, used for [Embed] Structures
* @category Utils
*/
class EmbedField extends basestructure_1.Structure {
constructor(data) {
super();
this._keys = keysEmbedField;
this.name = '';
this.value = '';
this.merge(data);
}
}
exports.EmbedField = EmbedField;
const keysEmbedFooter = new baseset_1.BaseSet([
constants_1.DiscordKeys.ICON_URL,
constants_1.DiscordKeys.PROXY_ICON_URL,
constants_1.DiscordKeys.TEXT,
]);
/**
* Utils Embed Footer Structure, used for [Embed] Structures
* @category Utils
*/
class EmbedFooter extends basestructure_1.Structure {
constructor(data) {
super();
this._keys = keysEmbedFooter;
this.text = '';
this.merge(data);
}
}
exports.EmbedFooter = EmbedFooter;
const keysEmbedImage = new baseset_1.BaseSet([
constants_1.DiscordKeys.HEIGHT,
constants_1.DiscordKeys.PROXY_URL,
constants_1.DiscordKeys.URL,
constants_1.DiscordKeys.WIDTH,
]);
/**
* Utils Embed Image Structure, used for [Embed] Structures
* @category Utils
*/
class EmbedImage extends basestructure_1.Structure {
constructor(data) {
super();
this._keys = keysEmbedImage;
this.url = '';
this.merge(data);
}
}
exports.EmbedImage = EmbedImage;
const keysEmbedProvider = new baseset_1.BaseSet([
constants_1.DiscordKeys.NAME,
constants_1.DiscordKeys.URL,
]);
/**
* Utils Provider Structure, used for [Embed] Structures
* @category Utils
*/
class EmbedProvider extends basestructure_1.Structure {
constructor(data) {
super();
this._keys = keysEmbedProvider;
this.merge(data);
}
}
exports.EmbedProvider = EmbedProvider;
/**
* Utils Embed Thumbnail Structure, used for [Embed] Structures
* @category Utils
*/
class EmbedThumbnail extends EmbedImage {
}
exports.EmbedThumbnail = EmbedThumbnail;
/**
* Utils Embed Video Structure, used for [Embed] Structures
* @category Utils
*/
class EmbedVideo extends EmbedImage {
}
exports.EmbedVideo = EmbedVideo;