Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automod: Censor images #573

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions modules/automod/automod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@ import warn from "../punishments/warn.js";
import tryCensor, { badWordRegexps, badWordsAllowed } from "./misc.js";
import { stripMarkdown } from "../../util/markdown.js";
import { joinWithAnd } from "../../util/text.js";
import { createWorker } from "tesseract.js";
const worker = await createWorker("eng");
TheYande marked this conversation as resolved.
Show resolved Hide resolved
async function getMessageImageText(message: Message): Promise<string> {
if (message.attachments.size === 0) {
return ""; // No images
}
TheYande marked this conversation as resolved.
Show resolved Hide resolved

const imageUrls: string[] = Array.from(message.attachments.values())
TheYande marked this conversation as resolved.
Show resolved Hide resolved
.filter((attachment) => attachment.width && attachment.height)
TheYande marked this conversation as resolved.
Show resolved Hide resolved
.map((attachment) => attachment.url);

if (imageUrls.length === 0) {
return "";
}
TheYande marked this conversation as resolved.
Show resolved Hide resolved

const imageTextPromises: Promise<string>[] = imageUrls.map(async (url) => {
TheYande marked this conversation as resolved.
Show resolved Hide resolved
const ret = await worker.recognize(url || "");
RedGuy12 marked this conversation as resolved.
Show resolved Hide resolved
return ret.data.text;
});

const imageTextResults = await Promise.all(imageTextPromises);
return imageTextResults.join(" ");
TheYande marked this conversation as resolved.
Show resolved Hide resolved
}

const WHITELISTED_INVITE_GUILDS = new Set([
config.guild.id,
Expand Down Expand Up @@ -120,6 +143,7 @@ export default async function automodMessage(message: Message): Promise<boolean>

const badWords = [
tryCensor(stripMarkdown(message.content)),
tryCensor(await getMessageImageText(message)),
...message.stickers.map(({ name }) => tryCensor(name)),
...invites.map(([, invite]) => !!invite?.guild && tryCensor(invite.guild.name)),
].reduce(
Expand Down
214 changes: 213 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading