【问题标题】:How to terminate an async function in discord.py from another command如何从另一个命令终止 discord.py 中的异步函数
【发布时间】:2021-10-21 08:57:40
【问题描述】:

我正在使用discord.ext.commands 制作一个不和谐的机器人。我做了一个命令,即使有人被禁止,也会不断地取消禁止人。这是代码:

@client.command(name="unban")
async def unban(ctx):
  while True:
    bans = await ctx.guild.bans()
    if(len(bans)>0):
      for ban in bans:
        ctx.guild.unban(ban)
    await asyncio.sleep(5)

但这是一个 while 循环,所以我想通过另一个命令(比如 stop_unban)终止这个函数。所以我想知道如何通过另一个函数(与stop_unban命令相关)来终止unban函数。

【问题讨论】:

    标签: python asynchronous discord discord.py terminate


    【解决方案1】:

    一种简单的方法是使用两个函数都可以访问的全局布尔变量来控制禁止状态。
    例如:

    ban_state = False
    @client.command(name="unban")
    async def unban(ctx):
      global ban_state
      ban_state = True
      while ban_state:
        bans = await ctx.guild.bans()
        if(len(bans)>0):
          for ban in bans:
            await ctx.guild.unban(ban.user)
        await asyncio.sleep(5)
    @client.command(name="stop_unban")
    async def stop_unban(ctx):
      global ban_state
      ban_state = False
      await ctx.send('Unban Mode stopped')
    

    但是,如果您希望 unban 模式持续很长时间并且不使用全局变量,另一个可能更好的解决方案可能是使用 background task 而不是 while True。 例如:

    from discord.ext import tasks
    @tasks.loop(seconds=5.0)
    async def unbanning(ctx):
        bans = await ctx.guild.bans()
        if(len(bans)>0):
          for ban in bans:
            await ctx.guild.unban(ban.user) #btw you need to await this and you have provide the user attribute of the ban not just the ban itself
        
    @client.command(name="unban")
    async def unban(ctx):
      unbanning.start(ctx)
    @client.command(name="stop_unban")
    async def stop_unban(ctx):
      unbanning.cancel()
    

    Here 是关于后台任务的更多信息 :)

    【讨论】:

    • 好答案@x-1-x,虽然我建议你可以从后台任务版本中删除asyncio.sleep(5)——后台任务已经隐式休眠——然后让它每 5 秒循环一次。这样它在功能上与 while 循环版本大致相同
    • @lemonhead 好地方!没看到那行:)
    • 非常感谢。我实际上尝试了全局变量方法。但是我的程序正在创建一个具有相同名称的新局部变量。我不知道 global 关键字。
    猜你喜欢
    • 2022-06-21
    • 2020-10-09
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多