![](/img/trans.png)
[英]How to convert string to discord.Emoji class in discord.py?
[英]How can I convert a string with the emoji name into a discord emoji?
假设我有一个带有表情符号名称的字符串,例如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'
以便我可以用它做其他事情。 此外,钓鱼竿和鱼只是一个示例表情符号
您是否查看过表情符号模块?
为了让机器人能够对内置的标准表情符号做出反应,它需要表情符号是符号“🎣”,而不是“: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)
它确实需要在任何一侧都有“:”,否则它将无法工作。 但是如果需要,您可以使用''.join(":"+string+":")
您可以使用unicodedata.lookup
函数:
from unicodedata import lookup
def emoji_from_name(name):
return lookup(name.replace("_", " "))
对于公会中的自定义表情符号,您可以使用它。 请记住,您必须在公会中拥有表情符号。
emoji = discord.utils.get(ctx.guild.emojis, name="CUSTOM EMOJI NAME")
其他替代方法是使用bot.get_emoji获取表情符号的 id,您可以使用它来跨公会自定义表情符号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.