Your First Discord Bot: A Beginner’s Step-by-Step Guide (Python)

Your First Discord Bot: A Beginner’s Step-by-Step Guide (Python)

Ever wanted your own Discord bot but didn’t know where to start? With Python, it’s easier than you think—even if you’re totally new to coding. In this guide, I’ll show you step by step how to create a simple bot for your server. Let’s get started!

For this example we will create a discord bot to send us alerts every time a Boss is about to spawn in a game called Pixel Heroes that runs on the Ronin Chain.

To get started with this bot, you'll need:

  1. Python 3.11: Ensure you have Python 3.11 installed, as it's the best compatible version with discord.py.

  2. Discord Developer Mode: Make sure to toggle the developer mode as shown in the image.

  3. Discord Developer Portal:Go to https://discord.com/developers/applications

    1. Click on New Application
  4. Discord Bot Token: To get your Bot Token you have to click on Bot and after click on Reset Token, this will give you a ned Token !!!IMPORTANT DON’T SHARE THIS TOKEN!!!.

    1. Scroll down to Bot Permissions, and for this example we are only going to check Send Messages in Text Permission

  5. Discord Server: Create your channer for the bot testing

    1. Right click on the channel to get the Channel ID

Once you have all the information listed before you go to the repository github.com/VahagnCh/DiscordBot

Setup Instructions:

  1. Clone the Repository: Download the code from the repository to your local machine.

  2. Install Dependencies: Use pip to install the required packages, primarily discord.py.

  3. Configure Environment Variables: Rename .env.example to .env and input your Discord bot token.

    1. It is important to NOT hardcode your token and use .env file, or for extra security your can encrypt it.
  4. Run the Bot: Execute bot.py to start the bot.

    1. You can edit, update or change totally the cody for your needs and preferences.

And if you want to start from zero I would recommend following the Quickstark guide from discord.py documentation.

# This example requires the 'message_content' intent.

import discord

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

client = discord.Client(intents=intents)

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

Documentation you might need:

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