【问题标题】:How do i lock a channel that is mentioned? Discord.py我如何锁定提到的频道?不和谐.py
【发布时间】:2020-10-23 16:05:54
【问题描述】:

这是我的代码,它只锁定我当前所在的频道,我觉得有点愚蠢,因为我认为我应该知道这个大声笑。例如,我想说'-lock #channelname 但它只锁定我当前的频道。

@client.command()
@commands.has_permissions(manage_channels=True)
async def lock(ctx, channel : discord.TextChannel=None):
    overwrite = ctx.channel.overwrites_for(ctx.guild.default_role)
    overwrite.send_messages = False
    await ctx.channel.set_permissions(ctx.guild.default_role, overwrite=overwrite)
    await ctx.send('Channel locked.')
@lock.error
async def lock_error(ctx, error):
    if isinstance(error,commands.CheckFailure):
        await ctx.send('You do not have permission to use this command!')

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    涉及两个通道对象:ctx.channel 是调用命令的通道,或 channel 是作为命令调用的一部分传递的通道。下面是一些使用channel(如果可用)的代码,否则使用ctx.channel

    @commands.has_permissions(manage_channels=True)
    async def lock(ctx, channel : discord.TextChannel=None):
        channel = channel or ctx.channel
        overwrite = channel.overwrites_for(ctx.guild.default_role)
        overwrite.send_messages = False
        await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite)
        await ctx.send('Channel locked.')
    

    【讨论】:

    • 啊,是的,谢谢。我的思路是正确的,并且实际上在某些时候使用过“channel = channel 或 ctx.channel”,但当时肯定有其他一些小问题。欣赏它
    猜你喜欢
    • 2021-11-15
    • 2021-04-06
    • 2021-02-24
    • 2021-06-22
    • 2021-12-25
    • 2020-05-07
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多