1
0
Fork 0

Add chan permission check on command

This commit is contained in:
Ethanell 2020-05-17 23:11:50 +02:00
parent 3a1e6745ff
commit 3b1dd1b5bd

View file

@ -7,10 +7,6 @@ from bot_bde.logger import logger
extension_name = "chan" extension_name = "chan"
logger = logger.getChild(extension_name) logger = logger.getChild(extension_name)
REACTIONS = []
for i in range(10):
REACTIONS.append(str(i) + "\ufe0f\u20E3")
REACTIONS.append("\U0001F51F")
def check_editable_chan(): def check_editable_chan():
@ -27,6 +23,16 @@ def check_editable_chan():
return commands.check(predicate) return commands.check(predicate)
def check_permissions():
async def predicate(ctx: commands.Context):
if len(ctx.message.channel_mentions) == 1:
return ctx.message.channel_mentions[0].permissions_for(ctx.author).manage_channels
else:
return ctx.author.guild_permissions.manage_channels
return commands.check(predicate)
def chan_permissions(chan: GuildChannel, allow: bool): def chan_permissions(chan: GuildChannel, allow: bool):
if type(chan) == TextChannel: if type(chan) == TextChannel:
return dict(read_messages=allow, send_messages=allow) return dict(read_messages=allow, send_messages=allow)
@ -49,6 +55,7 @@ class Chan(commands.Cog):
@chan.group("create", pass_context=True) @chan.group("create", pass_context=True)
@commands.guild_only() @commands.guild_only()
@check_permissions()
async def chan_create(self, ctx: commands.Context, name: str): async def chan_create(self, ctx: commands.Context, name: str):
chan: TextChannel = await ctx.guild.create_text_channel(name) chan: TextChannel = await ctx.guild.create_text_channel(name)
if len(ctx.message.role_mentions) != 0 or len(ctx.message.mentions) != 0: if len(ctx.message.role_mentions) != 0 or len(ctx.message.mentions) != 0:
@ -61,6 +68,7 @@ class Chan(commands.Cog):
@chan.group("deny", pass_context=True) @chan.group("deny", pass_context=True)
@commands.guild_only() @commands.guild_only()
@check_editable_chan() @check_editable_chan()
@check_permissions()
async def chan_deny(self, ctx: commands.Context): async def chan_deny(self, ctx: commands.Context):
for r in ctx.message.role_mentions: for r in ctx.message.role_mentions:
await ctx.message.channel_mentions[0].set_permissions(r, await ctx.message.channel_mentions[0].set_permissions(r,
@ -74,6 +82,7 @@ class Chan(commands.Cog):
@chan.group("allow", pass_context=True) @chan.group("allow", pass_context=True)
@commands.guild_only() @commands.guild_only()
@check_editable_chan() @check_editable_chan()
@check_permissions()
async def allow_deny(self, ctx: commands.Context): async def allow_deny(self, ctx: commands.Context):
for r in ctx.message.role_mentions: for r in ctx.message.role_mentions:
await ctx.message.channel_mentions[0].set_permissions(r, await ctx.message.channel_mentions[0].set_permissions(r,