MessageBuffer: take embed length into account

This commit is contained in:
Dragory 2021-09-11 19:53:20 +03:00
parent bd87e1f000
commit a26eaa480c
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
2 changed files with 29 additions and 2 deletions

View file

@ -1,5 +1,6 @@
import { StrictMessageContent } from "../utils";
import Timeout = NodeJS.Timeout;
import { calculateEmbedSize } from "./calculateEmbedSize";
type ConsumeFn = (part: StrictMessageContent) => void;
@ -18,6 +19,7 @@ export interface MessageBufferOpts {
}
const MAX_CHARS_PER_MESSAGE = 2000;
const MAX_EMBED_LENGTH_PER_MESSAGE = 6000;
const MAX_EMBEDS_PER_MESSAGE = 10;
/**
@ -76,8 +78,16 @@ export class MessageBuffer {
}
if (content.embeds) {
if (chunk.content.embeds && chunk.content.embeds.length + content.embeds.length > MAX_EMBEDS_PER_MESSAGE) {
this.startNewChunk(contentType);
if (chunk.content.embeds) {
if (chunk.content.embeds.length + content.embeds.length > MAX_EMBEDS_PER_MESSAGE) {
this.startNewChunk(contentType);
} else {
const existingEmbedsLength = chunk.content.embeds.reduce((sum, embed) => sum + calculateEmbedSize(embed), 0);
const embedsLength = content.embeds.reduce((sum, embed) => sum + calculateEmbedSize(embed), 0);
if (existingEmbedsLength + embedsLength > MAX_EMBED_LENGTH_PER_MESSAGE) {
this.startNewChunk(contentType);
}
}
}
if (chunk.content.embeds == null) chunk.content.embeds = [];

View file

@ -0,0 +1,17 @@
import { MessageEmbedOptions } from "discord.js";
function sumStringLengthsRecursively(obj: any): number {
if (obj == null) return 0;
if (typeof obj === "string") return obj.length;
if (Array.isArray(obj)) {
return obj.reduce((sum, item) => sum + sumStringLengthsRecursively(item), 0);
}
if (typeof obj === "object") {
return Array.from(Object.values(obj)).reduce((sum: number, item) => sum + sumStringLengthsRecursively(item), 0);
}
return 0;
}
export function calculateEmbedSize(embed: MessageEmbedOptions): number {
return sumStringLengthsRecursively(embed);
}