缺少 1 个必需的位置参数:'number'

Missing 1 required positional argument: 'number'

嗨,我有一个问题 运行 异步循环要求 missing 1 required positional argument: 'number'.

这是我正在处理的内容:

async def purge_modlog(ctx, number):
    tomorrow = datetime.now()+timedelta(days=1)
    midnight = datetime(year=tomorrow.year, month=tomorrow.month, 
    day=tomorrow.day, hour=20, minute=35, second=0)
    number = int(number)
    server = before.server
    db = fileIO(self.direct, "load")
    if not server.id in db:
        return
    channel = db[server.id]["Channel"]
    if number > 99 or number < 1:
        await ctx.send("I can only delete messages within a range of 1 - 99", delete_after=10)
    else:
        author = ctx.message.author
        authorID = author.id
        mgs = []
        number = int(number)
        channel = modlog
        async for x in bot.logs_from((channel), limit = int(number+1)):
            mgs.append(x)
            await asyncio.sleep((midnight - datetime.now()).seconds)
            print("Deleting modlog messages 14 day or older")
            await asyncio.sleep(5) 
            await delete_messages(mgs)
            await ctx.send('Success!', delete_after=4)
            await asyncio.sleep(86400) # Wait 24 hours




def check_folder():
    if not os.path.exists('data/modlogset'):
        print('Creating data/modlogset folder...')
        os.makedirs('data/modlogset')


def check_file():
    f = 'data/modlogset/settings.json'
    if not fileIO(f, 'check'):
        print('Creating default settings.json...')
        fileIO(f, 'save', {})


def setup(bot):
    check_folder()
    check_file()
    q = ModLog(bot)
    loop = asyncio.get_event_loop()
    loop.create_task(q.purge_modlog())
    bot.add_cog(q)

循环def下的事件def setup(bot):可以看到

loop = asyncio.get_event_loop()
loop.create_task(q.purge_modlog())

这应该会循环事件 (q.purge_modlog()) 我不确定我做错了什么。我已经尝试过 (q.purge_modlog(ctx, number))

line 686, in setup
loop.create_task(q.purge_modlog(ctx, number))
NameError: name 'ctx' is not defined

如果有人能帮助我,那就太好了。添加这是一个模块。

class ModLog:
    def __init__(self, bot):
        self.bot = bot
    async def on_ready(self):
        await self.bot.wait_until_ready()
        for guild in self.bot.guilds:
            channel = await self.get_channel(guild)
            if channel:
                self.bot.loop.create_task(self.modlog_purge(channel))
    async def get_channel(guild):
        # Whatever your logic for getting the Channel for a given guild is
    async def modlog_purge(self, channel):
        while True:
            now = datetime.utcnow()
            two_weeks_ago = now - timedelta(days=14)
            await channel.purge(before=two_weeks_ago)
            await asyncio.sleep(86400)

def setup(bot):
    q = ModLog(bot)
    bot.add_cog(q)

以下是我的结构(我目前不在 discord.py 的计算机上,因此可能存在一些错误)。我们有一个 on_ready 事件,它为每个有我们要维护的频道的服务器启动后台任务(这可以通过频道列表或类似的东西循环)。

实际清除全部由 TextChannel.purge 协程处理。我们只是向它传递一个 datetime 对象,它会删除该日期之前的 100 条消息(这是可配置的)。然后它会休眠一天并重复。

我在这里纠正了一些错误。尝试阅读有关 purging 消息的 discord.py 文档。

 class ModLog:
    def __init__(self, bot):
        self.bot = bot


async def on_ready(self):
    await self.bot.wait_until_ready()
    for server in self.bot.servers:
        channel = self.bot.get_channel("channel ID here")
        if channel:
            self.bot.loop.create_task(self.modlog_purge(channel))

async def modlog_purge(self, channel):
    while True:
        now = datetime.utcnow()
        two_weeks_ago = now - timedelta(days=14)
        await self.bot.purge_from(channel, before=two_weeks_ago)
        await asyncio.sleep(86400)

def setup(bot):
    q = ModLog(bot)
    bot.add_cog(q)