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 // Require the necessary discord.js classes
const { Client, Events, GatewayIntentBits, REST, Routes, Collection, FLAGS, MessageMentionOptions, codeBlock, SlashCommandBuilder, blockQuote } = require('discord.js'); const { Client, Events, GatewayIntentBits, REST, Routes, Collection, FLAGS, MessageMentionOptions, codeBlock, SlashCommandBuilder, blockQuote } = require('discord.js');
const Discord = require('discord.js'); const Discord = require('discord.js');
import { createWorker } from 'tesseract.js';
const { clientId, guildId, token } = require('./key.json'); const { clientId, guildId, token } = require('./key.json');
let fs = require('node:fs'); let fs = require('node:fs');
let path = require('node:path'); let path = require('node:path');
@ -131,29 +132,32 @@ function replyMessage(message, correctedMessage, abbrsUsed) {
} }
client.on('messageCreate', message => { client.on('messageCreate', message => {
if (typeof message === undefined) {
return;
}
if (message.author.id == "1091120267851485215") { if (message.author.id == "1091120267851485215") {
return; 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); madlibNextMessage(message.content, client);
} }
//checkMessage(message, false); //checkMessage(message, false);
if (message !== undefined) { colonThree(message);
colonThree(message); tylerStop(message);
tylerStop(message); clenseDegeneracy(message);
clenseDegeneracy(message);
}
}); });
client.on('messageUpdate', (undefined, newMessage) => { client.on('messageUpdate', (undefined, newMessage) => {
//checkMessage(newMessage, false); //checkMessage(newMessage, false);
if (typeof message === undefined) {
return;
}
if (message.author.id == "1091120267851485215") { if (message.author.id == "1091120267851485215") {
return; return;
} }
if (typeof message !== undefined) {
return;
}
colonThree(message); colonThree(message);
tylerStop(message); tylerStop(message);
clenseDegeneracy(message); clenseDegeneracy(message);
@ -184,16 +188,12 @@ function getMatchingPhrase(inputString, targetCharacter) {
} }
global.clenseDegeneracy = function clenseDegeneracy(message) { 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 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) { function checkLoop(message, content) {
console.log("Checking " + content + "..."); console.log("Checking " + content + "...");
for (let i = 0; i < checkArray.length; i++) { for (let i: checkArray) {
if (content.includes(checkArray[i])) { if (content.includes(i)) {
console.log("match!"); console.log("match!");
message.delete(); message.delete();
return true; return true;
@ -206,6 +206,21 @@ function getMatchingPhrase(inputString, targetCharacter) {
} }
checkString = checkString.replace(/[^a-zA-Z0-9]/g, ''); checkString = checkString.replace(/[^a-zA-Z0-9]/g, '');
checkLoop(message, checkString); 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. 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.