fix: allow bun sticker (#40)

closes https://github.com/xhyrom/bun-discord-bot/issues/39
This commit is contained in:
Jozef Steinhübl 2024-11-08 17:32:51 +01:00 committed by GitHub
parent 24451c1fc3
commit cc50af7396
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 24 additions and 9 deletions

View file

@ -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";

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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(/<a:|>/g, "") == `${emoji.name}:${emoji.id}`
: content.replace(/<:|>/g, "") == `${emoji.name}:${emoji.id}`,
? message.content!.replace(/<a:|>/g, "") == `${emoji.name}:${emoji.id}`
: message.content!.replace(/<:|>/g, "") == `${emoji.name}:${emoji.id}`,
);
}