Implement OCR

This commit is contained in:
Chloe Fontenot 🏳️‍⚧️ 2024-08-09 22:56:31 -05:00
parent 84c819f134
commit dc5cfd3d7b

View File

@ -2,6 +2,7 @@
// Require the necessary discord.js classes
const { Client, Events, GatewayIntentBits, REST, Routes, Collection, FLAGS, MessageMentionOptions, codeBlock, SlashCommandBuilder, blockQuote } = require('discord.js');
const Discord = require('discord.js');
import { createWorker } from 'tesseract.js';
const { clientId, guildId, token } = require('./key.json');
let fs = require('node:fs');
let path = require('node:path');
@ -131,29 +132,32 @@ function replyMessage(message, correctedMessage, abbrsUsed) {
}
client.on('messageCreate', message => {
if (typeof message === undefined) {
return;
}
if (message.author.id == "1091120267851485215") {
return;
}
if (global.madlibState.gameChannel == message.channel.id && message.author.id != "1091120267851485215") { // Pass message to madlib game handler if message was sent in the active game channel.
if (global.madlibState.gameChannel == message.channel.id) { // Pass message to madlib game handler if message was sent in the active game channel.
madlibNextMessage(message.content, client);
}
//checkMessage(message, false);
if (message !== undefined) {
colonThree(message);
tylerStop(message);
clenseDegeneracy(message);
}
colonThree(message);
tylerStop(message);
clenseDegeneracy(message);
});
client.on('messageUpdate', (undefined, newMessage) => {
//checkMessage(newMessage, false);
if (typeof message === undefined) {
return;
}
if (message.author.id == "1091120267851485215") {
return;
}
if (typeof message !== undefined) {
return;
}
colonThree(message);
tylerStop(message);
clenseDegeneracy(message);
@ -184,16 +188,12 @@ function getMatchingPhrase(inputString, targetCharacter) {
}
global.clenseDegeneracy = function clenseDegeneracy(message) {
function normalizeString(str) {
const sMap = { '$': 's', '5': 's', 'š': 's', 'ß': 's', 'ʂ': 's', '𝔲': 'u', '𝔰': 's'};
return str.split('').map(char => sMap[char] || char).join('');
}
let checkString = message.content.toLowerCase().normalize("NFKC");
let checkArray = ["ussy", "ussies", "uthy", "yssu", "suuy"];
let checkArray = ["ussy", "ussies", "uthy", "yssu", "suuy", "urssy"];
function checkLoop(message, content) {
console.log("Checking " + content + "...");
for (let i = 0; i < checkArray.length; i++) {
if (content.includes(checkArray[i])) {
for (let i: checkArray) {
if (content.includes(i)) {
console.log("match!");
message.delete();
return true;
@ -206,6 +206,21 @@ function getMatchingPhrase(inputString, targetCharacter) {
}
checkString = checkString.replace(/[^a-zA-Z0-9]/g, '');
checkLoop(message, checkString);
(async () => {
// Does the message have an attachment? Check the attachment with OCR
if (message.attachment.contentType.contains("image")) {
const worker = await createWorker('eng');
const ret = await worker.recognize(message.attachment.proxyURL);
console.log("OCR Results: " + ret.data.text);
checkString = ret.data.text;
await worker.terminate();
if (checkLoop(message, checkString)) {
return;
}
checkString = checkString.replace(/[^a-zA-Z0-9]/g, '');
checkLoop(message, checkString);
}
})();
}
/*
Main bot logic. Check for abbreviations by splitting the message at any non-word characters, then if we find a match, create a new array, replace the abbreviation with the phrase, then call replyMessage() and reply to the original message with the corrected one.