From 37b411a025e411b654c2ade54fd4cf6eaf0b475f Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Fri, 31 Mar 2023 12:10:42 -0500 Subject: [PATCH] First working build:tm: --- abbreviation_key.json | 2 +- commands/addPhrase.ts | 1 - main.ts | 55 +++++++++++++++++++++++++++++++++++-------- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/abbreviation_key.json b/abbreviation_key.json index a786c8b..99b6f0d 100644 --- a/abbreviation_key.json +++ b/abbreviation_key.json @@ -1 +1 @@ -{"target_phrases":{"test":"test2","test3":"test4","test5":"test6"}} \ No newline at end of file +{"target_phrases":{"idk":"I don't know", "hoco":"homecoming","bbl":"be back later","bbg":"baby girl","mb":"my bad", "mfw":"my face when"}} \ No newline at end of file diff --git a/commands/addPhrase.ts b/commands/addPhrase.ts index 7eb5704..f7e54c9 100644 --- a/commands/addPhrase.ts +++ b/commands/addPhrase.ts @@ -30,7 +30,6 @@ function addPhrase(abbrevation, phrase) { console.log(abbrevation, phrase); abbreviationKey.target_phrases[abbrevation] = phrase; console.log(abbreviationKey); - updatePhraseList(); // Write data to file var jsonString = JSON.stringify(abbreviationKey); diff --git a/main.ts b/main.ts index 64b3640..ec8a470 100644 --- a/main.ts +++ b/main.ts @@ -68,26 +68,61 @@ client.on(Events.InteractionCreate, async interaction => { // My code var abbreviationKey = require("./abbreviation_key.json"); -function updatePhraseList() { - abbreviationKey = require("./abbreviation_key.json"); + +function arrayRotate(arr, reverse, amount) { + for (var i = 0; i < amount; i++) { + if (reverse) arr.unshift(arr.pop()); + else arr.push(arr.shift()); + return arr; + } } +function matchAbbr(abbrTarget) { + for (var abbr in abbreviationKey.target_phrases) { + if (abbreviationKey.target_phrases[abbr] === abbrTarget) { + return abbr; + } else { + return ""; + } + } +} +function replyMessage(message, correctedMessage) { + message.reply("Your message contains an acronym! Let me fix that for you: \n `" + correctedMessage + "1"); +} client.on('messageCreate', message => { console.log(`${message.author.tag} in #${message.channel.name} sent: ${message.content}`); if (message.author.bot) { return; } - var messageArray = message.content.toLowerCase().split(/[ ,!@#$%^&*()]+/); - console.log(messageArray); - for (let i = 0; i < messageArray.length; ++i) { - if (abbreviationKey.target_phrases[messageArray[i]] != undefined) { - console.log("Found an abbreviation!"); - break; + var matchMessageArray = message.content.toLowerCase().split(/[ ,!@#$%^&*()]+/); + + console.log(matchMessageArray); + + for (let i = 0; i < matchMessageArray.length; ++i) { + if (abbreviationKey.target_phrases[matchMessageArray[i]] !== undefined) { + //Return key + var phrase = abbreviationKey.target_phrases[matchMessageArray[i]]; + var abbr = matchAbbr(phrase); + + console.log("Found abbreviation: " + abbr); + console.log("Phrase that matches used acronym: " + phrase); + var rebuildMessageArray = message.content.split(new RegExp(abbr, 'i')); + arrayRotate(rebuildMessageArray, true, 1); + rebuildMessageArray.unshift(phrase); + arrayRotate(rebuildMessageArray, true, 2); + console.log(rebuildMessageArray); + // Build into string and set to variable! + var correctedMessage = ""; + for (let j = 0; j < rebuildMessageArray.length; j++) { + correctedMessage += rebuildMessageArray[j]; + } + console.log(correctedMessage); + replyMessage(message, correctedMessage); + //break; } } - message.reply("Your message contains an acronym. Here's what your message would say without it: \n " + ""); - console.log(abbreviationKey.target_phrases[messageArray[0]]); + } );