How to make a discord bot in python!
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.
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
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
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)
hey im having an error on line twelve can smn help
from discord.ext import commands, tasks
#setup dont remove
client = discord.Client()
client = commands.bot(command_prefix="!")
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")