discord.py删除用户频道权限并发送错误消息“您已阻止此频道”

2024-04-25 07:02:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我对这些命令有一些问题。 在第一种情况下,我无法使其显示为错误消息,即通道当前已被阻止(我不知道如何执行此操作,但我需要它以避免将来出现错误)

代码:

@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

Tags: namesendaddfalseurlchannelembedawait
1条回答
网友
1楼 · 发布于 2024-04-25 07:02:18

要跟踪锁定的频道,可以创建dict:

#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)

相关问题 更多 >