B
B
BuBux2020-01-11 18:09:03
Python
BuBux, 2020-01-11 18:09:03

DISCORD.BOT: Loops async def on_message. How to decide?

@Bot.event
async def on_message(message):
    if message.author.id in white_list:
        await message.channel.send('+')
    if message.author.id not in white_list:
        await message.channel.send('-')

In this script, I check if the user is on the "white" list. If I write from a user who is included in the "white" list, the bot displays "+" for me, and then four minuses, and after 3 seconds it displays 5 minuses in a row. And so it continues until I stop the script. What could be the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Adam Salavatov, 2020-01-11
@BuBux

I'm not strong at the moment in Python and discord.py, but I can say:
You have a "recursion" when the function calls itself.
This is due to the fact that your code also reacts to bot messages and issues a response.
Here is the fix:

@Bot.event
async def on_message(message):
    if message.author.bot and message.author.id in white_list:
        await message.channel.send('+')
    else: await message.channel.send('-')

1
1keda, 2020-01-30
@1keda

I know it's a long, long time. I will answer, maybe someone else needs it.
At the beginning of the function, you need to put the following condition: It ignores bots and the function will not be recursively executed. After all, on_message is also called when you send messages on behalf of your bot. And also, if required, you can tighten the white list for bots:
if message.author.bot: return

if message.author.bot:
    if not message.author.in in BOT_WHITE_LIST:
        return

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question