How should the value of the after attribute for the operation .logs_from() be formatted?
我正在尝试从我的bot可以访问的所有通道中检索今天发送的所有消息(如在运行脚本的当天)。
就目前而言,我一直试图使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import discord import asyncio import time client = discord.Client() today = time.strftime("%Y-%m-%d") @client.event async def on_ready(): for i in client.get_all_channels(): async for c in client.logs_from(i, after=today): print(c.content) client.run(INSERT_SESSION_KEY) |
运行这个程序似乎只是输出我的bot可以访问的所有消息的列表。我假设我输入的
after (Message or datetime) – The message or date after which all returned messages must be. If a date is provided it must be a timezone-naive datetime representing UTC time.
在我的印象中。
是否有人能够建议声明
感谢https://stackoverflow.com/users/8360823/squaswin的建议。感谢https://stackoverflow.com/users/131187/bill-bell指出了UTC时区的差异。
使用
见下文:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import discord import asyncio import datetime client = discord.Client() @client.event async def on_ready(): for i in client.get_all_channels(): async for c in client.logs_from(i, after=date.today()): print(c.content) client.run(INSERT_SESSION_KEY) |
上面返回了今天发送的所有消息。
包括一天中的特定时间,使用以下工作:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import discord import asyncio import datetime client = discord.Client() @client.event async def on_ready(): for i in client.get_all_channels(): async for c in client.logs_from(i, after=datetime.now()): print(c.content) client.run(INSERT_SESSION_KEY) |
但是,上面的两个都返回当前时区,要以UTC格式获取当前日期,可以使用以下命令:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import discord import asyncio import datetime client = discord.Client() @client.event async def on_ready(): for i in client.get_all_channels(): async for c in client.logs_from(i, after=datetime.utcnow().date()): print(c.content) client.run(INSERT_SESSION_KEY) |