Learn to Code via Tutorials on Repl.it!

← Back to all posts
DISCORD.PY TUTORIAL [POG] MAKE YOUR OWN DISCORD BOT!! [POG]
h
elipie (353)

Hello peoples

Today I will be teaching you some discord.py stuff. Its gonna be epiiccc!!

Lets get into it :))

Number one VERY IMPORTANT

Create an app then make bot.

Learn more here < pls go to that link its really important

Lets get started! Make a folder named src, and create two files named: bot.py and keep_alive.
Write the following code in main.py:

import src
from src import bot 
from src import keep_alive 

Yay you just imported those files from the src folder!

Lets go into the FUN stuff.

Oh wait nevermind, we need the .env!
Create a file called .env. Write this in your .env:
token=YourBotTokenHere
IMPORTANT: IT DOESN'T HAVE TO BE IN THE FOLDER

If you do not know how to get your bot token go here.

Lets go in bot.py, and setup our starter framework! Yes our prefix will be ~

import discord 
import os 
import random

token = os.environ.get("token")
client = discord.Client()
botname = "LearnBot"
@client.event
async def on_message(message):
    if message.author == client.user:
        return

    elif message.content.startswith('~help'):
      
        msg = f'Hi, I am {botname}!\nI am **A GREAT** bot that likes to Help people out!\n`Commands:`\nPrefix:`~`\nHelp: `~help`\nSay hello:`~hello`\n'.format()#add more later 

        await message.channel.send(msg)

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

client.run(token)

Woah that was a lot
Lets break it down

  • First, Gets token with token = os.environ.get("token")
  • Second, It defines client and botname
  • Third, Creates the function on_message
  • Fourth, Checks if the author is the user not the bot, with if message.author == client.user:
  • Fifth, if the message starts with the command ~help, then do the following:
    • Create the variable msg.
    • Send the message.(await message.channel.send(msg))
  • Sixth, create the function on_ready
  • Log the ID and the NAME
  • Run the commands on the bot.

and that was a lot to recover

Review

  • How do you send a message?

Answer: First define the msg, then await (NOTE: MAKE SURE YOU ALWAYS HAVE THE await) after that, message.channel.send(msg).

  • How do you run the commands?

Answer: client.run(token)

Keep Your Bot alive!

🎵 Stayin Alive 🎵

from flask import Flask
from threading import Thread
botname = "PieBot"

app = Flask('')
@app.route('/')
def main():
    return f'{botname} is alive!'

def run():
    app.run(host="0.0.0.0", port=8080)
def keep_alive():
    server = Thread(target=run)
    server.start()

Add this into keep_alive.py.

Part Two: Adding More Commands

Now that bot is BORING, Lets add some more!

Random "Hello" message!

elif message.content.startswith('~hello'):
  rand_hellos = ['Hello!', 'Hai', 'E', 'hElLo', 'no u']
  random_hello = random.choice(rand_hellos)
  await message.channel.send(random_hello)

Summon Command!

elif message.content.startswith('~summon'):
  msg = '**WHO SUMMONED THE ALMIGHTY BOT**'.format()
  await message.channel.send(msg)

Deleting a Message

Alrighty, this is where some powers come in:
Your bot needs to have permissions! The way I do it is:
Make a bot role. Give the bot role administrator so they can delete messages.
OR...
You can give them permissions on your app! (AOUTH > that box at the buttom)

elif message.content.startswith('~delete'):
  msg = 'no u'
  await message.channel.send(msg)
  await message.delete()

This can be most useful with nono words.

Check if the command is a actual command

We are going to have to add some stuff before we do this. Create a list with all the command names:
commands = ['hello', 'summon', 'delete'] #more commands

if message.content[0:1] == "~" and message.content[2:] not in commands:
  msg = 'Hey, please enter a command!'.format()
  await message.channel.send(msg)

Review

  • How do you delete a message?

Answer: You must await message.delete()

Links:

Make app IMPORTANT
Get Bot Token
Keep Alive Tutorial

Credits

These people helped me make my first discord bot:
@Coder100 - giving me the run forever tutorial
@CoolJames1610 - Teaching me how to send message.

Tanks

Suggestions? Comments? PUT THEM IN THE COMMENTS!!!

kthxbai

Comments
hotnewtop
TsunamiOrSumth (545)

i have a suggestion; make the title

DISCORD.PY TUTORIAL [POG] MAKE YOUR OWN DISCORD BOT!! [POG]