Reputation:
im trying to add a role when the user react to this emojis but i cant get
@bot.command()
async def roles(ctx):
global IDMessage
reaction = await ctx.reply("Select your Game" + '\n' + '\n' + "- Valorant 🦸♀️" '\n' + "- World of Warcraft ⚔" + '\n' + "- League of Legends 🧙♀️" + '\n' + "- Cs:Go 🔫")
await reaction.add_reaction('🦸♀️')
await reaction.add_reaction('⚔')
await reaction.add_reaction('🧙♀️')
await reaction.add_reaction('🔫')
IDMessage = reaction.message_id
And this is the part of the code that doesnt work for me
@bot.event
async def on_reaction_add(reaction, user):
ChID = '920320584032870424'
if reaction.message.channel.id != ChID:
return
if reaction.emoji == "🦸♀️":
Valorant = discord.utils.get(user.server.roles, name="Valorant")
await bot.add_roles(user, Valorant)
Upvotes: 0
Views: 2262
Reputation: 1
##Embedd##
@client.command()
@commands.has_permissions(administrator=True)
async def roles(ctx):
embed = discord.Embed(title="Server Roles", description="roles you can pick", color = discord.Colour.green())
embed.add_field(name="**NOTIFICATIONS**", value="🟡\n", inline=True)
embed.add_field(name="**STREAMER**", value="🟣\n", inline=True)
embed.set_footer(text="Bot created by @FreakSheep#3267")
embed.set_author(name='Flockersbot', icon_url='https://cdn.discordapp.com/attachments/898921206420484108/1066931617643364433/Flockers_Bot.png')
embed.set_thumbnail(url='https://cdn.discordapp.com/attachments/898921206420484108/1066931617643364433/Flockers_Bot.png')
msg = await ctx.send(embed=embed)
msg = await msg.fetch()
await msg.add_reaction('🟡')
await msg.add_reaction('🟣')
##Role React##
if payload.emoji.name == "🟣" and payload.message_id == 1066863494475034644:
role = discord.utils.get(guild.roles, name="Streamer")
if role is not None:
member = discord.utils.find(lambda m: m.id == payload.user_id, guild.members)
await member.add_roles(role)
Upvotes: -1
Reputation: 713
There are some issues here. Instead of on_reaction_add
, use on_raw_reaction_add
. Most bots use the latter instead of the former.
Why you may ask? Because, on_reaction_add
only works for messages in bot's cache.
So every message sent after your bot can read messages is stored in a dictionary or list (this is what a cache is). By default the limit of caching is 1000
messages.
So if you ever restarted your bot, the message will never be in cache, and thus the event won't trigger.
Another issue is that you are comparing a string (ChID
) variable and an integer (reaction.message.channel.id
), they can never be equal so if reaction.message.channel.id != ChID
will always return True and thus won't do anything (as you return for that condition being True).
Third issue is that bot.add_roles
does not exist. add_roles
is a method of discord.Member
object.
Fourth issue is, Member.server
is not a thing, Member.guild
is
So your updated code will be like:
@bot.event
async def on_raw_reaction_add(payload: discord.RawReactionActionEvent):
ChID = 920320584032870424
if payload.channel.id != ChID:
return
if str(payload.emoji) == "🦸♀️":
valorant = discord.utils.get(payload.member.guild.roles, name="Valorant")
await payload.member.add_roles(valorant)
Also my personal advice of not using global
there are very rare few cases where you would actually require this keyword
But it is highly unlikely for you to use this in a bot made with discord.py
lib
discord.on_reaction_add
discord.RawReactionActionEvent
discord.Member.guild
discord.Member.add_roles
Upvotes: 1
Reputation: 31
I had the same problem, and this is how I solved it.
You have the reactions being added, now you need to wait for the user to react.
For this, you can use discord.py’s wait_for function.
Check function:
def check(reaction):
if str(reaction.emoji) == “EMOJIHERE”:
return “emoji name”
elif str(reaction.emoji) == “SECONDEMOJI”:
return “emoji name”
elif str(reaction.emoji) == “THIRDEMOJI”:
return “emoji name”
elif str(reaction.emoji) == “FOURTHEMOJI”:
return “emoji name”
else:
return “error”
Code:
async def roles(ctx):
global IDMessage
reaction = await ctx.reply("Select your Game" + '\n' + '\n' + "- Valorant 🦸♀️" '\n' + "- World of Warcraft ⚔" + '\n' + "- League of Legends 🧙♀️" + '\n' + "- Cs:Go 🔫")
await reaction.add_reaction('🦸♀️')
await reaction.add_reaction('⚔')
await reaction.add_reaction('🧙♀️')
await reaction.add_reaction('🔫')
confirm = await bot.wait_for(“reaction_add”, check=check)
if confirm == "emoji name 1":
doSomething()
elif confirm == "emoji name 2":
doSomething()
elif confirm == "emoji name 3":
doSomething()
elif confirm == "emoji name 4":
doSomething()
else:
print("Error")
await ctx.send("An error occurred.")
Replace EMOJIHERE and emoji name with the appropriate values.
Upvotes: 0
Reputation:
ChID = '920320584032870424'
(string) is being compared to an integer reaction.message.channel.id
.
ChID = '920320584032870424'
Instead, use
ChID = 920320584032870424
Which is an integer.
Upvotes: 2