Creating a Discord Content Safety Detection Bot: A Complete Guide
Written on
Introduction to Content Moderation on Discord
In this tutorial, we will explore the process of programming a Discord bot designed to moderate content within channels. Content moderation is vital for online platforms where diverse individuals interact, ensuring a sense of safety. As online communication becomes increasingly prevalent, the importance of effective moderation cannot be overstated.
Moderation practices have become standard across various social media platforms, especially those reliant on user-generated content. Such practices are prevalent in social media, marketplaces, dating applications, and online forums. This project will specifically target content moderation in voice-driven platforms, making Discord an ideal candidate for our AI-driven content moderator.
Table of Contents:
- Getting Started
- Step 1: Required Libraries
- Step 2: Connecting Your Bot to a Discord Channel
- Step 3: Function Implementation
- Step 4: Discord Commands
- Final Step: Testing the Program
Getting Started
In this project, we will utilize an online API to facilitate content moderation on Discord. Specifically, we will employ AssemblyAI's Speech-to-Text API to analyze audio messages shared in Discord channels. This means that our content safety detection system will identify potentially harmful content in audio or video files, providing details on what was spoken and when.
The trained model can recognize a variety of topics, including gambling, hate speech, drug-related content, negative news, and more. For further details, you can refer to the documentation for the content moderation module.
Step 1: Required Libraries
We will be working within a code editor for this project. The necessary libraries must be installed and imported into our Python program. We will be using five libraries: os, discord, dotenv, requests, and json. The first two libraries come pre-installed with Python, while the other three can be installed using PIP, which allows for easy management of Python libraries. To install them, run the following command in your terminal:
pip install python-dotenv requests discord.py
For reference, here are the official documentation links for these libraries:
- Python-dotenv
- Requests
- Discord.py
Next, let's import these libraries into our program:
import os
import discord
from dotenv import load_dotenv
import requests
import json
Step 2: Connecting Your Bot to a Discord Channel
In this section, we'll focus on creating and connecting a bot to Discord. Although I won't delve deeply into the bot creation process here, you can check out Eric Chi's article, "Build a Discord Bot with Python," for more information.
Once the bot creation is complete, navigate to your project folder and create a new file named .env. This file will store your API keys in the following format:
DISCORD_TOKEN = {Your Discord API Key}
DISCORD_GUILD = {your-guild-name}
Returning to our main program, we will load the environment file we just created:
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
Next, we will define an asynchronous function to synchronize with the Discord API and print a message in the terminal when our bot connects successfully:
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)
At this point, you can run the Python program to ensure that your bot connects without issues. If you see the appropriate connection messages, your bot setup is complete.
Step 3: Function Implementation
With the bot connected, we will now create three essential functions. Here’s what each function will do:
- upload_audio: This function uploads the audio file from your project folder to AssemblyAI's cloud storage, returning the URL of the uploaded audio.
- get_transcript: This function activates the content moderation capability by including the content_safety key set to True. The API will respond with a request URL for tracking the request's status and results.
- check_result: This function allows us to verify the status of the request sent previously. Once completed, it provides the results regarding content safety.
Here are the function definitions:
def upload_audio():
audio_data = "audio_message.mp4"
def up_audio(audio_data, chunk_size=5242880):
with open(audio_data, 'rb') as audio_file:
while True:
data = audio_file.read(chunk_size)
if not data:
breakyield data
headers = {
"authorization": "API Key"}
response = requests.post(endpoint, headers=headers, data=up_audio(audio_data))
return response
def get_transcript():
json = {
"audio_url": upload_url,
"content_safety": True
}
headers = {
"authorization": "API Key",
"content-type": "application/json"
}
response = requests.post(endpoint, json=json, headers=headers)
return response
def check_result(request_id):
headers = {
"authorization": "API Key"}
response = requests.get(request_url, headers=headers)
return response
Step 4: Discord Commands
In this step, we will define commands to trigger the functions we just created. These commands will be initiated through Discord chat, allowing our code to listen for specific keywords. To ensure continuous listening, we will implement an async function.
First, we will establish two global variables to maintain their values across multiple function calls:
response_id = ""
upload_url = ""
Next, we will write the on_message function and define specific keywords:
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')if message.content.startswith('$upload_fun'):
response = upload_audio()
global upload_url
upload_url = response.json()['upload_url']
await message.channel.send(upload_url)
if message.content.startswith('$transcript_fun'):
response = get_transcript()
global response_id
response_id = response.json()['id']
await message.channel.send(response_id)
if message.content.startswith('$check_fun'):
response = check_result(response_id)
response_status = response.json()['status']
await message.channel.send(response_status)
if message.content.startswith('$result_fun'):
response = check_result(response_id)
response_transcript = response.json()['content_safety_labels']
await message.channel.send(response_transcript)
Final Step: Testing the Program
Now that we've set everything up, let's test our bot's functionality! Imagine you receive audio through Discord, and you want to conduct a content safety check before listening to it. For our test, we will use a short clip from a podcast featuring Elon Musk discussing social media. The audio has been renamed to "elon_speech.mp3" and is located in the same project folder as our main program.
Run the main program from your terminal, and once you see the confirmation message indicating that the bot has joined your Discord server, start sending messages to the bot and execute the following commands in order:
$hello
$upload_fun
$transcript_fun
$check_fun
$result_fun
What will you observe? The bot will return the text version of the speech, highlighting potential safety issues. The identified issues may include profanity, weapons, or sensitive social topics, along with a confidence score and timestamps of the flagged content.
Conclusion
Congratulations! You've successfully built a Discord bot that connects with a cloud-based speech-to-text API for content safety detection. The program returns labels and timestamps for potentially unsafe topics discussed in audio files. Future enhancements could include the ability to crop out flagged sections from the original audio or video.
Engaging in such projects allows you to apply your machine learning and AI skills in practical scenarios. I hope you found this guide informative and that you learned something new. Feel free to reach out with any questions!
I am Behic Guven, and I love sharing insights on programming, education, and life. If you enjoy my content, please subscribe to stay inspired!
For more articles, check out:
- Building a Face Recognizer using Python
- Speech Recognition in Real-Time using Python
- Step-by-Step Guide — Building a Prediction Model in Python
In this video, you'll learn how to create a verification system for Discord, essential for managing your server effectively.
This step-by-step tutorial guides you through creating a Discord chatbot on Replit, perfect for beginners looking to dive into bot development.