0

我有以下脚本,

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)

我将该脚本与以下字典一起使用,

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":""}}

当我运行脚本时,出现以下错误,

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'

我知道一个字符串没有价值,一个对象没有价值,但我不明白为什么当 perms 被声明并被告知等于字典值时,它认为它是一个字符串或对象。特别是当我使用与上述工作值相同的语法时。

更让我困惑的是,如果我使用以下脚本,

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

它完美地工作并具有以下输出,

8
1610079683
470011072
67226688

包括打印两个空值。(我尝试将这些更改为一个值,但仍然出现错误。)

任何帮助、想法等将不胜感激。

4

1 回答 1

0

简单的答案:该函数guild.create_role()期望作为discord.Permissions参数的对象permissions,并且您正在传递一个字符串。

也许这会起作用:

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

API 参考可能有用:https ://discordpy.readthedocs.io/en/latest/api.html

于 2020-02-09T18:59:19.497 回答