如何在discord.py中收集超过特定日期的消息



我试图添加一个功能,将检查用户在过去2天内发送的消息数量,现在我循环通过所有频道的历史和检查消息1次,但与公会,每天获得数千条消息,这需要很长时间,是否有一种方法,我可以指定一个最大的时间来回顾?

这里是我当前的代码

# This is also inside of a cog
now = datetime.datetime.now()
joined_on = ctx.author.joined_at
delta = datetime.timedelta(joined_on, now)
if delta.days >= 2:
message = 0
for channel in ctx.guild.channels:
if channel.id != 863871156298055751:
for message in channel.messages:
sent_at = message.created_at
delta = datetime.timedelta(sent_at, now)
if delta.days <= 2 and message.author.id == ctx.author.id:
messages += 1

有一个版本的history可以在通道上工作。您还可以指定查看的时间范围。

参见TextChannel.history

counter = 0
for channel in ctx.guild.channels:
try:
if not isinstance(channel, discord.TextChannel):
continue
# set a hard limit on the messages taken, or limit the amount of time to search
async for msg in channel.history(limit=100, after=datetime.datetime.utcnow()-datetime.timedelta(hours=1)):
# change this
if msg.author.id == ctx.author.id:
counter += 1
except discord.errors.Forbidden:
pass
await ctx.send(counter)

最新更新