Skip to content
← Back to Community
Tutorial: Building a Discord Bot with Python
Profile icon
h
has Hacker Plan
GarethDwyer1

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.

https://www.codementor.io/garethdwyer/building-a-discord-bot-with-python-and-repl-it-miblcwejz

Voters
Profile icon
OlivernOil
Profile icon
JunayedSoliman
Profile icon
mamun001
Profile icon
AkshaySivadas
Profile icon
Chiqueedits
Profile icon
Kylethedoge
Profile icon
justzineddine
Profile icon
JaydenYeet
Profile icon
SreerupDas
Profile icon
AustinRaju
Comments
hotnewtop
Profile icon
Luke_Watson

lol my pig birthded a hen

Profile icon
DysonBird

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
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?

Profile icon
GarethDwyer1

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

Profile icon
DysonBird

@GarethDwyer1 Here it is:

from keep_alive import keep_alive
import discord
import os

client = discord.Client()

@client.event
async def on_ready():
print("I'm in")
print(client.user)

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

keep_alive()
token = os.environ.get("DISCORD_BOT_SECRET")
client.run(token)

Profile icon
GarethDwyer1

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

Profile icon
DysonBird
Profile icon
GarethDwyer1

@DysonBird
image

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

Profile icon
NitroType

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

Profile icon
CHanlon

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

to:

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

Profile icon
TheSatIsBack

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

error

Also:
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.

Profile icon
Kylethedoge

how do i let my bot do /spoiler

Profile icon
AnimeCartoonCar

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

Profile icon
swethasuresh08

hi

Profile icon
kazito9

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

Profile icon
EdwinGMI

can u show how to add commands im confused

Profile icon
GarethDwyer1

@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`
Profile icon
Morgana123

how does one make the bot post images?

Profile icon
GarethDwyer1

@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

Profile icon
mikiec84

🤘😉

Profile icon
ComputerWizard

The website mentioned above no longer exists

Profile icon
GarethDwyer1

@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

Profile icon
[deleted]

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

Profile icon
Brawn

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

Profile icon
AlexanderHall4

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

Profile icon
GalenJauss

noice matie

Profile icon
123Alefantastic

noice