[Python] How to make Discord bot? (Beginner Friendly)
Tags: python, programming
Published: 2021-06-27 16:36:25

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!

{% github https://github.com/Yuma-Tsushima07/Python-Tutorial-Bot %}

## 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`.

```bash
pip install discord.py
```
or

```bash
pip3 install discord.py
```
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.

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

```py
import discord
client = discord.Client()
```
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:

```python
import discord
client = discord.Client()

@client.event
async def on_ready():
    print("The bot is ready!")
```

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:

```python
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"))
```
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_presence `method 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.

```py
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
```

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.

```py
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")
```

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!

```py
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)
```
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 
{% github https://github.com/Yuma-Tsushima07/Yuma-Tsushima07 %}

### Follow me!
* Github: https://github.com/Yuma-Tsushima07
* Medium: https://0xv37r1x3r.medium.com/
* SoundCloud: https://soundcloud.com/0c7av3h4ck5

### Discord Servers!!
**Bounty Hunters**: An amazing bug hunting community full of developers and exploiters!!!
* Link: https://discord.gg/J5PsgKqdWq

**CyberArtByte**: My server full of bling and joy!!
* Link: https://discord.com/invite/mNAWykv67W

### New Soundcloud Track!!

{% soundcloud https://soundcloud.com/0c7av3h4ck5/hack-with-your-heart-0c7av3h4ck5 %}

Author: Yuma-Tsushima