将表情符号对象转换为unicode

时间:2020-09-27 22:46:36

标签: python unicode discord.py emoji

现在,在我制作Discordbot的旅程中,我正在处理反应角色!我想我已经完成了大多数设置,但是我似乎找不到从表情符号对象转换为表情符号unicode的方法,因此我可以比较它们并添加反应。这是我到目前为止的内容:

@bot.event
async def on_raw_reaction_add(payload):
    print(payload)
    channel = bot.get_channel(payload.channel_id)
    emoji = payload.emoji
    print(emoji.id)

@bot.command(pass_context=True)
async def reactionrole(ctx, title:str, description:str, name:str, value:str):
    title = ''.join(title)
    description = ''.join(description)
    name = ''.join(name)
    value = ''.join(value)
    embed = discord.Embed(colour = discord.Colour.teal(), title = title, description = description)
    embed.add_field(name = name, value = value, inline=False)
    message = await ctx.send(embed = embed)
    with open("reactionroles.json", 'r') as f:
        data = json.load(f)
    addon = {"guild-id" : ctx.guild.id}, {"message-id" : message.id}, {"roles" : []}, {"emojis" : []}
    data[ctx.guild.id] = addon
    with open("reactionroles.json", 'w') as f:
        json.dump(data, f)

@bot.command(pass_context=True)
async def reactionadd(ctx, emoji, *role:str):
    role = ''.join(role)
    with open("reactionroles.json", 'r') as f:
        data = json.load(f)
    message_id = data[str(ctx.guild.id)][1]['message-id']
    message_id = int(message_id)
    message = await ctx.channel.fetch_message(message_id)
    await message.add_reaction(emoji)
    role = discord.utils.get(ctx.guild.roles, name=role)
    data[str(ctx.guild.id)][3]['emojis'].append(emoji)
    print(role)
    data[str(ctx.guild.id)][2]['roles'].append(str(role))
    with open("reactionroles.json", 'w') as f:
        json.dump(data, f, indent=4)

因此,命令reactionrole创建嵌入,并将一些占位符数据写入json文件。之后,reactionadd命令将表情符号unicode添加到json数组,并将角色添加到其他json数组。在添加响应时,在on_raw_reaction_add(payload)中,有效负载仅具有表情符号的名称,而没有unicode。因此,我无法将两者进行比较,以了解表情符号对什么起作用。我无法在reactionadd中保存原始数据,因为我会在on_reaction_add中遇到问题。我迷失了从有效载荷中获取unicode的能力,这是我最后的援助。这是有效载荷内部的内容:

<RawReactionActionEvent message_id=759903170721087508 user_id=146348630926819328 channel_id=754904403710050375 guild_id=665787149513261057 emoji=<PartialEmoji animated=False name='�?' id=Non
 event_type='REACTION_ADD' member=<Member id=146348630926819328 name='Chai' discriminator='6396' bot=False nick=None guild=<Guild id=665787149513261057 name="ChaiBot's Playground" shard_id=None chunked=True member_count=34>>>

2 个答案:

答案 0 :(得分:0)

Emoji对象中可以从emoji.name获取unicode字符,或者,如果要查找unicode名称,则可以使用python unicodedata库:

import unicodedata
unicodedata.name(emoji.name)

答案 1 :(得分:0)

我已经意识到我的错误!我一直可以比较reactionaddpayload.emoji的unicode!我最初看不到,但是vs代码将unicode显示为表情符号(在终端中),而不显示为字符串。现在这更有意义了!感谢他们的帮助!