Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I make my discord.py bot repeat the trigger word back to me?

So I want my discord.py bot to use the word that triggered the listener response in the message it sends.

For example:

@commands.Cog.listener()
async def on_message(self, message):
    if message.author == self.bot.user:
        return
    msg = message.content
    
    trigger = ["cat", "dog", "Snake", "Rabbit"]
    
    embed = discord.Embed(description=f"{animal} is the best animal!")

    if any(word in msg for word in trigger):
        await message.channel.send(embed=embed)

At this point all I need to do is to define what animal is.
I know that I could write a custom answer for every single word but I would have to write it for every animal I add in the list which would take a long time. So I want to have the code to pick up the word that triggered the response in the first place.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So when I type ‘I love my cat’ the bot responds with ‘cat is the best animal!’

It’s probably super simple and I just can’t see it but thanks for your help in advance!

>Solution :

It seems like you were along the correct lines of thinking. All you have to do is iterate through the list of animals; if one of the animals is contained with message.content, format the animal into the embed and send the embed.

@commands.Cog.listener()
async def on_message(self, message):
    if message.author == self.bot.user:
        return
    
    trigger = ["cat", "dog", "Snake", "Rabbit"]
    
    for animal in trigger:
        if animal in message.content:
            embed = discord.Embed(description=f"{animal} is the best animal!")
            await message.channel.send(embed=embed)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading