Now that you’ve got a basic Discord bot up and running, it’s time to enhance its functionality by adding the ability to collect user information and store it in an SQLite3 database. This part of the tutorial will guide you through creating a feature where your bot collects a user’s Discord ID, name, email, and whether they are interested in beta testing an application.
1. Setting Up SQLite3
Before we begin, you’ll need to set up SQLite3, a lightweight database that comes bundled with Python. This database will be used to store the user information collected by your bot.
In your discord_bot_project
directory, create a new file called database.py
and add the following code to set up your database:
import sqlite3
def init_db():
conn = sqlite3.connect('discord_bot.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS users
(user_id TEXT PRIMARY KEY, name TEXT, email TEXT, beta_tester TEXT)''')
conn.commit()
conn.close()
def add_user(user_id, name, email, beta_tester):
conn = sqlite3.connect('discord_bot.db')
c = conn.cursor()
c.execute("INSERT INTO users (user_id, name, email, beta_tester) VALUES (?, ?, ?, ?)",
(user_id, name, email, beta_tester))
conn.commit()
conn.close()
def get_user(user_id):
conn = sqlite3.connect('discord_bot.db')
c = conn.cursor()
c.execute("SELECT * FROM users WHERE user_id = ?", (user_id,))
user = c.fetchone()
conn.close()
return user
This code sets up the database with a table called users
that stores the user ID, name, email, and beta tester status. The add_user
function inserts new user data into the database, and get_user
retrieves information from the database.
2. Collecting User Information
Next, we’ll update your bot to collect information from users. Open bot.py
and add the following code to handle user input:
import discord
from discord.ext import commands
from database import init_db, add_user, get_user
TOKEN = 'your_token_here'
bot = commands.Bot(command_prefix='!')
# Initialize the database
init_db()
@bot.event
async def on_ready():
print(f'We have logged in as {bot.user}')
@bot.command()
async def register(ctx):
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
await ctx.send('What is your name?')
name = await bot.wait_for('message', check=check)
await ctx.send('What is your email?')
email = await bot.wait_for('message', check=check)
await ctx.send('Are you interested in beta testing the application? (yes/no)')
beta_tester = await bot.wait_for('message', check=check)
user_id = str(ctx.author.id)
add_user(user_id, name.content, email.content, beta_tester.content)
await ctx.send(f'Thank you, {name.content}! Your information has been recorded.')
Here’s a breakdown of what’s happening in this code:
- Event Handling: The
register
command triggers the bot to ask the user for their name, email, and whether they want to be a beta tester. - Data Collection: The bot collects the responses and uses
add_user
to store the information in the SQLite3 database. - Confirmation: After collecting and storing the data, the bot confirms back to the user that their information has been recorded.
3. Running Your Bot
To test this new functionality, run your bot using python bot.py
. In your Discord server, type !register
and follow the prompts. The bot will guide you through entering your name, email, and beta testing preference.
After the process is complete, your bot will store the collected information in the SQLite3 database and confirm it back to the Discord channel.
4. Verifying Data Collection
You can verify that the data was stored correctly by querying the SQLite3 database directly. Open a Python shell and run:
from database import get_user
print(get_user('your_user_id_here'))
Replace 'your_user_id_here'
with the actual Discord user ID that was collected during the registration process. This command will print out the stored information, confirming that it was correctly recorded.
Thank you for following along with this tutorial. We hope you found it helpful and informative. If you have any questions, or if you would like to suggest new Python code examples or topics for future tutorials/articles, please feel free to join and comment. Your feedback and suggestions are always welcome!
You can find the same tutorial on Medium.com.