Python-based discord bot reply errors (infinite loop)
Application Type
Traditional Web

I've been learning java for a few months and is quite new to python. I'm currently building a simple deep learning AI bot for discord that takes a message from the user, then replies with a message from a pre-established database. I've managed to complete the AI with no issues but I'm currently encountering some problems with setting up the code for discord.

Below is my final code block responsible for receiving the message from discord and replying the user in discord.

Current Error: My code wouldn't stop from executing after sending its first message in the discord chat, causing the bot to repeatedly send messages nonstop. This is most likely caused by the while loop within the function.

My attempted solution: New to python, I'm not sure how I create a boolean type class variable the way you would do it in java. I attempted to use the boolean variable new_message as the conditional statement for the while loop. This variable is set to false once a reply by the bot is sent to ensure that the program does not scan the bot's own reply and cause an infinite loop. This solution didn't seem to work because I couldn't seem to figure out a way to create the new_message variable outside of the function.

Note: I can't simply remove the while loop because I need the program to terminate once the user enters the input "quit".

async def on_message(message):
     bot_testing = client.get_channel(0000000000)
     user_input = message.content
     new_message = True

  while new_message:
    if user_input.lower() == "quit": # type quit to stop the program
        print("Program Terminated")

    results = model.predict([bag_of_words(user_input, words)])[0]
    results_index = numpy.argmax(results) # returns the greatest value's index
    tag = labels[results_index]


    if results[results_index] > 0.9: # accuracy threshold for a database reply
        for tg in data["replyData"]:
            if tg['tag'] == tag:
                responses = tg['responses']

        await bot_testing.send(random.choice(responses))
        new_message = False
        await bot_testing.send("I'm not sure I understand. Please try again or ask a different question!")
        new_message = False


Also, in addition to the problem above, if anyone knows how to set up a bot that only responds when the bot itself is tagged by a user, please let me know how that is done!

Thanks in advance!

Rank: #172

Hi Jain!
Is it related in any way with OutSystems?
What component are you using?
If not maybe the best place to put this question is in some other forum like Stack Overflow for example.

Any way, some ideas that can help you.

Your python code seems ok and will work, however depending on your business rule since you are using an while you should check if the Author is a Bot and if yes, return from the function.

Check this topic from stack overflow.

It is very similar with the problem you are having:

I believe if you use:

    if message.author.bot:

You can solve your problem.

Cheers and Regards,

RR :)

Rank: #172


Seems to me that the on_message function will be triggered every time a message is sent.

So you should not need to use a while, because the next message from the user will also trigger this function.

Try to see if this is really the correct way of doing what you are trying.

Rank: #415

Hi Saniya,

Based on your comments you are learning Java and new to Phython. However, doing coding in Phython, so could you please confirm what help you are looking from OutSystems community members?

Thanks & Kind Regards,


Rank: #17

This discussion should be made on a phyton developer forum, not on the OutSystems forum.

So I closed this discussion.