on raw reaction add throws an error: discord.py
Question:
I was working on an "self-reaction" script for my discord bot, so I was using on_reaction_add()
and it worked fine, but I realized that as I restarted my bot, he couldn’t register the reactions on that first message, so I did some research and I figured out that I had to use on_raw_reaction_add()
I tried it and I worked fine, but the last line of code threw an error:
@Client.event
async def on_raw_reaction_add(payload):
if payload.user_id == Client.user.id:
return
if str(payload.emoji) == " " and payload.channel_id == 1078702317386524170:
guild =Client.get_guild(1075533700804530239)
role = guild.get_role(1078715150363795417)
await discord.User(payload.user_id).add_roles(role)
Error:
TypeError: BaseUser.__init__() takes 1 positional argument but 2 were given
Whole Error: here
Thanks! 🙂
Answers:
The issue is that you’re trying to instantiate a discord.User
yourself. You’ve already gotten the guild
, let’s use get_member
to get the user so we can add the role.
@Client.event
async def on_raw_reaction_add(payload):
if payload.user_id == Client.user.id:
return
if str(payload.emoji) == " " and payload.channel_id == 1078702317386524170:
guild = Client.get_guild(1075533700804530239)
role = guild.get_role(1078715150363795417)
user = guild.get_member(payload.user_id)
if not user:
# incase they are not cached
user = await guild.fetch_member(payload.user_id)
await user.add_roles(role)
I was working on an "self-reaction" script for my discord bot, so I was using on_reaction_add()
and it worked fine, but I realized that as I restarted my bot, he couldn’t register the reactions on that first message, so I did some research and I figured out that I had to use on_raw_reaction_add()
I tried it and I worked fine, but the last line of code threw an error:
@Client.event
async def on_raw_reaction_add(payload):
if payload.user_id == Client.user.id:
return
if str(payload.emoji) == " " and payload.channel_id == 1078702317386524170:
guild =Client.get_guild(1075533700804530239)
role = guild.get_role(1078715150363795417)
await discord.User(payload.user_id).add_roles(role)
Error:
TypeError: BaseUser.__init__() takes 1 positional argument but 2 were given
Whole Error: here
Thanks! 🙂
The issue is that you’re trying to instantiate a discord.User
yourself. You’ve already gotten the guild
, let’s use get_member
to get the user so we can add the role.
@Client.event
async def on_raw_reaction_add(payload):
if payload.user_id == Client.user.id:
return
if str(payload.emoji) == " " and payload.channel_id == 1078702317386524170:
guild = Client.get_guild(1075533700804530239)
role = guild.get_role(1078715150363795417)
user = guild.get_member(payload.user_id)
if not user:
# incase they are not cached
user = await guild.fetch_member(payload.user_id)
await user.add_roles(role)