Discord Bot Development
What is a bot?
A bot is a software application that can communicate with users or systems on the internet. Bots are a useful tool for automating repetitive tasks and customizing the experiences you deliver. We encounter bots almost everywhere on the web. One common example is the chat-bot. This type of bot facilitates communications between businesses and customers.
In this quest, you will use Python in a real-world application. Your task is to create a basic Discord bot using Python.
Getting Started
This tutorial will teach you how to set up a bot using Discord’s developer portal to get a unique bot token, set up your bot’s name and profile picture, and finally show you how to invite your bot to a Discord server.
The guide will serve as a step-by-step instruction manual. You are encouraged to write down your questions and interact within our Discord community for further assistance and deeper explanations.
Quest Instructions
Creating a Discord Application
Start by heading to https://discord.com/developers/applications
Make sure that you’re logged into your Discord account. If you don’t have a Discord account, you can create one here.
Discord supports different software applications. These software applications communicate with Discord through its Application Programming Interface (API).
You can find Discord’s API documentation here.
Note: The App icon is not the bot’s profile picture. This icon is used to distinguish your bot from other applications on the developer’s portal.
Declaring a Bot Application
The most important part of creating a Discord bot is generating your unique bot TOKEN. You should always keep this token secret to prevent other users from impersonating your bot to perform unwanted tasks.
Note: You can set your bot’s custom profile picture and username from here!
Scroll down and make sure to enable all intents as shown in the image below.
Inviting the Bot to Your Discord Server
Invite your bot to a Discord server that you can manage (admin rights).
You can create your own Discord server easily as well.
We’ve only given this bot permission to send messages. Other functionalities will require us to give additional permissions in the future.
Copy the invite URL as shown below and paste it into your browser.
Select the server you would like to invite the bot to from the drop-down menu.
Your bot should have joined the server, but wait!!
We still need to bring it online!
Coding Your Discord Bot
We’ll be using https://replit.com/ to code and host our Discord bots!
Create an account and create your first Repl!
We must install the discord.py package as shown below.
We must create our secret variable with our bot’s token.
Don’t worry, the bot token key shown here has already been reset. Make sure to always keep your bot’s token secret.
Making a secret variable is important because it allows us to share code without disclosing our token!
We will provide the base template for your Discord bot, but it is up to you to bring it online using your unique Token from the previous steps!
copy the code and get ready to paste it in your repl!
#required module for secret variables
import os
#discord.py module to connect to the discord API
import discord
# a module that allows us to create custom Discord commands
from discord.ext import commands
# configures the bot intents parameter to enable all. This must match with the bot configuration in the Discord Developer Portal
intents = discord.Intents.all()
#tells the bot which prefix precedes a command
client = commands.Bot(command_prefix = '!', intents = intents, case_insensitive = True)
#assigns the secret token to a variable called "my_secret"
my_secret = os.environ['TOKEN']
@client.event
async def on_ready():
print('Ready!')
@client.command()
async def ping(ctx):
await ctx.send('pong!')
#runs the bot file using the unique bot token
client.run(my_secret)
Running Your Discord Bot
Paste the bot’s code into the repl editor and click the green play button to run your bot’s code.
The bot should now appear “online” on Discord!
Testing Your Bot
This simple bot is known as the !ping-pong! bot. It will reply with pong! whenever you type !ping into a chat that is visible to your bot.
Congratulations! You made it!
Join our Discord community!
We hope you enjoyed making your own Discord bot! join our Discord community where you can make friends, play games and let us know what you’d like to learn next!