简体   繁体   English

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

[英]How can I convert a string with the emoji name into a discord emoji?

Let's say I have a string with an emoji name, for example emoji = 'fishing_pole_and_fish' .假设我有一个带有表情符号名称的字符串,例如emoji = 'fishing_pole_and_fish'

Is there a way to do msg.add_reaction(emoji) without getting the error:有没有办法在没有得到错误的情况下做msg.add_reaction(emoji)

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: HTTPException: 400 Bad Request (error code: 10014): Unknown Emoji discord.ext.commands.errors.CommandInvokeError:命令引发异常:HTTPException:400 错误请求(错误代码:10014):未知表情符号

Or can I convert the string to a discord.Emoji ?或者我可以将字符串转换为discord.Emoji吗?

I know I could use '\\N{FISHING POLE AND FISH}' , but I need it as 'fishing_pole_and_fish' so I can do other things with it.我知道我可以使用'\\N{FISHING POLE AND FISH}' ,但我需要它作为'fishing_pole_and_fish'以便我可以用它做其他事情。 Also, fishing pole and fish is just an example emoji此外,钓鱼竿和鱼只是一个示例表情符号

Have you looked in to the emoji module ?您是否查看过表情符号模块

For the bot to be able to react with the standard built in emojis, it needs the emojis to be the symbol "🎣 ", not ":fishing_pole:", that only works with custom emojis <:name:id> .为了让机器人能够对内置的标准表情符号做出反应,它需要表情符号是符号“🎣”,而不是“:fishing_pole:”,它只适用于自定义表情符号<:name:id>

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)

It does require having ":" on either side, otherwise it wont work.它确实需要在任何一侧都有“:”,否则它将无法工作。 But that you can add with ''.join(":"+string+":") if needed但是如果需要,您可以使用''.join(":"+string+":")

You can use the unicodedata.lookup function:您可以使用unicodedata.lookup函数:

from unicodedata import lookup

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

For custom emojis in the guild you can use this.对于公会中的自定义表情符号,您可以使用它。 keep in mind you must have the emoji in the guild.请记住,您必须在公会中拥有表情符号。

emoji = discord.utils.get(ctx.guild.emojis, name="CUSTOM EMOJI NAME")

Other alternative is to use the bot.get_emoji which takes the id of the emoji, you can use this to have across guild custom emojis其他替代方法是使用bot.get_emoji获取表情符号的 id,您可以使用它来跨公会自定义表情符号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM