Skip to content

Instantly share code, notes, and snippets.

@Apexal
Last active March 27, 2021 14:09
Show Gist options
  • Save Apexal/31662c0a2b3b9b8fe5b26d00a8552a83 to your computer and use it in GitHub Desktop.
Save Apexal/31662c0a2b3b9b8fe5b26d00a8552a83 to your computer and use it in GitHub Desktop.
Python Discord Bot Quickstart

Creating a Discord Bot in Python

What is a Discord bot?

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

How do we create Discord bots?

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!

How do we register our bot?

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

Permissions and Adding the Bot

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.

  1. Under "Scopes" check bot
    • Now a whole tab below will appear with permissions!
  2. Only check the permissions you need. The less, the better!
  3. 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

What is the token?

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.

Preparing Python

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!

Code

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')

What is @client.event

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

What is async and await?

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!

Running the Bot

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

Documentation

https://discordpy.readthedocs.io/en/latest/index.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment