如何在discord.py中重新启动循环?

2024-09-27 00:23:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用^{}制作一个不和谐机器人。我想发出一个命令,每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秒重新启动一次


Tags: insendfalsetruemessageifawaitcommand
1条回答
网友
1楼 · 发布于 2024-09-27 00:23:14

对于此类命令,应该使用^{}而不是^{}

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")

相关问题 更多 >

    热门问题