From cc50af73961d9bce99dd86ef8461257c9c7ce5eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Fri, 8 Nov 2024 17:32:51 +0100 Subject: [PATCH] fix: allow bun sticker (#40) closes https://github.com/xhyrom/bun-discord-bot/issues/39 --- src/constants.ts | 1 + src/listeners/message_create.tsx | 2 +- src/listeners/message_update.ts | 2 +- src/util.ts | 28 +++++++++++++++++++++------- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 51a4289..26f5a64 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -54,6 +54,7 @@ export const BUN_EMOJIS = [ { name: "bunpet", id: "1172808445737574591", animated: true }, { name: "bunsegfault", id: "1175208306533486632", animated: true }, ]; +export const BUN_STICKER = "994093337080516698"; export const MDN_API = "https://developer.mozilla.org"; export const MDN_DISCORD_EMOJI = "mdn:1236028636826566758"; diff --git a/src/listeners/message_create.tsx b/src/listeners/message_create.tsx index e34cba0..f672281 100644 --- a/src/listeners/message_create.tsx +++ b/src/listeners/message_create.tsx @@ -33,7 +33,7 @@ function handleOthers(message: Message): void { function handleBunOnlyChannel(message: Message): boolean { if (message.channelId !== process.env.BUN_ONLY_CHANNEL_ID) return false; - if (!isBunOnlyLikeMessage(message.content)) { + if (!isBunOnlyLikeMessage(message)) { message.delete(); return true; } diff --git a/src/listeners/message_update.ts b/src/listeners/message_update.ts index a522b90..72494e4 100644 --- a/src/listeners/message_update.ts +++ b/src/listeners/message_update.ts @@ -12,7 +12,7 @@ $listener({ function handleBunOnlyChannel(message: PartialMessage): boolean { if (message.channelId !== process.env.BUN_ONLY_CHANNEL_ID) return false; - if (!isBunOnlyLikeMessage(message.content)) { + if (!isBunOnlyLikeMessage(message)) { message.delete(); return true; } diff --git a/src/util.ts b/src/util.ts index 34b2ff6..75a3fd9 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,5 +1,5 @@ -import { GuildMember } from "@lilybird/transformers"; -import { BUN_EMOJIS, wolframApiClient } from "./constants.ts"; +import { GuildMember, PartialMessage } from "@lilybird/transformers"; +import { BUN_EMOJIS, BUN_STICKER, wolframApiClient } from "./constants.ts"; import { parseAndRemap, formatMarkdown } from "bun-tracestrings"; const URL_REGEX = /\(\s*(https?:\/\/[^\s\[\]]+)\s*\)/gi; @@ -31,14 +31,28 @@ export async function moderateNick(member: GuildMember) { ); } -export function isBunOnlyLikeMessage(content?: string) { - if (!content) return false; - if (content === "bun") return true; +export function isBunOnlyLikeMessage(message: PartialMessage) { + // No content or stickers + if (!message.content && message.stickerItems?.length == 0) return false; + // Has attachments + if (message.hasAttachments()) return false; + // Has more than one sticker or a sticker that isn't the bun sticker + if ( + message.stickerItems && + ((message.stickerItems.length == 1 && + message.stickerItems[0].id !== BUN_STICKER) || + message.stickerItems.length > 1) + ) + return false; + + // has valid sticker + if (message.stickerItems) return true; + if (message.content === "bun") return true; return BUN_EMOJIS.some((emoji) => emoji.animated - ? content.replace(//g, "") == `${emoji.name}:${emoji.id}` - : content.replace(/<:|>/g, "") == `${emoji.name}:${emoji.id}`, + ? message.content!.replace(//g, "") == `${emoji.name}:${emoji.id}` + : message.content!.replace(/<:|>/g, "") == `${emoji.name}:${emoji.id}`, ); }