user13878676
user13878676

Reputation:

How to change channel permissions in discord.py?

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.

Upvotes: 2

Views: 6656

Answers (1)

Ecks Dee
Ecks Dee

Reputation: 471

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)

Upvotes: 5

Related Questions