DEV Community

Cover image for [Python] How to make Discord bot? (Beginner Friendly)
Yuma-Tsushima
Yuma-Tsushima

Posted on

[Python] How to make Discord bot? (Beginner Friendly)

Ever wanted to make a Discord Bot but failed or didn't understand how? Must have kept your heads scraching for a while!

Never fear! I will guide you through on how to make a bot using python.

Remember to install python and discord or use discord on your browser.

Before you keep reading: give me a ✨ on my repo <3!


Introduction

What will you need?

Firstly, before you start you will need an IDE or code editor!
Use something like Visual Studio Code, Sublime Text or Atom.
Then you once you have installed Discord or opened it.
Make a server and make sure its organised so you know what you are doing. Use the Discord Developers Portal to make a bot.

Since you’re learning how to make a Discord bot with Python, you’ll be using discord.py.

Discord.py is a Python library that exhaustively implements Discord’s APIs in an efficient and Pythonic way. This includes utilizing Python’s implementation of Async IO.

Use pip to install discord.py.

pip install discord.py
Enter fullscreen mode Exit fullscreen mode

or

pip3 install discord.py
Enter fullscreen mode Exit fullscreen mode

Now that you’ve installed discord.py, we will begin!

Coding the Bot

Now, we can move on to the actual coding of the bot. Open up a new .py file in whatever IDE you prefer, and import the discord library.

import discord
Enter fullscreen mode Exit fullscreen mode

Next, we need to establish our Discord client. The bot client that is connecting to discord.

import discord
client = discord.Client()
Enter fullscreen mode Exit fullscreen mode

Now, this is where the fun begins. Within the discord.py library, there are certain events that the bot is able to respond to, including when a message is sent and when the bot is ready. Take this example:

import discord
client = discord.Client()

@client.event
async def on_ready():
    print("The bot is ready!")
Enter fullscreen mode Exit fullscreen mode

Now, whenever the bot is ready for use after startup, it will print out The bot is ready!. It runs anything that is in the function. Cool, right? We can use this to do other things, too, such as set a play status for the bot, as so:

import discord
client = discord.Client()

@client.event
async def on_ready():
    print("The bot is ready!")
    await client.change_presence(game=discord.Game(name="Making a    bot"))
Enter fullscreen mode Exit fullscreen mode

This may look a little bit confusing, but hear me out. We have to put “await” in front of it since it is a coroutine, which basically allows multi-tasking so the bot can respond to different events at the same time. Got it? So, when we call the change_presencemethod on the Discord client, it changes the presence of the client to show that it is playing the game “Making a bot”, since we created a Game object with the name “Making a bot”.

Next, we can get on to actually making bot commands. We detect if a message is sent with this function that takes the parameter “message”, which will be equal to the message that was sent. We don’t want the bot to be able to respond to itself in any circumstances, so if the author of the message is equal to the client, we stop the function.

import discord
client = discord.Client()

@client.event
async def on_ready():
    print("The bot is ready!")
    await client.change_presence(game=discord.Game(name="Making a    bot"))

@client.event
async def on_message(message):
    if message.author == client.user:
        return
Enter fullscreen mode Exit fullscreen mode

We can make some commands that our bot can respond to now. For starters, we’ll make a really simple thing, so if we say “Hello” the bot will say “World” back.

import discord
client = discord.Client()

@client.event
async def on_ready():
    print("The bot is ready!")
    await client.change_presence(game=discord.Game(name="Making a    bot"))

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content == "Hello":
        await client.send_message(message.channel, "World")
Enter fullscreen mode Exit fullscreen mode

First, we check if the content of the message is equal to “Hello”. If it’s anything else, the bot will not respond with “World”. Then, the send_message method takes two parameters, the channel to send the message to, and the message to send (in the form of a string). The bot awaits until it sends this message, then looks for another response. Amazing! We just made our first bot using discord.py, but how can we connect it to our server now? Well, there’s one more piece of code we forgot.

At the end of the code, you MUST add the client.run() function, which runs the bot. It only takes one parameter, which is your bot’s token, which you should have seen earlier. Put this token in, and your bot should start up when you run the program!

import discord
client = discord.Client()

@client.event
async def on_ready():
    print("The bot is ready!")
    await client.change_presence(game=discord.Game(name="Making a    bot"))

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content == "Hello":
        await client.send_message(message.channel, "World")
client.run(TOKEN)
Enter fullscreen mode Exit fullscreen mode

Thank you for reading it would be really nice to look around bellow at my credits <33.
Comment if you want more tutorials like this!

Credits

Welcome to Yuma-Tsushima's Github page!

Visitor count

Support Discord Discord SoundCloud


About Myself

Hello, my name is Yuma-Tsushima (frequently shortened to Yuma). I am an ambitious coder, and I enjoy coding in JavaScript (mainly). I also love making websites using HTML, CSS and (of course) JS! I started programming (self taught) at the age of 13. Originally, I came from the creative field ; I draw/sing/animate/make music.


Talents and Hobbies

I love drawing (I have been drawing all of my life). I play strategy games, I code and I do CTFs! I am also good at animation, making AMVs and image editing. My favourite game is Mindustry, followed by Flow Free and Sudoku. I love watching anime (I love Code Geass - I relate to Lelouch a lot) and I aspire to create my own anime!

Drawing Music Digital Artwork ICT Electronics Desgins Web dev Strategy

Check out my work!:
❯ Soundcloud : 0c7av3h4ck5
❯ Discord : {CyberArtByte}
❯ Artwork : AcceleratorArts07

Recent Medium

Follow me!

Discord Servers!!

Bounty Hunters: An amazing bug hunting community full of developers and exploiters!!!

CyberArtByte: My server full of bling and joy!!

New Soundcloud Track!!

Author: Yuma-Tsushima

Top comments (0)