如果消息包含不和谐 py 的表情符号,如何检查 on_message? [英] How check on_message if message has emoji for discord py?

查看:18
本文介绍了如果消息包含不和谐 py 的表情符号,如何检查 on_message?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 python 方面相当新,我正在尝试编写一个具有基本功能的不和谐机器人,我希望其中一个是在有人使用前缀后,如果他们发布了机器人回复的表情符号表情符号的网址,但我无法让它工作.我不知道如何使用 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.

这篇关于如果消息包含不和谐 py 的表情符号,如何检查 on_message?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