UNPKG

detritus-client

Version:

A Typescript NodeJS library to interact with Discord's API, both Rest and Gateway.

266 lines (265 loc) 8.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MessageEmbedVideo = exports.MessageEmbedThumbnail = exports.MessageEmbedProvider = exports.MessageEmbedImage = exports.MessageEmbedFooter = exports.MessageEmbedField = exports.MessageEmbedAuthor = exports.MessageEmbed = void 0; const basecollection_1 = require("../collections/basecollection"); const baseset_1 = require("../collections/baseset"); const constants_1 = require("../constants"); const basestructure_1 = require("./basestructure"); const keysMessageEmbed = 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, ]); /** * Embed Structure, used for [Message] Structures * @category Structure */ class MessageEmbed extends basestructure_1.BaseStructure { constructor(client, data, isClone) { super(client, undefined, isClone); this._keys = keysMessageEmbed; this.type = constants_1.MessageEmbedTypes.RICH; this.merge(data); } get hasAttachment() { return !!((this.image && this.image.hasAttachment) || (this.thumbnail && this.thumbnail.hasAttachment) || (this.video && this.video.hasAttachment)); } get isApplicationNews() { return this.type === constants_1.MessageEmbedTypes.APPLICATION_NEWS; } get isArticle() { return this.type === constants_1.MessageEmbedTypes.ARTICLE; } get isGifV() { return this.type === constants_1.MessageEmbedTypes.GIFV; } get isImage() { return this.type === constants_1.MessageEmbedTypes.IMAGE; } get isLink() { return this.type === constants_1.MessageEmbedTypes.LINK; } get isRich() { return this.type === constants_1.MessageEmbedTypes.RICH; } get isTweet() { return this.type === constants_1.MessageEmbedTypes.TWEET; } get isVideo() { return this.type === constants_1.MessageEmbedTypes.VIDEO; } 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; } async fetchApplicationNews() { if (!this.isApplicationNews) { throw new Error('Embed isn\'t of Application News Type'); } if (!this.referenceId) { throw new Error('Embed is missing Application News Id'); } return this.client.rest.fetchApplicationNewsId(this.referenceId); } mergeValue(key, value) { switch (key) { case constants_1.DiscordKeys.AUTHOR: { value = new MessageEmbedAuthor(this.client, value, this._clone); } ; 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, new MessageEmbedField(this.client, value[i], this._clone)); } } ; return; case constants_1.DiscordKeys.FOOTER: { value = new MessageEmbedFooter(this.client, value, this._clone); } ; break; case constants_1.DiscordKeys.PROVIDER: { value = new MessageEmbedProvider(this.client, value, this._clone); } ; break; case constants_1.DiscordKeys.IMAGE: { value = new MessageEmbedImage(this.client, value, this._clone); } ; break; case constants_1.DiscordKeys.TIMESTAMP: { value = new Date(value); } ; break; case constants_1.DiscordKeys.THUMBNAIL: { value = new MessageEmbedThumbnail(this.client, value, this._clone); } ; break; case constants_1.DiscordKeys.VIDEO: { value = new MessageEmbedVideo(this.client, value, this._clone); } ; break; } return super.mergeValue(key, value); } } exports.MessageEmbed = MessageEmbed; const keysMessageEmbedAuthor = new baseset_1.BaseSet([ constants_1.DiscordKeys.ICON_URL, constants_1.DiscordKeys.NAME, constants_1.DiscordKeys.PROXY_ICON_URL, constants_1.DiscordKeys.URL, ]); /** * Embed Author Structure, used for [MessageEmbed] Structures * @category Structure */ class MessageEmbedAuthor extends basestructure_1.BaseStructure { constructor(client, data, isClone) { super(client, undefined, isClone); this._keys = keysMessageEmbedAuthor; this.merge(data); } } exports.MessageEmbedAuthor = MessageEmbedAuthor; const keysMessageEmbedField = new baseset_1.BaseSet([ constants_1.DiscordKeys.INLINE, constants_1.DiscordKeys.NAME, constants_1.DiscordKeys.VALUE, ]); /** * Embed Field Structure, used for [MessageEmbed] Structures * @category Structure */ class MessageEmbedField extends basestructure_1.BaseStructure { constructor(client, data, isClone) { super(client, undefined, isClone); this._keys = keysMessageEmbedField; this.inline = false; this.name = ''; this.value = ''; this.merge(data); } } exports.MessageEmbedField = MessageEmbedField; const keysMessageEmbedFooter = new baseset_1.BaseSet([ constants_1.DiscordKeys.ICON_URL, constants_1.DiscordKeys.PROXY_ICON_URL, constants_1.DiscordKeys.TEXT, ]); /** * Embed Footer Structure, used for [ApplicationNews] and [MessageEmbed] Structures * @category Structure */ class MessageEmbedFooter extends basestructure_1.BaseStructure { constructor(client, data, isClone) { super(client, undefined, isClone); this._keys = keysMessageEmbedFooter; this.text = ''; this.merge(data); } } exports.MessageEmbedFooter = MessageEmbedFooter; const keysMessageEmbedImage = new baseset_1.BaseSet([ constants_1.DiscordKeys.HEIGHT, constants_1.DiscordKeys.PROXY_URL, constants_1.DiscordKeys.URL, constants_1.DiscordKeys.WIDTH, ]); /** * Embed Image Structure, used for [MessageEmbed] Structures * @category Structure */ class MessageEmbedImage extends basestructure_1.BaseStructure { constructor(client, data, isClone) { super(client, undefined, isClone); this._keys = keysMessageEmbedImage; this.height = 0; this.url = ''; this.width = 0; this.merge(data); } get hasAttachment() { return !!(this.proxyUrl && this.proxyUrl.startsWith(constants_1.MEDIA_ATTACHMENT_URL_PREFIX)); } } exports.MessageEmbedImage = MessageEmbedImage; const keysMessageEmbedProvider = new baseset_1.BaseSet([ constants_1.DiscordKeys.NAME, constants_1.DiscordKeys.URL, ]); /** * Embed Provider Structure, used for [MessageEmbed] Structures * @category Structure */ class MessageEmbedProvider extends basestructure_1.BaseStructure { constructor(client, data, isClone) { super(client, undefined, isClone); this._keys = keysMessageEmbedProvider; this.merge(data); } } exports.MessageEmbedProvider = MessageEmbedProvider; /** * Embed Thumbnail Structure, used for [ApplicationNews] and [MessageEmbed] Structures * @category Structure */ class MessageEmbedThumbnail extends MessageEmbedImage { } exports.MessageEmbedThumbnail = MessageEmbedThumbnail; /** * Embed Video Structure, used for [MessageEmbed] Structures * @category Structure */ class MessageEmbedVideo extends MessageEmbedImage { } exports.MessageEmbedVideo = MessageEmbedVideo;