Skip to content

Latest commit

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Welcome to this workshop! We're going to build a Discord bot with discord.py and Python. Let's dive right into it!

Build a Discord Bot

Prerequisites

  • Python and Pip
  • A Discord account

Step 1: Create a Bot Account

  1. Make sure you’re logged on to the Discord website.

  2. Navigate to the application page

  3. Click on the “New Application” button.

    The new application button.

  4. Give the application a name and click “Create”.

    The new application form filled in.

  5. Create a Bot User by navigating to the “Bot” tab and clicking “Add Bot”.

    The Add Bot button.

    • Click “Yes, do it!” to continue.
  6. If you want others to invite your bot, make sure that Public Bot is ticked. Also, make sure that Require OAuth2 Code Grant is unchecked.

    How the Bot User options should look like for most people.

  7. Copy the token using the “Copy” button.

    • NOTE: This is not the same thing as the Client Secret on the General Information page
    • WARNING: This token is equivalent to your bot's password, so do not share it with anyone.

Step 2: Invite Your Bot

  1. Go to the “OAuth2” tab

    How the OAuth2 page should look like.

  2. Tick the “bot” checkbox under “scopes”

    The scopes checkbox with "bot" ticked.

  3. Tick the permissions required for your bot to function under “Bot Permissions”

    The permission checkboxes with some permissions checked.

  4. Now the resulting URL can be used to add your bot to a server. Copy and paste the URL into your browser, choose a server to invite the bot to, and click “Authorize”.

Source: https://discordpy.readthedocs.io/en/latest/discord.html

Step 3: Setup Your Environment

  1. Install or upgrade discord.py: pip3 install -U discord.py
    • NOTE: you may need to use a variation of the pip command depending on your platform.
  2. Create a Python file and name it anything, such as coolio_bot.py
  3. Open the file in your favorite code editor

Step 4: Let's Get Coding

  1. Start off by importing the discord.py library:

    importdiscordclient=discord.Client()
    # Add event handlers hereclient.run('Your token from Step 1')
  2. Listen to the ready event

    # Event handlers@client.eventasyncdefon_ready():
    print('We have logged in as {0.user}'.format(client))
  3. Listen for a new message:

    @client.eventasyncdefon_message(message):
    ifmessage.author==client.user:
    returnifmessage.content.startswith('$hello'):
    awaitmessage.channel.send('Hello!')

Final Bootstrap Code:

importdiscordclient=discord.Client()
# Event handlers@client.eventasyncdefon_ready():
print('We have logged in as {0.user}'.format(client))
@client.eventasyncdefon_message(message):
ifmessage.author==client.user:
returnifmessage.content.startswith('$hello'):
awaitmessage.channel.send('Hello!')
client.run('Your token from Step 1')

Step 5: Start Hacking

So now that you've got your bot set up, what's next? Well, of course, start building! Take the next 30 minutes and see what you can come up with. Be ready to share your bot's commands with others for them to try out what you make!

Examples

Add a command

Let's say we want to add a new command. Here's what you would do:

@client.eventasyncdefon_message(message):
ifmessage.author==client.user:
returnifmessage.content.startswith('$hello'):
awaitmessage.channel.send('Hello!')
elifmessage.content.startswith('$hola'):
awaitmessage.channel.send('Hola amigo!')

Echo

A simple echo bot:

@client.eventasyncdefon_message(message):
ifmessage.author==client.user:
returnawaitmessage.channel.send(message.content)

Send a random picture

When someone sends $pic, send a link to a random picture from https://picsum.photos/. Note: the ? + randrange() is just for making discord fetch a new picture everytime you send the same link.

fromrandomimportrandrange
...
@client.eventasyncdefon_message(message):
ifmessage.author==client.user:
returnifmessage.content.startswith('$pic'):
awaitmessage.channel.send('https://picsum.photos/400/400?'+str(randrange(1000)))

About

Learn how to build a Discord bot! 🤖

Resources

Stars

0 stars

Watchers

3 watching

Forks

Releases

Packages

Contributors