How can I check if one role is in front of another? - python

I want to make a Discord Bot that change the User Nickname. The Bot is working, but when i give me the role Co-Owner hes renaming me to [Co-Owner] User. But i already have the role Owner. I like when a certain role is in front of another that he doesn't change my nickname. How can i do that?
Thats my Code right now:
#bot.event
async def on_member_update(before, after):
if len(before.roles) < len(after.roles):
new_role = next(role for role in after.roles if role not in before.roles)
if new_role.name in ('Twitch VIP'):
await after.edit(nick=f"[VIP] {after.name}")
elif new_role.name in ('Co-Owner'):
await after.edit(nick=f"[Co-Owner] {after.name}")
elif new_role.name in ('Owner 👑'):
await after.edit(nick=f"[Owner] {after.name}")

Probably because Owner and Co-Owner have the same word in it.

Related

How to check if the player has a role when the bot starts and sends the message "on_ready"

Blockquote
I have a bug, I created a bot that issues a role when entering the members server, but sometimes the bot is offline and cannot issue a role
Therefore, I wanted the bot to display a role when the bot is started if the member does not have a role, and to do nothing if the member already has a role
I tried it but it doesn't work and yes I work because of the cogenter image description here
#commands.Cog.listener()
async def on_ready(self, guild: discord.Guild):
role = discord.utils.get(guild.roles, id=775777298111791104)
for m in guild.members:
for r in role.guild.members:
if m.guild.roles != r.get_role(775777298111791104):
print("Ok")
await m.add_roles(role)
=======================================================================
async def setup(bot):
await bot.add_cog(JoinMemberMessages(bot))
I think there's a few fundamental issues with your code.
You're looping over the guild members and then for each guild member you're starting another loop where you're looping over the guild members again? Then you're checking if all the server's role is equal to one particular role?
Additionally, the on_ready event doesn't take guild as a parameter
Your code doesn't quite do what you suggest it's trying to do.
async def on_ready(self):
# if your client/bot is instantiated as another var - then change this
# perhaps if it's `client` or `self.bot` or whatever
guild = await bot.fetch_guild(YOUR_GUILD_ID)
role = discord.utils.get(guild.roles, id=MY_ROLE_ID)
for member in guild.members:
member_roles = member.roles
member_role_ids = [role.id for role in member_roles]
if MY_ROLE_ID in member_roles_ids:
# user already has our role
continue
await member.add_roles(role)
print(f"Given {member.name} the {role.name} role")
Replace CAPITAL_VARIABLES with yours or the relevant magic numbers.
Here, we loop over the members and check they don't have the existing role ID; and assign it to them if they don't.
Perhaps check the docs:
on_ready
discord.Guild
discord.Member

How to explicitly add role to a user in discord bot

I'm relatively new to programming and am trying to code a bot for a server I'm in. I'd ideally like to assign a user to a specific role based on them sending a message containing 'gm' or 'good morning'. Right now, the bot can read the message and send a reply. But I'm a bit lost trying to figure out how to actually add the role to a user once the 'gm' message is read.
`#client.event
async def on_ready():
print(f'We have logged in as {client.user}')
async def addRole(user : discord.Member, role : discord.Role = BagChaser):
if role in user.roles:
return
else: await user.add_roles(role)
#client.event
async def on_message(message):
if message.author == client.user:
return
msg = message.content.lower()
words_list = ['gm', 'good morning']
if any(word in msg for word in words_list):
# await addRole(message.author, BagChaser)
await message.channel.send(f'Lets get this bag, {message.author}')
await message.author.add_roles(BagChaser)`
the commented line and the last line were some ideas of how to add the role 'BagChaser' to the author of the message. I tried setting the role parameter in the addRole function to BagChaser since that will never change, but this seems incorrect. The role is already made in my server, but I'm not sure how I can make the bot aware of that role in the code. Any help would be greatly appreciated!
I tried explicitly calling out my role but i can't get it recognized.
You need a role object, and to do that, you need a guild object, which you can get with message.author.guild.
From this, you can get the Role object:
role = await message.author.guild.get_role(ROLE_ID)
Note that you need to get the role ID yourself. The easiest method to do so is to go into Discord and enable Developer settings, then right click the role on someone's profile and click "Copy ID". Once you have this role object, you can just apply it with message.author.add_roles(role).
Complete code:
role_id = ...
author = message.author;
role = await author.guild.get_role(role_id)
await author.add_roles(role)
Make sure your bot has the Manage Roles permission

Discord.py Server Roles Command, Roles Starting From Down

Hi I do a server roles command and it working perfectly but the top role is showing in the down. My code is this:
#bot.command(pass_context=True)
async def roles(ctx):
guild = ctx.guild
roles = [role for role in guild.roles if role != ctx.guild.default_role]
embed = discord.Embed(title="Server Roles", description=f"\n \n".join([role.mention for role in roles]))
await ctx.send(embed=embed)
I want the top role to start form the top not down, how can I do that?
I will be very happy if someone help me :)
list type has a reverse method. So if you write roles.reverse(), top role will be the first one.

How to whitelist roles from being banned

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.

would it be possible to make a system that gives a role if they type in a certain channel using discord.py

Right so im trying to make a bot for a roleplay server and i want it so that when someone with the "No oc" role makes a character in character-subbmisons it gives them the "Waiting for approval" role is this possible?
You can use the on_message event:
#client.event
async def on_message(message):
if message.content == 'Give Me The Role With ID 123456789': # text goes there
role = message.guild.get_role(123456789) # role ID goes there
await message.author.add_roles(role, reason='Bot Role')

Categories