【问题标题】:Getting emoji object to unicode将表情符号对象转换为 unicode
【发布时间】:2020-09-27 22:46:36
【问题描述】:

现在我正在制作 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>>>

【问题讨论】:

    标签: python unicode discord.py emoji


    【解决方案1】:

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

    import unicodedata
    unicodedata.name(emoji.name)
    

    【讨论】:

      【解决方案2】:

      我已经意识到我的错误了!我一直能够比较从reactionaddpayload.emoji 的unicode!不过,我最初无法看到,vs code 将 unicode 显示为表情符号(在终端中),而不是字符串。这现在更有意义了!感谢他们提供的所有帮助!

      【讨论】:

        猜你喜欢
        • 2015-10-18
        • 2015-01-15
        • 1970-01-01
        • 2018-11-16
        • 1970-01-01
        • 2019-07-11
        • 2012-01-27
        • 2018-07-19
        • 2019-05-20
        相关资源
        最近更新 更多