UNPKG

detritus-client

Version:

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

150 lines (149 loc) 4.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Sticker = exports.StickerItem = void 0; const detritus_client_rest_1 = require("detritus-client-rest"); const baseset_1 = require("../collections/baseset"); const constants_1 = require("../constants"); const utils_1 = require("../utils"); const basestructure_1 = require("./basestructure"); const user_1 = require("./user"); const keysStickerItem = new baseset_1.BaseSet([ constants_1.DiscordKeys.FORMAT_TYPE, constants_1.DiscordKeys.ID, constants_1.DiscordKeys.NAME, ]); /** * Sticker Item Structure * @category Structure */ class StickerItem extends basestructure_1.BaseStructure { constructor(client, data, isClone) { super(client, undefined, isClone); this._keys = keysStickerItem; this.formatType = constants_1.StickerFormats.UNKNOWN; this.id = ''; this.name = ''; this.merge(data); } get assetUrl() { return this.assetUrlFormat(); } get createdAt() { return new Date(this.createdAtUnix); } get createdAtUnix() { return utils_1.Snowflake.timestamp(this.id); } get format() { switch (this.formatType) { case constants_1.StickerFormats.PNG: return constants_1.StickerExtensions.PNG; case constants_1.StickerFormats.APNG: return constants_1.StickerExtensions.APNG; case constants_1.StickerFormats.LOTTIE: return constants_1.StickerExtensions.LOTTIE; default: { throw new Error(`Unexpected format type: ${this.formatType}`); } ; } } assetUrlFormat(format, query) { if (!format) { format = this.format; } return utils_1.addQuery(detritus_client_rest_1.Endpoints.CDN.URL + detritus_client_rest_1.Endpoints.CDN.STICKER(this.id, this.format), query); } toString() { return this.name; } } exports.StickerItem = StickerItem; const keysSticker = new baseset_1.BaseSet([ constants_1.DiscordKeys.ASSET, constants_1.DiscordKeys.AVAILABLE, constants_1.DiscordKeys.DESCRIPTION, constants_1.DiscordKeys.FORMAT_TYPE, constants_1.DiscordKeys.GUILD_ID, constants_1.DiscordKeys.ID, constants_1.DiscordKeys.NAME, constants_1.DiscordKeys.PACK_ID, constants_1.DiscordKeys.PREVIEW_ASSET, constants_1.DiscordKeys.SORT_VALUE, constants_1.DiscordKeys.TAGS, constants_1.DiscordKeys.USER, ]); /** * Sticker Structure * @category Structure */ class Sticker extends StickerItem { constructor(client, data, isClone) { super(client, undefined, isClone); this._keys = keysSticker; this.asset = ''; this.description = ''; this.formatType = constants_1.StickerFormats.UNKNOWN; this.id = ''; this.name = ''; this.previewAsset = null; this.tags = ''; this.merge(data); } get guild() { if (this.guildId) { return this.client.guilds.get(this.guildId) || null; } return null; } get member() { if (this.guildId && this.user) { return this.client.members.get(this.guildId, this.user.id) || null; } return null; } delete() { if (!this.guildId) { throw new Error('Can\'t delete a global sticker!'); } return this.client.rest.deleteGuildSticker(this.guildId, this.id); } edit() { if (!this.guildId) { throw new Error('Can\'t edit a global sticker!'); } return this.client.rest.editGuildSticker(this.guildId, this.id); } fetch() { if (!this.guildId) { throw new Error('Can\'t edit a global sticker!'); } return this.client.rest.fetchGuildSticker(this.guildId, this.id); } mergeValue(key, value) { if (value !== undefined) { switch (key) { case constants_1.DiscordKeys.USER: { let user; if (this.isClone) { user = new user_1.User(this.client, value, this.isClone); } else { if (this.client.users.has(value.id)) { user = this.client.users.get(value.id); user.merge(value); } else { user = new user_1.User(this.client, value); this.client.users.insert(user); } } value = user; } ; break; } return super.mergeValue(key, value); } } } exports.Sticker = Sticker;