-
Hi there I try to learn more from python while using a discord bot. Everything works fine but im trying new stuff to learn. What I try this time is
A User should use a command to get the right role for him. At this point @everyone has no right to access roles. My try looks like: @client.command(aliases=['location','loc']) async def newlocation(ctx, role: str=None): user=ctx.message.author if role is None: await ctx.send(f"{user.mention}```You have to choose a location\nBsp.: !location USA\n```") else: if role!='Administrator': role=discord.utils.get(ctx.guild.roles,name=role) if role is None: await ctx.guild.create_role(name=role) if role not in user.roles: await discord.Member.add_roles(user,role) await ctx.send(f"{user.mention}```Your location is saved```") else: await ctx.send(f"{user.mention}```You are still in this group```") So is it only because default user has not the right? I wanted to get faster input make a command to set up multiple roles. I tried at first time with 2 or 3 roles to see if it works. My try looks like: @client.command(aliases=['add']) async def addnew(ctx): addroles=['USA','CANADA',...... in my example i think i have about 90 entries] for role in addroles: await ctx.guild.create_role(name=role) await ctx.send(f"{role} created") After I deletet all new roles and add the over 80 in the arrey, the code dont make new roles, is I put the send code befor the create.role one, I get the mess but not the role and no error. Can someone help me looking throught the trees? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
Hello! Your first issue is caused by re-using the same variable multiple times in an unlucky configuration. You are correct that only the bot needs permissions to create and assign roles, rather than users using the commands. Let's step through the relevant part of the code: @client.command(aliases=['location', 'loc'])
async def newlocation(ctx, role: str = None):
...
# If no role matching the content of the `role` variable exists the variable will be set to `None` here
role = discord.utils.get(ctx.guild.roles, name=role)
if role is None:
# A new role is being created, however the `name` argument being passed will always be `None`
# As the `role` variable is `None`, meaning every role being created here will be called "new role"
await ctx.guild.create_role(name=role)
# At this point the `role` variable is still `None`, it will not be a part of `user.roles`
if role not in user.roles:
# This call should use the Member instance, not the class
# You might want to read up on classes and instances elsewhere
await discord.Member.add_roles(user,role)
await ctx.send(f"{user.mention}```Your location is saved```") I would suggest naming the argument for the user's location in your command something like @client.command(aliases=['location', 'loc'])
async def newlocation(ctx, location: str = None): Secondly, if you'd like to assign the newly created role to a user you will need the actual role = await ctx.guild.create_role(name=location)
await ctx.author.add_roles(role) Your second issue is likely because there are additional anti abuse rate-limits on the API which could for example limit the amount of roles you can create within a single day, rather than an issue with your code. I would suggest waiting this one out, and also not running this sort of operation very often or at all in the future to avoid running into it again. Creating roles as-needed with the In the future please also post the full error you receive when asking for help, it makes everyone else's job a lot easier. |
Beta Was this translation helpful? Give feedback.
-
one another question, I didnt try this one yet, but if I do like: @client.command(aliases=['s', 'standort', 'Standort', 'ort', 'Ort']) async def newlocation(ctx, location: str=None): if location is None: await ctx.send(f'{ctx.message.author.mention}```you need to use a location```') else: role = await ctx.guild.create_role(name=location) await ctx.author.add_roles(role) will he add the role even if it exist? do i need a if else for the role? |
Beta Was this translation helpful? Give feedback.
-
So, I can try it at evening, not now, but this is my solution. Waht do you think? @client.command(aliases=['location']) async def newlocation(ctx, location: str=None): if location is None: await ctx.send(f'{ctx.message.author.mention}```Location is empty, I dont think you live nowhere :)```') return False else: role = discord.utils.get(ctx.guild.roles, name=location) if not role: role = await ctx.guild.create_role(name=location) await ctx.send(f"{ctx.message.author.mention}```Creating location first```") if role not in ctx.message.author.roles: await ctx.send(f"{ctx.message.author.mention}```Your location has been saved```") await ctx.author.add_roles(role) else: await ctx.send(f"{ctx.message.author.mention}```You allready saved your location```") |
Beta Was this translation helpful? Give feedback.
Hello!
Your first issue is caused by re-using the same variable multiple times in an unlucky configuration. You are correct that only the bot needs permissions to create and assign roles, rather than users using the commands. Let's step through the relevant part of the code: