ja-dev
ja-dev

Reputation: 3

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.

Thanks for any help!!!

Cheers

Upvotes: 0

Views: 1179

Answers (1)

goldenotaste
goldenotaste

Reputation: 355

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

Upvotes: 2

Related Questions