M
M
maxwell20102020-10-01 20:38:24
Python
maxwell2010, 2020-10-01 20:38:24

How to get messages for the last few hours?

Is it possible to pull out the last messages in a few hours from the channel by force or do I need to drag the database?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Nevzorov, 2020-10-02
@maxwell2010

async for ... in history(...): https://discordpy.readthedocs.io/en/stable/api.htm...

counter = 0
async for message in channel.history(limit=200):
    if message.author == client.user:
        counter += 1

messages = await channel.history(limit=123).flatten()
# messages is now a list of Message...

To receive messages for the last 2 hours:
from datetime import datetime, timedelta
# ...
async for message in channel.history(
    limit=None,  # If None, retrieves every message in the channel. Note, however, that this would make it a slow operation.
    after=datetime.utcnow()-timedelta(hours=2) # If a date is provided it must be a timezone-naive datetime representing UTC time. (https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow)
):

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question