Find message attachments by md5 name
This commit is contained in:
@@ -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'));
|
||||
}
|
||||
|
||||
@@ -34,11 +34,11 @@ export async function getE621User(idOrName: string | number): Promise<E621User |
|
||||
}
|
||||
|
||||
export async function getE621Post(id: string | number): Promise<E621Post | null> {
|
||||
return (await request(`/posts/${id}`)).post as E621Post;
|
||||
return (await request(`/posts/${id}`))?.post as E621Post ?? null;
|
||||
}
|
||||
|
||||
export async function getE621PostByMd5(md5: string): Promise<E621Post | null> {
|
||||
return (await request('/posts', { md5 })).post as E621Post;
|
||||
return (await request('/posts', { md5 }))?.post as E621Post ?? null;
|
||||
}
|
||||
|
||||
export function hasBlacklistedTags(post: E621Post): boolean {
|
||||
|
||||
Reference in New Issue
Block a user