Introduction
In this tutorial, we’ll walk through the process of creating a Discord bot using Python. Discord is a popular platform for gamers and communities to communicate, and creating a bot can enhance your server’s functionality in various ways.
Prerequisites
Before we begin, ensure you have the following installed:
- Python (https://www.python.org/)
- Discord.py library (https://discordpy.readthedocs.io/en/stable/)
You’ll also need a Discord account and permissions to create a bot and add it to a server.
Steps
-
Set Up a Discord Application
- Go to the Discord Developer Portal (https://discord.com/developers/applications) and create a new application.
- Navigate to the “Bot” tab and click “Add Bot”.
- Customize your bot’s name, profile picture, etc., as desired.
-
Get Your Bot’s Token
- In the “Bot” tab of your application, you’ll find a “Token” section. Click “Copy” to copy your bot’s token.
-
Install Discord.py
- Open your terminal or command prompt.
- Run the following command to install the Discord.py library:
pip install discord.py
- Write Your Bot Code
- Create a new Python file (e.g., bot.py) and open it in your favorite text editor.
- Write your bot’s code using Discord.py. Here’s a simple example to get you started
# bot.py
import discord
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!hello'):
await message.channel.send('Hello!')
client.run('YOUR_BOT_TOKEN_HERE')
Replace ‘YOUR_BOT_TOKEN_HERE’ with the token you copied earlier.
- Run Your Bot
- Save your Python file.
- Run the following command in your terminal to start your bot
python bot.py
Your bot should now be online and responding to commands in your Discord server!