How to use discord.PartialEmoji/discord.Emoji object?

Viewed 455

I need to get an emoji as an emoji object for reaction roles but i whatever i do i ether get the error

discord.ext.commands.errors.PartialEmojiConversionFailure: Couldn't convert " " to PartialEmoji.

if i am using discord.PartialEmoji or

discord.ext.commands.errors.EmojiNotFound: Emoji " " not found.

if i am using discord.Eomji.

Example Code where these errors happen

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

Edit:

At first the reaction role should be created like this:

@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!')

Then if an reaction happens an method should get the message out of the db where the msgid and the emoji is the same as the reacted one like this:

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
1 Answers

Are you using default emojis for this or Emojis from other servers? Because discord.Emoji only works with custom Emojis from servers with your bot on it.

If you wanna get all of these you have to do something like this:

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

And for reaction roles you could compare in on_raw_reaction_add like this:

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