darkcord
Version:
A NodeJS Package to interact with Discord API
200 lines (199 loc) • 5.86 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Reaction = exports.Emoji = void 0;
const UserDataManager_1 = require("../manager/UserDataManager");
const v10_1 = require("discord-api-types/v10");
const Base_1 = require("./Base");
class Emoji {
/**
* Whether this emoji must be wrapped in colons
*/
requireColons;
/**
* Whether this emoji is managed
*/
managed;
/**
* Whether this emoji can be used, may be false due to loss of Server Boosts
*/
available;
/**
* Emoji name (can be null only in reaction emoji objects)
*/
name;
/**
* Whether this emoji is animated
*/
animated;
/**
* Emoji id
*/
id;
rawData;
constructor(data) {
this._update(data);
}
_update(data) {
if ("id" in data)
this.id = data.id;
if ("require_colons" in data)
this.requireColons = data.require_colons;
if ("managed" in data)
this.managed = data.managed;
if ("available" in data)
this.available = data.available;
if ("name" in data)
this.name = data.name;
if ("animated" in data)
this.animated = Boolean(data.animated);
else
this.animated ??= false;
this.rawData = Object.assign({}, data, this.rawData);
return this;
}
createdAt() {
return this.id && Base_1.Snowflake.getCreatedAt(this.id);
}
/**
* Emoji url
*/
get url() {
return (this.id &&
v10_1.RouteBases.cdn +
v10_1.CDNRoutes.emoji(this.id, this.animated ? v10_1.ImageFormat.GIF : v10_1.ImageFormat.PNG));
}
get uriComponent() {
return this.id
? `${this.animated ? "a:" : ""}${this.name}:${this.id}`
: encodeURIComponent(this.name);
}
/**
* Update information of this emoji
*
* Util if this is forged
* @returns
*/
async fetchInformation(client, guildId) {
if (!this.id)
throw new Error("Emoji id is required to fetch emoji information");
if (!guildId)
throw new Error("Guild id is required to fetch emoji information");
const data = await client.rest.getEmoji(guildId, this.id);
return this._update(data ?? {});
}
toString() {
return this.id
? `<${this.animated ? "a" : ""}:${this.name}:${this.id}>`
: this.name;
}
static from(emoji) {
const r = Emoji.parse(emoji);
return r && new Emoji(r);
}
static parse(emoji) {
if (emoji === encodeURIComponent(emoji))
emoji = decodeURIComponent(emoji);
if (!emoji.includes(":"))
return null;
const r = emoji.match(/<?(?:(a):)?(\w{2,32}):(\d{17,19})?>?/);
return (r && {
name: r[2],
id: r[3],
animated: Boolean(r[1]),
});
}
static getEncodedURI(emoji) {
if (typeof emoji === "string") {
emoji = Emoji.parse(emoji);
}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
return emoji && emoji.id
? `${emoji.animated ? "a:" : ""}${emoji.name}:${emoji.id}`
: encodeURIComponent(emoji.name);
}
}
exports.Emoji = Emoji;
class Reaction {
/**
* Whether the current user reacted using this emoji
*/
me;
/**
* Emoji information
*/
emoji;
/**
* Times this emoji has been used to react
*/
count;
/**
* Users reacted
*/
users;
/**
* HEX colors used for super reaction
*/
burstColors;
/**
* Reaction count details object
*/
countDetails;
_client;
/**
* The message of this reaction
*/
message;
/**
* The message id of this reaction
*/
messageId;
/**
* The channel id of the message of this reaction
*/
channelId;
constructor(data) {
Object.defineProperty(this, "_client", { value: data.client });
this._update(data);
}
_update(data) {
if ("me" in data)
this.me = data.me;
if ("emoji" in data)
this.emoji = new Emoji(data.emoji);
if ("count" in data)
this.count = data.count;
if ("users" in data)
this.users = new UserDataManager_1.UserDataManager(this._client.cache._cacheLimit("users"), this._client.cache);
if ("burst_colors" in data)
this.burstColors = data.burst_colors;
if ("count_details" in data)
this.countDetails = data.count_details;
if ("channel_id" in data)
this.channelId = data.channel_id;
if ("message_id" in data)
this.messageId = data.message_id;
if (this.messageId && this.channelId) {
const channel = this._client.channels.cache.get(this.channelId);
if (channel && channel.isText()) {
const message = channel.messages.cache.get(this.messageId);
this.message = message ?? null;
}
}
return this;
}
/**
* Update information of this reaction
*
* Util if this is forged
* @returns
*/
async fetchInformation(channelId = this.channelId, messageId = this.messageId) {
if (!channelId || !messageId) {
throw new Error("Channel id and message id must be provided to fetch information of this reaction");
}
const data = await this._client.rest.getMessage(channelId, messageId);
const updated = data.reactions?.find((r) => r.emoji.id === this.emoji.id || r.emoji.name === this.emoji.name);
return this._update(updated ?? {});
}
}
exports.Reaction = Reaction;