discord.py 对嵌入消息没有反应

时间:2021-05-02 18:25:51

标签: python discord discord.py bots

所以我正在尝试编写一个命令,当它看到带有“Diona”的消息时做出反应

async def on_message(message):
channel = message.channel
emoji = '\N{THUMBS UP SIGN}'
if "Diona" in message.content: 
    await message.add_reaction(emoji)

当用户发送“Diona”时,它正在对用户起作用,但是当它来自带有嵌入消息的机器人时,它不会对其做出反应。我如何使它工作?

1 个答案:

答案 0 :(得分:0)

您可以通过遍历 message.embeds 并遍历所有字段,获取标题和描述来获取嵌入内容:

@client.event
async def on_message(message):
    all_data = ""
    if len(message.embeds) > 0:
        for embed in message.embeds: #loop through all embeds in the message
            try: #I use try and except for every, because when the field is empty, TypeError is raised.
                all_data = all_data + embed.description #get embed description
            except TypeError:
                pass
            try:
                all_data = all_data + embed.title #get embed title
            except TypeError:
                pass
            for field in embed.fields: #loop through each field
                try:
                    all_data = all_data + field.value #get value of field
                except TypeError:
                    pass
                try:
                    all_data = all_data + field.name #get name of field
                except TypeError:
                    pass
    if "Diona" in all_data: #test if Diona is in any of the data we got
        await message.add_reaction(emoji)
    if "Diona" in message.content: 
        await message.add_reaction(emoji)

参考: