Infinite loop in the on_message method

Asked

Viewed 321 times

2

Hello, everyone. I have an infinite loop on my Discord bot, but I don’t know how to fix it.

import discord
import asyncio
import strings_codes #minha classe de string e códigos
import auxiliary_functions #minha classe de funções auxiliares


client = discord.Client()


@client.event
async def on_ready():
    await client.send_message(client.get_channel(strings_codes.test_channel_id), strings_codes.welcome_message)


@client.event
async def on_message(message):
        lower_command = message.content.lower()

        if lower_command.startswith('!'):
            await client.send_message(message.channel, auxiliary_functions.switch_command(message))
        else:
            await client.send_message(message.channel, strings_codes.invalid_commands['wrong_prefix'])

client.run(strings_codes.token)

The method on_ready() calls the method send_message() with the greeting message and triggers the method on_message(), which also has the method send_message() in its scope, thus causing the bot to endlessly capture its own message. I saw the code of many people and they did not need to put a condition to trigger the event that has the method on_message(). How do I fix it? Thank you.

  • There is a way to see if the user is a bot or not. message.author.bot. With this you can filter who sent the message, and ignore all the messages the bots send. You must stop the loop. Another idea, it will be better if the message does not start with the prefix just ignore and do not respond. It might get boring in all messages having the bot say the wrong prefix has been used.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.