From ff98670cf52d2fe3d501c1bc9c44baec0a46a2e3 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Wed, 23 Dec 2020 02:27:45 +0200 Subject: [PATCH] Improve empty message detection when rendering tags --- .../src/plugins/Tags/util/onMessageCreate.ts | 7 ++-- backend/src/utils/messageHasContent.ts | 35 +++++++++++++++++++ backend/src/utils/messageIsEmpty.ts | 6 ++++ 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 backend/src/utils/messageHasContent.ts create mode 100644 backend/src/utils/messageIsEmpty.ts diff --git a/backend/src/plugins/Tags/util/onMessageCreate.ts b/backend/src/plugins/Tags/util/onMessageCreate.ts index 9dd4d1a6..e2dbb961 100644 --- a/backend/src/plugins/Tags/util/onMessageCreate.ts +++ b/backend/src/plugins/Tags/util/onMessageCreate.ts @@ -6,6 +6,7 @@ import { validate } from "../../../validatorUtils"; import { LogType } from "../../../data/LogType"; import { TextChannel } from "eris"; import { matchAndRenderTagFromString } from "./matchAndRenderTagFromString"; +import { messageIsEmpty } from "../../../utils/messageIsEmpty"; export async function onMessageCreate(pluginData: GuildPluginData<TagsPluginType>, msg: SavedMessage) { if (msg.is_bot) return; @@ -91,11 +92,7 @@ export async function onMessageCreate(pluginData: GuildPluginData<TagsPluginType return; } - if ( - tagResult.renderedContent.content && - !tagResult.renderedContent.embed && - tagResult.renderedContent.content.trim() === "" - ) { + if (messageIsEmpty(tagResult.renderedContent)) { pluginData.state.logs.log(LogType.BOT_ALERT, { body: `Tag \`${tagResult.tagName}\` resulted in an empty message, so it couldn't be sent`, }); diff --git a/backend/src/utils/messageHasContent.ts b/backend/src/utils/messageHasContent.ts new file mode 100644 index 00000000..ca3dd8bf --- /dev/null +++ b/backend/src/utils/messageHasContent.ts @@ -0,0 +1,35 @@ +import { MessageContent } from "eris"; + +function embedHasContent(embed: any) { + for (const [key, value] of Object.entries(embed)) { + if (typeof value === "string" && value.trim() !== "") { + return true; + } + + if (typeof value === "object" && value != null && embedHasContent(value)) { + return true; + } + + if (value != null) { + return true; + } + } + + return false; +} + +export function messageHasContent(content: MessageContent): boolean { + if (typeof content === "string") { + return content.trim() !== ""; + } + + if (content.content != null && content.content.trim() !== "") { + return true; + } + + if (content.embed && embedHasContent(content.embed)) { + return true; + } + + return false; +} diff --git a/backend/src/utils/messageIsEmpty.ts b/backend/src/utils/messageIsEmpty.ts new file mode 100644 index 00000000..2cd3f255 --- /dev/null +++ b/backend/src/utils/messageIsEmpty.ts @@ -0,0 +1,6 @@ +import { MessageContent } from "eris"; +import { messageHasContent } from "./messageHasContent"; + +export function messageIsEmpty(content: MessageContent): boolean { + return !messageHasContent(content); +}