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

it works, thank you

Thank you very much for the feedback, wouldn't you mind leaving a star? :)

@wmealing
Copy link

I dont know much about API keys, but you probably dont want to leave that API key as valid.

@manhbi18112005
Copy link
Author

I dont know much about API keys, but you probably dont want to leave that API key as valid.

I can't share that API key, so I decided to insert it with a quote from our nations. :>
You can register your own Google API keys by searching Google. It's will be very helpful. 😍
Thank you for reading this. :3

@wmealing
Copy link

Ah cool. I don't need the API key, just didnt want you to end up with a big bill for someone using it crypto mining.

Have a good one.

@manhbi18112005
Copy link
Author

:)

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