我如何锁定提到的频道?不和谐

时间:2020-07-03 00:29:17

标签: discord.py-rewrite

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

@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!')```

1 个答案:

答案 0 :(得分:0)

涉及两个通道对象: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.')