首页 > 解决方案 > 如何使用 discord.PartialEmoji/discord.Emoji 对象?

问题描述

我需要一个表情符号作为反应角色的表情符号对象,但无论我做什么我都会得到错误

discord.ext.commands.errors.PartialEmojiConversionFailure:无法将“”转换为 PartialEmoji。

如果我使用 discord.PartialEmoji 或

discord.ext.commands.errors.EmojiNotFound:找不到表情符号“”。

如果我使用 discord.Eomji。

发生这些错误的示例代码

@commands.command() 
async def test(self,ctx,emoji: discord.Emoji = None): 
    await ctx.send(emoji)

编辑:

首先应该像这样创建反应角色:

@commands.command() 
async def reactionrole(self,ctx,msgid: int = None,emoji: discord.Emoji = None , role: discord.Role = None ): 
    
    
    if msgid is None:
        await ctx.send('Use the following template to create reactionroles (ex. treactionrole <#messageid> <emoji> <@role>)')
    elif emoji is None:
        await ctx.send('Use the following template to create reactionroles (ex. treactionrole <#messageid> <emoji> <@role>)')
    elif role is None:
        await ctx.send('Use the following template to create reactionroles (ex. treactionrole <#messageid> <emoji> <@role>)')
    else:
        if ctx.message.author.guild_permissions.manage_roles:
                    db = sqlite3.connect("db.sqlite")
                    cursor = db.cursor() 
                    sql = ("INSERT INTO reaction_role(message_id,emoji,role) VALUES(?,?,?)")

                    val = (msgid,emoji,str(role))
                    msg = await ctx.channel.fetch_message(int(msgid))
                    await msg.add_reaction(emoji)
                    cursor.execute(sql,val)
                    db.commit()
                    cursor.close()
                    db.close()
        else:
            await ctx.author.send('You dont have the permission to use reactionroles on this Server!')

然后,如果发生反应,一种方法应该从数据库中获取消息,其中 msgid 和表情符号与反应的相同,如下所示:

async def reaction_roles_role(msgid,emoji):
        db = sqlite3.connect("db.sqlite")
        cursor = db.cursor()
        cursor.execute(f"SELECT role FROM reaction_role WHERE message_id = {msgid} and emoji = {emoji}")
        result = cursor.fetchone()
        return result

标签: pythondiscorddiscord.pyemoji

解决方案


您是为此使用默认表情符号还是来自其他服务器的表情符号?因为 discord.Emoji 仅适用于来自服务器的自定义表情符号,并且您的机器人在上面。

如果您想获得所有这些,则必须执行以下操作:

@commands.command() 
async def test(self, ctx, emoji): 
    print(str(emoji))

对于反应角色,您可以on_raw_reaction_add像这样比较:

if str(payload.emoji) == str(your_emoji_here):

推荐阅读