diff --git a/.flake8 b/.flake8 index 7b3133a..2ccbf95 100644 --- a/.flake8 +++ b/.flake8 @@ -1,6 +1,7 @@ [flake8] extend-ignore = - Q0, E501, C812, E203, W503 # These default to arguing with Black. We might configure some of them eventually + Q0, E501, C812, E203, W503, # These default to arguing with Black. We might configure some of them eventually + ANN101, # Ignore self annotation ANN204, ANN206, # return annotations for special methods and class methods D105, D107, # Missing Docstrings in magic method and __init__ S311, # Standard pseudo-random generators are not suitable for security/cryptographic purposes. diff --git a/jarvis/cogs/admin/lock.py b/jarvis/cogs/admin/lock.py index eaf4f13..6fe34b7 100644 --- a/jarvis/cogs/admin/lock.py +++ b/jarvis/cogs/admin/lock.py @@ -1,134 +1,113 @@ """J.A.R.V.I.S. LockCog.""" -from contextlib import suppress -from typing import Union +from dis_snek import Scale -from discord import Role, TextChannel, User, VoiceChannel -from discord.ext.commands import Bot -from discord_slash import SlashContext, cog_ext -from discord_slash.utils.manage_commands import create_option - -from jarvis.db.models import Lock -from jarvis.utils.cachecog import CacheCog -from jarvis.utils.permissions import admin_or_permissions +# TODO: Uncomment 99% of code once implementation is figured out +# from contextlib import suppress +# from typing import Union +# +# from dis_snek import InteractionContext, Scale, Snake +# from dis_snek.models.discord.enums import Permissions +# from dis_snek.models.discord.role import Role +# from dis_snek.models.discord.user import User +# from dis_snek.models.discord.channel import GuildText, GuildVoice, PermissionOverwrite +# from dis_snek.models.snek.application_commands import ( +# OptionTypes, +# PermissionTypes, +# slash_command, +# slash_option, +# ) +# from dis_snek.models.snek.command import check +# +# from jarvis.db.models import Lock +# from jarvis.utils.permissions import admin_or_permissions -class LockCog(CacheCog): +class LockCog(Scale): """J.A.R.V.I.S. LockCog.""" - def __init__(self, bot: Bot): - super().__init__(bot) - - async def _lock_channel( - self, - channel: Union[TextChannel, VoiceChannel], - role: Role, - admin: User, - reason: str, - allow_send: bool = False, - ) -> None: - overrides = channel.overwrites_for(role) - if isinstance(channel, TextChannel): - overrides.send_messages = allow_send - elif isinstance(channel, VoiceChannel): - overrides.speak = allow_send - await channel.set_permissions(role, overwrite=overrides, reason=reason) - - async def _unlock_channel( - self, - channel: Union[TextChannel, VoiceChannel], - role: Role, - admin: User, - ) -> None: - overrides = channel.overwrites_for(role) - if isinstance(channel, TextChannel): - overrides.send_messages = None - elif isinstance(channel, VoiceChannel): - overrides.speak = None - await channel.set_permissions(role, overwrite=overrides) - - @cog_ext.cog_slash( - name="lock", - description="Locks a channel", - choices=[ - create_option( - name="reason", - description="Lock Reason", - option_type=3, - required=True, - ), - create_option( - name="duration", - description="Lock duration in minutes (default 10)", - option_type=4, - required=False, - ), - create_option( - name="channel", - description="Channel to lock", - option_type=7, - required=False, - ), - ], - ) - @admin_or_permissions(manage_channels=True) - async def _lock( - self, - ctx: SlashContext, - reason: str, - duration: int = 10, - channel: Union[TextChannel, VoiceChannel] = None, - ) -> None: - await ctx.defer(hidden=True) - if duration <= 0: - await ctx.send("Duration must be > 0", hidden=True) - return - elif duration >= 300: - await ctx.send("Duration must be < 5 hours", hidden=True) - return - if len(reason) > 100: - await ctx.send("Reason must be < 100 characters", hidden=True) - return - if not channel: - channel = ctx.channel - for role in ctx.guild.roles: - with suppress(Exception): - await self._lock_channel(channel, role, ctx.author, reason) - _ = Lock( - channel=channel.id, - guild=ctx.guild.id, - admin=ctx.author.id, - reason=reason, - duration=duration, - ).save() - await ctx.send(f"{channel.mention} locked for {duration} minute(s)") - - @cog_ext.cog_slash( - name="unlock", - description="Unlocks a channel", - choices=[ - create_option( - name="channel", - description="Channel to lock", - option_type=7, - required=False, - ), - ], - ) - @admin_or_permissions(manage_channels=True) - async def _unlock( - self, - ctx: SlashContext, - channel: Union[TextChannel, VoiceChannel] = None, - ) -> None: - if not channel: - channel = ctx.channel - lock = Lock.objects(guild=ctx.guild.id, channel=channel.id, active=True).first() - if not lock: - await ctx.send(f"{channel.mention} not locked.", hidden=True) - return - for role in ctx.guild.roles: - with suppress(Exception): - await self._unlock_channel(channel, role, ctx.author) - lock.active = False - lock.save() - await ctx.send(f"{channel.mention} unlocked") + # @slash_command(name="lock", description="Lock a channel") + # @slash_option(name="reason", + # description="Lock Reason", + # opt_type=3, + # required=True,) + # @slash_option(name="duration", + # description="Lock duration in minutes (default 10)", + # opt_type=4, + # required=False,) + # @slash_option(name="channel", + # description="Channel to lock", + # opt_type=7, + # required=False,) + # @check(admin_or_permissions(Permissions.MANAGE_CHANNELS)) + # async def _lock( + # self, + # ctx: InteractionContext, + # reason: str, + # duration: int = 10, + # channel: Union[GuildText, GuildVoice] = None, + # ) -> None: + # await ctx.defer(ephemeral=True) + # if duration <= 0: + # await ctx.send("Duration must be > 0", ephemeral=True) + # return + # + # elif duration > 60 * 12: + # await ctx.send("Duration must be <= 12 hours", ephemeral=True) + # return + # + # if len(reason) > 100: + # await ctx.send("Reason must be <= 100 characters", ephemeral=True) + # return + # if not channel: + # channel = ctx.channel + # + # # role = ctx.guild.default_role # Uncomment once implemented + # if isinstance(channel, GuildText): + # to_deny = Permissions.SEND_MESSAGES + # elif isinstance(channel, GuildVoice): + # to_deny = Permissions.CONNECT | Permissions.SPEAK + # + # overwrite = PermissionOverwrite(type=PermissionTypes.ROLE, deny=to_deny) + # # TODO: Get original permissions + # # TODO: Apply overwrite + # overwrite = overwrite + # _ = Lock( + # channel=channel.id, + # guild=ctx.guild.id, + # admin=ctx.author.id, + # reason=reason, + # duration=duration, + # ) # .save() # Uncomment once implemented + # # await ctx.send(f"{channel.mention} locked for {duration} minute(s)") + # await ctx.send("Unfortunately, this is not yet implemented", hidden=True) + # + # @cog_ext.cog_slash( + # name="unlock", + # description="Unlocks a channel", + # choices=[ + # create_option( + # name="channel", + # description="Channel to lock", + # opt_type=7, + # required=False, + # ), + # ], + # ) + # @check(admin_or_permissions(Permissions.MANAGE_CHANNELS)) + # async def _unlock( + # self, + # ctx: InteractionContext, + # channel: Union[GuildText, GuildVoice] = None, + # ) -> None: + # if not channel: + # channel = ctx.channel + # lock = Lock.objects(guild=ctx.guild.id, channel=channel.id, active=True).first() + # if not lock: + # await ctx.send(f"{channel.mention} not locked.", ephemeral=True) + # return + # for role in ctx.guild.roles: + # with suppress(Exception): + # await self._unlock_channel(channel, role, ctx.author) + # lock.active = False + # lock.save() + # await ctx.send(f"{channel.mention} unlocked") diff --git a/jarvis/cogs/admin/lockdown.py b/jarvis/cogs/admin/lockdown.py index 6f7055c..cd711fb 100644 --- a/jarvis/cogs/admin/lockdown.py +++ b/jarvis/cogs/admin/lockdown.py @@ -8,7 +8,8 @@ from discord_slash.utils.manage_commands import create_option from jarvis.db.models import Lock from jarvis.utils.cachecog import CacheCog -from jarvis.utils.permissions import admin_or_permissions + +# from jarvis.utils.permissions import admin_or_permissions class LockdownCog(CacheCog): @@ -25,30 +26,30 @@ class LockdownCog(CacheCog): create_option( name="reason", description="Lockdown Reason", - option_type=3, + opt_type=3, required=True, ), create_option( name="duration", description="Lockdown duration in minutes (default 10)", - option_type=4, + opt_type=4, required=False, ), ], ) - @admin_or_permissions(manage_channels=True) + # @check(admin_or_permissions(manage_channels=True)) async def _lockdown_start( self, ctx: SlashContext, reason: str, duration: int = 10, ) -> None: - await ctx.defer(hidden=True) + await ctx.defer(ephemeral=True) if duration <= 0: - await ctx.send("Duration must be > 0", hidden=True) + await ctx.send("Duration must be > 0", ephemeral=True) return elif duration >= 300: - await ctx.send("Duration must be < 5 hours", hidden=True) + await ctx.send("Duration must be < 5 hours", ephemeral=True) return channels = ctx.guild.channels roles = ctx.guild.roles @@ -87,7 +88,7 @@ class LockdownCog(CacheCog): update = False locks = Lock.objects(guild=ctx.guild.id, active=True) if not locks: - await ctx.send("No lockdown detected.", hidden=True) + await ctx.send("No lockdown detected.", ephemeral=True) return await ctx.defer() for channel in channels: