Discord.py get_member with on_ready - python

We have a bot command on a server !verifyme1
Which adds a member to a role.
async def verfiyme1(ctx):
user=ctx.message.author
#user=ctx.guild.get_member(536147700115308545)
print(user)
channel = bot.get_channel(858993936932274196)
print(client)
await user.send('Welcome to the Member Club!')
role= discord.utils.get(user.guild.roles, name="Holder Club")
if role in user.roles:
await channel.send(f"You are already a member of the holder club!")
#exit()
else:
await user.add_roles(role)
await channel.send(f"Congrats {user.mention} You are now a member of the Holder Club!")
#exit()
We need to change the command so it isn't triggered by a user but by us triggering the script (so we can do some extra verification checks) ie loading members.py triggers it to add the role to the chosen member then exits.
Looking at the docs i need to use on_ready()
async def on_ready():
#user=ctx.message.author
print("this is a test")
channel = bot.get_channel(858993936932274196)
print(channel)
user=ctx.guild.get_member([we will pass this ID in])
print(user)
await user.send('Welcome to the Member Club!')
role= discord.utils.get(user.guild.roles, name="Holder Club")
if role in user.roles:
await channel.send(f"You are already a member of the holder club!")
#exit()
else:
await user.add_roles(role)
await channel.send(f"Congrats {user.mention} You are now a member of the Holder Club!")
#exit()
bot.run(DISCORD_TOKEN)
But the issue with this is you can't use ctx with on_ready thus ctx.guild.get_member returns an error. Is there any alternative to guild.get.member that would work?

You can get the guild, just like you get the channel
guild = bot.get_guild(guild_id)
Found in the docs.
#bot.event
async def on_ready():
guild = bot.get_guild(guild_id)
if guild == None:
guild = await bot.fetch_guild(guild_id) # if guild not in cache
user = guild.get_member(536147700115308545)
channel = guild.get_channel(858993936932274196)
await user.send('Welcome to the Member Club!')
role = discord.utils.get(guild.roles, name="Holder Club")
if role in user.roles:
await channel.send(f"You are already a member of the holder club!")
#exit()
else:
await user.add_roles(role)
await channel.send(f"Congrats {user.mention} You are now a member of the Holder Club!")
#exit()

Related

MissingRequiredArgument: context is a required argument that is missing

Im Still New (Because I Took A Break And Forgot Almost Everything). I Was Coding An AFK Bot And This Happened...
Error: MissingRequiredArgument: context is a required argument that is missing.
Code:
#client.command()
async def start(ctx,context,user: discord.Member):
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
name = context.author.display_name
author = context.author
guild = context.guild
AFKrole = discord.utils.get(guild.roles, name="AFK")
if not AFKrole:
AFKrole = await guild.create_role(name="AFK")
await context.author.edit(nick=f"[AFK]{name}")
await author.add_roles(AFKrole)
await ctx.send(f"AFK Mode Started For **{author}**.")
gt = await client.wait_for('message', check=check, timeout=180)
if gt.content.lower() == "afk stop":
await context.author.edit(nick=f"{name}")
await author.remove_roles(AFKrole)
await ctx.send(f"AFK Mode Stopped For **{author}**.")
else:
pass
please help.
Remove context parameter and replace any context part in the code with just ctx. ctx is already context.
Ex.
name = ctx.author.display_name
author = ctx.author
guild = ctx.guild

role react bot discord python

I'm trying to create a react role on a message that my bot would have sent, how can I do it?
here is my code debut:
#bot.command(name = 'role_react_1')
async def role_react_1(ctx):
del = await ctx.channel.history(limit=1).flatten()
for each_messages in del:
await each_messages.delete()
message = await ctx.send("for have the role : evil : :smiling_imp: \nwhat role you want ?")
await message.add_reaction("????")
def checkEmoji(reaction):
return message.id == reaction.message.id and str(reaction.emoji) == ":evil:"
reaction = await bot.wait_for("reaction_add", check=checkEmoji)
if reaction.emoji == ":evil:":
role = discord.utils.get(ctx.guild.roles, name="evil")
await ctx.author.add_roles(role)
await ctx.send(f"the role {role} was assigned")
I've been told about 'on_raw_reaction_add' but I don't know how to use it. how to do it
if someone want a answer, it need a bot.event with reaction_add

My bot doesn't give role, but it doesn't send error

(Sorry for my english)My bot must give role by reaction on emoji, but it doesn't do it. But it doesn't send error-message. Please help.
My code:
import discord
from discord.ext import commands
from discord.utils import get
client = commands.Bot(command_prefix = ".",intents = discord.Intents.all())
#client.event
async def on_ready():
print(discord.__version__)
Channel = client.get_channel(815949348948934716)
Text= "Выбери свою роль😇"
Moji = await Channel.send(Text)
await Moji.add_reaction('🏃')
#client.event
async def on_reaction_add(reaction, user):
Channel = client.get_channel(815949348948934716)
if reaction.message.channel.id != Channel:
return
if reaction.emoji == "🏃":
Role = discord.utils.get(user.server.roles, name="PUBG")
await user.add_roles(Role)
client.run("My token")
You must use an event called on_raw_reaction_add. You might want to read the Docs.
Now to the further procedure:
Define the event:
#client.event
async def on_raw_reaction_add(payload): # We use payload
Define the role(s):
guild = client.get_guild(payload.guild_id) # Define the guild
member = get(guild.members, id=payload.user_id) # Get the member from the guild
# channel and message IDs should be integer:
if payload.channel_id == Channel_ID and payload.message_id == Message_ID: # Inser your ID's
if str(payload.emoji) == "YourEmoji":
role = get(payload.member.guild.roles, name='RoleName') # ID also possible.
Add the role:
if role is not None:
await payload.member.add_roles(role)
So your entire code would be:
#client.event
async def on_raw_reaction_add(self, payload):
guild = client.get_guild(payload.guild_id)
member = get(guild.members, id=payload.user_id)
if payload.channel_id == Channel_ID and payload.message_id == Message_ID:
if str(payload.emoji) == "YourEmoji":
role = get(payload.member.guild.roles, name='RoleName') # Or id='RoleID'
else:
role = get(guild.roles, name=payload.emoji)
if role is not None:
await payload.member.add_roles(role) # Add the role
To add more roles just use elif statements.
What are we doing?
Send a message into the defined channel (Channel_ID)
Add a role when someone reacts with the defined emoji to the message

