Skip to content
← Back to Community
How to make a discord bot in python!
Profile icon
[deleted]

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('') @app.route('/') def main(): return 'SQUIDN' def run(): app.run(host='0.0.0.0', port=8080) def b(): server = Thread(target=run) server.start()

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.

key=os.getenv('key') wkey=os.getenv('wkey') 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)") client.remove_command('help')

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.

@client.command() 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.

@client.command() 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

B.b() client.run(os.getenv('TOKEN'))

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)

Voters
Profile icon
minecraftMaaz
Profile icon
AmmarFaris1
Profile icon
INDIANFf
Profile icon
oskdoejisji
Profile icon
dominikstrzelc1
Profile icon
AnnAbdo
Profile icon
RifahReza1
Profile icon
MohamedAdhil
Profile icon
kockalazar
Profile icon
HendraRebo
Comments
hotnewtop
Profile icon
Skylinewox28

i keep getting this error, smn pls help me out

image

Profile icon
CoderTeach60

@Skylinewox28 remove the parentheses

Profile icon
BlakeLeee

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

Profile icon
[deleted]

@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!

Profile icon
CodingCactus

@eekboi haha

i'm pretty busy at the moment @BlakeLeee sorry

Profile icon
ItzMalakai

@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.

Profile icon
ryeit01

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

Profile icon
ArcheryAce

@RyanYow What did you change to get it working. I'm getting a 'module' object is not callable eroor

Profile icon
ryeit01

I don't remember @ArcheryAce sorry but there's autocode discord bots which is a pretty good website to build your bot on

Profile icon
GameMaker4

Hey, i have this error, please help me

image

Profile icon
LoucoPor

.env files are deprecated, instead we should use the sidebar function

Profile icon
Venombite201

hey im having an error on line twelve can smn help
import discord
from discord.ext import commands, tasks
import B
import os
#setup dont remove
key=os.getenv('key')

wkey=os.getenv('wkey')

client = discord.Client()

client = commands.bot(command_prefix="!")
client.remove_command('help')
#commands
@client
.command()
async def command(ctx):
await ctx.send('This command was successful!')

@client.command()
async def help(ctx):
embed = discord.Embed(name="Help", value="help command", color=0xFFFFFF)
#copy and make more for different commands
embed.add_field(name="command", value="command description")
await ctx.send(embed=embed)

#runs bot
B.b()
client.run(os.getenv('TOKEN'))

Profile icon
Fungal

I can't use @client.command() in replit, only @client.event. If I use @client.command() as my decorator, then everything just gets ignored. I get no error, but I also don't receive any output. Please help me!

Profile icon
Tasfinbd

Traceback (most recent call last):
File "main.py", line 14, in
client = commands.bot(command_prefix="(prefix)")
TypeError: 'module' object is not callable

Profile icon
AdrianSinn
  1. idk why my bot not online
  2. A suggestion, u should give us the final code of the bot
    Thanks!
Profile icon
Seven7Four4

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

Profile icon
AndrsOlalla

Since replit updated i cant use .env files heeeeelp

Profile icon
FishballNooodle

@AndrsOlalla
Check my tutorials for updated tutorials

Profile icon
Fungal

@AndrsOlalla You need the os library imported, as well as a repl secret.

Profile icon
MAX2005

They were working and needed some economy cmds

Profile icon
01mvrcus

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.

Profile icon
[deleted]

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

Profile icon
ShitposterBruh
Profile icon
[deleted]

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

Profile icon
CodingCactus

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

Profile icon
ShitposterBruh

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

Profile icon
CodingCactus

@ShitposterBruh remove line 1

Profile icon
ShitposterBruh

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

Profile icon
CodingCactus

@ShitposterBruh remove line 65

Profile icon
ShitposterBruh
Profile icon
CodingCactus

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

Profile icon
cassidyfailsalo

what is the bots prefix though?

Profile icon
legitcats84

@cassidyfailsalo don't got a clue

Profile icon
[deleted]

@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!

Profile icon
HyperData

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

Profile icon
ShinyDuck21

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

Profile icon
MasterwuYT

there is a problem on mine and I don't know how to fix it.
Screenshot 2020-12-01 at 8.33.30 AM

Profile icon
[deleted]

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

Profile icon
MasterwuYT
Profile icon
Typh-Volk

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

image

![image]
![screenshot-repl.it-2020.07.07-15_25_58]

Profile icon
[deleted]

@Eyseedoubleyou Did you import the file?

Profile icon
Typh-Volk

@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

Profile icon
Typh-Volk

@eekboi i invited you to the repl ;)

Profile icon
[deleted]

@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.

Profile icon
Typh-Volk

@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)

Profile icon
[deleted]

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

Profile icon
Typh-Volk

@eekboi
same.
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.

Profile icon
[deleted]

@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.

Profile icon
Typh-Volk

@eekboi yeah
you're welcome!

Profile icon
Typh-Volk

@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

Profile icon
[deleted]

@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.

Profile icon
Typh-Volk

@eekboi ok, will do
thanks

Profile icon
[deleted]

@Eyseedoubleyou No problem.

Profile icon
Typh-Volk

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

Profile icon
[deleted]

bruh.

Profile icon
[deleted]
Profile icon
Macefilms

bruh.

Profile icon
CDK1r

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

Profile icon
[deleted]

@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 :)

Profile icon
[deleted]

Node.js is better than Python in discord bot maker

Profile icon
[deleted]

@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).

Profile icon
[deleted]

@eekboi ummm, ok

Profile icon
CoolJames1610

Nice

Profile icon
[deleted]

@CoolJames1610 thanks, any questions?

Profile icon
CoolJames1610

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

Profile icon
[deleted]

@CoolJames1610 Alright, sounds cool! :)