使用from_logs生成器删除特定月份的所有消息

时间:2019-02-19 13:40:55

标签: python-3.x discord.py

我有某个月(八月)的消息,希望摆脱使用我的机器人进行测试的过程,环顾了互联网和文档,并遇到了以下代码:

@Client.command(pass_context = True)
async def clear(ctx, number, around):
    mgs = []
    number = int(number)
    async for x in Client.logs_from(ctx.message.channel, limit = number, around = datetime):
        mgs.append(x)
    await Client.delete_messages(mgs)

我看了一下文档,看logs_from的作用,这似乎正是我所需要的,因为它具有around参数,可以让我从八月份返回消息,问题是我似乎无法弄清楚如果可以的话,将timedate放入哪种格式,我该如何指定August?

谢谢

1 个答案:

答案 0 :(得分:1)

around比您要查找的要模糊。我建议您改用beforeafter

from datetime import datetime

def around_month(month, year):
    begin = datetime.strptime(f'1 {month} {year}', '%d %b %Y')
    if begin.month == 12:
        end = datetime(begin.year+1, 1, 1)
    else:
        end = datetime(begin.year, begin.month+1, 1)
    return begin, end

@Client.command(pass_context = True)
async def clear(ctx, number: int, month, year):
    mgs = []
    begin, end = around_month(month, year)
    async for x in Client.logs_from(ctx.message.channel, limit = number, after=begin, before=end):
        mgs.append(x)
    await Client.delete_messages(mgs)

因此,要从丑闻者中删除消息,您可以像

这样调用此命令。
!clear 100 AUG 2018