mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-16 14:11:50 +00:00
Improve empty message detection when rendering tags
This commit is contained in:
parent
3def728ab5
commit
ff98670cf5
3 changed files with 43 additions and 5 deletions
|
@ -6,6 +6,7 @@ import { validate } from "../../../validatorUtils";
|
||||||
import { LogType } from "../../../data/LogType";
|
import { LogType } from "../../../data/LogType";
|
||||||
import { TextChannel } from "eris";
|
import { TextChannel } from "eris";
|
||||||
import { matchAndRenderTagFromString } from "./matchAndRenderTagFromString";
|
import { matchAndRenderTagFromString } from "./matchAndRenderTagFromString";
|
||||||
|
import { messageIsEmpty } from "../../../utils/messageIsEmpty";
|
||||||
|
|
||||||
export async function onMessageCreate(pluginData: GuildPluginData<TagsPluginType>, msg: SavedMessage) {
|
export async function onMessageCreate(pluginData: GuildPluginData<TagsPluginType>, msg: SavedMessage) {
|
||||||
if (msg.is_bot) return;
|
if (msg.is_bot) return;
|
||||||
|
@ -91,11 +92,7 @@ export async function onMessageCreate(pluginData: GuildPluginData<TagsPluginType
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (messageIsEmpty(tagResult.renderedContent)) {
|
||||||
tagResult.renderedContent.content &&
|
|
||||||
!tagResult.renderedContent.embed &&
|
|
||||||
tagResult.renderedContent.content.trim() === ""
|
|
||||||
) {
|
|
||||||
pluginData.state.logs.log(LogType.BOT_ALERT, {
|
pluginData.state.logs.log(LogType.BOT_ALERT, {
|
||||||
body: `Tag \`${tagResult.tagName}\` resulted in an empty message, so it couldn't be sent`,
|
body: `Tag \`${tagResult.tagName}\` resulted in an empty message, so it couldn't be sent`,
|
||||||
});
|
});
|
||||||
|
|
35
backend/src/utils/messageHasContent.ts
Normal file
35
backend/src/utils/messageHasContent.ts
Normal file
|
@ -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;
|
||||||
|
}
|
6
backend/src/utils/messageIsEmpty.ts
Normal file
6
backend/src/utils/messageIsEmpty.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
import { MessageContent } from "eris";
|
||||||
|
import { messageHasContent } from "./messageHasContent";
|
||||||
|
|
||||||
|
export function messageIsEmpty(content: MessageContent): boolean {
|
||||||
|
return !messageHasContent(content);
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue