@wilcosp/rex
Version:
Rex is an automated command manager for discord js
92 lines (91 loc) • 2.76 kB
JavaScript
;
/*!
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.RexEmbed = void 0;
const discord_js_1 = require("discord.js");
class RexEmbed extends discord_js_1.EmbedBuilder {
setFooterText(text) {
this.setFooter({
text: text,
iconURL: this.data.footer?.icon_url,
});
return this;
}
combinedSum() {
let total = 0;
total += this.data.title?.length ?? 0;
total += this.data.description?.length ?? 0;
total += this.data.footer?.text?.length ?? 0;
total += this.data.author?.name?.length ?? 0;
if (this.data.fields) {
for (const field of this.data.fields) {
total += field.name.length;
total += field.value.length;
}
}
return total;
}
static chopString(txt, splitter, maxLength) {
if (txt.length < maxLength)
return [txt];
let copy = `${txt}`;
const result = [];
do {
if (copy.length < maxLength) {
result.push(copy);
break;
}
const left = copy.slice(0, maxLength);
const r = left.slice(0, left.lastIndexOf(splitter));
result.push(r);
copy = copy.replace(copy.slice(0, r.length + splitter.length), "");
} while (copy.length > 0);
return result;
}
static splitEmbeds(embeds) {
const result = [[]];
embeds: for (const embed of embeds) {
const last = result[result.length - 1];
if (last.length >= RexEmbed.limits.embedsPerMessage) {
result.push([embed]);
continue embeds;
}
let comSum = 0;
for (const em of last) {
comSum += em?.combinedSum() ?? 0;
}
if (comSum + embed.combinedSum() > RexEmbed.limits.combinedSum) {
result.push([embed]);
continue embeds;
}
last.push(embed);
}
return result;
}
static from(embed) {
if (typeof embed === "object" && "toJSON" in embed) {
return new this(embed.toJSON());
}
return new this(embed);
}
}
exports.RexEmbed = RexEmbed;
RexEmbed.limits = {
title: 256,
description: 4096,
field: {
title: 256,
value: 1024,
max: 25,
},
footer: 2048,
author: {
name: 256,
},
combinedSum: 6000,
embedsPerMessage: 10,
};