简体   繁体   English

如何让我的 discord.py 机器人向我重复触发词?

[英]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.所以我希望我的discord.py机器人在它发送的消息中使用触发侦听器响应的词。

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.在这一点上,我需要做的就是定义什么是animal 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!它可能非常简单,我只是看不到它,但提前感谢您的帮助!

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.如果其中一只动物包含在message.content中,则将动物格式化为嵌入并发送嵌入。

@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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM