具有权限的不和谐py角色创建

时间:2020-04-06 20:55:11

标签: python-3.x discord.py

作为辅助项目,我已经在这个项目上工作了2个月,在我的生命中,我无法让机器人创建具有权限的角色。这就是我所拥有的。

levels={
        "Admin":{"name":"ADMIN","hoist":"1","colour":"0x6F0A0A","permissions":""},
    "Moderator":{"name":"WATCHMEN","hoist":"1","colour":"0xFF611A","permissions":"discord.permissions(manage_roles=True,kick_members=True,ban_members=True,create_instant_invite=True,mention_everyone=True,change_nickname=True,manage_nicknames=True,read_message_history,send_messages=True,embed_links=True,send_tts_messages,attach_files=True,external_emojis=True,add-reactions=True)"},
    "Member":{"name":"MEMBER","hoist":"0","colour":"0x52D41A","permissions":"discord.permissions(send_messages=True)"},
    "Verify":{"name":"VERIFY","hoist":"1","colour":"0xFFFFFF","permissions":"discord.permissions(send_messages=True)"},

}

async def cook_roles(ctx):
    for level in levels.keys():
        guild=ctx.guild
        name=levels[level]['name']
        hoist=levels[level]['hoist']
        colour=levels[level]['colour']
      #  perms=levels[level]['permissions']
        if name == "Admin":
            perms=discord.Permissions.all()
        else:
            perms=discord.Permissions(levels[level]['permissions'])
        print=(perms)
        await ctx.send(perms)
        await guild.create_role(name=name,hoist=hoist,permissions=perms,colour=discord.Colour(int(colour,16)))

需要任何帮助!

我试图带走discord.Permissions()并以这种方式格式化

perms=discord.Permissions(levles[level]['permissions'])

但是那也不起作用。 (我尝试了很多事情,但还没有弄清楚。)


这里是对第一个提供的答案的追溯:

回溯(最近通话最近): 文件“ /usr/local/lib/python3.6/dist-packages/discord/ext/commands/core.py”,第83行,已包装 ret =等待coro(* args,** kwargs) 在构建文件“ ny.py”,第483行 等待cook_roles(ctx) 在cook_roles中的文件“ ny.py”,第551行 等待公会.create_role(名称=名称,葫芦=葫芦,权限=烫发,颜色=不一致。颜色(整数(颜色,16)) TypeError:int()无法使用显式基数转换非字符串

上述异常是以下异常的直接原因:

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: TypeError: int() can't convert non-string with explicit base

1 个答案:

答案 0 :(得分:1)

您在dict中有一个字符串值,这不视为真实对象。

您可以在dict中存储任何类型的对象,因此,基本上,您要做的就是在具有实际类型的dict实际对象中创建值。

您还试图使用permissions模块而不是Permissions类。 并始终确保权限名称存在:discord.Permissions (您的错误是因为缺少一些=Trueadd-reactions而不是add_reactions

levels = {
    "Admin": {"name": "ADMIN", "hoist": True, "colour": 0x6F0A0A, "permissions": discord.Permissions()},
    "Moderator": {
        "name": "WATCHMEN",
        "hoist": True,
        "colour": 0xFF611A,
        "permissions": discord.Permissions(manage_roles=True,
        kick_members=True,
        ban_members=True,
        create_instant_invite=True,
        mention_everyone=True,
        change_nickname=True,
        manage_nicknames=True,
        read_message_history=True,
        send_messages=True,
        embed_links=True,
        send_tts_messages=True,
        attach_files=True,
        external_emojis=True,
        add_reactions=True),
    },
    "Member": {
        "name": "MEMBER",
        "hoist": False,
        "colour": 0x52D41A,
        "permissions": discord.Permissions(send_messages=True),
    },
    "Verify": {
        "name": "VERIFY",
        "hoist": True,
        "colour": 0xFFFFFF,
        "permissions": discord.Permissions(send_messages=True),
    },
}