如何检查on_message消息中是否包含不和谐的py表情符号? [英] How check on_message if message has emoji for discord py?
问题描述
我在使用python方面还比较陌生,我正在尝试使用基本功能编写一个不和谐的bot,我希望其中之一是在有人使用前缀后,如果他们张贴了该bot回复的表情符号,表情符号的网址,但是我很难使它起作用.我不知道如何使用on_message检查邮件中是否包含表情符号.有人可以帮我吗?谢谢.
I'm fairly new at using python and I'm trying to code a discord bot with basics functions, and I want for one of them to be that after someone uses a prefix if they post an emoji that the bot replies with the url of the emoji, but I have trouble trying to make it work. I don't know how to use on_message to check if a message contains an emoji. Could anyone help me? Thanks.
#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自定义表情符号的形式为<:name:id>
.因此,如果您使用Python正则表达式库 re
,则可以在给定消息中找到所有自定义表情符号.
Discord custom emoji has a form of <:name:id>
. So if you use re
, Python regex library, you can find all the custom emojis in the given message.
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.
我现在没有测试此代码,但是我以前使用过此方法,并发现它可以工作.因此,如果这不起作用,请给我反馈,然后我会发现我输错了.
I didn't test this code now, but i used this method before and found it worked. So please give me feedback if this doesn't work, then i will find if i mis-typed.
这篇关于如何检查on_message消息中是否包含不和谐的py表情符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!