Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to make a discord bot in python!
eekboi (285)

Hello, today I will be teaching you how to make a discord bot in python.
First thing you need to do is make a discord bot client (https://discord.com/developers) and make a client for your bot there.
Next you need a repl to put all of its code on, so create a 3.8.2 python repl. Next you need to put in the necessary code!

import discord
from discord.ext import commands, tasks
import B
import os 

B is the import we will be using to host the bot
Now that we have the imports out of the way we can now move onto the fun part! make a file called B.py and copy paste this code into it.

from flask import Flask
from threading import Thread

app = Flask('')

def main():
    return 'SQUIDN'

def run():
    app.run(host='', port=8080)

def b():
    server = Thread(target=run)

This code is for hosting the server required for the bot to run on.
That code is going to make a webserver with flask and B so you can host your bot. Then create a file called .env and put

TOKEN=(your bots token here)

as the bot will not be connected to the code if you do not. Now finally go back to main.py and put the following code in there.



client = discord.Client()

This is just setting up some things that are needed for your bot, nothing much.

And now to make the bots prefix.

client = commands.bot(command_prefix="(prefix)")

The first part in this example here (Prefix) isn't that complicated, but i'll explain anyways. cleint = commands.bot(command_prefix="") is for setting up your prefix, pretty self explanatory.
And client.remove_command('help') is removing the default help command as it can be buggy a LOT.
Now FINALLY we can get to making commands.

async def command(ctx):
    await ctx.send('This command was successful!')

Thats what a normal command in discord.py looks like. Now I bet youre asking "Well eekboi, what does all this mean? Looks like gibberish to me!" Well heres the thing, All I write is gibberish I was about to go over that with you. So lets start from the top, @client.command() Thats the decorator for letting the bot know its a command and to look for it. (The () is the because that can be modified, but we wont do that now.) Next async def (command name)(ctx): this line is just for calling the command name. await ctx.send('(command output)')This is for the bot to execute when it sees the command. There are some things that can be changed here such as the ctx.send('bla bla bla') needing a ' or not but we wont get into that too much. Now we are at the help command part. The normal discord help command can be buggy at times so i'd advise to not rely on it. What you want to do is put in this code.

async def help(ctx):
    embed = discord.Embed(name="Help", value="help command", color=0xFFFFFF)

As that makes an embed in the help command where you can put all of your commands in there. To do that you need to put this code in the bot
@client.command() and async def help(ctx): are the same thing as what i've said before, but we have a new part to explain here. embed = discord.Embed(name="Help", value"Help command") Is calling the command function and the name of it. value="Help command" is the description of the command.

embed.add_field(name="command", value"command description")

Now, lets dissect this part of the bot, embed = discord.embed is declaring the embed and making sure the embed has a variable. embed.add_field is making a field and so on. To finish off the help embed we need a way to send this. So put this in the code

await ctx.send(embed=embed)

And that should be it for the help command.
BUT we need one more thing to top off our bot to make it run


And now your bot is running! Ive also attached a working example of this tutorial at https://repl.it/@eekboi/example-bot
If this helped you please give it an upvote, thanks!
(ps the env.txt file is supposed to be the .env file but it wont show to you guys as its supposed to be secret.)
(pps the imitate command there is a good example of how to use f-strings in discord.py and custom errors built into the command)
(ppps i also made an example of an error message for your bot to run if it sees an error)

BlakeLeee (13)

If I use this today is it like up to date like will it still work or?

eekboi (285)

@BlakeLeee Unfortunately not. Since the API updddated you now need promises to make your bot work, let alone other things. I'm sure @CodingCactus would be glad to help you with d.py though. Sorry!

CodingCactus (4366)

@eekboi haha

i'm pretty busy at the moment @BlakeLeee sorry

ItzMalakai (6)

@eekboi Yea, it still works. Only thing it says after it's connected is:
"Missing 1 positional argument: 'ctx'" I somehow fixed it but don't know how I did it. I'm new to coding anyways.

RyanYow (0)

it works but u have to change a couple of things including the .env @BlakeLeee

Seven7Four4 (14)

What commands do I do? Every time I do music.play Overnight it keeps on giving me an error in the chat.

AndrsOlalla (0)

Since replit updated i cant use .env files heeeeelp

FishballNooodle (207)

Check my tutorials for updated tutorials

MAX2005 (0)

They were working and needed some economy cmds

01mvrcus (0)

Thanks a lot eekboi, you're the first person I've understood so far. Appreciate it man. I got the code to work btw. I just did some minor editing.

eekboi (285)

@01mvrcus I'm glad.
Also congrats on getting your code working!

ShitposterBruh (0)

can someone help me?

eekboi (285)

@ShitposterBruh I believe that error is coming from the old discord module. Try installing it via the package manager instead of just import discord.

CodingCactus (4366)

hey @ShitposterBruh don't put your token out in the open like this, use a .env file

ShitposterBruh (0)

it dont work i select edited what you said and i get error

ShitposterBruh (0)

@CodingCactus i removed all errors with keep_alive but i have this error

ShitposterBruh (0)

i removed line 65 it what it says

CodingCactus (4366)

@ShitposterBruh you need a file called .env and inside it write Token=fhiheoiiheiieoghiehghehgehgep== (but replace the spam with your token)

cassidyfailsalo (0)

what is the bots prefix though?

eekboi (285)

@cassidyfailsalo The bot prefix can be defined by you, in the case of my example the bot's prefix is !; however you can change this by editing the string inside the line that contains client = commands.Bot(command_prefix = '!'). Sorry for any miscommunication!

HyperData (0)

is it possible to make it a moderartion bot no autmod. just warns/miutes/kicks/bans

ShinyDuck21 (0)

The module B isn't in the files so therefore doesn't work

JasonWu19 (9)

there is a problem on mine and I don't know how to fix it.

eekboi (285)

@JasonWu19 You need to unindent B.b() :)

