A program that when running can respond to different events like:
- someone sending a message
- somebody going online/offline
- someone reacting to a message
- someone joining a server
- someone joining/leaving a voice channel
Discord made something called an API that lets us send and receive information to have a bot run. People have written packages of code in many different languages that we can use to write bots, then. One of the most popular ones is written in Python, called discord.py. If you like other languages better, you can write a Discord bot with it!
Discord makes you register your bot before hand, and bans you from turning your actual user account into a bot! We must go to Discord's Developers page and create a bot user in order to start.
https://discord.com/developers/applications
By making a bot with Discord you accept their Developer Terms of Service
Imagine if every bot somebody made had total control over a Discord server. That'd be a security nightmare! Bots, just like users, have permissions that limit what they can and can't do on a server. After creating your bot, go to the OAuth2 tab to choose the permissions you want your bot to start with.
- Under "Scopes" check
bot
- Now a whole tab below will appear with permissions!
- Only check the permissions you need. The less, the better!
- Now you can copy the link that appears under the "Scopes" tab.
- It will look something like this:
https://discord.com/api/oauth2/authorize?client_id=xxxxxxxxxxxxxxxxxx&permissions=231488&scope=bot
- It will look something like this:
When we register a bot with Discord, it gives us a secret token. This is basically the username AND password for our bot, so we must keep it super super safe. If anyone accidentally sees this, they can take over your bot entirely.
Python doesn't automatically come with code for working with Discord, so we must download and install it! Luckily it just takes one command at the command line:
- Linux/Mac:
python3 -m pip install -U discord.py
- Windows:
py -3 -m pip install -U discord.py
And now you can write the actual Python program!
import discord
client = discord.Client()
@client.event
async def on_ready():
print(f'We have logged in as {client.user}')
@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 token here')
This is called a "decorator" and tells Python that the function underneath it is an "event handler". This simply means that it is a function that will "handle" an event once it occurs by running. The function has to be named after the event, and you can see the events here
Normal Python programs have functions that will pause your entire program until they complete and move on. These are called "synchronous". This would not work well for Discord bots that might need to handle many events at once! Therefore, we mark our event handlers as async
(asynchronous) which tells Python that they can be started and Python can move on to the next thing before waiting for them to complete. Most of the discord.py functions like sending a message require you to use await
as they are also async
functions. Do not forget to await
the discord.py functions or they will not run and you'll probably get weird errors!
Once you have written a program for it, you must run the bot program with Python on your computer. You'll need to be in a Terminal/Commnand Prompt in the same folder as your Python program. Replace bot.py
with whatever you named your file!
python3 bot.py