Switchback
Switchback

Reputation: 95

Why i get double or triple messages sent when i write script for one time send, discord.py

Why isn't my code going into debug 4 zone? I think it may be the discord server's fault, if you have any answers please share.

globals()['i'] = 0

@bot.event
async def on_message(message):
    time.sleep(3)
    apple_channel = bot.get_channel(829849505775091742)
    print('debug 1')
    if str(message.author) == "Hyetabot11#2254" or globals()['i'] == 0:
        print('debug 2')
        while globals()['i'] < 12:
            print('debug 3')
            await apple_channel.send(i)
            print('debug 4')
            globals()['i'] += 11
            print('debug 5')
            break

result:

debug 1
debug 2
debug 3
debug 1
debug 2
debug 3
debug 1
debug 2
debug 3
debug 4
debug 5

in this example I get two times sent messages, when I code it to only once.

Also, how did it go from debug 3 to debug 1 ?

SOLUTION:

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return

Upvotes: 1

Views: 65

Answers (1)

IPSDSILVA
IPSDSILVA

Reputation: 1839

I see that the bot may be responding to it's own messages! To fix that, you should add this to the top of your on_message() function:

if message.author == bot.user:
        return

This will check if the author of the message (message.author) is the bot (bot.user). If so, it will just ignore the message (return). Otherwise, it will continue to execute the code from within the function.

Upvotes: 4

Related Questions