Eyseedoubleyou (25)

mine says B.b() is a syntax error and so is client.run(os.getenv('TOKEN'))


eekboi (285)

@Eyseedoubleyou Did you import the file?

Eyseedoubleyou (25)

@eekboi which file? i've fixed B.b() now its the line below
I typed in exactly what you did, and fixed the code to how it'd work for my bot without any bugs, and a parts i left the same have errors

Eyseedoubleyou (25)

@eekboi i invited you to the repl ;)

eekboi (285)

@Eyseedoubleyou I'll take a look at it later. For some reason the exact same way I made MY bot works fine, but everyone else is having troubles.

Eyseedoubleyou (25)

@eekboi oh ok. It might be because they may have updated the IDE or python may have had an update that changes some existing features (which we should've been warned about prior and reminded after)

eekboi (285)

@Eyseedoubleyou Yeah, my money's down on that.

Eyseedoubleyou (25)

but also confused, as to the first few times, it worked fine. one day, before i had lunch i ran it and it was fine, i came back, i ran it with no changes to the code and it had errors with those two lines.

eekboi (285)

@Eyseedoubleyou Hmm, sounds like a bug with repl.it itself. If you have another problem don't be scared to reach out! Thanks for reading.

Eyseedoubleyou (25)

@eekboi I fixed the bug, I'm now stuck on how to send a random message from a tuple using random.choice() when the command is sent. I'm looking for how to do it, as my way for it was the cause of the bug i found out

eekboi (285)

@Eyseedoubleyou Instead of using a tuple try using a list. I didn't have any problems with it that way so I'll go on a limb and say its safe.

Eyseedoubleyou (25)

@eekboi :D
lol im currently working on mine, debugging a random command

CDK1r (0)

I’m sorry I’m not good at this but your example says file error

eekboi (285)

@CDK1r Its not supposed to be run lol. Its just showing you what to do, not a fully running example. Its all fine too, everyone starts somewhere :)


Node.js is better than Python in discord bot maker

eekboi (285)

@Zuhdi28 Some people only know python, some only know js, some only know raku or C# or any other lang. Im just making a tutorial for the majority of repl talk (as it seems).


@eekboi ummm, ok

CoolJames1610 (767)

no I might try it out later but I will ask for help when I need it :D @eekboi

eekboi (285)

@CoolJames1610 Alright, sounds cool! :)