From e43e812b6e07ed162bef1912b5c72461db269333 Mon Sep 17 00:00:00 2001 From: Techbot121 Date: Mon, 11 Nov 2024 19:37:45 +0100 Subject: [PATCH] =?UTF-8?q?reacting=205x=F0=9F=91=8E=20to=20cached=20media?= =?UTF-8?q?=20will=20now=20remove=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/discord/modules/shitposting.ts | 57 ++++++++++++++----- app/services/webapp/api/game-server-status.ts | 1 + 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/app/services/discord/modules/shitposting.ts b/app/services/discord/modules/shitposting.ts index 90d11a6a..3dc7aa1e 100644 --- a/app/services/discord/modules/shitposting.ts +++ b/app/services/discord/modules/shitposting.ts @@ -37,6 +37,17 @@ const REPLY_FREQ = 0.5; // how often to when to take a word from a previous mess const ALLOWED_IMG_PROVIDERS = ["tenor", "imgur", "discordapp", "tumblr"]; +const getMediaUrl = (url: string) => { + const match = url.match( + new RegExp( + `https?://(?:(?:\\w+)?\\.?)+(?:${ALLOWED_IMG_PROVIDERS.join( + "|" + )})\\.(?:com|io)/[^?\\s]+` + ) + ); + if (match) return match[0]; +}; + const IGNORE_LIST = ["437294613976449024"]; function getWord(msg?: string) { @@ -335,15 +346,38 @@ export default async (bot: DiscordBot) => { }); bot.discord.on("messageReactionAdd", async (reaction, user) => { + const message = reaction.message; if ( - !bot.config.bot.allowedShitpostingChannels.includes(reaction.message.channelId) || - reaction.message.author?.id !== bot.discord.user?.id || - lastRespondedReactionMsgs.includes(reaction.message.id) + !bot.config.bot.allowedShitpostingChannels.includes(message.channelId) || + message.author?.id !== bot.discord.user?.id ) return; + if ( + message.content?.startsWith("http") && + reaction.emoji.name === "👎" && + reaction.count && + reaction.count >= 5 + ) { + const url = getMediaUrl(message.content); + if (url) { + await message + .delete() + .then(() => { + db.run("DELETE FROM media_urls WHERE url = ?", url); + }) + .catch(err => + console.error( + "[shitposting] Failed to delete requested message from cache", + err + ) + ); + } + } + if ( user.id !== lastReactionUserId && + !lastRespondedReactionMsgs.includes(message.id) && Math.random() <= (reaction.emoji.name === "h_" ? 0.025 : MSG_REPLY_REACTION_FREQ) ) { const mk = await bot.container @@ -351,8 +385,8 @@ export default async (bot: DiscordBot) => { ?.generate(reaction.emoji.toString(), DefaultMarkovConfig); if (mk) { lastReactionUserId = user.id; - lastRespondedReactionMsgs.push(reaction.message.id); - await (reaction.message.channel as Discord.TextChannel) + lastRespondedReactionMsgs.push(message.id); + await (message.channel as Discord.TextChannel) .send(`${user.mention} ` + mk) .catch(); } @@ -440,18 +474,11 @@ export default async (bot: DiscordBot) => { // image collector if (msg.content.startsWith("http") && !isBot && !isHidden) { - const match = msg.content.match( - new RegExp( - `https?://(?:(?:\\w+)?\\.?)+(?:${ALLOWED_IMG_PROVIDERS.join( - "|" - )})\\.(?:com|io)/[^?\\s]+` - ) - ); - if (match) { + const url = getMediaUrl(msg.content); + if (url) db.run("INSERT INTO media_urls VALUES($url) ON CONFLICT DO NOTHING", { - $url: match[0], + $url: url, }); - } } }); }; diff --git a/app/services/webapp/api/game-server-status.ts b/app/services/webapp/api/game-server-status.ts index a00aec7d..684e6518 100644 --- a/app/services/webapp/api/game-server-status.ts +++ b/app/services/webapp/api/game-server-status.ts @@ -53,6 +53,7 @@ export default (webApp: WebApp): void => { }); res.send(server.playerListImage); } catch (err) { + console.error("game-server-status image failed", err); res.send(err); } } else {