Connor Hancock
Connor Hancock

Reputation: 37

How do i lock a channel that is mentioned? Discord.py

Here is my code, It only locks the current channel I'm in, I feel a bit stupid as i think i should know this lol. For example, I want to say '-lock #channelname but it only locks my current channel.

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

Upvotes: 1

Views: 6591

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 60994

There are two channel objects involved: ctx.channel which is the channel the command was invoked in, or channel which is the channel that was passed as part of the command invocation. Below is some code for using channel if it's available, otherwise using 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.')

Upvotes: 2

Related Questions