You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3中如何将discord.py反应对象的Emoji转换为对应名称

How to Get the Emoji Name/Shortcode from a discord.py Reaction Object

Absolutely! You can definitely get the corresponding shortcode (like :one: for 1⃣) from a discord.py Reaction object. Here's how to do it:

Step 1: Install the emoji library

First, you'll need the emoji package to convert unicode emojis to their shortcode names. Install it via pip:

pip install emoji

Step 2: Use the library with your Reaction object

In your discord.py event handler (like on_reaction_add), you can check the type of emoji and retrieve its name/shortcode accordingly:

import discord
from emoji import demojize

intents = discord.Intents.default()
intents.reactions = True
intents.message_content = True

client = discord.Client(intents=intents)

@client.event
async def on_reaction_add(reaction, user):
    # Check if the emoji is a custom emoji (not unicode)
    if isinstance(reaction.emoji, discord.Emoji):
        # Custom emojis have a 'name' attribute directly
        emoji_name = f":{reaction.emoji.name}:"
    else:
        # For unicode emojis, use demojize to get the shortcode
        emoji_name = demojize(reaction.emoji)
    
    print(f"Reaction emoji name: {emoji_name}")
    # If you just want the name without the colons, use:
    # print(emoji_name.strip(':'))

client.run('YOUR_BOT_TOKEN')

How it works:

  • For custom emojis (uploaded to a server), reaction.emoji is a discord.Emoji object, which has a name property we can wrap in colons to match the shortcode format.
  • For unicode emojis like 1⃣, reaction.emoji will be either a string (in older discord.py versions) or a PartialEmoji object (in v2.x+). Either way, passing it to demojize() will return the shortcode like :one:.

Testing with your example:

If the reaction is 1⃣, the code will print Reaction emoji name: :one:—exactly what you're looking for!


内容的提问来源于stack exchange,提问作者W. Reyna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:38:55