不和谐赋予角色

时间:2020-08-07 10:46:50

标签: python discord.py

@client.command()
@commands.has_permissions(manage_messages=True)
async def mute(ctx, member: discord.Member):
    try:
        guild = ctx.guild
        rolecreate = "MutedByCloudy"
        await guild.create_role(rolecreate, colour=discord.Colour.red())
        if member.guild_permissions > ctx.author.guild_permissions or member.guild_permissions.administrator:
            await ctx.send("I can't mute that user. User probably has more perms than me or you")
        else:
            if member.id == 739424025205538817:
                await ctx.send("Nice try. I can't mute myself.")
            else:
                role = get(member.guild.roles, name='MutedByCloudy')
                await member.add_roles(role)
                embed=discord.Embed(title="User Muted! :white_check_mark:", description=f"**{member}** was muted by **{ctx.message.author}**!", color=0x2f3136)
                await ctx.send(embed=embed)
    except:
            await ctx.send(":x: Something happened. I don't know what.")

所以我有此命令来创建角色,并将其提供给用户。但是它不会创建角色并且不会引发任何错误,你们可以帮忙吗?给予角色部分有效,而创造角色无效。

1 个答案:

答案 0 :(得分:1)

不是要创建角色,您可以尝试类似以下操作:

role = discord.utils.get(ctx.guild.roles, name='MutedByCloudy')
if not role:
    role = await ctx.guild.create_role(name='MutedByCloudy', reason='Role for muting, auto generated by Cloudy')
    for channel in ctx.guild.channels:
        await channel.set_permissions(role, send_messages=False,
                                      read_message_history=False,
                                      read_messages=False)

您也可以添加try和discord除外。禁止处理权限错误