I’d like to know if it’s possible to check if the last 2 messages in a channel (either the specific channel or the whole server, doesn’t really matter for my purpose) were sent by the same person in discord.py, e.g. something like this:
JavaScript
x
5
1
async def on_message(self, message):
2
lastmessage = #something to check the last message
3
if message.author.id == lastmessage.author.id:
4
#...
5
Thanks.
Advertisement
Answer
You can get the channel of the message, and then get the history:
JavaScript
1
5
1
async def on_message(self, message):
2
lastmessage = [msg async for msg in message.channel.history(limit=2)][1]
3
if message.author.id == lastmessage.author.id:
4
#...
5
Or, alternatively, if you want to check if all of the last n
messages were sent by the same author:
JavaScript
1
5
1
async def on_message(self, message):
2
author_ids = [msg.author.id async for msg in message.channel.history(limit=n)]
3
if len(set(author_ids)) <= 1:
4
#...
5