Learn to Code via Tutorials on Repl.it!

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

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

Commentshotnewtop
Luke_Watson (2)

lol my pig birthded a hen

DysonBird (0)

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 (194)

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

DysonBird (0)

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

GarethDwyer1 (194)

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

GarethDwyer1 (194)

@DysonBird

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 (0)

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

TheSatIsBack (4)

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

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.

Morgana123 (0)

how does one make the bot post images?

GarethDwyer1 (194)

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

@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

[deleted]

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