如何将带有表情符号名称的字符串转换为不和谐的表情符号?

时间:2020-09-09 13:32:10

标签: python python-3.x discord.py

假设我有一个带有表情符号名称的字符串,例如emoji = 'fishing_pole_and_fish'

有没有办法做msg.add_reaction(emoji)而又没有错误:

discord.ext.commands.errors.CommandInvokeError:命令引发了 异常:HTTPException:400错误的请求(错误代码:10014):未知 表情符号

还是可以将字符串转换为discord.Emoji

我知道我可以使用'\N{FISHING POLE AND FISH}',但我需要将其用作'fishing_pole_and_fish',以便可以使用它进行其他操作。另外,钓鱼竿和鱼只是表情符号的一个例子

3 个答案:

答案 0 :(得分:1)

您可以使用unicodedata.lookup函数:

from unicodedata import lookup

def emoji_from_name(name):
    return lookup(name.replace("_", " "))

答案 1 :(得分:1)

您是否看过emoji module

为了使机器人能够对内置表情符号中的标准做出反应,它需要将表情符号作为符号“?”,而不是“:fishing_pole:”,仅适用于自定义表情符<:name:id>。 / p>

import emoji

string = ":fishing_pole:"
demoji = emoji.demojize("? ")   # This takes the emoji and gives--> :fishing_pole:
emoji = emoji.emojize(string)    # This takes :fishing_pole: and gives--> ?    
print(demoji)
print(emoji)

它确实需要在两边都带有“:”,否则它将无法工作。但是您可以根据需要添加''.join(":"+string+":")

答案 2 :(得分:0)

对于公会中的自定义表情符号,您可以使用它。请记住,公会中必须有表情符号。

{"name":["This field is required."],"email_id":["This field is required."],"subject":["This field is required."]}

另一种替代方法是使用bot.get_emoji来获取表情符号的ID,您可以使用它来跨公会自定义表情符号