Building A Simple Python Discord Bot with DiscordPy in 2022/2023

Sharing is caring!

Last Updated on October 29, 2022 by Jay

This tutorial will walk through how to build a Python Discord Bot using the latest DiscordPy library in the year 2022/2023.

Discord had some drastic changes in its APIs in 2021, so most of the existing tutorials on Discord Python bot out there are outdated, which is why I wanted to make this tutorial.

Discord Bot Application

Go to this URL to register your bot:

See the video walkthrough for the details about registering your bot.

Add Bot To Discord Server

Once you have set up the bot successfully, copy the generated URL and paste it into a web-browser, then hit enter. Give the bot access, then your bot will be added to your discord server!

We notice that the bot’s status is showing as offline. Indeed, the bot’s engine will be the Python code!

Install Library

We’ll build the discord bot using the excellent library. We can install the library using pip:

pip install

Code Our First Python Discord Bot

We’ll start off by building a very simple bot that will print out (in terminal/console) messages whenever a user sends a chat in a discord channel.

The below code creates a bare-bone Discord bot:

import discord
from discord.ext import commands

TOKEN = 'your_token_from_discord'

intents = discord.Intents.default()
intents.message_content = True

client = commands.Bot(command_prefix='.', intents=intents)

## Insert other functions here

Note the use of intents in the code. This is in compliance with the new Discord API. After 2022, your bot must have an intent in order to work with Discord. You can think of intents similar to permissions. The bot will only work if we (admin) give it certain permissions.

In the Bot constructor (see the last line of code), by setting command_prefix = ‘.’, we are telling the bot that whenever a user’s message starts with “.” (dot), treat that as a command, we’ll talk about commands in the next section.

Now we have a basic bot, let’s add two functions to it.

The first function on_ready() will print ‘bot ready’ in console when we start the bot.

The second function on_message() will print out the following three pieces of information:

  1. the user (author)’s name who sent the message
  2. the message content
  3. the channel id that the message was sent to
async def on_ready():
    print('bot ready')

async def on_message(message):

    print(, message.content,

Not the above syntax:

  1. We use a decorator before the functions
  2. We use asynchronous functions here by using the async def syntax

Bot React to Commands

Next, let’s make a “Discord command”. We can think of a command similar to a function. A Discord user can type a command in a channel, then the bot receives the command and performs some actions that are defined by our Python function.

To trigger a command, Discord users will need to first type a dot “.”, followed by the command keyword. I’m aware that the “slash” command (i.e. using “/”, or “slash” to trigger commands) is the default way, but I’m using a different approach that also works perfectly fine.

The below function implements a Discord “command”, which users can activate by typing “.hello” into the Discord server. Once bot receives this command, it will send a message “hello there @user” back into the channel to greet the user.

async def hello(ctx):
    channel = client.get_channel(1030698526075785298)

    await channel.send(f'hello there {}')
Python Discord Bot Commands

Next Step – Stable Diffusion Bot!

This guide is long enough at this point. If you have followed through and got your Python Discord bot running successfully, good job! We’ll walk through how to integrate Stable Diffusion with our Discord bot in the next tutorial.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *