Get the last message a user sent using discord.py?
Old answer discord.py async (pre-rewrite)
Use log_froms
to get the messages from a channel.
and use get_all_channels
to go through all the channels.
Then search through the results for the latest version of the author. You have to go through each channel for a reasonable amount until you find a message from that person and then stop. The compare the first from each channel to get the latest time.
To get better help in the future consider joining the "discord api" discord server
Edit: Method compatible with discord.py rewrite (+1.0)
You can use channel.history()
to get the history of a channel. It by default only fetches the latest 100 messages from a channel. You can increae this limit with the limit
keyword. (e.g. channel.history(limit = 200)
You can combine this with a find
async iterator to get just the messages from the user with the id you are looking for await channel.history().find(lambda m: m.author.id == users_id)
.
You then need to loop through each text channel in the server doing this and find the latest message in channel by comparing them to the previous fetched message and keeping the one which was created newer.
Example command which find the latest message by the user.
@commands.command()
async def lastMessage(self, ctx, users_id: int):
oldestMessage = None
for channel in ctx.guild.text_channels:
fetchMessage = await channel.history().find(lambda m: m.author.id == users_id)
if fetchMessage is None:
continue
if oldestMessage is None:
oldestMessage = fetchMessage
else:
if fetchMessage.created_at > oldestMessage.created_at:
oldestMessage = fetchMessage
if (oldestMessage is not None):
await ctx.send(f"Oldest message is {oldestMessage.content}")
else:
await ctx.send("No message found.")
This is a fair slow operation in my testing due to having to make many requests to discord but should work.
Related videos on Youtube

Dave Oink
Updated on July 03, 2020Comments
-
Dave Oink almost 3 years
I'm wondering if there is a way for the bot to get the last message a user has sent in the server chat using discord.py in Python? Thanks a lot
-
makeworld almost 3 yearslog_froms no longer is part of discord.py, but it seems that using
channel.history
is the new way to do this. You can iterate through all the messages and record the authors. -
Tim almost 3 years@Cole128 Thank you for pointing that out! I've just updated my answer to be compatible with discord.py rewrite. If you have any feedback or questions please comment. :)
-
Libra about 2 yearsThis code doesnt seem to work. Claims that certain users have never sent a message despite having done so.
-
Tim about 2 years@Laif Have you tried increased the fetch limit in
await channel.history().find(lambda m: m.author.id == users_id)
likeawait channel.history(limit = 1000).find(lambda m: m.author.id == users_id)
-
Libra about 2 years@Tim I actually did just try this, after working through why it wasn’t working. This doesn’t really work for my use case though. I’m attempting to make a script that auto band users after they have not spoken for x amount of time. Is there some kind of API hook that allows me to check this? Maybe utilizing the search feature?
-
Tim about 2 years@Laif Ah ok, yeah don't think this method would work. My first though is you could do that by keeping a database with all members of server and updating last active date in database when a message is sent. Then maybe every day have a job which runs through the databases and gets all inactive members and assigns role. You may also be able to use purge feature somehow. Have a look at this bot maybe for some more ideas github.com/bhigginsuk/discord-activity-monitor (not used it myself and in discord.js)
-
Libra about 2 years@Tim That’s not a bad idea for the future, but u was kinda trying to fix an issue that has already come to pass. Oh well.