** BEST USED TO FILTER PROFANITY FROM YOUR SERVER ** Perspective API allows you to have the best chatting environment without toxic messages. The bot will automatically delete messages that contain toxic messages. Perspective returns a percentage that represents the likelihood that someone will perceive the text as toxic. Messages with a 70% or greater toxicity score will be deleted. You can modify the percentage on line 40! Add your guild id on line 10. Credits to Kangabru for helping me out with the code!
// authenticates you with the API standard library
// type `await lib.` to display API autocomplete
//Added Comments in case you haven't read the Description!
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const {google} = require('googleapis');
let API_KEY = `${process.env.APIKEY}`;
let message = context.params.event.content;
if (context.params.event.guild_id === 'YOUR_GUILD_ID_HERE') {
//if you want it to work for every servers, remove === 'YOUR_GUILD_ID_HERE' and it should be working just fine.
const toxicity = await google
.discoverAPI(
'https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1'
)
.then((client) => {
const analyzeRequest = {
comment: {
text: message,
},
requestedAttributes: {
TOXICITY: {},
},
};
return new Promise((resolve, reject) => {
client.comments.analyze(
{
key: API_KEY,
resource: analyzeRequest,
},
(err, response) => {
if (err) reject(err);
resolve(response.data.attributeScores.TOXICITY.summaryScore.value);
}
);
});
});
console.log(`Toxicity`, toxicity);
if (toxicity > 0.7) {
//you can decide how toxic the word will be. Even if the language is slightly rude for the Google API, it will delete it when it is at 0.7 or 70% of toxicity.
await lib.discord.channels['@0.2.0'].messages.destroy({
message_id: `${context.params.event.id}`,
channel_id: `${context.params.event.channel_id}`,
});
await lib.discord.channels['@0.2.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `Please use appropriate words to describe your sentences better!😬 it would make a huge difference for our community and to the user experience!🥰`,
});
}
}