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.
Related
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
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
I'm currently trying to add reaction roles to my bot, but I'm having trouble getting the bot to remove roles when the reactions are removed
#client.event
async def on_raw_reaction_add(payload):
print("Reaction Added")
if ((payload.message_id == 874372383736205343) and (payload.emoji.name == '🎓')):
guild = client.get_guild(payload.guild_id)
role = guild.get_role(874364790099836948)
await payload.member.add_roles(role)
#client.event
async def on_raw_reaction_remove(payload):
print("Reaction Removed")
if ((payload.message_id == 874372383736205343) and (payload.emoji.name == '🎓')):
guild = client.get_guild(payload.guild_id)
role = guild.get_role(874364790099836948)
await payload.remove_roles(role)
Adding roles works perfectly as intended but whenever I try removing a reaction I always get some kind of error. I've tried a few different things, but the code that I provided returns "Attribute Error: "RawReactionAddEvent" object has no attribute "remove_roles"
As I said, I've tried things other than remove_roles but none of them worked.
I'm pretty new to discord.py and python as a whole so this is probably a really obvious mistake but I just can't seem to find anything that works.
You'll need to retrieve a member object in order to remove the roles. You're currently trying to remove a role from payload instead of a valid member.
guild = client.get_guild(payload.guild_id)
member = await guild.fetch_member(payload.user_id)
role = guild.get_role(874364790099836948)
await member.remove_roles(role)
As Lukasz points out, you can't use payload.member when a reaction is removed because it is only available when a reaction is added. Docs.
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.
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')