From 035fa196ea7d10b58289a429be0d951355455412 Mon Sep 17 00:00:00 2001 From: Tarrgon <61888458+Tarrgon@users.noreply.github.com> Date: Sat, 31 May 2025 15:10:25 -0400 Subject: [PATCH] Find message attachments by md5 name --- src/events/handle-message.ts | 17 +++++++++++++++++ src/utils/e621-utils.ts | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/events/handle-message.ts b/src/events/handle-message.ts index 1a662d2..b96d716 100644 --- a/src/events/handle-message.ts +++ b/src/events/handle-message.ts @@ -17,6 +17,8 @@ const imageRegex = new RegExp('!?https?://(?:.*@)?static[0-9]*\\.(?:e621|e926)\\ const postRegex_DEV = new RegExp('!?https?://(?:.*@)?localhost:3000/+posts/+([0-9]+)', 'gi'); const imageRegex_DEV = new RegExp('!?https?://(?:.*@)?localhost:3000/+data/+(?:sample/+|preview/+|)[\\da-f]{2}/+[\\da-f]{2}/+([\\da-f]{32})\\.[\\da-z]+', 'gi'); +const md5Regex = new RegExp('^([a-f0-9]{32}).(?:png|apng|jpg|jpeg|gif|webm|mp4)$', 'gi'); + const postIDRegex = new RegExp('post #([0-9]+)', 'gi'); const userIDRegex = new RegExp('user #([0-9]+)', 'gi'); const forumTopicIDRegex = new RegExp('topic #([0-9]+)', 'gi'); @@ -81,6 +83,21 @@ export async function handleMessageCreate(message: Message) { } } + for (const attachment of message.attachments.values()) { + const match = md5Regex.exec(attachment.name); + md5Regex.lastIndex = 0; + + if (match) { + const post = await getE621PostByMd5(match[1]); + + if (post) { + if (await blacklistIfNecessary(message, [post])) return; + + responses.push(`<${getPostUrl(post)}>`); + } + } + } + if (responses.length > 0) { await message.reply(responses.join('\n')); } diff --git a/src/utils/e621-utils.ts b/src/utils/e621-utils.ts index 678390d..6f47c8d 100644 --- a/src/utils/e621-utils.ts +++ b/src/utils/e621-utils.ts @@ -34,11 +34,11 @@ export async function getE621User(idOrName: string | number): Promise { - return (await request(`/posts/${id}`)).post as E621Post; + return (await request(`/posts/${id}`))?.post as E621Post ?? null; } export async function getE621PostByMd5(md5: string): Promise { - return (await request('/posts', { md5 })).post as E621Post; + return (await request('/posts', { md5 }))?.post as E621Post ?? null; } export function hasBlacklistedTags(post: E621Post): boolean {