Reputation: 13
Here's the basic idea - I don't want the triggering message's author, but the message that came right before the triggering message's author.
Hopefully giving an example will how what I mean:
Initial message: Author1: Foo
Trigger message:Author2: That thing you said is cool
Bot's Response:Bot: @Author1 is cool!
Clearly, the message from Author2
is what is triggering the bot, but how would I get the bot to find the author of the previous message? Here is what I got so far:
@commands.Cog.listener('on_message')
async def cool(self, message):
if message.author == self.bot.user :
return
elif "cool" in message.content.lower():
response = f"{<this_is_where_ill_put_author1_id>} is cool!"
await message.channel.send(response)
Upvotes: 1
Views: 311
Reputation: 184
@commands.Cog.listener('on_message')
async def cool(self, message):
if message.author == self.bot.user :
return
elif "cool" in message.content.lower():
counter = 0
async for msg in message.channel.history(limit = 2, oldest_first = False):
if counter == 1:
author = msg.author
counter += 1
response = f"{author} is cool!"
await message.channel.send(response)
You can do it like this. I just look at the last two posts in the channel and take the first of the two (in time), then get the author.
Here is the documentation of the method I use on the text channel: https://discordpy.readthedocs.io/en/stable/api.html#discord.TextChannel.history
If the channel is very active, you can also take only the messages posted before the time of the message that triggered the event with the before parameter of the history method.
Upvotes: 1