下面是我的案例的当前工作代码
我正在为我的Discord服务器开发一个机器人,并试图为有反应的用户分配一个角色。
我目前的状态如下:(我使用了最新的Python/Discord.py版本(
import discord
from discord.ext import commands
bot = commands.Bot(intents=intents)
class GetRole(commands.Cog):
def __init__(self, bot):
self.bot = bot
@bot.event
async def on_raw_reacion_add(payload):
ourMessageID = YourMessageID
if ourMessageID == payload.message_id:
member = payload.member
guild = member.guild
if payload.emoji.name == '<:YouTube:864980517630902302>':
role = discord.utils.get(guild.roles, name="YouTube")
elif payload.emoji.name == '<:Twitch:864980843176787988>':
role = discord.utils.get(guild.roles, name="Twitch")
elif payload.emoji.name == '<:Discord:864980938068852757>':
role = discord.utils.get(guild.roles, name="Discord")
elif payload.emoji.name == '<a:alert:864983948987990081>':
role = discord.utils.get(guild.roles, name="Community-Events")
await member.add_roles(role)
(MessageID所指的消息已经发送(
它不会在Visual Studio代码中产生错误,但角色仍然不会添加到对消息做出反应的用户中。
也许是因为Discord.py的版本,但我不知道确切的原因。
确保启用了适当的意图。根据Discord.py API参考,成员和反应意图都需要使用on_raw_reaction_add
事件。在开发人员门户中启用它们,并在代码中添加如下意图:
intents = discord.Intents.default()
intents.members = True
intents.reactions = True
bot = commands.Bot(intents=intents)
这是现在的工作代码:
@commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
print("test")
print(payload.emoji.name)
ourMessageID = 867876893411508234
if ourMessageID == payload.message_id:
member = payload.member
guild = member.guild
if payload.emoji.name == 'YouTube':
role = discord.utils.get(guild.roles, name="YouTube")
elif payload.emoji.name == 'Twitch':
role = discord.utils.get(guild.roles, name="Twitch")
elif payload.emoji.name == 'Discord':
role = discord.utils.get(guild.roles, name="Discord")
elif payload.emoji.name == 'alert':
role = discord.utils.get(guild.roles, name="Community-Events")
await member.add_roles(role)
我有打字错误,使用了(我认为(过时的evets等。现在一切都恢复了,至少对我来说是