基础概念: Discord.py 是一个用于与 Discord API 交互的 Python 库。反应角色(Reaction Role)是一种自动化角色分配机制,允许用户通过点击表情符号来获得或移除角色。
可能的原因及解决方案:
on_reaction_add
和 on_reaction_remove
事件监听器。示例代码: 以下是一个简单的反应角色实现示例:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.reactions = True
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
print(f'Bot is ready. Connected to {len(bot.guilds)} guilds.')
@bot.event
async def on_reaction_add(reaction, user):
if user.bot:
return # Ignore bot reactions
message_id = 1234567890 # Replace with your message ID
emoji = '👍' # Replace with your emoji
role_id = 9876543210 # Replace with your role ID
if reaction.message.id == message_id and str(reaction.emoji) == emoji:
guild = reaction.message.guild
role = guild.get_role(role_id)
if role:
await user.add_roles(role)
print(f'{user.name} was given the role {role.name}')
@bot.event
async def on_reaction_remove(reaction, user):
if user.bot:
return # Ignore bot reactions
message_id = 1234567890 # Replace with your message ID
emoji = '👍' # Replace with your emoji
role_id = 9876543210 # Replace with your role ID
if reaction.message.id == message_id and str(reaction.emoji) == emoji:
guild = reaction.message.guild
role = guild.get_role(role_id)
if role:
await user.remove_roles(role)
print(f'{user.name} had the role {role.name} removed')
bot.run('YOUR_BOT_TOKEN')
注意事项:
YOUR_BOT_TOKEN
、message_id
、emoji
和 role_id
为实际值。如果上述方法仍然无法解决问题,建议检查 Discord.py 的官方文档或社区论坛以获取更多帮助。