How to make a simple discord bot with python in less than 5 minutes
--
Hello! Here I’m going to be showing you how to make a simple discord bot with Python!
I’ve simplified the tutorial as much as I could, thus if you find me elaborating too much, feel free to skip ahead!
For resources -
- if you want to find other discord bot developers, join this discord server
- if you would like to advertise your bot and get it into more servers, check this website out.
Requirements
- Python > 3.5.3 (Anything above 3.5.3 except for 3.6.0)
- Basic Python Knowledge and Understanding
- A Discord account (duh!)
Contents
Creating your bot’s account
- How to create a Discord application and bot account
- How to get an invite link for your discord bot
Adding your bot to your server
- How to add your bot to your server
Coding your bot
- Downloading the required packages for your bot
- Writing a basic bot structure
Resources
- A list of resources you can use to make your bot better
Getting Started
1. Creating your bot’s account
Alright, let’s get started! First, you need to make your bot client/account. So head over to the discord developer portal and login with your discord user account! (I’m assuming you already have a discord user account for yourself)
Your portal should look something like this -
Now, here’s how you create your bot -
- Click on the ‘New Application’ button on the top right corner and then enter a name for your Bot Application, and click on ‘Create’
You should now be on a page that looks something like this -
- Click on ‘Bot’ on the left navigation bar and click on the ‘Add Bot’ button on the top right. Keep note of the token they provide for the bot (we’ll be using it later), and never give it to someone else!
- Once you add your bot to the application, head over to the OAuth2 page via the left navigation bar.
- Check ‘bot’ in the Scopes section, scroll down and select the permissions your bot would need. Then proceed to copy the link provided. You now have a link to invite your bot to your server!
Build better voice apps. Get more articles & interviews from voice technology experts at voicetechpodcast.com
2. Adding your bot to your server
Now as you’ve got hold of a handy bot invite, you have to add your bot to your server to test out everything you do!
Copy the link you received from the last step in ‘Creating your bot’s account’ and open it in your browser.
- Select the server you want to add your bot to, and then click on ‘Continue’. Authorize the permissions and complete the Captcha to prove you’re human!
Your bot should now be on your server!
3. Coding your bot
You now have your bot on your server! Unfortunately, it’s offline and doesn’t do anything… let’s fix that right now! I expect, you already have python3 and pip package manager installed.
You now need to install the discord API library (discord.py) for python and then you can get into coding your bot.
Open command prompt (or terminal, depending on your OS) and run the following commands.
Run python -V to get to know what version of python you are running. Make sure you have a version from the requirements list above!
After that, run pip install discord.py to download the discord python module. The installation process might vary and will not look exactly as it is in the provided photo!
Now, you have everything you need to make your bot! It’s time to code!
Start by importing the discord library into your code file.
import discord
from discord.ext import commands
Now, you need to create a bot instance, with the command prefix you desire. A prefix is what you use to run commands with your bot. For example, if you run a help command !help, ! is the prefix for your bot.
bot = commands.Bot(command_prefix='!')
Now, we’re going to add a basic event to your bot. In simple terms, an event is something that is triggered when a certain activity is completed. Let me demonstrate!
@bot.event
async def on_ready():
print("The bot is ready!")
Now, when the bot is ready (thus, on_ready()), it will run this function (i.e output “The bot is ready!”).
Now let’s add a simple command that sends you a message when you run it
@bot.command()
async def hello(ctx):
await ctx.send("Hello!")
You just need to add one last bit! Run the bot! (Replace token with the token you got from the developer portal earlier!
bot.run('token')
Your final code should now look something like this -
import discord
from discord.ext import commandsbot = commands.Bot(command_prefix='!')@bot.event
async def on_ready():
print("The bot is ready!")@bot.command()
async def hello(ctx):
await ctx.send("Hello!")bot.run('Njk2ODI3NzMwNzU0MjczMzEw.XoykQA.VMAvV9CrWeQ_SE8EPVnkc0j9rAs') # Don't reveal your bot token, regenerate it asap if you do
Let’s try this out! Run the bot as you would run any other script! (python3 file.py)
A couple of seconds after I ran the bot, it printed out “The bot is ready!” looks like our bot is working so far!
Your bot should now be online and have two commands -
!help and !hello, let’s check!
As you can see, the bot is working perfectly!
Resources
Alright, that’s it for the tutorial. I know! It was probably a huge disappointment, but no, that’s not all you can do with a discord bot. I simply cannot make a text tutorial large enough to show everything you can do with a discord bot and that's why I’m recommending better resources that can explain it!
First, a link to the discord.py documentation. You should also join the discord.py support server, they can help you out, but read the documentation first!
Alright, this is all for the tutorial! If you need help with anything, feel free to shoot me a message on discord at trustedmercury#2007 and I’ll do my best to be of assistance!