Skip to content

Instantly share code, notes, and snippets.

@manhbi18112005
Last active March 6, 2022 13:46
Show Gist options
  • Save manhbi18112005/99c4fea9424968f8c37c4492fce62f8c to your computer and use it in GitHub Desktop.
Save manhbi18112005/99c4fea9424968f8c37c4492fce62f8c to your computer and use it in GitHub Desktop.
Moderate messages in your servers automatically using Artificial Intelligence. The code below was made to put in index.js, you can modify it to put in an event file.
//...
const GOOGLE_API_KEY = "CóCáiLồnĐịtConBàMày"
const snekfetch = require('snekfetch');
client.on("message", async message => {
if (message.author.bot) return; // return if message was sent from bot
const emojiId = message.content.replace(/<a?:(.*?):+/g, '');
let imageUrl = `https://cdn.discordapp.com/emojis/${emojiId}`;
if(message.content === imageUrl) return; // return if message only contains emojis
const text = message.content;
try {
const { body } = await snekfetch
.post('https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze')
.query({ key: GOOGLE_API_KEY})
.send({
comment: { text },
languages: ['en'],
requestedAttributes: { TOXICITY: {} }
});
const toxicity = Math.round(body.attributeScores.TOXICITY.summaryScore.value * 100);
if (toxicity > 85) {
message.delete();
message.channel.send(`Hey ${message.author}, please do not use toxic words here!`)
}
} catch (err) {
return message.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
})
@manhbi18112005
Copy link
Author

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment