Learn to Code via Tutorials on Repl.it!

← Back to all posts
Tutorial: Building a Discord Bot with Python
GarethDwyer1 (299)

Hey all, I published this tutorial on writing chatbots with Python and Discord a few weeks ago and I'm excited to share it with you here now. I'm watching the comments here and over at codementor so let me know if you have any questions.


Luke_Watson (3)

lol my pig birthded a hen

DysonBird (2)

I did everything exactly as it should be, but I still got this error:

Traceback (most recent call last):
File "main.py", line 1, in <module>
from keep_alive import keep_alive
File "/home/runner/Hugo-Dyson/keep_alive.py", line 13
def keep_alive():
SyntaxError: invalid syntax

anyone know how to fix this?

GarethDwyer1 (299)

@DysonBird Can you share the rest of your code? it looks like there's something wrong just before line 13.

DysonBird (2)

@GarethDwyer1 Here it is:

from keep_alive import keep_alive
import discord
import os

client = discord.Client()

async def on_ready():
print("I'm in")

async def on_message(message):
if message.author != client.user:
await message.channel.send(message.content[::-1])

token = os.environ.get("DISCORD_BOT_SECRET")

GarethDwyer1 (299)

@DysonBird The error is in the other file keep_alive.py. Can you share a repl link to the whole project?

GarethDwyer1 (299)


you need to add a closing bracket after the port number on the line above in keep_alive.py

NitroType (0)

@DysonBird I think that code he showed is outdated. 2 years ago, its outdated. so upgrade, write it newly.

CHanlon (1)

Just to let you know, to make this work with the newest version of discord.py, you need to change:

await client.send_message(message.channel, message.content[::-1])


await message.channel.send(message.content[::-1])

Kylethedoge (1)

how do i let my bot do /spoiler

AnimeCartoonCar (0)

How can i take a response from a user and store the information

kazito9 (0)

i did this and there were some eerror but i managed to fix them but now there were endless eerrors and i canot fix them
pls help!!!

EdwinGMI (0)

can u show how to add commands im confused

GarethDwyer1 (299)

@EdwinGMI commands just start with an ! - so you can check if the user input starts with that and have the bot take action accordingly - e.g.

if message.content.startswith("!mycommand"):
    # do things for `!mycommand`
TheSatIsBack (4)

When I try to run the bot code that you put on the tutorial, this error comes up:

when I ran discord.py it created 'poetry.lock' and 'pyproject.toml' but I cant see these files in the pictures you provided.
And when I run main.py, in requirements.text, underneath discord.py repl adds types 'discord' automatically.

Morgana123 (0)

how does one make the bot post images?

GarethDwyer1 (299)

@Morgana123 you'll have to pass an image Embed into the send message call. Take a look at the documentation for full details - a good starting point would be here https://discordpy.readthedocs.io/en/latest/api.html#discord.Embed

ComputerWizard (0)

The website mentioned above no longer exists

GarethDwyer1 (299)

@ComputerWizard Sorry about the inconvenience! It looks like all codementor.io posts are currently going to 404 pages :/ I've reached out to them and hopefully they'll be back soon.

In the meantime you can see the post at https://hackmd.io/s/HJ71Hu4SX


I can't go to that site. It says that it's temporarily down or permanently moved to a new site.

Brawn (5)

Two questions
1)Could you do all of the steps in a browser or do you need the app to do it?
2)Is the code too advanced for a beginner, or is it do able with the basics?
I am a newbie in programming "for now" and I was interested in this challenge hoping that I could learn some js during the process. I would also love some tips for how to learn a language for a beginner like myself.
I would also like to hear your opinion on Rust, which I am attempting to learn. Things like is it too hard for a new comer and if it is worth learning

AlexanderHall4 (1)

this is pretty neat, i have been working on a text reaction program i called ARS (based of of the echo discord bots name), maybe i should throw it in the api and make a bot :)