Skip to content

Instantly share code, notes, and snippets.

@funnbot
Last active August 25, 2017 04:03
Show Gist options
  • Save funnbot/b6ba6928f87207ab4ea61339844e9b15 to your computer and use it in GitHub Desktop.
Save funnbot/b6ba6928f87207ab4ea61339844e9b15 to your computer and use it in GitHub Desktop.
auto correct self bot
/*
Create auth.js in same dir with module.exports = "token"
*/
const spc = require('spellchecker');
const {
Client
} = require("discord.js");
const client = new Client({
messageCacheMaxSize: 10,
disabledEvents: ["TYPING_START"]
});
client.on("ready", () => console.log("Started."));
client.on("message", async m => {
if (m.author.id !== client.user.id) return;
let edited = getSpellingSuggestions(m.content);
console.log(edited)
edited = edited.suggestion ? edited.suggestion : m.content
if (edited === m.content) return;
try {
await m.edit(edited);
} catch (e) {}
});
client.login(require("./auth.js"));
function getSpellingSuggestions(str) {
var misspellings = false,
output = {},
suggestion = [],
corrections = {};
output.original = str;
var words = str.split(' ');
var lastChar = getEnding(words[words.length - 1])
var word, noPunctuation, correctSpelling, hasMistakes, hasCorrections;
for (var i = 0; i < words.length; i++) {
word = words[i];
noPunctuation = word.replace(/\W/g, '');
if (getEnding(word)) {
word = word.slice(0, -1)
}
if (spc.isMisspelled(word)) {
hasMistakes = true;
correctSpelling = spc.getCorrectionsForMisspelling(word);
if (correctSpelling.length) {
hasCorrections = true;
corrections[word] = correctSpelling;
} else {
corrections[word] = null;
}
}
}
for (correction in corrections) {
if (correction && corrections[correction]) {
var regex = new RegExp(correction, 'g');
str = str.replace(regex, corrections[correction][0]);
}
}
if (hasMistakes) {
output.suggestion = hasCorrections ? str : null;
output.corrections = corrections;
} else {
output.suggestion = false;
}
return output;
}
function getEnding(str) {
var lastChar = str.slice(-1);
if (!lastChar.match(/^[0-9a-z]+$/)) {
return lastChar;
} else {
return false;
}
}
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment