How should the value of the after attribute for the operation .logs_from() be formatted?

290 views Asked by At

I am attempting to retrieve an iterable of all messages sent today (as in the day that the script is run) from all channels that my bot has access to.

As it stands I have been attempting to use the after attribute of .logs_from() however I cannot seem to get this to act as I would like it to:

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)

Running this seems to just output a list of all messages from the channels my bot has access to. I'm assuming that I my input for the value of after is not in the correct format, however the documentation merely states:

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.

Which I was under the impression I was doing.

Is anyone able to advise what the proper way to declare the value of the after attribute for .logs_from() is?

1

There are 1 answers

0
Ethan Field On BEST ANSWER

Thanks to https://stackoverflow.com/users/8360823/squaswin for the suggestion. Thanks to https://stackoverflow.com/users/131187/bill-bell for pointing out the UTC timezone differences.

Using datetime instead of time seems to work as required.

See below:

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)

The above returns all messages sent today.

To include a specific time of day, using the below works:

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)

However both of these above return the current timezone, to get the current date in UTC, you can use the below:

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)