Infinite loop in the on_message method

2

Hello everyone. I have an infinite loop in my discord bot, but I do not know how to solve 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 on_ready() method calls the send_message() method with the greeting message and triggers the on_message() method, which also has the send_message() method in its scope, thus making the bot infinitely capturing the your own message. I've seen many people code and they did not have to put a condition to trigger the event that has the on_message() method. How do I solve this? Thankful.

    
asked by anonymous 24.02.2018 / 04:16

1 answer

0

There is a way to see if the user is a bot or not. message.author.bot . This can filter who sent the message, and ignore all the messages that the bots send. You should stop the loop.

Another idea, it will be better if the message does not start with the prefix just ignore and not respond. It may become annoying in all messages to have the bot say that the wrong prefix was used.

    
26.03.2018 / 19:09