Skip to content

Instantly share code, notes, and snippets.

@manhbi18112005
Last active February 4, 2025 17:20
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.
This code snippet is used to detect toxic messages in a Discord server using Google's Perspective API. It checks the toxicity of a message and deletes it if it exceeds a certain threshold. The message author is then notified about the toxic content. The code uses regular expressions to remove emojis and emotes from the message content before anaโ€ฆ
/*
Author: Le Ngo Duc Manh
ModifiedDate: 2024-10-14
Description: This code snippet is used to detect toxic messages in a Discord server using Google's Perspective API. It checks the toxicity of a message and deletes it if it exceeds a certain threshold. The message author is then notified about the toxic content. The code uses regular expressions to remove emojis and emotes from the message content before analyzing it for toxicity. The Google API key is required to make requests to the Perspective API. The maximum allowed toxicity threshold can be adjusted as needed.
*/
const axios = require('axios');
const GOOGLE_API_KEY = "Your Google API Key";
const EMOJIS_REGEX = /<a?:([^:]+):(\d+)>|\p{Emoji_Presentation}|\p{Extended_Pictographic}/gmu;
/* This is the maximum threshold */
const MAX_ALLOWED_TOXICITY_THRESHOLD = 85;
/** Check if a string is empty or only contains emojis
*
* @param {string} str The string to check
* @returns {boolean} Whether the string is empty
*/
function isEmptyMessage(str) {
return !str?.length;
}
/** Format the content of a message to remove emojis and emotes
*
* @param {string} str the string to format
* @returns {string} The formatted string
*/
function formatContent(str) {
return str.replace(EMOJIS_REGEX, '').trim();
}
module.exports = async (message) => {
// We skip checking bot message for less calling to the API
if (message.author.bot) return;
// This check if a message content is empty or not, if it is then quick return
const messageContent = formatContent(message.content);
if (isEmptyMessage(messageContent)) return;
try {
const response = await axios.post(
'https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze',
{
comment: { text: messageContent },
languages: ['en'],
requestedAttributes: { TOXICITY: {} }
},
{
params: { key: GOOGLE_API_KEY }
}
);
// round the score received to an int from 0 to 100
const toxicity = Math.round(response.data.attributeScores.TOXICITY.summaryScore.value * 100);
// if larger, do the executions you want.
if (toxicity > MAX_ALLOWED_TOXICITY_THRESHOLD) {
await message.delete();
// send a warning message to the channel in which message was sent from
message.channel.send(`Hey ${message.author}, please do not use toxic words here!`);
}
} catch (err) {
console.error(`Error analyzing message: ${err.message}`);
}
};
@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

Just gave it a fresh update today. ๐Ÿ˜™

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