首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从Bot.event on_message返回第一个函数

无法从Bot.event on_message返回第一个函数
EN

Stack Overflow用户
提问于 2021-08-26 15:44:49
回答 1查看 49关注 0票数 1

代码

下面是(我的代码的一部分):

代码语言:javascript
复制
@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

我的代码没有引发错误。

问题

return使Spy命令结束,但事件仍在运行。

问题

您知道如何使所有事件(on_typingon_message不是唯一的事件)以返回结束吗?

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-26 15:50:46

回答

也许我找到了我自己问题的答案:

我必须将on_message作为函数中的第一个事件,然后使用End结束所有其他事件。

编辑

正如@Łukaszński解释的那样,我应该使用Ł方法。

代码语言:javascript
复制
@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

比我以前的解决方案更简单更短。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68941614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档