How can I give member a role with role id? - discord

role = client.get_role("697037307060027482")
await message.author.add_roles(role)
I Tried this, but Error occurs.
How can I give a role vith role id?

You can get guild and get role. Example:
#client.command()
async def addrole(ctx):
role = client.get_guild(your_guild_id).get_role(your_role_id)
await ctx.author.add_roles(role)

Related

Check user for a role

I am trying to check if a user has a specific role, and if they have that role they can use the command but no matter if I have the role I get the else triggered.
Code
#bot.slash_command(name="test", description = "testing cmd")
async def test(ctx, member: discord.Member):
role = "1023451893575450665"
if get(member.roles, id=role):
await ctx.send("your application has been accepted")
else:
await ctx.send("your application has been denied")
If you know the role id, use the Member.get_role method. The role id should be left as an integer, not a string.
if member.get_role(role):
await ctx.send("your application has been accepted")
If you know the role name, you need to check through the member.roles property which is a list of Roles
if len(member_role for member_role in member.roles if member_role.name == role]):
await ctx.send("your application has been accepted")

How to get user id from nickname in discord.py?

I'm writing a discord bot for my discord-server and the people on the server change their nicknames like their underwear. Can i get the user id from their changing nicknames?
I want to code a command like this:
!command #mention
I need the user id of the mention. Thanks in advance!
Hope this helps :)
#client.command()
async def id(ctx, member: discord.Member = None):
if not member:
await ctx.send('Please mention a user')
else:
await ctx.send(member.id)

how i get an user status and i give him a specific role, i tried but it doesn't work

async def on_message(message):
if message.content.startswith("-test"):
await message.channel.send("Checking for the Status :pencil:..")
time.sleep(1)
async def on_member_update(before, after):
if str("discord.gg / discord") in member.activities[0].name():
print("Correct Status giving role to User")
guild = client.get_guild(8516587165187561)
role = discord.utils.get(guild.roles, id=9157981598717517571)
await message.channel.send("roled")
i try to get an user status and if he have that status specified he get the role, i don't know why it doesn't work, i use discord.py
There's no need to put on_member_update in the code. It wouldn't work anyway even if you do due to the fact it's an event function, not something you can call on.
async def on_message(message):
if message.content.startswith("-test"):
await message.channel.send("Checking for the Status :pencil:..")
time.sleep(1)
if str("discord.gg / discord") in message.author.activities[0].name():
print("Correct Status giving role to User")
guild = client.get_guild(8516587165187561)
role = discord.utils.get(guild.roles, id=9157981598717517571)
await message.author.add_roles(role)
await message.channel.send("roled")
I've also added the await message.author.add_roles(role) part because there wasn't any command that assign roles in your original code.

How to remove roles from a user in every server? Discord.py

I am making a verification bot and I want to do a command where if somebody unlinks their Roblox account then it removes the verified role from them in all the servers they are in. I know how to do it in a single server like this:
role1 = discord.utils.get(ctx.guild.roles, name='Verified')
await ctx.author.remove_roles(role1)
But how would I do it across all of the servers the member is in with the bot. Thanks!!!
A role is unique per guild, you'd need to iterate through every guild, get the member and role, and remove it.
for guild in bot.guilds:
member = guild.get_member(member_id) # Change the ID accordingly
if member is not None:
role = discord.utils.get(guild.roles, name='Verified')
await member.remove_roles(role)
This code loops through every guild the bot is in, tried to get the member, if it's not a nonetype (cause the member doesn't have to share that specific guild with the bot), it gets the role obj and removes it.
Reference:
Bot.guilds
Guild.get_member
Also you need intents.guilds and intents.members

discord.py Remove all Roles from all Players

I tried to make a bot that removes all roles from each member, but it didn't work.
does anyone have an idea how I can do that?
async def safety(ctx,*role:discord.Role):
for user in client.get_all_members():
if not user.bot:
try:
await user.remove_roles(*role, reason=None)
except:
print(",,,,,")```
If you want to remove all roles from a member
for member in ctx.guild.members:
if not member.bot:
await member.remove_roles(*member.roles)
if you want to remove the roles in the role argument
for member in ctx.guild.members:
if not member.bot:
await member.remove_roles(*role)
Also make sure to enable intents.members, here's how

Resources