Jump to content

How do I get my discord bot up and responding to messages

So im trying to make a discord bot so I started following this freecodecamp.org tutorial and made some changes to it because somethings don't work any more my bot is up online but it won't repond to me 

 

 

import os

import discord

token = os.environ['token']

intents = discord.Intents.default()
intents.messages = True  # Enable message intents
client = discord.Client(intents=intents)


@client.event
async def on_ready():
  print('we have logged in as {0.user}'.format(client))


@client.event
async def on_message(message):
  if message.author == client.user:
    return
  if message.content.startswith("!hello"):
    await message.channel.send("hello")


client.run(token)

here's the tutorial im following

https://www.youtube.com/watch?v=SPTfmiYiuok&t=622s

 

 

I googled for solutions but could'nt find any working ones, pls help

Link to comment
Share on other sites

Link to post
Share on other sites

What is the console spitting out when you try to ping the bot? 
Those Print statements are your friends

5950X/3080Ti primary rig  |  1920X/1070Ti Unraid for dockers  |  200TB TrueNAS w/ 1:1 backup

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, OddOod said:

What is the console spitting out when you try to ping the bot? 
Those Print statements are your friends

it says it's logged in

image.png.8602a83168721acb88a1d0efd64b0918.png

Link to comment
Share on other sites

Link to post
Share on other sites

And what happens if you send "!hello" in the channel with the bot?
I'd add some print messages around the if statements
Which... hmmm not sure about that author == user line...

This is how bonkers I'd go with print statements

Quote
@client.event
async def on_message(message):
  print(message)
  if message.author == client.user:
    print('author == client')
    return
  if message.content.startswith("!hello"):
    print('started with "!hello"!')
    await message.channel.send("hello")

 

5950X/3080Ti primary rig  |  1920X/1070Ti Unraid for dockers  |  200TB TrueNAS w/ 1:1 backup

Link to comment
Share on other sites

Link to post
Share on other sites

API Reference (discordpy.readthedocs.io)

 

reading the api docs, send is supposed to raise an exception if on failure. try surrounding it in a try block and see if it is actually successful or erroring perhaps? 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, OddOod said:

And what happens if you send "!hello" in the channel with the bot?
I'd add some print messages around the if statements
Which... hmmm not sure about that author == user line...

This is how bonkers I'd go with print statements

 

it did nothing so it's not reading the messages at all

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, wasab said:

API Reference (discordpy.readthedocs.io)

 

reading the api docs, send is supposed to raise an exception if on failure. try surrounding it in a try block and see if it is actually successful or erroring perhaps? 

 

8 hours ago, OddOod said:

And what happens if you send "!hello" in the channel with the bot?
I'd add some print messages around the if statements
Which... hmmm not sure about that author == user line...

This is how bonkers I'd go with print statements

 

I read the docs and changed the intents from only messages to all, i had to go to the portal and enable some privileges but now when I said !hello it gave me an error

image.png.61e26e940125b4b2c7027ca6a36f81c9.png

 

looked the error up and the only answer in stack overflow isn't working on it

Link to comment
Share on other sites

Link to post
Share on other sites

Seems like the server responded with a 403 forbidden. Did you have your authorization header set correctly and does the bot actually have permission to post in that discord channel? 

 

Printing is a bad way to debug btw. It is kinda unavoidable for front end js web but for python, you can use a debugger and set breakpoints. Most ide ships one. Pycharm or any jetbrains IDE, are my favorites. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, wasab said:

Seems like the server responded with a 403 forbidden. Did you have your authorization header set correctly and does the bot actually have permission to post in that discord channel? 

 

Printing is a bad way to debug btw. It is kinda unavoidable for front end js web but for python, you can use a debugger and set breakpoints. Most ide ships one. Pycharm or any jetbrains IDE, are my favorites. 

holy fukin zhit, all this time the only problem was the channel I was posting to didn't have permissions for anyone to send messages except admins

man upto now I have been coding on python using idle, this pycharm, visual studio and all confuse me a little so i went with replit for this one as it was on the tutorial.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, VirusDumb said:

holy fukin zhit, all this time the only problem was the channel I was posting to didn't have permissions for anyone to send messages except admins

man upto now I have been coding on python using idle, this pycharm, visual studio and all confuse me a little so i went with replit for this one as it was on the tutorial.

Yeah... very common mistakes, i lump these together with things like bad firewall rules, cors issues from bad server headers, incorrect api tokens, file permission errors and ect. I have been doing web dev long enough to guess pretty accurately what's causing them most of the time but when i started out, these drove me crazy. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×