discord.py Remove all Roles from all Players - discord

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

Related

Get the role(s) of a member in discord.py

I am trying to make a command that shows the roles of the mentions user.
This command is a test command which I am going to implement into my mute command. (the command will remove the member's current role and add the mute role)
This is what I have:
#client.command()
async def roles(ctx, member: discord.Member):
roles = member.roles
role_names = [role.name for role in roles]
await ctx.send(role_names)
The command works fine, but the output isn't quite what I expected.
Output:
['#everyone', 'Member']
It correctly displays the 2 roles mentioned by the user, but it isn't formatted in the specific way I want.
I want the output to simply be "Member" or whatever other roles the mentioned member has besides #everyone. basically, I want to remove the square brackets and "#everyone" from the output, leaving only the role name.
Hopefully, somebody can help me with this.
Thanks!
To fix your problem you just have to format the information from role_names in string form and remove the '#everyone'. You can use a list comprehension and the join() method .
Here would be the amended code:
#client.command()
async def roles(ctx, member: discord.Member):
roles = member.roles
role_names = ' '.join([role.name for role in roles if role.name != '#everyone'])
await ctx.send(role_names)

How can I allow people to only kick members lower than their role with discord.py?

I'm trying to make a kick command using Python and the discord.py library, though I cannot figure out how to allow people to only be able to kick members lower than their role, so Moderators wouldn't be able to kick Admins.
enter image description here
You can compare the Member object with a function called top_role
An example code would be:
#client.command()
#commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
if member.top_role >= ctx.author.top_role: # Check if the role is below the authors role
await ctx.send("You can only kick users with a lower role!")
return
else:
await member.kick(reason=reason)
await ctx.send(f"{member.id} got kicked with the reason: {reason}.")
Add all the moderators to a list and regular users to another list.
Like this;
string[] moderator = all moderators.
string[] regularUsers = all regular users.

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 - How can I add_role to user on_message?

I want to use regex to look at and accept literally ANY incoming message from a user and assign them a role. But ONLY if that user does not have a role already assigned to them. I don't get any errors when running the code, but it does not work.
Here is what I have:
#client.event
async def on_message(message):
match = re.search(r'(.*?)', message.content)
member = message.author
role = discord.utils.get(member.guild.roles, name="Creators")
if message.author == client.user:
return
if role not in member.roles:
if match and message.channel.id == target_channel:
# add member to role
# send message to to users
await message.channel.send(
f'Hi {message.author}, welcome to the server! Don\'t forget to choose your #roles'
)
await discord.Member.add_roles(member, role)
You wrote:
await discord.Member.add_roles(member, role)
https://discordpy.readthedocs.io/en/latest/api.html?highlight=add_role#discord.Member.add_roles
As you can see from the documentation the passed arguments is *roles, reason=None, atomic=True. roles is something you have to specify, reason and atomic are optional.
You tried passing member which is not a valid argument.
discord.Member is a class. You need to get an instance of that class. ctx.author is an instance of discord.Member.
So the final call should be:
await ctx.author.add_roles(role)
To get the Member object from on_message, you can use member = message.author.
Then simply do member.add_roles(role).
Thanks for the help guys, it turns out it was something very minor (and dumb on my part) that prevented it from working.
when comparing message.channel.id.id to target_channel, I forgot that the channel id object does not come in as a string, but target_channel was set up as a string, so it failed to see them as equal. Simply converting message.channel.id to string did the trick.

How can I give member a role with role id?

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)

Resources