Skip to content
Advertisement

Check if the last 2 messages were sent by the same person in discord.py

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:

async def on_message(self, message):
    lastmessage = #something to check the last message
    if message.author.id == lastmessage.author.id:
        #...

Thanks.

Advertisement

Answer

You can get the channel of the message, and then get the history:

async def on_message(self, message):
    lastmessage = [msg async for msg in message.channel.history(limit=2)][1]
    if message.author.id == lastmessage.author.id:
        #...

Or, alternatively, if you want to check if all of the last n messages were sent by the same author:

async def on_message(self, message):
    author_ids = [msg.author.id async for msg in message.channel.history(limit=n)]
    if len(set(author_ids)) <= 1:
        #...
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement