Last active
December 25, 2016 07:54
-
-
Save appellation/98825f40dddb32e9c7677c8547598454 to your computer and use it in GitHub Desktop.
Only practical for Discord selfbots. Use with Discord.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rp = require('request-promise-native'); | |
const dns = require('dns'); | |
const os = require('os'); | |
function checkSpelling(client, text) { | |
return new Promise((resolve, reject) => { | |
dns.lookup(os.hostname(), (err, address, family) => { | |
if(err) return reject(err); | |
resolve(address); | |
}); | |
}).then(ip => { | |
rp = rp.defaults({ | |
baseUrl: 'https://api.cognitive.microsoft.com/bing/v5.0', | |
headers: { | |
'Ocp-Apim-Subscription-Key': process.env.BING, | |
'X-MSEdge-ClientID': client.user.id, | |
'X-Search-ClientIP': ip | |
}, | |
method: 'post' | |
}); | |
return rp({ | |
uri: 'SpellCheck', | |
qs: { | |
mode: 'proof', | |
text: text | |
} | |
}); | |
}).then(JSON.parse).then(res => { | |
let corrected = text; | |
let offset = 0; | |
for(const correction of res.flaggedTokens) { | |
let start = correction.offset + offset; | |
let end = start + correction.token.length; | |
const suggestion = correction.suggestions[0].suggestion; | |
offset += suggestion.length - correction.token.length; // how much shorter or longer the string has become through replacement. | |
if(suggestion.length === 0) { | |
offset--; | |
start--; | |
} | |
corrected = corrected.substring(0, start) + suggestion + corrected.substring(end); | |
} | |
return corrected; | |
}); | |
} | |
module.exports = checkSpelling; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment