【问题标题】:How to restart a loop in discord.py?如何在 discord.py 中重新启动循环?
【发布时间】:2020-07-24 20:59:40
【问题描述】:

我正在使用discord.py 制作一个不和谐机器人。我想发出一个命令,每 100 秒清除一次频道内的所有消息。这是我的代码:

autodeletetime = -100
autodeletelasttime = 1



@client.command()
@commands.has_permissions(manage_messages=True)
async def autodelete(ctx):
        global autodeleterunning
        if autodeleterunning == False:
            autodeleterunning = True
            asgas = True
            while asgas:
                message = await ctx.send(f'All messages gonna be deleted in 100 seconds')
                await message.pin()
                for c in range(autodeletetime,autodeletelasttime):
                    okfe = abs(c)
                    await message.edit(content=f"All messages gonna be deleted in {okfe} seconds" )
                    await asyncio.sleep(1)
                    if c == 0:
                    
                        await ctx.channel.purge(limit=9999999999999999999999999999999999999999999999999999999999999999999)


                        await time.sleep(1)
            autodeleterunning = False
        else:
            await ctx.send(f'The autodelete command is already running in this server')
                    

我希望循环在清除完成后每 100 秒重新启动一次。

【问题讨论】:

    标签: python python-3.x discord.py


    【解决方案1】:

    对于这类命令,您应该使用tasks 而不是commands

    import discord
    from discord.ext import commands, tasks
    import asyncio
    
    
    @tasks.loop(seconds=100)
    async def autopurge(channel):
        message = await channel.send(f'All messages gonna be deleted in 100 seconds')
        await message.pin()
        try:
            await channel.purge(limit=1000)
        except:
            await channel.send("I could not purge messages!")
    
    
    @client.group(invoke_without_command=True)
    @commands.has_permissions(manage_messages=True)
    async def autopurge(ctx):
        await ctx.send("Please use `autopurge start` to start the loop.")
    
    
    # Start the loop
    @autopurge.command()
    async def start(ctx):
        task = autopurge.get_task()
        if task and not task.done():
            await ctx.send("Already running")
            return
        autopurge.start(ctx.channel)
    
    
    # Stop the loop
    @autopurge.command()
    async def stop(ctx):
        task = autopurge.get_task()
        if task and not task.done():
            autopurge.stop()
            return
        await ctx.send("Loop was not running")
    

    【讨论】:

    • 但它做错了discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Loop' object has no attribute 'is_running'
    • 停止查看的命令是什么,我已经尝试了很多方法,例如 stop(autopurge) 我搜索了很多但没有找到答案 你能帮帮我吗?
    • 我更新了我的答案。我使用commands.group 让事情变得更容易。使用autopurge start 启动循环并使用autopurge stop 停止它。注意:您可能需要使用cancel 而不是stop 来停止循环(同时检查它们)。
    猜你喜欢
    • 2020-01-29
    • 2021-09-29
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 2022-01-13
    相关资源
    最近更新 更多