How To Make A Discord Bot With Python(discord.py)

Canturk bal
4 min readJul 6, 2022

--

I think that you all use discord and know about discord bots. So did you ever wonder how they are made?

To make a discord bot we have a library called discord.py. It is a library that allows us to code a discord bot easily.To install discord.py open your terminal and write this:

pip install discord.py

Now let’s create a discord application.

  1. Go to the developer portal and login.
  2. Click “New Application”
  3. Enter a name (select “Personal” for team unless you’re using Teams)
  4. Add a bot to the new application by going to the “Bot” settings on the left and clicking “Add Bot”
  5. To invite your bot go to Oauth2 and select bot option. After that select your bot’s permissions.
  6. copy the link of your bot,paste it and add it to a selected server.
  7. When you make your bot you will be granted with a bot token.DON’T EVER GIVE IT TO SOMEONE. Because that is how discord knows which discord bot are you editing or making. With that token they can do anything to your bot.

Now Lets start coding

First we import the library that we need to make a discord bot.

import discord

After that we choose our intents to our bot. Intents are like permissions to your bot. there are 2 types intents:

  1. normal intents
  2. privileged intents

Normal intents are intents that are important but not that serious. Like reacting to messages,sending messages,etc.

privileged intents are intents that are important permissions like kicking,banning etc.To enable Privileged Intents go to your discord developer page and enter your bot’s page:

I am going to enable all of them.

To set intent on our code we use syntax like this:

import discordintents = discord.Intents.default()
intents.typing = False
intents.presences = False

To set all permission write this:

import discord
intents = discord.Intents.all()

Now let’s make our bot:

import discord
intents = discord.Intents.all()
bot = discord.Client(intents=intents)#setting our intents to all #intents

EVENTS

We use decorators to modify a function.Event decorator is there to register this event as an actual discord event. discord.py revolves around the concept of events. An event is something you listen to and then respond to. For example you want to make a code that prints that bot is online in terminal. This is a example about that:

import discord
intents = discord.Intents.all()#making all intents avaliable
bot = discord.client(intents = intents)#making the bot
@bot.event #using events
async def on_ready(): #a event concept
print("bot is online") #when bot is online print it is online
bot.run("enter your token")

We use async function to make a function in discord.py

Now let’s make a command that send a dm when a new person joins to the server.We use priviliged intents in this event.

@bot.eventasync def on_member_join(member):#everytime a members come
#setting member as a parameter
guild = member.guild #guild is synyonym of server guild_name = guild.name #setting guild name to a var dmchannel = await member.create_dm()
#creating the new member's dm channel
await dmchannel.send(f"welcome to {guild_name}")
#sending a welcome message to new member as a dm
bot.run("enter your token")

We use await because this function is Coroutine made , meaning it’s a asynchronous function .

Commands

commands are the things that we want our bot to make. For example we want our bot to play rock,paper and scizzors with us or kick and ban,etc. We can do those things with commands.

To make command we want to set our command prefix. This is a symbol that we want to call our bot with.Also we are going to import another library.

So to set our command prefix we use this code:

import discord
from discord.ext import commands
intents = discord.Intents.all()bot = commands.Bot(command_prefix = "!r ",intents = intents)
#setting our bot command prefix

Now we make our command:

@bot.command()async def ping(context):  await context.send("pong")

Command decorator is there to register this event as an actual discord command.after that we define our function and the result of it.When we write command prefix with command name it will send pong to the channel.

context:Represents the context in which a command is being invoked under.

We use context parameter to represent which command is being invoked under and it has attributes like channel,author,bot,etc.

Now we understand the basics of command.Let’s make a coinflip command.

I used embeds in this code.An Embed object is another component of Discord messages that can be used to present data with special formatting and structure. It looks better and much ordered. You can also put images to them

I made a list that contains two options:heads and tails. after that i defined a variable that picks a random element of the list i made.So when i combined these with a simple if and elif command and send it with await context.send

And that is the basics.If you want to learn more you can go to documentation website:

--

--