Learn to Code via Tutorials on Repl.it!

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

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)

Commentshotnewtop
Eyseedoubleyou (24)

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

image
screenshot-repl.it-2020.07.07-15_25_58

Eyseedoubleyou (24)

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

@eekboi i invited you to the repl ;)

eekboi (272)

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

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

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

Eyseedoubleyou (24)

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

eekboi (272)

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

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

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

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

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

[deleted]

Node.js is better than Python in discord bot maker

eekboi (272)

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

[deleted]

@eekboi ummm, ok

CoolJames1610 (619)

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