Tech With Tim Logo
Go back

Setup and Introduction

Discord.py Rewrite

At the time of creating this tutorial there is two versions of Discord.py. One is an older stable version and the other is known as Discord.py rewrite. For this tutorial we will use the rewrite version as it is newer and will be used in the future.

Note: Currently Discord.py rewrite is only supported on Python 3.6 and lower.

Setting up Our Environment

There are many different ways to go about installing Discord.py rewrite but I am choosing do to so through Anaconda and to use PyCharm as my IDE. The steps that need to be followed are outlined below.

  • Download/Install Pycharm

  • Download/Install Anaconda

  • Create a new conda enviorment using python 3.6

  • Install discord.py into that environment using pip (pip install -U git+https://github.com/Rapptz/discord.py@rewrite)

  • Configure the pycharm interpreter to use the environment you created

  • Import Discord.py

Creating a Anaconda Enviorment

After downloading Anaconda we need to create a conda environment that will hold all of our packages.

To do this we need to open our command prompt and type the following. Note: "disc" can be any name you want.

conda create -n disc python=3.6

kuva_2023-04-23_183620043.png

After the environment has been created we can activate by doing the following. If you named your environment something else activate that name.

activate disc

kuva_2023-04-23_183643376.png

Installing Discord.py Rewrite

To install the correct version of discord.py we need to clone a GitHub repository containing it. Once your environment is activated type the following.

pip install -U discord.py[voice] kuva_2023-04-23_183710646.png

Setting Up PyCharm

If you haven't done so already please download and install Pycharm. PyCharm is an IDE(Integrated Development Environment) that has many useful features and tools we can utilize. However, it does require a bit of setup.

Once you've downloaded PyCharm you need to create a new project and configure the interpreter.

Step 1: Create a new project and select the existing interpreter option. Then click the three dots on the right (outlined in red below). kuva_2023-04-23_183751435.png

Step 2: Select conda environment and again click the three dots. kuva_2023-04-23_183812414.png

Step 3: Find your Anaconda folder (default location is C:\Users\Your_User\Anaconda) select the "envs" folder. Now navigate to your environment (mine is called disc). Inside your environment folder select pythonw.exe. Now click OK until you are redirected to the project view inside PyCharm. kuva_2023-04-23_183833510.png

Now that our interpreter is setup all we need to do is create a new python file and create a configuration for it.

To create a new python file right click on your project folder from the left file menu and click New > Python File. I named my file bot.py but you can name it whatever you'd like. kuva_2023-04-23_183914747.png

Next we will setup a configuration to run the script we just created. To do this we need to click "Add Configuration" in the top right hand corner of PyCharm. The we will click the + icon and select python. kuva_2023-04-23_183943390.png

Next we need to select the script we want to run. We will need to navigate to the file and select it. Also ensure that your project interpreter is set to Project Default (Python 3.6). You can name the configuration anything you want. kuva_2023-04-23_184020495.png

Now we have setup pycharm and are ready to create our discord bot!

Creating a Discord Bot

Now we need to create a new Discord Bot from the discord developer portal.

You will need to sign in with discord and should see something that looks like this. kuva_2023-04-23_184106369.png Yours will likely not have any applications already created.

Now we are going to create a new application. I named mine "Test Server". Once you click create take note of the client ID and click on the BOT tab from the left menu bar. kuva_2023-04-23_184155722.png Here we will need to create a BOT. I already have one created but you will likely have to click something that says "create bot". Next you need to add some permissions to your bot. I recommend allowing it to Read and Send Messages at a minimum. kuva_2023-04-23_184228545.png You will also need to copy the BOT token.

Creating a Discord Server

Now that we have created a discord bot we need to add it to our server. To do this we need a discord server! If you do not already have one simply go to discord and select the plus icon and create one.

Inviting Our Bot

Here is where we are going to need our BOTS client ID. To add your discord bot to your server you need to invite it. The easiest way of doing this is by replacing the text "ID_HERE" with the client ID of you bot in the link below and pasting it into your browser.

https://discordapp.com/oauth2/authorize?client_id=ID_HERE&scope=bot&permissions=0

Once you open this link in your browser you can select your discord server and hit "Authorize".

If you did this correctly you should see the bot appearing in your discord server member list.

Connecting to Our Bot

Now we can get into the fun stuff! Writing code for our bot. At this point we will need our Bots token.

To connect to our bot we will need to setup a client using our bots token.

import discord

client = discord.Client()
client.run("YOUR BOT TOKEN GOES HERE")

Now once you run this script you should see your bot appearing online in your discord server.

Sending and Receiving Messages

Discord Rewrite works off an event system. This means functions will be called each time an event occurs. To read user messages we need to use the on_message event.

import discord

client = discord.Client()

@client.event
async def on_message(message):
    print(message.content) # Now every message sent will be printed to the console


client.run("token")

To send a message we can do the following.

import discord

client = discord.Client()

@client.event
async def on_message(message):
    if message.content.find("!hello") != -1:
        await message.channel.send("Hi") # If the user says !hello we will send back hi


client.run(token)
Design & Development by Ibezio Logo