Discord.py Reaction Roles with Embed dont work - discord.py

Im Trying to make a discord bot which sends an embed message with a reaction, and if a user reacts to that message he/she would get a role. This is for the Rules of the Server.
The Problem is, the Embed message gets sent but there is no reaction. If I manually react to it, and get someone else to react too he/she will get no role. (The embed message is the only message in that channel). I also get no Errors in the console.
The 'channel_id_will_be_here' is always replaced with the correct channel Id.
Thank you.
import discord
from discord.ext import commands
client = discord.Client()
#client.event
async def on_ready():
Channel = client.get_channel('channel_id_will_be_here')
print("Ready as always chief")
#client.event
async def on_message(message):
if message.content.find("|rules12345654323") != -1:
embedVar = discord.Embed(title="**Rules**", description="The Rules Everybody needs to follow.", colour=discord.Colour.from_rgb(236, 62, 17))
embedVar.add_field(name="Rule 1", value="Be nice etc", inline=False)
await message.channel.send(embed=embedVar)
async def on_reaction_add(reaction, user):
Channel = client.get_channel('channel_id_will_be_here')
if reaction.message.channel.id != Channel:
return
if reaction.emoji == ":white_check_mark:":
Role = discord.utils.get(user.server.roles, name="Player")
await client.add_roles(user, Role)

In if reaction.message.channel.id != Channel you compare the id to the Channel object, not the Channel.id.
You don't need to use the object there, just the id (which you use to create the channel object in the first place) would be fine
if reaction.message.channel.id != channel_id_will_be_here:
return
You could also use the message id like(So it'll only trigger when reacting to that exact message):
if reaction.message.id != rules_message_id_will_be_here:
return
The way you do your check is pretty strange too, why check to make the function return if False? Why not just make it add the role when True?
async def on_reaction_add(reaction, user):
if reaction.message.channel.id == channel_id_will_be_here and reaction.emoji == ":white_check_mark:":
Role = discord.utils.get(user.server.roles, name="Player")
await client.add_roles(user, Role)
You could even leave out the if reaction.emoji == ":white_check_mark:": part if it is the only message/emote reaction in that channel

Related

Sending a message through discord.py after message edit check

I have a function that checks if 'your' is changed to 'you're' and I am having trouble having the bot send a message after this check. What is the proper way to do this? (I want this to apply to all channels.)
import discord
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix='$')
#client.event
async def on_ready():
print("We have logged in as {0.user}".format(client))
#client.event
async def on_message_edit(before,after):
if(before.content == "your" and after.content == "you're"):
# I want to send message here
client.run('Token') # I have my token here
Add ctx into the parameters (before,after,ctx), and
then put in await ctx.send("message") where you want your message to send.
the parameters before and after are of type discord.Message. discord.Message has an attribute channel, with which you can use the function send.
#client.event
async def on_message_edit(before, after):
if before.content == 'your' and after.content == "you're":
await before.channel.send(send_whatever_you_want)

Using CTX in on_reaction_add event -- Discord.py

async def on_reaction_add(reaction, user):
emoji = reaction.emoji
if emoji == "💌":
await user.channel.send("HI")
I got problem here with user.
I want to use here with ctx like ctx.channel.send.
but also it occured error how to use ctx in here?
Instead of using the on_reaction_add event, it's better in this case to use a wait_for command event. This would mean the event can only be triggered once and only when the command was invoked. However with your current event, this allows anyone to react to a message with that emoji and the bot would respond.
By using client.wait_for("reaction_add"), this would allow you to control when a user can react to the emoji. You can also add checks, this means only the user would be able to use the reactions on the message the bot sends. Other parameters can be passed, but it's up to you how you want to style it.
In the example below shows, the user can invoke the command, then is asked to react. The bot already adds these reactions, so the user would only need to react. The wait_for attribute would wait for the user to either react with the specified emojis and your command would send a message.
#client.command()
async def react(ctx):
message = await ctx.send('React to this message!')
mail = '💌'
post = '📮'
await message.add_reaction(mail)
await message.add_reaction(post)
def check(reaction, user):
return user == ctx.author and str(
reaction.emoji) in [mail, post]
member = ctx.author
while True:
try:
reaction, user = await client.wait_for("reaction_add", timeout=10.0, check=check)
if str(reaction.emoji) == mail:
await ctx.send('Hi you just received')
if str(reaction.emoji) == post:
await ctx.send('Hi you just posted...')
You need to use reaction.message.channel.send
async def on_reaction_add(reaction, user):
emoji = reaction.emoji
if str(emoji) == "💌": await reaction.message.channel.send("HI")