Discord.py add roles

I am trying to add roles in discord.py but I can't really make it work.
#bot.command(brief="Report member")
async def member(ctx):
if ctx.author.id != 783430063076147210:
await ctx.send("Mention the member you want to report")
message = await bot.wait_for('message', check=lambda message: ctx.author == ctx.author)
msg = message.content.replace("<","")
msg = msg.replace(">","")
msg = msg.replace("#","")
msg = msg.replace("!","")
#try:
msg = int(msg)
user = bot.get_user(msg)
await ctx.send('Are you sure you want to report ' + user.mention + "?")
message = await bot.wait_for('message', check=lambda message: ctx.author == ctx.author)
if message.content.lower() == "yes" or message.content.lower() == "y":
member = ctx.message.author
role = get(member.guild.roles, name="Reported")
user = ctx.guild.get_member(msg)
await bot.add_roles(user, role)
await ctx.send(user.mention + " was reported by " + member.mention + ".")
else:
await ctx.send("User was not reported!")
#except:
await ctx.send("Oops! That was not a user!")
I'm getting an AttributeError: 'Bot' object has no attribute 'add_roles', and the same with the other way
await user.add_roles(role)
returns AttributeError: 'NoneType' object has no attribute 'add_roles'
Is there something I'm missing?
Two ways of making this work:
1.
#bot.command()
async def role(ctx, member:commands.MemberConverter, role:commands.RoleConverter):
await member.add_roles(role)
^^ This method uses a role converter that accepts role ID, name, or mention.
I also use commands.MemberConverter because it accepts more methods of referencing a user than discord.Member, and the same thing for discord.Role.
2.
#bot.command()
async def role(ctx, member:commands.MemberConverter, role):
role = discord.utils.get(ctx.guild.roles, name=role)
await member.add_roles(role)
^^ This method uses discord.utils.get to get a guild role. From my knowledge, it only accepts role names and not role IDs or mentions.
bot.get_user() will return a User object, to which you cannot add roles, as no guild is specified. You could do ctx.guild.get_member(id) to get the Member object then add roles to that, but you'd be better of getting the member from the mentions attribute of a Message. Docs: https://discordpy.readthedocs.io/en/latest/api.html?highlight=mentions#discord.Message.mentions
Try something like this:
#bot.command()
async def role(ctx):
test = ctx.author
role = discord.utils.get(test.guild.roles, name="Name")
if role in test.roles:
await ctx.send("you already have a role")
else:
await test.add_roles(role)
Your line
await bot.add_roles(user, role)
needs to be replaced by
await user.add_roles(role)
these are my addrole and remouverole commands:
#bot.command()
#commands.has_permissions(administrator=True)
async def addrole(ctx, member : discord.Member, role : discord.Role):
await member.add_roles(role)
await ctx.send(f"{role} is added to {member}.")
#bot.command()
#commands.has_permissions(administrator=True)
async def removerole(ctx, member : discord.Member, role : discord.Role):
await member.remove_roles(role)
await ctx.send(f"{role} is removed from {member}.")

how do I check if a user has a specific role in discord

This should check if the specific person does or doesn't have the mute role
#bot.command(pass_context=True)
#commands.has_role("Admin")
async def unmute(ctx, user: discord.Member):
role = discord.utils.find(lambda r: r.name == 'Member',
ctx.message.server.roles)
if user.has_role(role):
await bot.say("{} is not muted".format(user))
else:
await bot.add_roles(user, role)
This error is thrown
Command raised an exception: AttributeError: 'Member' object has no attribute 'has_role'
I don't know how to do it so i would really appreciate every help I can get
Member does not have a .has_role() method, you can however get a list of all their roles using .roles.
To see if a user has a given role we can use role in user.roles.
#bot.command(pass_context=True)
#commands.has_role("Admin")
async def unmute(ctx, user: discord.Member):
role = discord.utils.find(lambda r: r.name == 'Member', ctx.message.guild.roles)
if role in user.roles:
await bot.say("{} is not muted".format(user))
else:
await bot.add_roles(user, role)
Docs for reference: https://discordpy.readthedocs.io/en/latest/api.html#member
Note: ctx.message.guild.roles use to be ctx.message.server.roles. Updated due to API change.
Personally I use this:
#bot.command(pass_context=True)
#commands.has_role("Admin")
async def unmute (ctx,user:discord.Member):
role = discord.utils.get(ctx.guild.roles, name="Muted")
if role in user.roles:
await user.remove_roles(role)
await user.add_roles(role)
embed = discord.Embed(title="Unmuute Members", description=f"{user.mention} has been unmuted" , color = discord.Color.blue())
embed.add_field(name='Unmuted by:' , value = f"{ctx.author.mention}")
await user.remove_roles(role)
await ctx.send(embed=embed)
else:
await ctx.send("Invalid Argumnets or The user is not muted.")
So as you can see
role = discord.utils.get(ctx.guild.roles, name="Muted") this variable locates the Muted role in the server
if role in user.roles:
await user.remove_roles(role)
and this will remove the role from the user

Categories

Resources