我正在尝试使此for_log协程正常工作,我试图做的是删除特定月份的所有消息,在这种情况下,八月,我浏览了Internet和文档,并在另一个问题的帮助下张贴在这里,并提出了这个:
<div class="wrapper">
<div class="container">
<div>A very</div>
<div>Big chicken</div>
<div>Comes to</div>
<div>Dine with us</div>
<div>Every day</div>
</div>
<div class="container-label">
<h1>my label</h1>
</div>
</div>
然后使用@Client.command(pass_context = True)
async def clear(ctx, number: int, month, year):
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)
return begin, end
if ctx.message.author.id not in AdminIDs:
await Client.send_message(ctx.message.channel, 'You do not have permission to use this command')
return
counter = 0
begin, end = around_month(month, year)
tmsg = await Client.send_message(ctx.message.channel, 'Progress: 0/' + str(number))
async for x in Client.logs_from(ctx.message.channel, limit = number, after=begin, before=end):
counter += 1
print(counter)
await Client.edit_message(tmsg, 'Progress:' + counter + '/' + str(number))
await Client.delete_messages(x)
await asyncio.sleep(1.5)
await Client.send_message(ctx.message.channel, 'Operation completed! ' + 'Cleared: ' + str(counter) + ' items')
乍一看似乎不错,但是由于某种原因,它不会删除任何消息,我插入了一个计数器,看看for循环是否确实通过了该计数器,但是什么也没得到,它仍然为零,很有趣,我尝试在计数器位于for循环中时打印计数器,但没有将其打印到控制台,我能想到的唯一原因是,如果它根本没有进入for循环,是因为它找不到任何消息?我不确定。控制台或其他任何地方都没有错误
答案 0 :(得分:0)
一无所获
这是正确的推论。以此为基础,您会发现around_month
返回了相同日期的元组。
begin, end = around_month(month, year)
begin == end # True
来自Discord文档 1 ,
之前,之后和周围的键是互斥的,一次只能传递一个。
鉴于您需要从特定月份删除邮件,因此您将需要提取该月份的邮件,直到用尽该月份创建的邮件列表为止。
此外,您还需要在雪花时间调用Client.logs_from
。有一个方便的实用程序函数,用于将datetime
对象转换为时间雪花。 2
from discord.utils import time_snowflake
from datetime import time delta
# ...
def before_time(messages, before):
return [
message for message in messages
if message.id < before
]
after_dt = datetime.strptime(f'1 {month} {year}', '%d %b %Y')
before_dt = (after_dt + timedelta(days=31)).replace(day=1)
after = time_snowflake(after_dt)
before = time_snowflake(before_dt)
messages = await Client.logs_from(
ctx.message.channel, limit=number, after=after
)
marked_for_deletion = before_time(messages, before)
while marked_for_deletion:
await Client.delete_messages(marked_for_deletion)
messages = await Client.logs_from(
ctx.message.channel, limit=number, after=after
)
marked_for_deletion = before_time(messages, before)
await asyncio.sleep(1.5)