I'm trying to check to see if a user has a certain role. I'm aware of #commands.has_role(), but that won't work for the context of what I'm making. I have example code below, and I'm not sure how I can get it to check if the user has the role.
#commands.command()
async def hasthisroletest(self, ctx):
if ctx.author.role.id == 746831268683186268:
await ctx.send("You did it!")
else:
await ctx.send("Nope :(")
You can check Member.roles if the wanted role is there.
Use discord.utils.get to get the role itself.
#commands.command()
async def hasthisroletest(ctx):
# you can also use name = 'Test_role'
role = discord.utils.get(ctx.guild.roles, id=746831268683186268)
if role in ctx.author.roles:
await ctx.send("You did it!")
else:
await ctx.send("Nope :(")
Related
i'm trying to make a user mute command and event i’ve got the command working but now the event is preventing any and all commands from working only events respond now even if user is not muted, but when user does have muted role their messages are properly deleted, please help
#bot.event
async def on_message(message):
user = message.author
channel = bot.get_channel(1060922421491793981)
role = discord.utils.get(user.guild.roles, id=1060693595964842165)
if role in message.author.roles:
await message.delete()
await channel.send(f"#{user.display_name} **You are muted, You cant do this right now**")
command arguments are always strings, except the first argument ctx which is the context. What you can do in this case is derive the user from the mention in the ctx which you can get from ctx.message.mentions, so in this case you can make something like this.
#bot.command()
async def mute(ctx, user: str, duration_arg: str = "0", unit: str = "m"):
duration = int(duration_arg)
role = discord.utils.get(ctx.message.guild.roles, id=1060693595964842165)
await ctx.send(f":white_check_mark: Muted {user} for {duration}{unit}")
await ctx.message.mentions[0].add_roles(role)
if unit == "s":
wait = 1 * duration
await asyncio.sleep(wait)
elif unit == "m":
wait = 60 * duration
await asyncio.sleep(wait)
await user.remove_roles(role)
await ctx.send(f":white_check_mark: {user} was unmuted")
#bot.event
#commands.has_role("Muted")
async def on_message(message):
role = discord.utils.get(message.guild.roles, name="Muted")
if role in message.author.roles:
await message.delete()
await ctx.send("You cant do this")
await bot.process_commands(message)
Also two notes:
try not to write the type in the variable name. We know that the roleobject is an object, as everything in python is an object. Using role is sufficient enough.
You might not want to wait during an async function for something like this. Instead I suggest you look into tasks, and have some sort of mechanism that stores who is currently muted, in case the bot gets turned off
found the error was missing a process line
await bot.process_commands(message)
event now works
I wanna do a ban appeal system on my server. I wanna remove every single role of someone and give them the [Banned] role. I cannot just do something like I did for the member role for all of them since there are a lot of roles, even some custom ones that are made and deleted every day.
member_role = get(user.guild.roles, name="『✅』· 𝙼𝚎𝚖𝚋𝚛𝚞")
await user.remove_roles(member_role, reason=None, atomic=True)
I tried this: discord.py trying to remove all roles from a user but it didn't work. Also tried this:
for role in user.roles:
if role.name == '[Banned]':
pass
else:
await user.remove_roles(role)
but couldn't get it to work. (I have no experience in python or discord.py)
So. How can I remove every role from a user instead of only the member_role ?
#bot.command()
#commands.has_permissions(ban_members=True)
async def ban(ctx, user: discord.Member, *, reason=None):
await asyncio.sleep(1)
banned_role = get(user.guild.roles, name="[Banned]")
await user.add_roles(banned_role, reason=None, atomic=True)
member_role = get(user.guild.roles, name="『✅』· 𝙼𝚎𝚖𝚋𝚛𝚞")
await user.remove_roles(member_role, reason=None, atomic=True)
banemb = discord.Embed(title="Ban", description=f"{user.mention} a fost banat/a. ", colour=discord.Colour.dark_red())
banemb.add_field(name="Motiv:", value=reason, inline=False)
await ctx.send(embed=banemb)
You can't get the list of roles that way.
To remove all the roles of the user you have to apply an edit to it and provide an empty list of roles.
It also changes the order of your code as you first have to remove and then re-assign a role to the user.
Have a look at the following code:
#bot.command()
#commands.has_permissions(ban_members=True)
async def ban(ctx, user: discord.Member, *, reason=None):
await asyncio.sleep(1)
banned_role = discord.utils.get(user.guild.roles, name="[Banned]")
await user.edit(roles=[]) # Remove all roles
await user.add_roles(banned_role, reason=None) # Assign the new role
banemb = discord.Embed(title="Ban", description=f"{user.mention} a fost banat/a. ",
colour=discord.Colour.dark_red())
banemb.add_field(name="Motiv:", value=reason, inline=False)
await ctx.send(embed=banemb)
I have changed/removed a few things from the code. Of course you have to add them again, according to your wishes.
I asked this question before, but due to my next to zero experience with stack-overflow it messed it up. So i will rewrite the question with the anwser so people looking for this can have it right away.
So my question was, how can i whitelist role from being banned. Now later, with more experience with discord.py i can tell you the anwser listed down below is the way to do it.
The working code: Credit to CaptAngryEyes
#bot.command()
async def ban(ctx, member : discord.Member):
whitelisted_roles = [728345678923456, 923478569378456, 8923475627893456] # Put the role IDs here
for role in member.roles:
if role.id in whitelisted_roles:
await ctx.send("You can't ban this user! He is a moderator!")
return
else:
# Ban code goes here...
pass
You can iterate through the roles of the user and check if he has a role in a list. I use the role IDs since it can't be changed.
#bot.command()
async def ban(ctx, member : discord.Member):
whitelisted_roles = [728345678923456, 923478569378456, 8923475627893456] # Put the role IDs here
for role in member.roles:
if role.id in whitelisted_roles:
await ctx.send("You can't ban this user! He is a moderator!")
return
else:
# Ban code goes here...
pass
If he has a "whitelisted" role we just return nothing and the command ends.
I've been trying to set up a bot that gives a new user who joined my discord server the role "mietzekatze" when he reacts with ":white_check_mark:" to a message. It's like a type of verification or approval. So I found a solution on how to do that, but it does not work. I tried different things, but it won't work. It doesn't return any error, it just wont do, what it's supposed to. Please help, I'm losing my mind.
#client.event
async def on_reaction_add(reaction, member):
Channel = client.get_channel('828667703966433331')
if reaction.message.channel.id != Channel:
return
if reaction.emoji == ":white_check_mark:":
Role = discord.utils.get(member.guild.roles, name="mietzekatze")
await member.add_roles(Role)
I would use a different event here called on_raw_reaction_add. For that you also need payload. You can read the docs for that here.
Re-written your code would look like this:
#client.event
async def on_raw_reaction_add(payload):
guild = client.get_guild(payload.guild_id) # Get the guild
if payload.channel_id == Channel_ID_here: # If the defined channel is correct
if str(payload.emoji) == "✅":
role = get(payload.member.guild.roles, name='mietzekatze') # 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
print("Added role") # Check if the role was added
You can use as many if str(payload.emoji) == "YourEmoji": events as you want, just pass them under the other event(s).
Note that you have to pass the ID of a channel/guild without ' '
You have to check for channel's id
if reaction.message.channel.id != Channel.id:
return
I am trying to add a role to someone but when I do the
client.add_roles(member, role)
I have tried everything I could think of with the code it just gives me that message, I have looked at several other questions around looking for the answer but everyone says to do that command that I have tried but it wont work.
#client.command(pass_context=True)
#commands.has_role('Unverified')
async def verify(ctx, nickname):
gmember = ctx.message.author #This is the looking for the memeber
role = discord.utils.get(gmember.server.roles, name='Guild Member')
channel = client.get_channel(401160140864094209)
await gmember.edit(nick=f"{nickname}")
await ctx.channel.purge(limit=1)
r = requests.get("This is the link to the API but it shows my key and everything so not going to put it here but it works in other commands")
#if nickname in role:
# await ctx.send(f"You have already verified your account.")
if nickname.encode() in r.content:
await channel.send(f'#here ``{nickname}`` is in the guild.')
await client.add_roles(gmember, role)
else:
await gmember.kick()
Instance of Bot has no add_roles member pylint (no-member)
await gmember.add_roles(role)
You can read more here