Overview
- Introduction
- Video tutorial
- Create a server
- Create an app
- Create a bot account for your app
- Authorize the bot for your server
- Install the python package discord.py
- Run the sample code
- Adding more features to the bot
- Conclusion
- Links
Introduction
This tutorial walks through the process of creating a server, creating a bot, and writing a custom Python script to power the bot.
Also check out part two, available at Make a Discord Bot with Python, Part 2.
If you want to check out some bots that are already made, check out two DevDungeon projects: Chatty Cathy AI chat bot and Help Desk Bot a fun utility bot, both written in Python.
If you are interested in learning how to make a Discord bot in JavaScript, check out the JavaScript Discord Bot Tutorial and check out all of the other Discord related tutorials on DevDungeon.
Video tutorial
Create a server
If you don't already have a server, create one free one at https://discordapp.com. Simply log in, and then click the plus sign on the left side of the main window to create a new server.
Create an app
Go to https://discordapp.com/developers/applications/me and create a new app. On your app detail page, save the Client ID. You will need it later to authorize your bot for your server.
Create a bot account for your app
After creating app, on the app details page, scroll down to the section named bot, and create a bot user. Save the token, you will need it later to run the bot.
Authorize the bot for your server
Visit the URL https://discordapp.com/oauth2/authorize?client_id=XXXXXXXXXXXX&scope=bot but replace XXXX with your app client ID. Choose the server you want to add it to and select authorize.
Install the python package discord.py
Run pip install from your system terminal/shell/command prompt.
python -m pip install discord.py==0.16.12
Run the sample code
Replace the token value with the token from your bot that you saved earlier.
# Work with Python 3.6
import discord
TOKEN = 'XXXXXXXXXX'
client = discord.Client()
@client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
if message.content.startswith('!hello'):
msg = 'Hello {0.author.mention}'.format(message)
await client.send_message(message.channel, msg)
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.run(TOKEN)
After running the Python script, your bot should appear online in the server. You can go type !hello to the bot on Discord and it should respond.
Adding more features to the bot
In part two we add some more features to our bot and demonstrate some of the potential capabilities.
Conclusion
With this knowledge, you should feel comfortable setting up your own very basic bot. To learn how to add more features and extend the bot, check out part 2.
Links
- Part 2 of the Make a Discord bot with Python tutorial
- Discord App
- Discord Developer Portal
- Discord: My Apps page
- Bot authorization
- Discord.py Library
- Discord.py Documentation
- DevDungeon Chatty Cathy AI Discord Chat bot
- DevDungeon Chatty Cathy AI Discord Chat bot - Source
- Python AI chat bot with AIML tutorial