pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import fetch from "node-fetch"; |
Skyler Grey | 3217998 | 2023-03-07 23:59:06 +0000 | [diff] [blame] | 2 | import { writeFileSync } from "fs"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 3 | import generateFileName from "../utils/temp/generateFileName.js"; |
| 4 | import Tesseract from "node-tesseract-ocr"; |
| 5 | import type Discord from "discord.js"; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 6 | import client from "../utils/client.js"; |
TheCodedProf | b5e9d55 | 2023-01-29 15:43:26 -0500 | [diff] [blame] | 7 | import { createHash } from "crypto"; |
Skyler Grey | 3217998 | 2023-03-07 23:59:06 +0000 | [diff] [blame] | 8 | import * as nsfwjs from "nsfwjs"; |
Skyler Grey | 0a4846c | 2023-03-08 00:32:01 +0000 | [diff] [blame] | 9 | import ClamScan from "clamscan"; |
Skyler Grey | 62da9bf | 2023-03-08 00:11:00 +0000 | [diff] [blame] | 10 | import * as tf from "@tensorflow/tfjs-node"; |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 11 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
| 12 | import getEmojiByName from "../utils/getEmojiByName.js"; |
| 13 | import { ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js"; |
Skyler Grey | d115731 | 2023-03-08 10:07:38 +0000 | [diff] [blame] | 14 | import config from "../config/main.js"; |
Skyler Grey | ea0937b | 2023-03-09 00:36:38 +0000 | [diff] [blame] | 15 | import gm from "gm"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 16 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 17 | interface NSFWSchema { |
| 18 | nsfw: boolean; |
TheCodedProf | 5b53a8c | 2023-02-03 15:40:26 -0500 | [diff] [blame] | 19 | errored?: boolean; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 20 | } |
| 21 | interface MalwareSchema { |
Skyler Grey | 0d88522 | 2023-03-08 21:46:37 +0000 | [diff] [blame] | 22 | malware: boolean; |
TheCodedProf | 5b53a8c | 2023-02-03 15:40:26 -0500 | [diff] [blame] | 23 | errored?: boolean; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 24 | } |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 25 | |
Skyler Grey | 7a966df | 2023-03-09 12:53:57 +0000 | [diff] [blame] | 26 | const nsfw_model = await nsfwjs.load("file://dist/reflex/nsfwjs/example/nsfw_demo/public/model/", { size: 299 }); |
Skyler Grey | d115731 | 2023-03-08 10:07:38 +0000 | [diff] [blame] | 27 | const clamscanner = await new ClamScan().init({ |
| 28 | clamdscan: { |
Skyler Grey | 21f5229 | 2023-03-10 17:58:30 +0000 | [diff] [blame] | 29 | socket: "socket" in config.clamav ? (config.clamav.socket as string) : false, |
| 30 | host: "host" in config.clamav ? (config.clamav.host as string) : false, |
| 31 | port: "port" in config.clamav ? (config.clamav.port as number) : false |
Skyler Grey | d115731 | 2023-03-08 10:07:38 +0000 | [diff] [blame] | 32 | } |
| 33 | }); |
TheCodedProf | d8ef1f3 | 2023-03-06 19:15:18 -0500 | [diff] [blame] | 34 | |
Skyler Grey | 7416964 | 2023-03-09 11:59:09 +0000 | [diff] [blame] | 35 | export async function testNSFW(url: string): Promise<NSFWSchema> { |
| 36 | const [fileStream, hash] = await streamAttachment(url); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 37 | const alreadyHaveCheck = await client.database.scanCache.read(hash); |
Skyler Grey | ea0937b | 2023-03-09 00:36:38 +0000 | [diff] [blame] | 38 | if (alreadyHaveCheck && "nsfw" in alreadyHaveCheck!) { |
| 39 | return { nsfw: alreadyHaveCheck.nsfw }; |
| 40 | } |
TheCodedProf | d8ef1f3 | 2023-03-06 19:15:18 -0500 | [diff] [blame] | 41 | |
Skyler Grey | ea0937b | 2023-03-09 00:36:38 +0000 | [diff] [blame] | 42 | const converted = (await new Promise((resolve, reject) => |
| 43 | gm(fileStream) |
| 44 | .command("convert") |
| 45 | .toBuffer("PNG", (err, buf) => { |
| 46 | if (err) return reject(err); |
| 47 | resolve(buf); |
| 48 | }) |
| 49 | )) as Buffer; |
TheCodedProf | d8ef1f3 | 2023-03-06 19:15:18 -0500 | [diff] [blame] | 50 | |
Skyler Grey | 7416964 | 2023-03-09 11:59:09 +0000 | [diff] [blame] | 51 | const img = tf.node.decodeImage(converted, 3, undefined, false) as tf.Tensor3D; |
Skyler Grey | 0d88522 | 2023-03-08 21:46:37 +0000 | [diff] [blame] | 52 | |
| 53 | const predictions = (await nsfw_model.classify(img, 1))[0]!; |
Skyler Grey | 7416964 | 2023-03-09 11:59:09 +0000 | [diff] [blame] | 54 | img.dispose(); |
TheCodedProf | d8ef1f3 | 2023-03-06 19:15:18 -0500 | [diff] [blame] | 55 | |
Skyler Grey | d115731 | 2023-03-08 10:07:38 +0000 | [diff] [blame] | 56 | const nsfw = predictions.className === "Hentai" || predictions.className === "Porn"; |
| 57 | await client.database.scanCache.write(hash, "nsfw", nsfw); |
| 58 | |
| 59 | return { nsfw }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 60 | } |
| 61 | |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 62 | export async function testMalware(link: string): Promise<MalwareSchema> { |
Skyler Grey | 0a4846c | 2023-03-08 00:32:01 +0000 | [diff] [blame] | 63 | const [fileName, hash] = await saveAttachment(link); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 64 | const alreadyHaveCheck = await client.database.scanCache.read(hash); |
Skyler Grey | 0d88522 | 2023-03-08 21:46:37 +0000 | [diff] [blame] | 65 | if (alreadyHaveCheck?.malware !== undefined) return { malware: alreadyHaveCheck.malware }; |
Skyler Grey | d115731 | 2023-03-08 10:07:38 +0000 | [diff] [blame] | 66 | let malware; |
Skyler Grey | 0a4846c | 2023-03-08 00:32:01 +0000 | [diff] [blame] | 67 | try { |
Skyler Grey | d115731 | 2023-03-08 10:07:38 +0000 | [diff] [blame] | 68 | malware = (await clamscanner.scanFile(fileName)).isInfected; |
Skyler Grey | 0a4846c | 2023-03-08 00:32:01 +0000 | [diff] [blame] | 69 | } catch (e) { |
PineappleFan | 7056a3d | 2023-03-30 20:52:45 +0100 | [diff] [blame] | 70 | return { malware: false }; |
Skyler Grey | 0a4846c | 2023-03-08 00:32:01 +0000 | [diff] [blame] | 71 | } |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 72 | await client.database.scanCache.write(hash, "malware", malware); |
Skyler Grey | 0d88522 | 2023-03-08 21:46:37 +0000 | [diff] [blame] | 73 | return { malware }; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | export async function testLink(link: string): Promise<{ safe: boolean; tags: string[] }> { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 77 | const alreadyHaveCheck = await client.database.scanCache.read(link); |
Skyler Grey | 0d88522 | 2023-03-08 21:46:37 +0000 | [diff] [blame] | 78 | if (alreadyHaveCheck?.bad_link !== undefined) |
| 79 | return { safe: alreadyHaveCheck.bad_link, tags: alreadyHaveCheck.tags ?? [] }; |
| 80 | return { safe: true, tags: [] }; |
| 81 | // const scanned: { safe?: boolean; tags?: string[] } = {} |
| 82 | // await client.database.scanCache.write(link, "bad_link", scanned.safe ?? true, scanned.tags ?? []); |
| 83 | // return { |
| 84 | // safe: scanned.safe ?? true, |
| 85 | // tags: scanned.tags ?? [] |
| 86 | // }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Skyler Grey | 0d88522 | 2023-03-08 21:46:37 +0000 | [diff] [blame] | 89 | export async function streamAttachment(link: string): Promise<[Buffer, string]> { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 90 | const image = await (await fetch(link)).arrayBuffer(); |
Skyler Grey | 3217998 | 2023-03-07 23:59:06 +0000 | [diff] [blame] | 91 | const enc = new TextDecoder("utf-8"); |
Skyler Grey | 0d88522 | 2023-03-08 21:46:37 +0000 | [diff] [blame] | 92 | const buf = Buffer.from(image); |
| 93 | return [buf, createHash("sha512").update(enc.decode(image), "base64").digest("base64")]; |
Skyler Grey | 3217998 | 2023-03-07 23:59:06 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | export async function saveAttachment(link: string): Promise<[string, string]> { |
| 97 | const image = await (await fetch(link)).arrayBuffer(); |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 98 | const fileName = await generateFileName(link.split("/").pop()!.split(".").pop()!); |
TheCodedProf | 5b53a8c | 2023-02-03 15:40:26 -0500 | [diff] [blame] | 99 | const enc = new TextDecoder("utf-8"); |
Skyler Grey | 0d88522 | 2023-03-08 21:46:37 +0000 | [diff] [blame] | 100 | writeFileSync(fileName, new DataView(image)); |
Skyler Grey | 3217998 | 2023-03-07 23:59:06 +0000 | [diff] [blame] | 101 | return [fileName, createHash("sha512").update(enc.decode(image), "base64").digest("base64")]; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 102 | } |
| 103 | |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 104 | const linkTypes = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 105 | PHISHING: "Links designed to trick users into clicking on them.", |
| 106 | DATING: "Dating sites.", |
| 107 | TRACKERS: "Websites that store or track personal information.", |
| 108 | ADVERTISEMENTS: "Websites only for ads.", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 109 | FACEBOOK: "Facebook pages. (Facebook has a number of dangerous trackers. Read more on /privacy)", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 110 | AMP: "AMP pages. (AMP is a technology that allows websites to be served by Google. Read more on /privacy)", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 111 | "FACEBOOK TRACKERS": "Websites that include trackers from Facebook.", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 112 | "IP GRABBERS": "Websites that store your IP address, which shows your approximate location.", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 113 | PORN: "Websites that include pornography.", |
| 114 | GAMBLING: "Gambling sites, often scams.", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 115 | MALWARE: "Websites which download files designed to break or slow down your device.", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 116 | PIRACY: "Sites which include illegally downloaded material.", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 117 | RANSOMWARE: "Websites which download a program that can steal your data and make you pay to get it back.", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 118 | REDIRECTS: "Sites like bit.ly which could redirect to a malicious site.", |
| 119 | SCAMS: "Sites which are designed to trick you into doing something.", |
| 120 | TORRENT: "Websites that download torrent files.", |
| 121 | HATE: "Websites that spread hate towards groups or individuals.", |
| 122 | JUNK: "Websites that are designed to make you waste time." |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 123 | }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 124 | export { linkTypes }; |
| 125 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 126 | export async function LinkCheck(message: Discord.Message): Promise<string[]> { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 127 | const links = |
| 128 | message.content.match( |
| 129 | /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi |
| 130 | ) ?? []; |
| 131 | const detections: { tags: string[]; safe: boolean }[] = []; |
| 132 | const promises: Promise<void>[] = links.map(async (element) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 133 | let returned; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 134 | try { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 135 | if (element.match(/https?:\/\/[a-zA-Z]+\.?discord(app)?\.(com|net)\/?/)) return; // Also matches discord.net, not enough of a bug |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 136 | returned = await testLink(element); |
| 137 | } catch { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 138 | detections.push({ tags: [], safe: true }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 139 | return; |
| 140 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 141 | detections.push({ tags: returned.tags, safe: returned.safe }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 142 | }); |
| 143 | await Promise.all(promises); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 144 | const detectionsTypes = detections |
| 145 | .map((element) => { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 146 | const type = Object.keys(linkTypes).find((type) => element.tags.includes(type)); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 147 | if (type) return type; |
| 148 | // if (!element.safe) return "UNSAFE" |
| 149 | return undefined; |
| 150 | }) |
| 151 | .filter((element) => element !== undefined); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 152 | return detectionsTypes as string[]; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Skyler Grey | 7416964 | 2023-03-09 11:59:09 +0000 | [diff] [blame] | 155 | export async function NSFWCheck(url: string): Promise<boolean> { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 156 | try { |
Skyler Grey | 7416964 | 2023-03-09 11:59:09 +0000 | [diff] [blame] | 157 | return (await testNSFW(url)).nsfw; |
Skyler Grey | 0d88522 | 2023-03-08 21:46:37 +0000 | [diff] [blame] | 158 | } catch (e) { |
Skyler Grey | ea0937b | 2023-03-09 00:36:38 +0000 | [diff] [blame] | 159 | console.log(e); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 160 | return false; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 164 | export async function SizeCheck(element: { height: number | null; width: number | null }): Promise<boolean> { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 165 | if (element.height === null || element.width === null) return true; |
| 166 | if (element.height < 20 || element.width < 20) return false; |
| 167 | return true; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 168 | } |
| 169 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 170 | export async function MalwareCheck(element: string): Promise<boolean> { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 171 | try { |
Skyler Grey | 0d88522 | 2023-03-08 21:46:37 +0000 | [diff] [blame] | 172 | return (await testMalware(element)).malware; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 173 | } catch { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 174 | return true; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 178 | export function TestString( |
| 179 | string: string, |
| 180 | soft: string[], |
| 181 | strict: string[], |
| 182 | enabled?: boolean |
| 183 | ): { word: string; type: string } | null { |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 184 | if (!enabled) return null; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 185 | for (const word of strict) { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 186 | if (string.toLowerCase().includes(word)) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 187 | return { word: word, type: "strict" }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 188 | } |
| 189 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 190 | for (const word of soft) { |
| 191 | for (const word2 of string.match(/[a-z]+/gi) ?? []) { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 192 | if (word2 === word) { |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 193 | return { word: word, type: "soft" }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 197 | return null; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 198 | } |
| 199 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 200 | export async function TestImage(url: string): Promise<string | null> { |
TheCodedProf | ca387af | 2023-04-07 14:42:41 -0400 | [diff] [blame] | 201 | try { |
| 202 | const text = await Tesseract.recognize(url, { |
| 203 | lang: "eng", |
| 204 | oem: 1, |
| 205 | psm: 3 |
| 206 | }); |
| 207 | return text; |
| 208 | } catch { |
| 209 | return null; |
| 210 | } |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 211 | } |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 212 | |
Skyler Grey | 0d88522 | 2023-03-08 21:46:37 +0000 | [diff] [blame] | 213 | export async function doMemberChecks(member: Discord.GuildMember): Promise<void> { |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 214 | if (member.user.bot) return; |
Skyler Grey | 0d88522 | 2023-03-08 21:46:37 +0000 | [diff] [blame] | 215 | const guild = member.guild; |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 216 | const guildData = await client.database.guilds.read(guild.id); |
| 217 | if (!guildData.logging.staff.channel) return; |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 218 | const [loose, strict] = [guildData.filters.wordFilter.words.loose, guildData.filters.wordFilter.words.strict]; |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 219 | // Does the username contain filtered words |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 220 | // Does the nickname contain filtered words |
Samuel Shuert | eb13a1b | 2023-04-10 14:30:47 -0400 | [diff] [blame] | 221 | let nameCheck; |
TheCodedProf | 122b062 | 2023-04-21 22:45:53 -0400 | [diff] [blame^] | 222 | if (member.nickname) { |
Samuel Shuert | eb13a1b | 2023-04-10 14:30:47 -0400 | [diff] [blame] | 223 | nameCheck = TestString(member.nickname ?? "", loose, strict, guildData.filters.wordFilter.enabled); |
| 224 | } else { |
| 225 | nameCheck = TestString(member.user.username, loose, strict, guildData.filters.wordFilter.enabled); |
| 226 | } |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 227 | // Does the profile picture contain filtered words |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 228 | const avatarTextCheck = TestString( |
Skyler Grey | e9c3ef6 | 2023-03-09 14:09:00 +0000 | [diff] [blame] | 229 | (await TestImage(member.displayAvatarURL({ forceStatic: true }))) ?? "", |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 230 | loose, |
| 231 | strict, |
| 232 | guildData.filters.wordFilter.enabled |
| 233 | ); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 234 | // Is the profile picture NSFW |
Skyler Grey | 0d88522 | 2023-03-08 21:46:37 +0000 | [diff] [blame] | 235 | const avatar = member.displayAvatarURL({ extension: "png", size: 1024, forceStatic: true }); |
Skyler Grey | 7416964 | 2023-03-09 11:59:09 +0000 | [diff] [blame] | 236 | const avatarCheck = guildData.filters.images.NSFW && (await NSFWCheck(avatar)); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 237 | // Does the username contain an invite |
Skyler Grey | 1443271 | 2023-03-07 23:40:50 +0000 | [diff] [blame] | 238 | const inviteCheck = guildData.filters.invite.enabled && /discord\.gg\/[a-zA-Z0-9]+/gi.test(member.user.username); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 239 | // Does the nickname contain an invite |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 240 | const nicknameInviteCheck = |
Skyler Grey | 1443271 | 2023-03-07 23:40:50 +0000 | [diff] [blame] | 241 | guildData.filters.invite.enabled && /discord\.gg\/[a-zA-Z0-9]+/gi.test(member.nickname ?? ""); |
TheCodedProf | 122b062 | 2023-04-21 22:45:53 -0400 | [diff] [blame^] | 242 | if (nameCheck !== null || avatarCheck || inviteCheck || nicknameInviteCheck || avatarTextCheck !== null) { |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 243 | const infractions = []; |
Samuel Shuert | f545714 | 2023-04-10 14:34:15 -0400 | [diff] [blame] | 244 | if (nameCheck !== null) { |
| 245 | infractions.push(`Name contains a ${nameCheck.type}ly filtered word (${nameCheck.word})`); |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 246 | } |
| 247 | if (avatarCheck) { |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 248 | infractions.push("Profile picture is NSFW"); |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 249 | } |
| 250 | if (inviteCheck) { |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 251 | infractions.push("Username contains an invite"); |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 252 | } |
| 253 | if (nicknameInviteCheck) { |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 254 | infractions.push("Nickname contains an invite"); |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 255 | } |
| 256 | if (avatarTextCheck !== null) { |
| 257 | infractions.push( |
Skyler Grey | e9c3ef6 | 2023-03-09 14:09:00 +0000 | [diff] [blame] | 258 | `Profile picture contains a ${avatarTextCheck.type}ly filtered word (${avatarTextCheck.word})` |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 259 | ); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 260 | } |
| 261 | if (infractions.length === 0) return; |
| 262 | // This is bad - Warn in the staff notifications channel |
| 263 | const filter = getEmojiByName("ICONS.FILTER"); |
| 264 | const channel = guild.channels.cache.get(guildData.logging.staff.channel) as Discord.TextChannel; |
| 265 | const embed = new EmojiEmbed() |
| 266 | .setTitle("Member Flagged") |
| 267 | .setEmoji("ICONS.FLAGS.RED") |
| 268 | .setStatus("Danger") |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 269 | .setDescription( |
| 270 | `**Member:** ${member.user.username} (<@${member.user.id}>)\n\n` + |
| 271 | infractions.map((element) => `${filter} ${element}`).join("\n") |
| 272 | ); |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 273 | const buttons = [ |
| 274 | new ButtonBuilder() |
| 275 | .setCustomId(`mod:warn:${member.user.id}`) |
| 276 | .setLabel("Warn") |
| 277 | .setStyle(ButtonStyle.Primary), |
| 278 | new ButtonBuilder() |
| 279 | .setCustomId(`mod:mute:${member.user.id}`) |
| 280 | .setLabel("Mute") |
| 281 | .setStyle(ButtonStyle.Primary), |
TheCodedProf | 1cfa1ae | 2023-03-11 16:07:37 -0500 | [diff] [blame] | 282 | new ButtonBuilder().setCustomId(`mod:kick:${member.user.id}`).setLabel("Kick").setStyle(ButtonStyle.Danger), |
| 283 | new ButtonBuilder().setCustomId(`mod:ban:${member.user.id}`).setLabel("Ban").setStyle(ButtonStyle.Danger) |
| 284 | ]; |
Samuel Shuert | f545714 | 2023-04-10 14:34:15 -0400 | [diff] [blame] | 285 | if (nameCheck !== null) |
TheCodedProf | 1cfa1ae | 2023-03-11 16:07:37 -0500 | [diff] [blame] | 286 | buttons.concat([ |
| 287 | new ButtonBuilder() |
| 288 | .setCustomId(`mod:nickname:${member.user.id}`) |
| 289 | .setLabel("Change Name") |
| 290 | .setStyle(ButtonStyle.Primary) |
| 291 | ]); |
| 292 | if (avatarCheck || avatarTextCheck !== null) |
| 293 | buttons.concat([ |
| 294 | new ButtonBuilder().setURL(member.displayAvatarURL()).setLabel("View Avatar").setStyle(ButtonStyle.Link) |
| 295 | ]); |
| 296 | const components: ActionRowBuilder<ButtonBuilder>[] = []; |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 297 | |
| 298 | for (let i = 0; i < buttons.length; i += 5) { |
TheCodedProf | 1cfa1ae | 2023-03-11 16:07:37 -0500 | [diff] [blame] | 299 | components.push( |
| 300 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 301 | buttons.slice(i, Math.min(buttons.length - 1, i + 5)) |
| 302 | ) |
| 303 | ); |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 304 | } |
| 305 | |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 306 | await channel.send({ |
| 307 | embeds: [embed], |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 308 | components: components |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 309 | }); |
| 310 | } |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 311 | } |