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