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

Time:07-17

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.

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!

CodePudding user response:

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)
  • Related