0

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

2 回答 2

0

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

import unicodedata
unicodedata.name(emoji.name)
于 2020-09-28T00:33:25.543 回答
0

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

于 2020-09-28T05:01:24.150 回答