Search code examples
python-3.xdiscorddiscord.pypydictionary

string has no attribute value?


I have the following script,

for level in levels.keys():
    name=levels[level]['name']
    hoist=levels[level]['hoist']
    colour=levels[level]['colour']
    perms=levels[level]['permissions']
    await guild.create_role(name=name,hoist=hoist,permissions=perms)

I use that script with the following dictionary,

levels={"Admin":{"name":"Admin","hoist":"1","colour":"red","permissions":"8"},"Moderator":{"name":"Moderator","hoist":"1","colour":"yellow","permissions":"1610079683"},"Henchman":{"name":"Henchman","hoist":"1","colour":"yellow","permissions":"470011072"},"Member":{"name":"Member","hoist":"0","colour":"green","permissions":"67226688"},"Verify":{"name":"Verify","hoist":"1","colour":"white","permissions":""},"Leach":{"name":"Leach","hoist":"1","colour":"pink","permissions":""}}

When I run the script I get the following error,

Ignoring exception in command setup:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "bot2.py", line 62, in setup
    await guild.create_role(name=name,hoist=hoist,permissions=perms)
  File "/usr/local/lib/python3.6/dist-packages/discord/guild.py", line 1576, in create_role
    fields['permissions'] = perms.value
AttributeError: 'str' object has no attribute 'value'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'str' object has no attribute 'value'

I understand that a string has no value and an object has no value but what I don't understand is why when perms is declared and told to equal the dictionary value it thinks it is a string or object. Especially when I am using the same syntax as the above working values.

What confuses me more is if I use the following script,

for level in levels.keys():
    perms=levels[level]['permissions']
    print(perms)

it works perfectly and has the following output,

8
1610079683
470011072
67226688

Including printing the two empty values. (I have tried changing these to a value but still get an error.)

Any help, ideas, etc. would be appreciated.


Solution

  • Simple answer: The function guild.create_role() expects as discord.Permissions object for the parameter permissions, and you are passing a string.

    Perhaps this will work:

    await guild.create_role(name=name,hoist=hoist,permissions=discord.Permissions(int(perms)))
    

    The API reference might be of use: https://discordpy.readthedocs.io/en/latest/api.html