Discord.py adding a role through reactions - discord.py

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)

Related

How do I make it so this message gets sent a specific channel? (Discord.py)

I want the message to get sent to a specific channel
async def on_message_edit(before, after):
await before.channel.send(
f'{before.author} edited a message.\n'
f'Before: {before.content}\n'
f'After: {after.content}'
)
To send a message in a specific channel you need first to fetch the choosed channel by using his id by using get_channel() like this:
specificChannel = bot.get_channel(int(YOUR_CHANNEL_ID_HERE))
Then your event block should look like this:
#client.event
async def on_message_edit(before, after):
specificChannel = bot.get_channel(int(YOUR_CHANNEL_ID_HERE))
await specificChannel.send(
f'{before.author} edited a message.\n'
f'Before: {before.content}\n'
f'After: {after.content}'
)
Useful link:
get_channel()
Discord.py documentation
maybe this can help u :)
#client.event
async def on_member_join(member:Member):
if member.bot:
guild:discord.Guild = member.guild
role = guild.get_role(config.botRoleID)
channel:discord.TextChannel = guild.get_channel(YOUR_CHANNEL_ID)
await member.add_roles(role)
await channel.send(f'Set Bot role to "{member.display_name}" 😌')

Discord.py Reaction Roles with Embed dont work

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

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)

How do I make my logs server specific | discord.py

Currently, I have a logs on my bot, It works fine but the problem is that it takes logs from all servers that the bot it in. So, how do I make it server specific?
#commands.Cog.listener()
async def on_message_delete(self, message):
embed = Embed(
description=f"Message deleted in {message.channel.mention}", color=0x4040EC
).set_author(name=message.author, url=Embed.Empty, icon_url=message.author.avatar_url)
embed.add_field(name="Message", value=message.content)
embed.timestamp = message.created_at
channel=self.bot.get_channel(channel_id)
await channel.send(embed=embed)
This is my code for message deleting
Compare message's guild id with yours
Below is the revised code:
#commands.Cog.listener()
async def on_message_delete(self, message):
if message.guild.id == YOUR_GUILD_ID:
embed = Embed(
description=f"Message deleted in {message.channel.mention}", color=0x4040EC
).set_author(name=message.author, url=Embed.Empty, icon_url=message.author.avatar_url)
embed.add_field(name="Message", value=message.content)
embed.timestamp = message.created_at
channel = self.bot.get_channel(channel_id)
await channel.send(embed=embed)

Resources