Code
Here’s (a little part) of my code:
JavaScript
x
19
19
1
@Bot.command()
2
async def Spy(ctx, user:Member): #I did from discord import * before
3
author = ctx.message.author
4
5
@Bot.event
6
async def on_typing(channel, user, when):
7
#... means that there's some irrilevant code between two lines, but it's not a pass
8
@Bot.event
9
async def on_message(mess):
10
if mess.author == author:
11
if mess.content == "Stop spying!":
12
global End
13
End = True
14
await Bot.process_commands(mess)
15
global End
16
if End:
17
End = False
18
return #this returns the whole function/command Spy
19
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!
Advertisement
Answer
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.
JavaScript
1
11
11
1
@Bot.command()
2
async def Spy(ctx, user:Member):
3
4
while True:
5
channel, uSer, when = Bot.wait_for("typing") #Instead of on_typing event
6
if uSer == user:
7
await ctx.message.author.send(f"{user.mention} is typing in {channel}")
8
message = Bot.wait_for("message") #Instead of on_message event
9
if message.content == "Stop":
10
return
11
Easier and much shorter than my previous solution.