Discord Bot Integration
This guide demonstrates how to integrate SafeComms into a Discord bot to automatically filter toxic messages in real-time. Using our low-latency API (<500ms), you can intercept and delete harmful content before it disrupts your community.
01. REQUIREMENTS
- > Node.js v16.9.0 or higher
- > A Discord Bot Token (from Discord Developer Portal)
- > A SafeComms API Key (Get one free here)
02. INSTALLATION
Install the required packages:
npm install discord.js @safecomms/sdk
# OR
yarn add discord.js @safecomms/sdk03. IMPLEMENTATION
Create a new file `bot.js` and add the following code:
const { Client, GatewayIntentBits } = require('discord.js');
const { SafeComms } = require('@safecomms/sdk');
// > INITIALIZE_SYSTEMS
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
const safecomms = new SafeComms({
apiKey: process.env.SAFECOMMS_API_KEY
});
client.on('messageCreate', async (message) => {
// > IGNORE_VULNERABLE_TARGETS (Bots)
if (message.author.bot) return;
try {
// > SCAN_CONTENT
const analysis = await safecomms.text.analyze({
content: message.content,
profileId: 'default' // Optional: Use custom profile
});
// > EVALUATE_THREAT_LEVEL
if (analysis.flagged) {
console.log(`[THREAT_DETECTED] User: ${message.author.tag} | Type: ${analysis.primaryCategory}`);
// Action 1: Delete the toxic message
await message.delete();
// Action 2: Warn the user
const warning = await message.channel.send(
`> WARNING: Message removed. Reason: ${analysis.primaryCategory}`
);
// Auto-cleanup warning details after 5s
setTimeout(() => warning.delete(), 5000);
// Action 3: Log to SafeComms (Auto-logged by API)
}
} catch (error) {
console.error('> SYSTEM_ERROR:', error);
}
});
client.login(process.env.DISCORD_TOKEN);> OPTIMIZATION_TIP
For high-volume servers, consider using our Batch API or implementing a queue system to handle rate limits efficiently. SafeComms handles thousands of concurrent requests, but Discord has its own rate limits for message deletion.