Create a Discord Bot with Python in Minutes

Let us walk you through the step-by-step process of creating a Discord both with Python and deploying it in minutes.

November 11, 2021 4 Min Read
Create a Discord Bot with Python in Minutes
Create a Discord Bot with Python in Minutes

Codesphere

From everyone in the Codesphere Team:)

Table of Contents

If there’s any platform that’s become the darling of online communities, it's Discord. Its intuitive UX, vibrant communities, and diverse feature set have allowed it to grow rapidly in recent years.

Among developers at least, a lot of the love for Discord comes from its high programmability. In this tutorial, we will walk you through the process of creating a simple interactive Discord Bot with Python that you can add to your servers. So, without further ado let's start coding.


Setting Up Your Bot in the Developer Portal

To get started with bot creation, follow these steps:

  • First head to https://discord.com/developers/applications
  • Create a New Application
  • Now, head to the “Bot” tab and create a new bot
  • Finally, to add Your bot to a server, go to the oAuth2 tab, scroll down to scopes, check bot and visit the generated URL.
A video of steps to set up a discord bot using Python
A video of steps to set up a discord bot

You can then select the server you want to add the bot to and you should see it on your server under offline users

Visual of how your server looks on discord 

That’s all we need to do on the Discord side!


Project Setup for Our Discord Bot

Now let’s start coding the bot.

1- First, install the discord.py library by running the following command:

pip install discord

2- Now create a new file called main.py

3- With that in place, we'll proceed to validate our Discord access using the command:

You can find your authentication token in the Bot Tab in your developer portal.


Responding to Messages

Now let’s read and respond to messages that people send in channels

We need to include the bot.process_commands at the end in order to make sure the bot also checks if the message is a valid command.

And here you can see it working:


Custom Command Scripting

Now let’s add commands. Commands are just responses that are specifically invoked. We designated at the beginning that each command has to start with ‘!’, which will act as the “Alexa” to start listening for a command.

We are going to add two commands, one that returns the square of a number, and one that tells us the scrabble points for a specific word.

And here we can see it working!


Deploying Your Discord Bot

You’re not going to want to be running your script from your local machine 24/7. That’s why the next step is to deploy your script to the cloud.

If you are looking for the easiest solution to deploy apps to the cloud try out Codesphere. On Codesphere, you can just import your project and run it from a virtual machine as if it was being deployed locally. We provide 3 free workspaces for you to get started.

Zero config cloud made for developers

From GitHub to deployment in under 5 seconds.

Sign Up!

Review Faster by spawning fast Preview Environments for every Pull Request.

AB Test anything from individual components to entire user experiences.

Scale globally as easy as you would with serverless but without all the limitations.

It only takes minutes to deploy! That's how easy it is to create a discord bot using Python and get it running.

Did you enjoy building it with us? Let us know down below!

About the Author

Create a Discord Bot with Python in Minutes

Codesphere

From everyone in the Codesphere Team:)

We are building the next generation of Cloud, combining Infrastructure and IDE in one place, enabling a seamless DevEx and eliminating the need for DevOps specialists.

More Posts