Add allow and deny command to chan and move main command to create
This commit is contained in:
parent
7b68cd34fb
commit
3a1e6745ff
1 changed files with 77 additions and 17 deletions
|
@ -1,10 +1,10 @@
|
|||
from discord.ext import commands
|
||||
from discord import Embed, TextChannel
|
||||
from discord.ext.commands import CommandNotFound, MissingRequiredArgument
|
||||
from discord import Embed, TextChannel, VoiceChannel
|
||||
from discord.abc import GuildChannel
|
||||
from discord.ext.commands import CommandNotFound, MissingRequiredArgument, CheckFailure, BadArgument
|
||||
|
||||
from bot_bde.logger import logger
|
||||
|
||||
|
||||
extension_name = "chan"
|
||||
logger = logger.getChild(extension_name)
|
||||
REACTIONS = []
|
||||
|
@ -13,6 +13,29 @@ for i in range(10):
|
|||
REACTIONS.append("\U0001F51F")
|
||||
|
||||
|
||||
def check_editable_chan():
|
||||
async def predicate(ctx: commands.Context):
|
||||
if len(ctx.message.channel_mentions) == 0:
|
||||
return False
|
||||
elif len(ctx.message.channel_mentions) > 1:
|
||||
return False
|
||||
elif len(ctx.message.role_mentions) == 0 and len(ctx.message.mentions) == 0:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
return commands.check(predicate)
|
||||
|
||||
|
||||
def chan_permissions(chan: GuildChannel, allow: bool):
|
||||
if type(chan) == TextChannel:
|
||||
return dict(read_messages=allow, send_messages=allow)
|
||||
elif type(chan) == VoiceChannel:
|
||||
return dict(connect=allow, speak=allow)
|
||||
else:
|
||||
raise BadArgument("Chan type Invalid")
|
||||
|
||||
|
||||
class Chan(commands.Cog):
|
||||
def __init__(self, bot: commands.Bot):
|
||||
self.bot = bot
|
||||
|
@ -20,10 +43,13 @@ class Chan(commands.Cog):
|
|||
|
||||
@commands.group("chan", pass_context=True)
|
||||
@commands.guild_only()
|
||||
async def chan(self, ctx: commands.Context, name: str):
|
||||
if name == "help":
|
||||
async def chan(self, ctx: commands.Context):
|
||||
if ctx.invoked_subcommand is None:
|
||||
await ctx.invoke(self.chan_help)
|
||||
else:
|
||||
|
||||
@chan.group("create", pass_context=True)
|
||||
@commands.guild_only()
|
||||
async def chan_create(self, ctx: commands.Context, name: str):
|
||||
chan: TextChannel = await ctx.guild.create_text_channel(name)
|
||||
if len(ctx.message.role_mentions) != 0 or len(ctx.message.mentions) != 0:
|
||||
await chan.set_permissions(ctx.guild.default_role, read_messages=False, send_messages=False)
|
||||
|
@ -32,13 +58,47 @@ class Chan(commands.Cog):
|
|||
for m in ctx.message.mentions:
|
||||
await chan.set_permissions(m, read_messages=True, send_messages=True)
|
||||
|
||||
@chan.group("deny", pass_context=True)
|
||||
@commands.guild_only()
|
||||
@check_editable_chan()
|
||||
async def chan_deny(self, ctx: commands.Context):
|
||||
for r in ctx.message.role_mentions:
|
||||
await ctx.message.channel_mentions[0].set_permissions(r,
|
||||
**chan_permissions(ctx.message.channel_mentions[0],
|
||||
False))
|
||||
for m in ctx.message.mentions:
|
||||
await ctx.message.channel_mentions[0].set_permissions(m,
|
||||
**chan_permissions(ctx.message.channel_mentions[0],
|
||||
False))
|
||||
|
||||
@chan.group("allow", pass_context=True)
|
||||
@commands.guild_only()
|
||||
@check_editable_chan()
|
||||
async def allow_deny(self, ctx: commands.Context):
|
||||
for r in ctx.message.role_mentions:
|
||||
await ctx.message.channel_mentions[0].set_permissions(r,
|
||||
**chan_permissions(ctx.message.channel_mentions[0],
|
||||
True))
|
||||
for m in ctx.message.mentions:
|
||||
await ctx.message.channel_mentions[0].set_permissions(m,
|
||||
**chan_permissions(ctx.message.channel_mentions[0],
|
||||
True))
|
||||
|
||||
@chan.group("help", pass_context=True)
|
||||
@commands.guild_only()
|
||||
async def chan_help(self, ctx: commands.Context):
|
||||
embed = Embed(title="chan help")
|
||||
embed.add_field(name="chan <name> [@role|@user]",
|
||||
embed.add_field(name="chan create <name> [@role|@user]",
|
||||
value="Create a new chan, the roles and/or users mentioned will be the only one permitted to "
|
||||
"read and write in the chan\n",
|
||||
"read and write in the chan",
|
||||
inline=False)
|
||||
embed.add_field(name="chan deny <@chan> <@role|@user>",
|
||||
value="Edit chan permission, the roles and/or users mentioned will be deny for read and write "
|
||||
"in the chan",
|
||||
inline=False)
|
||||
embed.add_field(name="chan allow <@chan> <@role|@user>",
|
||||
value="Edit chan permission, the roles and/or users mentioned will be allow to read and write "
|
||||
"in the chan",
|
||||
inline=False)
|
||||
await ctx.send(embed=embed)
|
||||
|
||||
|
@ -49,7 +109,7 @@ class Chan(commands.Cog):
|
|||
if isinstance(error, CommandNotFound):
|
||||
await ctx.message.add_reaction("\u2753")
|
||||
await ctx.message.delete(delay=30)
|
||||
if isinstance(error, MissingRequiredArgument):
|
||||
if isinstance(error, MissingRequiredArgument) or isinstance(error, CheckFailure):
|
||||
await ctx.message.add_reaction("\u274C")
|
||||
await ctx.message.delete(delay=30)
|
||||
else:
|
||||
|
|
Reference in a new issue