[SOLVED] Can't return the first function from Bot.event on_message

Issue

Code


Here’s (a little part) of my code:

@Bot.command()
async def Spy(ctx, user:Member): #I did from discord import * before
    author = ctx.message.author
    ...
    @Bot.event
    async def on_typing(channel, user, when):
        ... #... means that there's some irrilevant code between two lines, but it's not a pass
    @Bot.event
    async def on_message(mess):
        if mess.author == author:
            if mess.content == "Stop spying!":
                global End
                End = True
        await Bot.process_commands(mess)
    global End
    if End:
        End = False
        return #this returns the whole function/command Spy

There’s no error raised with my code.

Problem

return makes the Spy command end, but events keep running.

Question

Do you know how to make all the events (on_typing and on_message aren’t the only ones) end with the return?
Thanks in advance!

Solution

Answer


Maybe I found the answer to my own question:
I have to put on_message as the first event in the function, and then use End bool to end all the others.

Edit


As @Łukasz Kwieciński explained me, I should use the Bot.wait_for() method.

@Bot.command()
async def Spy(ctx, user:Member):
    ...
    while True:
        channel, uSer, when = Bot.wait_for("typing") #Instead of on_typing event
        if uSer == user:
            await ctx.message.author.send(f"{user.mention} is typing in {channel}")
        message = Bot.wait_for("message") #Instead of on_message event
        if message.content == "Stop":
            return

Easier and much shorter than my previous solution.

Answered By – FLAK-ZOSO

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.