How to change channel permissions in discord.py?

Viewed 4608

I want the bot to toggle just send messages permissions when the command is invoked. code:

    @commands.command()
    @commands.has_permissions(manage_channels=True)
    async def lock(self,ctx):
        await ctx.channel.set_permissions(ctx.guild.default_role, send_messages=False)

this works , it changes the send messages permissions to false for default role but it also effects the other permissions, it sets them to neutral(default). I don't want that, I want it to just toggle send messages and leave everything as it is.

1 Answers

Use <TextChannel>.overwrites_for() to get the current permissions on the role and then set send_message to False from the permissions it has.

    @commands.command()
    @commands.has_permissions(manage_channels=True)
    async def lock(self,ctx):
        perms = ctx.channel.overwrites_for(ctx.guild.default_role)
        perms.send_messages=False
        await ctx.channel.set_permissions(ctx.guild.default_role, overwrite=perms)
Related