discord.py-删除用户频道权限并显示错误消息“您已经阻止了此频道”

时间:2020-07-16 04:50:30

标签: python discord.py discord.py-rewrite

这些命令存在一些问题。 在第一种情况下,我无法使它显示为错误消息,表明该通道当前已被阻止(我不知道该怎么做,但是我需要它来避免以后的错误)

代码:

@client.command(aliases=["Lock","LOCK","blocco","Blocco","BLOCCO"])
@commands.has_any_role('Triumvirato')
async def lock(ctx, *,arg="Motivo non specificato"):
    channel_ability_mention = ctx.channel.mention
    channel_ability_name = ctx.channel.name
    embed = discord.Embed(
        color=0xa61022
    )
    if oof == off: #here I put something at random because I don't know how to form the if
        embed.set_author(
            name=f'{channel_ability_name} è attualmente bloccato',
            icon_url=f'https://cdn.discordapp.com/attachments/640563710104043530/730639329453670420/DuscePeppe_FRIULI.png'
        )
        embed.set_footer(text=f'Motivo: {arg}')
        await ctx.send(embed=embed, delete_after=10.0)
        return

在第二种情况下,我不知道如何删除用户权限(这将临时显示该templock也将由没有管理权限的人使用

代码:

async def templock(ctx, seconds = 0, *,arg="Motivo non specificato"):
    channel_ability_mention = ctx.channel.mention
    channel_ability_name = ctx.channel.name
    triumvirato = discord.utils.get(ctx.guild.roles, id=690951634183782461)
    co_triumvirato = discord.utils.get(ctx.guild.roles, id=690954867346243624)
    presidente = discord.utils.get(ctx.guild.roles, id=690956686147453048)
    moderatore = discord.utils.get(ctx.guild.roles, id=700353561392971877)
    embed = discord.Embed(
        color=0xa61022
    )
    if not seconds:
        embed.set_author(
            name="Per favore specifica il tempo!",
            icon_url="https://cdn.discordapp.com/attachments/640563710104043530/730639329453670420/DuscePeppe_FRIULI.png"
        )
        await ctx.send(embed=embed, delete_after=10.0)
        return
    if triumvirato in ctx.author.roles:
        if seconds > 600:
            embed.set_author(
                name="Hai superato il limite stabilito per i Triumviri!",
                icon_url="https://cdn.discordapp.com/attachments/640563710104043530/730639329453670420/DuscePeppe_FRIULI.png"
            )
            await ctx.send(embed=embed, delete_after=10.0)
            return
        embed = discord.Embed(
            color=0x03c03c
        )
        if seconds == 1:
            embed.set_author(
                name=f'Hai bloccato {channel_ability_name} per un secondo',
                icon_url=f'{ctx.author.avatar_url}'
            )
            embed.set_footer(text=f'Motivo: {arg}')
            await ctx.send(embed=embed, delete_after=10.0)
            await ctx.channel.set_permissions(ctx.author, send_messages=True)
            await ctx.channel.set_permissions(ctx.guild.default_role, manage_channels=False, manage_permissions=False, read_messages=False, send_messages=False, manage_messages=False, add_reactions=False)
            embed = discord.Embed(
                color=0xFFD000
            )
            embed.set_author(
                name=f'{ctx.author._user} ha bloccato il canale {channel_ability_name}',
                icon_url=f'{ctx.author.avatar_url}'
            )
            embed.set_footer(text=f'Motivo: {arg}')
            embed.add_field(
                name='Bloccato da:',
                value=f'{ctx.author._user}',
                inline=True
            )
            embed.add_field(
                name='Canale:',
                value=f'{channel_ability_mention}',
                inline=True
            )
            embed.add_field(
                name='Durata:',
                value=f'{seconds}s',
                inline=True
            )
            channel = client.get_channel(729553772547932190)
            await channel.send(embed=embed)
        if seconds > 1:
            embed.set_author(
                name=f'Hai bloccato {channel_ability_name} per {seconds} secondi',
                icon_url=f'{ctx.author.avatar_url}'
            )
            embed.set_footer(text=f'Motivo: {arg}')
            await ctx.send(embed=embed, delete_after=10.0)
            await ctx.channel.set_permissions(ctx.guild.default_role, manage_channels=False, manage_permissions=False, read_messages=False, send_messages=False, manage_messages=False, add_reactions=False)
            embed = discord.Embed(
                color=0xFFD000
            )
            embed.set_author(
                name=f'{ctx.author._user} ha bloccato il canale {channel_ability_name}',
                icon_url=f'{ctx.author.avatar_url}'
            )
            embed.set_footer(text=f'Motivo: {arg}')
            embed.add_field(
                name='Bloccato da:',
                value=f'{ctx.author._user}',
                inline=True
            )
            embed.add_field(
                name='Canale:',
                value=f'{channel_ability_mention}',
                inline=True
            )
            embed.add_field(
                name='Durata:',
                value=f'{seconds}s',
                inline=True
            )
            channel = client.get_channel(729553772547932190)
            await channel.send(embed=embed)
        await asyncio.sleep(seconds)
        embed = discord.Embed(
            color=0x03c03c
        )
        embed.set_author(
            name=f'Hai sbloccato {channel_ability_name}',
            icon_url=f'{ctx.author.avatar_url}'
        )
        await ctx.send(embed=embed, delete_after=10.0)
        await ctx.channel.set_permissions(ctx.author, ) #I don't think set_permissions should stay here as I want the user permissions to disappear
        await ctx.channel.set_permissions(ctx.guild.default_role, manage_channels=False, manage_permissions=False, read_messages=False, manage_messages=False)
        embed = discord.Embed(
            color=0xFFD000
        )
        embed.set_author(
            name=f'{client.user} ha sbloccato il canale {channel_ability_name}',
            icon_url=f'{ctx.author.avatar_url}'
        )
        embed.add_field(
            name='Sbloccato da:',
            value=f'{client.user}',
            inline=True
        )
        embed.add_field(
            name='Canale:',
            value=f'{channel_ability_mention}',
            inline=True
        )
        channel = client.get_channel(729553772547932190)
        await channel.send(embed=embed)
        return

编辑:我试图同时进行锁定和解锁,但是如果我执行两个命令之一,则另一个命令无效。我不知道怎么解决

#lock [COMPLETATO]
@client.command(aliases=["Lock","LOCK","blocco","Blocco","BLOCCO"])
@commands.has_any_role('Triumvirato')
async def lock(ctx, *,arg="Motivo non specificato"):
    channel_ability_mention = ctx.channel.mention
    channel_ability_name = ctx.channel.name
    embed = discord.Embed(
        color=0xa61022
    )
    try:
        temp = locked_channels[ctx.channel.id]
    except:
        locked_channels[ctx.channel.id] = True
        embed = discord.Embed(
            color=0x03c03c
        )
        embed.set_author(
            name=f'Hai bloccato {channel_ability_name}',
            icon_url=f'{ctx.author.avatar_url}'
        )
        embed.set_footer(text=f'Motivo: {arg}')
        await ctx.send(embed=embed, delete_after=10.0)
        await ctx.channel.set_permissions(ctx.guild.default_role, manage_channels=False, manage_permissions=False, read_messages=False, send_messages=False, manage_messages=False, add_reactions=False)
        embed = discord.Embed(
            color=0xFFD000
        )
        embed.set_author(
            name=f'{ctx.author._user} ha sbloccato il canale {channel_ability_name}',
            icon_url=f'{ctx.author.avatar_url}'
        )
        embed.set_footer(text=f'Motivo: {arg}')
        embed.add_field(
            name='Bloccato da:',
            value=f'{ctx.author._user}',
            inline=True
        )
        embed.add_field(
            name='Canale:',
            value=f'{channel_ability_mention}',
            inline=True
        )
        embed.add_field(
            name='Durata:',
            value=f'∞',
            inline=True
        )
        channel = client.get_channel(729553772547932190)
        await channel.send(embed=embed)
        return
    else:
        embed.set_author(
            name=f'{channel_ability_name} è attualmente bloccato',
            icon_url=f'https://cdn.discordapp.com/attachments/640563710104043530/730639329453670420/DuscePeppe_FRIULI.png'
        )
        await ctx.send(embed=embed, delete_after=10.0)
        return
@lock.error
async def lock_error(ctx, error):
    embed=discord.Embed(
        color=0xa61022
    )
    if isinstance(error, commands.CheckFailure):
        embed.set_author(
            name="Non ti è permesso bloccare il canale!",
            icon_url='https://cdn.discordapp.com/attachments/640563710104043530/730639329453670420/DuscePeppe_FRIULI.png'
        )
        await ctx.send(embed=embed, delete_after=10.0)
        return

#unlock [MANCA : MESSAGGIO ERRORE CANALE GIA' SBLOCCATO]
@client.command(aliases=["Unock","UNLOCK","ul","Ul","UL","sblocco","Sblocco","SBLOCCO"])
@commands.has_any_role('Triumvirato')
async def unlock(ctx):
    channel_ability_mention = ctx.channel.mention
    channel_ability_name = ctx.channel.name
    embed = discord.Embed(
        color=0xa61022
    )
    try:
        temp = locked_channels[ctx.channel.id]
    except:
        locked_channels[ctx.channel.id] = False
        embed = discord.Embed(
            color=0x03c03c
        )
        embed.set_author(
            name=f'Hai sbloccato {channel_ability_name}',
            icon_url=f'{ctx.author.avatar_url}'
        )
        await ctx.send(embed=embed, delete_after=10.0)
        await ctx.channel.set_permissions(ctx.guild.default_role, manage_channels=False, manage_permissions=False, read_messages=False, manage_messages=False)
        embed = discord.Embed(
            color=0xFFD000
        )
        embed.set_author(
            name=f'{ctx.author._user} ha sbloccato il canale {channel_ability_name}',
            icon_url=f'{ctx.author.avatar_url}'
        )
        embed.set_footer(text=f'Motivo: {arg}')
        embed.add_field(
            name='Sbloccato da:',
            value=f'{ctx.author._user}',
            inline=True
        )
        embed.add_field(
            name='Canale:',
            value=f'{channel_ability_mention}',
            inline=True
        )
        channel = client.get_channel(729553772547932190)
        await channel.send(embed=embed)
        return
    else:
        embed.set_author(
            name=f'{channel_ability_name} è attualmente sbloccato',
            icon_url=f'https://cdn.discordapp.com/attachments/640563710104043530/730639329453670420/DuscePeppe_FRIULI.png'
        )
        await ctx.send(embed=embed, delete_after=10.0)
        return
@lock.error
async def lock_error(ctx, error):
    embed=discord.Embed(
        color=0xa61022
    )
    if isinstance(error, commands.CheckFailure):
        embed.set_author(
            name="Non ti è permesso bloccare il canale!",
            icon_url='https://cdn.discordapp.com/attachments/640563710104043530/730639329453670420/DuscePeppe_FRIULI.png'
        )
        await ctx.send(embed=embed, delete_after=10.0)
        return

1 个答案:

答案 0 :(得分:0)

要跟踪哪个频道被锁定,您可以创建一个字典:

#You can put it everywhere you want, it just has to be a global variable (not in a function, a class, ...)
locked_channels = {} 

然后,您可以将lock命令更改为:

@client.command(aliases=["Lock","LOCK","blocco","Blocco","BLOCCO"])
@commands.has_any_role('Triumvirato')
async def lock(ctx, *, arg="No reason specified"):
    channel_ability_mention = ctx.channel.mention
    channel_ability_name = ctx.channel.name
    embed = discord.Embed(color=0xa61022)

    try:
        temp = locked_channels[ctx.channel.id]
    except:
        locked_channels[ctx.channel.id] = True
        #block your channel
    else:
        if locked_channels[ctx.channel.id]:
            embed.set_author(name=f'This channel is already locked')
                await channel.send(embed=embed)

最后,您可以将templock命令更改为(我做了很多重构):

@client.command()
async def templock(self, ctx, seconds = 0, *,arg="Motivo non specificato"):
    channel_ability_mention = ctx.channel.mention
    channel_ability_name = ctx.channel.name
    channel = client.get_channel(729553772547932190)
    triumvirato = get(ctx.guild.roles, id=690951634183782461)
    embed = Embed()

    try: 
        temp = locked_channels[ctx.channel.id]
    except:
        if triumvirato in ctx.author.roles:
            if not seconds:
                embed.set_author(
                    name="Per favore specifica il tempo!",
                    icon_url="icon_url"
                )
                await ctx.send(embed=embed, delete_after=10.0)

            elif 0 > seconds >= 600: 
                embed.color = 0x03c03c
                embed.set_author(
                    name= f'{ctx.author} locked {channel_ability_name} for {seconds} second' + ('s' if seconds>1 else ''),
                    icon_url=f'{ctx.author.avatar_url}'
                )
                embed.set_footer(text=f'Reason: {arg}')
                await ctx.send(embed=embed)
                await ctx.channel.set_permissions(ctx.guild.default_role, read_messages=False, send_messages=False, add_reactions=False)
                locked_channels[ctx.channel.id] = True
                await channel.send(embed=embed)

                await asyncio.sleep(seconds)
                if locked_channels[ctx.channel.id]:
                    locked_channels[ctx.channel.id] = False
                    embed.color=0xFFD000
                    embed.set_author(
                        name=f'The {channel_ability_name} channel is now unlocked',
                        icon_url=f'{ctx.author.avatar_url}'
                    )
                    embed.set_footer('')
                    await channel.send(embed=embed)
                    await ctx.channel.set_permissions(ctx.author, overwrite=None)
                    await ctx.channel.set_permissions(ctx.guild.default_role, overwrite=None)
                else:
                    return

            else:
                embed.color = 0xa61022
                embed.set_author(
                    name="You have exceeded the limit set for Triumvirs!",
                    icon_url="icon_url"
                )
                await ctx.send(embed=embed, delete_after=10.0)

您的解锁命令如下所示:

@commands.command()
async def unlock(ctx):
    embed = Embed()
    try:
        if locked_channels[ctx.channel.id]:
             locked_channels[ctx.channel.id] = False
             embed.color = 0x03c03c
             embed.set_author(name=f'{ctx.author} has unlocked the {channel_ability_name} channel')
    except:
        embed.color = 0xa61022
        embed.set_author(name=f'This channel is already unlocked!')
    await ctx.send(embed=embed)