如何使用discord.py为机器人消息添加反应按钮并支持成员添加任意表情反应?
Hey there! Let's walk through how to handle both of your Discord bot needs step by step.
Good news—bot-sent messages already let members add any emoji reaction by default, as long as the member has the "Add Reactions" permission in the channel. If you're having issues with members not being able to add reactions (including that winking face with a plus sign), double-check these things:
- Channel permissions: Make sure the relevant role/member has the "Add Reactions" permission enabled for the channel
- Emoji availability: If it's a server custom emoji, ensure the member has access to use that emoji (via role permissions for the emoji's role group)
Whether it's a Unicode emoji like 😉➕ or a custom server emoji, as long as the member can send the emoji in the channel, they can add it as a reaction to your bot's messages—no extra code needed here!
For clickable reaction buttons (the interactive kind), you'll need to use discord.py's UI component system (this requires discord.py version 2.0 or higher). Here's a complete, working example:
First, set up a View class to hold your buttons, then attach it to a message:
import discord from discord.ext import commands # Initialize the bot with necessary intents bot = commands.Bot(command_prefix="!", intents=discord.Intents.all()) # Define a View class to contain our buttons class ReactionButtonView(discord.ui.View): def __init__(self): # Set timeout=None to make buttons permanent (they won't expire automatically) super().__init__(timeout=None) # Winking face button @discord.ui.button(label="😉", style=discord.ButtonStyle.primary) async def wink_button_callback(self, interaction: discord.Interaction, button: discord.ui.Button): # Send an ephemeral reply only the user can see await interaction.response.send_message(f"{interaction.user.mention} tapped the wink button!", ephemeral=True) # Winking face with plus sign button @discord.ui.button(label="😉+", style=discord.ButtonStyle.success) async def wink_plus_button_callback(self, interaction: discord.Interaction, button: discord.ui.Button): await interaction.response.send_message(f"{interaction.user.mention} tapped the wink+ button!", ephemeral=True) # Test command to send a message with buttons @bot.command() async def send_reaction_buttons(ctx): await ctx.send("Check out these interactive buttons!", view=ReactionButtonView()) # Run the bot with your token bot.run("YOUR_BOT_TOKEN_HERE")
Key Notes:
- View Class: All interactive components (buttons, select menus) live inside a View, which you pass to the
send()method via theviewparameter - Button Styles: Use
discord.ButtonStyleto change the button's appearance—options includeprimary(blue),success(green),danger(red),secondary(gray), andlink(for external URLs) - Callback Functions: Each button needs an async callback function that triggers when clicked. The
interactionparameter lets you interact with the user who clicked (send replies, get their info, etc.) - Timeout: Setting
timeout=Nonekeeps buttons active indefinitely. If you want them to expire after a set time, pass a number of seconds (e.g.,timeout=300for 5 minutes)
If you need to add buttons to an existing message, you can use message.edit(view=ReactionButtonView())—just make sure you have the Message object for the message you want to update.
内容的提问来源于stack exchange,提问作者heisenbaig




