Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Discord.py on_message() repeating execution

Long time user, first time poster. 🙂

I’m using discord.py==1.7.2. I have a probably complicated bot that I have been hacking out through trial and error (super sketchy documentation as far as I can tell and maybe I don’t really understand the Discord concept), but I am seeing some super strange behavior in the on_message() event. After having everything working like I wanted, I noticed an issue and simply wanted to check a value when the on_message() event was executed. I printed it to the chat. I then noticed it was printed repeatedly, non-stop to the chat until I killed the bot. Am I missing something here? My expectation is that the event on_message() runs when a user enters something and presses enter and then stops. Is this wrong? Code:

@bot.event
async def on_message(message):

    if len(message.content) < 5:
        return

    await message.channel.send('wtf')

If I type anything below 5 chars, nothing happens. If I type "12345", the chaos begins.. it spits out 5 lines of "wtf" and then 4 "wtf"s every few seconds until I kill the bot. Is this happening for everyone? Or did I break it somewhere else? Super confused.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks for any help!!!

Cheers

>Solution :

This is because your event is on_message, and you are sending a message inside this event, which then triggers the event again, and then sends a message again and so on, in a loop.

you should check the sender of the message, if its the bot, then return.
like so,

#... other code
@bot.event
async def on_message(message: Message):
    if message.author.bot: #if message's author is a bot, then ignore it.
        return
    #... rest of your on_message()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading