Coding a Discord Bot With Python!

A simple walkthrough of using discord.py on replit

Posted by AwesomeAg on 04-21-2020

So… you’ve probably heard of discord, a chat and stream app for gamers. Over time it has evolved but it is still there for one main thing: chatting. Nowdays, you can create bots for discord that act as users and put some fun into discord with games, moderation, and trolling. Bots can be coded in many different languages, and it’s pretty common to make one in discord.js or discord.py.
A bot
To start, you need to open repl.it and start a new python code. Write

1
import discord

on the first line
but before that, go to the packages section and search discord to install it.
To host the bot, create a new file and name it keep_alive.py. Now type the following into the file

1
2
3
4
5
6
7
8
9
10
11
from flask import Flask
from threading import Thread
app = Flask('')
@app.route('/')
def main():
return "Bot is online"
def run():
app.run(host="0.0.0.0", port=8080)
def keep_alive():
server = Thread(target=run)
server.start()

now go back to main.py.
Before you start coding, you need to create the actual discord bot. you can do this by going here
and then press the button that says new application in the top right:
New app button
Name it whatever you want, but when you’re done, go to the section that says bot
bot button
and click it.
You should see a screen like this screen with bot
Now to bring it to life, press add bot and then press “yes do it”. You can name it and edit the profile picture, but the big part is copying the token copy token
now head back to the main.py file and at the end of it, write the following:

1
2
3
import keep_alive #this should be the name of the file that you created earlier
keep_alive.keep_alive()
await bot.run("TOKEN") #replace token with your token.

now head back to the beginning of the file, where you imported discord. There, type

1
2
client = discord.Client()
bot = commands.Bot(command_prefix="!", description='My new bot!') #You can change the prefix if you wanr

Congrats! You have officially made your discord bot! It doesn’t do anything yet, but one last step before we finish is inviting it to a server.
If you scroll to the bottom of the OATH2 page (the sidebar next to bot), you will see this: panel
click on “bot” and another panel will appear. On this new panel, click “Administrator” and copy the generated link at the bottom.
panel2
link
go to the link you copied and invite it to a server. I would recommend making a testing server for this.
PERFECT! Your bot is up on a server, but it doesn’t do anything yet 😞. Well, stay tuned for part 2 on how to make commands, send embeds, and more! Go ahead and check the code here
discord.py documentation (the docs)
And check out the second part here!