Reputation: 164
so I'm trying to create a setup command in my bot where the user can choose what he wants. The problem is I can' get it to work as I want to.
I have this as my code
# Function to write changes to file
def set_adminrole(guild: Guild, *, role: Role):
with open("admins.json") as f:
roles = json.load(f)
roles[str(guild.id)] = role.id
with open("admins.json", 'w') as f:
json.dump(roles, f, indent=4)
# Actual command
-- Not important code --
await ctx.send(f"Now, mention the role you want it to be the admin role")
role: Message = await bot.wait_for("message", check=check)
actualrole: Role = Role(role)
set_adminrole(ctx.message.guild, role=actualrole)
await ctx.send(f"Admin role changed to {Role(role.content).mention}... Let's keep going")
When I mention a role to set it as teh admin role it throws this error:
Ignoring exception in command setup:
Traceback (most recent call last):
File "C:\Users\xlysa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "D:/Ficheiros/Pastas/Tudo/Coding/Python/Projects/Server-Utils/bot.py", line 262, in start_setup
actualrole: Role = Role(role)
TypeError: __init__() takes 1 positional argument but 2 were given
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\xlysa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\xlysa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\xlysa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: __init__() takes 1 positional argument but 2 were given
Help is appreciated
Upvotes: 1
Views: 2665
Reputation: 164
I retrieved the content of the message that would be <@&123456789>
being 123456789
the id of the role I mentioned. removed the <, @, & and >
from the string and got left with only the id. Just used discord.utils.get(ctx.guild.roles, id=123456789)
and boom. Thank you all for the help
Upvotes: 0
Reputation: 1371
So you receive a message, and then attempt to create a Role object using the message. Which makes no sense. Look at the constructor for Role objects, and call it correctly.
await ctx.send(f"Now, mention the role you want it to be the admin role")
role: Message = await bot.wait_for("message", check=check)
actualrole: Role = Role(role) #<- what are you doing here????
set_adminrole(ctx.message.guild, role=actualrole)
await ctx.send(f"Admin role changed to
Upvotes: 1