I have a code to remove one role from everyone on the server, but it doesn't work:
When I enter a command, the bot simply ignores it, there is nothing in the cmd and the bot does not do anything either
Code -
intents = discord.Intents()
intents.members = True
#client.command()
async def clearrole(ctx):
role = discord.utils.get(ctx.guild.roles, id = 795410455848812565)
members = ctx.guild.members
for member in members:
if role in member.roles:
await member.remove_roles(role)
Make sure the intents are enabled, otherwise your bot might not be able to get a list of all the members of your server : https://discordpy.readthedocs.io/en/latest/intents.html
Then, make sure the id indeed correspond to a role by typing <#795410455848812565> in your server, it should transform into the role mention, otherwise the ID is wrong
Related
I'm new to python, so can someone help me with my code, because it is not working properly. When the user that I want joins it doesn't give it a role. I want to make it work for a specific user only to give a role when joining discord server.
#client.event
async def on_member_join(member):
member = get(member.id, id=member_id)
role = get(member.guild.roles, id=role_id)
await member.id(member)
await member.add_roles(role)
I don't even know why you're making this so complicated.
Since you already have the member as an "argument" you can work with it and don't have to define member again.
We can get the ID of member very easily with member.id. If we want to compare this with a real ID, we do the following:
if member.id = TheIDHere:
# Do what you want to do
The function for the role is correct, yet I cannot find a use for await member.id(member). What is the point of this/has it any use?
How you add the role at the end is also correct, but the code must be indented properly and best you work with an if / else statement, otherwise the bot will still give an error at the end in the console if always the wrong member joins.
The whole code:
#client.event
async def on_member_join(member):
role = discord.utils.get(member.guild.roles, id=IDOfTheRole) # Get the role from member.guild and the id
if member.id == TheIDHere: # If the member.id matches
await member.add_roles(role) # Add the role for the specific user
else: # If it does not match
return # Do nothing
You may also need to enable the members Intent, here are some good posts on that:
https://discordpy.readthedocs.io/en/stable/intents.html
How do I get the discord.py intents to work?
I am creating a bot for whitelisting people but I can't check if users that called the command has a role for whitelisting people I tried:
#bot.command()
async def idk(ctx):
role = discord.utils.get(ctx.guild.roles, id='892437125293813790')
if role in ctx.author.roles:
print('y')
else:
print(ctx.author.roles)
print('n')
bot its returns n everytime so I tried many different ways but this was the best I found so... I need help
The problem is at line 3 in your code, the IDs in Discord.py are always int type.
Try this:
role = discord.utils.get(ctx.guild.roles, id=892437125293813790)
Then, there's a better way to do this:
role = ctx.guild.get_role(892437125293813790)
Is there a way to get only the members without the bots of a server?
#bot.command()
async def stats(ctx):
guild = bot.get_guild("guild")
await ctx.send(f'{guild.member_count}')
When I run this code, it sends a message with the member count including bots. I want it to only send me the REAL member count (no bots)!
You could use:
members = 0
for member in ctx.guild.members:
if not member.bot:
members += 1
using .bot checks if the account is a bot or not.
You need to enable the members intent (so your bot can see the whole members list, not just itself), then count up the number of non-bots in the list of members. Here's how...
from discord import Intents
from discord.ext.commands import Bot
# Enable the default intents, then specifically enable "members".
intents = Intents.default()
intents.members = True
# Initialize the bot with our customized intents object.
bot = Bot(command_prefix='!', intents=intents)
#bot.command()
async def stats(ctx):
# Booleans can be added up; True is 1 and False is 0.
num_members = sum(not member.bot for member in ctx.guild.members)
# Sending an integer is fine; we don't need to convert it to a
# string first.
await ctx.send(num_members)
bot.run(TOKEN)
For most intents, your bot can freely opt in or out of them, straight from Python. However, members is a privileged intent. In order to subscribe to it, you'll also need to enable it in Discord's developer web interface:
You can use the discord.Guild.members property to get all of the members in the guild. Then, use discord.Member.bot to check if each member is a bot.
This would simply be:
humans = []
for member in ctx.guild.members:
if not member.bot:
humans.append(member)
num_humans = len(humans)
Or, for the list comprehension enthusiasts:
humans = [m for m in ctx.guild.members if not m.bot]
num_humans = len(humans)
Note: This assumes that you have all the necessary intents activate.
I want make it so if a user leaves the server while they have the Muted or [Banned] role they get permanently banned.
This is the code that I tried:
#bot.event
async def on_member_remove(ctx, member, reason=None):
role="[Banned]"
guild = ctx.guild
if role in member.roles:
await guild.ban(discord.Object(id=member.id), reason="Leaved the server when soft banned")
*this is just a try with only the banned role.
The user doesn't get banned, there is also no error or anything that could help me troubleshoot it.
member.roles returns a List of Role
You need to get the Role object which one way you can use is:
role = discord.utils.find(lambda r: r.name == '[Banned]', member.guild.roles)
on_member_remove takes in Member. You cannot have reason or Context(ctx)
#bot.event
async def on_member_remove(member):
role = discord.utils.find(lambda r: r.name == '[Banned]', member.guild.roles)
guild = member.guild
if role in member.roles:
await guild.ban(discord.Object(id=member.id), reason="Leaved the server when soft banned")
Please also ensure you have the Members intent enabled. You can do this by going here then selecting Bot -> SERVER MEMBERS INTENT
You will need to do enable intents in your code by using:
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix=".", intents=intents)
Your code seems fine.
Check that you have enabled the Intents.members on your Discord Developper portal and in your bot code, if you're using Discord API v8 (as intents are mandatory in this API version), as explained here in Discord.py docs.
For your information, by default, all intents are enabled except members and presence intents.
On Discord Developer Portal, enable this in the "Bot" section of your app :
This is my code which is not working, Remember my bot is in multiple servers.
#bot.event
async def on_member_join(member: discord.Member):
role = member.guild.get_role(746380043344937081)
await member.add_roles(role, reason=None)
This code is not working and I have enabled the correct intents.
There's nothing wrong with your setup, but here's the mistake:
You can't define your desired role by ID. On every server the ID of the role is different.
If the bot is on multiple servers, you have to go through the name search and the role with the name must exist on the server or on every server you want to grant the role after joining.
Here is an example:
from discord.utils import get
intents = discord.Intents.default()
intents.members = True
#bot.event
async def on_member_join(member):
role = discord.utils.get(member.guild.roles, name="Test")
await member.add_roles(role)
print(f"Assigned {role} to {member}!")
Remember to also import the intents into your code, not just activate them. If you did this you will be fine.
What we did:
Get the role through the name, not ID.
Imported discord.utils.get to be able to get the role by name.
Printed out the result to see if it works.