Discord.py adding a role through reactions

I am using code that I found here.
But, I'm getting an error that the client object doesn't have the attribute send_message. I have tried message.channel.send but that failed as well.
#client.event
async def on_ready():
Channel = client.get_channel('YOUR_CHANNEL_ID')
Text= "YOUR_MESSAGE_HERE"
Moji = await client.send_message(Channel, Text)
await client.add_reaction(Moji, emoji='🏃')
#client.event
async def on_reaction_add(reaction, user):
Channel = client.get_channel('YOUR_CHANNEL_ID')
if reaction.message.channel.id != Channel:
return
if reaction.emoji == "🏃":
Role = discord.utils.get(user.server.roles, name="YOUR_ROLE_NAME_HERE")
await client.add_roles(user, Role)
One of the reasons why your code might not be working is because you may have your channel id stored in a string. Your code shows:
Channel = client.get_channel('YOUR CHANNEL ID')
The code above shows that your channel id is stored in a string, which it should not be. With the code above, your actual code may look like this:
Channel = client.get_channel('1234567890')
Instead you should have:
Channel = client.get_channel(1234567890)

The reaction-role function of my discord.py bot didn't function properly.. Why?

This is the code I got so far but for some reason, it doesn't really work...
async def on_ready():
Channel = client.get_channel('777877476558110737')
Text= "React to Verify"
Moji = await client.send_message(Channel, Text)
await client.add_reaction(Moji, emoji='🏃')
#client.event
async def on_reaction_add(reaction, user):
Channel = client.get_channel('777877476558110737')
if reaction.message.channel.id != Channel:
return
if reaction.emoji == "🏃":
Role = discord.utils.get(user.server.roles, name="12 Year Old")
await client.add_roles(user, Role)
Use Channel.send(Text) not client.send_message(Channel, Text) because send_message is not a method of client, but send is a method of Channel.
Use Moji.add_reaction("🏃") not client.add_reaction(Moji, emoji='🏃') because the add_reaction is a method of the Message class.
Change user.server.roles to user.guild.roles because server is not an attribute of the Member class, but guild is.
Use user.add_roles(Role) not client.add_roles(user, Role) because add_roles is a method of the Member class.
Make sure to reference the docs. You are calling many methods from the client when they are supposed to be called from other classes. Update discord.py to the latest version too.

How to make a discord.py reaction role code

I'm new to discord bot making, and I'd like to have the bot add a reaction to my message that, once pressed, allows the user reacting to pick up a role. What would be the code to allow me to do this? Thank you.
This is a very well written and formatted question! In order for the bot to detect that the reaction is on a specific message, you can do many ways. The easiest way would be to be by ID, so I'm just going to do this with a simple command.
messageIDs = []
#client.event
async def on_raw_reaction_add(payload):
global messageIDs
for messageID in messageIDs:
if messageID == payload.message_id:
user = payload.member
role = "roleName"
await user.add_roles(discord.utils.get(user.guild.roles, name = role))
#client.command()
async def addMessage(ctx, messageID):
global messageIDs
emoji = "👍"
channel = ctx.message.channel
try:
msg = await channel.fetch_message(messageID)
except:
await ctx.send("Invalid Message ID!")
return
await msg.add_reaction(emoji)
messageIDs.append(messageID)

Resources