如果消息有disco py的emoji,如何检查on_message?

问题描述 投票:0回答:1

我很擅长使用p​​ython而且我正在尝试使用基本函数对discord bot进行编码,并且我希望其中一个是在有人使用前缀之后如果他们发布了一个表情符号,那就是机器人用url回复表情符号,但我很难尝试使它工作。我不知道如何使用on_message来检查邮件是否包含表情符号。谁能帮助我?谢谢。

#if message.content[0] == "!" and message.content[1:4] == " <:":
            #print(message.content[4:-1])
            #emoji_name = ""
            #for i in str(message.content[4:-1]):
                #if i != ":":
                    #emoji_name += i
                #if i == ":":
                    #print(emoji_name)
                    #break
            #await client.send_message(message.channel, get(client.get_all_emojis(), name=emoji_name).url)
discord discord.py
1个回答
2
投票

Discord自定义表情符号有<:name:id>的形式。因此,如果您使用re,Python正则表达式库,您可以在给定的消息中找到所有自定义表情符号。

import re
import discord

@client.event
async def on_message(msg):
    custom_emojis = re.findall(r'<:\w*:\d*>', msg.content)
    custom_emojis = [int(e.split(':')[1].replace('>', '')) for e in custom_emojis]
    custom_emojis = [discord.utils.get(client.get_all_emojis(), id=e) for e in custom_emojis]
    # From now, `custom_emojis` is `list` of `discord.Emoji` that `msg` contains.

我现在没有测试这个代码,但之前我使用过这个方法,发现它有效。所以,如果这不起作用,请给我反馈,然后我会发现我输入错误。

© www.soinside.com 2019 - 2024. All rights reserved.