import { Message as DiscordMessage, GuildBasedChannel, GuildTextBasedChannel, spoiler, OmitPartialGroupDMChannel } from 'discord.js'; import { config } from '../config'; import { appealIDRegex, artistIDRegex, blipIDRegex, channelIgnoresLinks, channelIsInStaffCategory, channelIsSafe, commentIDRegex, flagIDRegex, forumTopicIDRegex, getE621Pool, getE621Post, getE621PostByMd5, getManyE621Posts, getPoolUrl, getPostUrl, isInSpoilerTags, issueRegex, poolIDRegex, PostAction, postIDRegex, prRegex, recordIDRegex, searchLinkRegex, setIDRegex, spoilerOrBlacklist, takedownIDRegex, ticketIDRegex, userIDRegex, wikiLinkRegex } from '.'; import { E621Post, E621Pool } from '../types'; export type Message = OmitPartialGroupDMChannel>; const postRegex = new RegExp('!?https?://(?:.*@)?(?:e621|e926)\\.net/+posts/+([0-9]+)', 'gi'); const postShareRegex = new RegExp('!?https?://(?:.*@)?(?:e621|e926)\\.net/+p/+([a-z0-9]+)', 'gi'); const imageRegex = new RegExp('!?https?://(?:.*@)?static[0-9]*\\.(?:e621|e926)\\.net/+data/+(?:sample/+|preview/+|)[\\da-f]{2}/+[\\da-f]{2}/+([\\da-f]{32})\\.[\\da-z]+', 'gi'); const poolRegex = new RegExp('!?https?://(?:.*@)?(?:e621|e926)\\.net/+pools/+([0-9]+)', 'gi'); 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 poolRegex_DEV = new RegExp('!?https?://(?:.*@)?localhost:3000/+pools/+([0-9]+)', 'gi'); export const md5Regex = new RegExp('^([a-f0-9]{32}).(?:png|apng|jpg|jpeg|gif|webm|mp4)$', 'gi'); export const regexTesters = [ { runInDev: false, regex: postRegex, handler: postHandler.bind(null, null) }, { runInDev: false, regex: postShareRegex, handler: postHandler.bind(null, (idString: string) => { return parseInt(idString, 32); }) }, { runInDev: false, regex: imageRegex, handler: imageHandler }, { runInDev: false, regex: poolRegex, handler: poolHandler }, { runInDev: true, regex: postRegex_DEV, handler: postHandler.bind(null, null) }, { runInDev: true, regex: imageRegex_DEV, handler: imageHandler }, { runInDev: true, regex: poolRegex_DEV, handler: poolHandler }, { runInDev: true, regex: postIDRegex, handler: postIdHandler }, { runInDev: true, regex: poolIDRegex, handler: poolIdHandler }, { runInDev: true, regex: userIDRegex, handler: idHandler.bind(null, 'users') }, { runInDev: true, regex: forumTopicIDRegex, handler: idHandler.bind(null, 'forum_topics') }, { runInDev: true, regex: commentIDRegex, handler: idHandler.bind(null, 'comments') }, { runInDev: true, regex: blipIDRegex, handler: idHandler.bind(null, 'blips') }, { runInDev: true, regex: setIDRegex, handler: idHandler.bind(null, 'post_sets') }, { runInDev: true, regex: takedownIDRegex, handler: idHandler.bind(null, 'takedowns') }, { runInDev: true, regex: recordIDRegex, handler: idHandler.bind(null, 'user_feedbacks') }, { runInDev: true, regex: ticketIDRegex, handler: idHandler.bind(null, 'tickets') }, { runInDev: true, regex: appealIDRegex, handler: idHandler.bind(null, 'appeals') }, { runInDev: true, regex: flagIDRegex, handler: idHandler.bind(null, 'post_flags') }, { runInDev: true, regex: artistIDRegex, handler: idHandler.bind(null, 'artists') }, { runInDev: true, regex: wikiLinkRegex, handler: wikiPageHandler }, { runInDev: true, regex: searchLinkRegex, handler: searchHandler }, { runInDev: true, regex: prRegex, handler: githubPullRequestHandler }, { runInDev: true, regex: issueRegex, handler: githubIssueHandler }, ]; export const uniqueRegexMatches = (g, i, a) => a.findIndex(v => v[1] == g[1]) == i; async function searchHandler(message: Message, matchedGroups: RegExpExecArray[]): Promise { const skip = await channelIgnoresLinks(message.channel as GuildBasedChannel); if (skip) return true; let content = ''; for (const group of matchedGroups) { content += `<${config.E621_BASE_URL}/posts?tags=${encodeURIComponent(group[1])}>\n`; } if (content.trim().length > 0) return content.trim(); return true; } async function wikiPageHandler(message: Message, matchedGroups: RegExpExecArray[]): Promise { const skip = await channelIgnoresLinks(message.channel as GuildBasedChannel); if (skip) return true; let content = ''; for (const group of matchedGroups) { content += `<${config.E621_BASE_URL}/wiki_pages/${group[1].split('#').map(t => encodeURIComponent(t)).join('#')}>\n`; } if (content.trim().length > 0) return content.trim(); return true; } export async function blacklistIfNecessary(message: Message, posts: E621Post[]): Promise { const blacklistedIds: number[] = []; const channel = await message.channel.fetch() as GuildTextBasedChannel; const isStaffChannel = await channelIsInStaffCategory(channel); for (const post of posts) { if (spoilerOrBlacklist(post).action == PostAction.Blacklist) { blacklistedIds.push(post.id); } } if (blacklistedIds.length == 0) return false; await message.delete(); if (channel.parentId && isStaffChannel) { await message.channel.send({ content: `_sucks message into the void._ ${message.author.toString()} nono, don't post links to ${blacklistedIds.length == 1 ? `post ${blacklistedIds[0]}` : `posts \`${blacklistedIds.join('`, `')}\``}. See rule #5.b for more details.`, allowedMentions: { users: [message.author.id] } }); } else { await message.channel.send({ content: `_sucks message into the void._ ${message.author.toString()} nono, don't post links to young/cub content. See rule #5.b for more details.`, allowedMentions: { users: [message.author.id] } }); } return true; } async function postIdHandler(message: Message, matchedGroups: RegExpExecArray[]): Promise { if (!message.guildId) return true; const posts: { post: E621Post, spoilered: boolean }[] = []; for (const match of matchedGroups) { try { const post = await getE621Post(match[1]); if (post) posts.push({ spoilered: isInSpoilerTags(message.content, match.index), post }); } catch (e) { console.error(e); } } if (await blacklistIfNecessary(message, posts.map(p => p.post))) return false; const skip = await channelIgnoresLinks(message.channel as GuildBasedChannel); if (skip) return true; const sfw = await channelIsSafe(message.channel as GuildBasedChannel); const content = posts.map((postData) => { if (sfw && postData.post.rating != 's') return ` [NSFW] <${getPostUrl(postData.post)}>`; const shouldSpoiler = spoilerOrBlacklist(postData.post); if (shouldSpoiler.action == PostAction.Spoiler) return `${spoiler(getPostUrl(postData.post))} (${shouldSpoiler.tag})`; return postData.spoilered ? spoiler(getPostUrl(postData.post)) : getPostUrl(postData.post); }).join('\n'); if (content.trim().length > 0) return content.trim(); return true; } async function poolIdHandler(message: Message, matchedGroups: RegExpExecArray[]): Promise { if (!message.guildId) return true; const pools: E621Pool[] = []; for (const match of matchedGroups) { try { const pool = await getE621Pool(match[1]); if (pool) pools.push(pool); } catch (e) { console.error(e); } } const posts: E621Post[] = []; try { const res = await getManyE621Posts(pools.map(p => p.post_ids[0])); if (res) posts.push(...res); } catch (e) { console.error(e); } if (await blacklistIfNecessary(message, posts)) return false; const skip = await channelIgnoresLinks(message.channel as GuildBasedChannel); if (skip) return true; const content = pools.map(getPoolUrl).join('\n'); if (content.trim().length > 0) return content.trim(); return true; } async function idHandler(path: string, message: Message, matchedGroups: RegExpExecArray[]): Promise { if (!message.guildId) return true; const skip = await channelIgnoresLinks(message.channel as GuildBasedChannel); if (skip) return true; const content = matchedGroups.map(m => `${config.E621_BASE_URL}/${path}/${m[1]}`).join('\n'); if (content.trim().length > 0) return content.trim(); return true; } async function postHandler(transform: ((idString: string) => number) | null, message: Message, matchedGroups: RegExpExecArray[]): Promise { if (!message.guildId) return true; const posts: E621Post[] = []; for (const match of matchedGroups) { try { const post = await getE621Post(transform ? transform(match[1]) : match[1]); if (post) posts.push(post); } catch (e) { console.error(e); } } if (await blacklistIfNecessary(message, posts)) return false; return true; } async function imageHandler(message: Message, matchedGroups: RegExpExecArray[]): Promise { if (!message.guildId) return true; const posts: E621Post[] = []; for (const match of matchedGroups) { try { const post = await getE621PostByMd5(match[1], false); if (post) posts.push(post); } catch (e) { console.error(e); } } if (await blacklistIfNecessary(message, posts)) return false; const skip = await channelIgnoresLinks(message.channel as GuildBasedChannel); if (skip) return true; const content = posts.map(post => `<${getPostUrl(post)}>`).join('\n'); if (content.trim().length > 0) return content.trim(); return true; } async function poolHandler(message: Message, matchedGroups: RegExpExecArray[]) { if (!message.guildId) return true; const pools: E621Pool[] = []; for (const match of matchedGroups) { try { const pool = await getE621Pool(match[1]); if (pool) pools.push(pool); } catch (e) { console.error(e); } } const posts: E621Post[] = []; try { const res = await getManyE621Posts(pools.map(p => p.post_ids[0])); if (res) posts.push(...res); } catch (e) { console.error(e); } if (await blacklistIfNecessary(message, posts)) return false; return true; } async function githubPullRequestHandler(message: Message, matchedGroups: RegExpExecArray[]): Promise { const skip = await channelIgnoresLinks(message.channel as GuildBasedChannel); if (skip) return true; let content = ''; for (const group of matchedGroups) { content += `${config.GIT_REPO_BASE_URL}/pull/${group[1]}\n`; } if (content.trim().length > 0) return content.trim(); return true; } async function githubIssueHandler(message: Message, matchedGroups: RegExpExecArray[]): Promise { const skip = await channelIgnoresLinks(message.channel as GuildBasedChannel); if (skip) return true; let content = ''; for (const group of matchedGroups) { content += `${config.GIT_REPO_BASE_URL}/issues/${group[1]}\n`; } if (content.trim().length > 0) return content.trim(); return true; }