Disable lock and lockdown until dis-snek permissions work

This commit is contained in:
Zeva Rose 2022-02-21 15:11:22 -07:00
parent 90506c136e
commit 2a84d50c65
2 changed files with 215 additions and 215 deletions

View file

@ -1,7 +1,7 @@
"""J.A.R.V.I.S. LockCog.""" """J.A.R.V.I.S. LockCog."""
from dis_snek import Scale # from dis_snek import Scale
#
# TODO: Uncomment 99% of code once implementation is figured out # # TODO: Uncomment 99% of code once implementation is figured out
# from contextlib import suppress # from contextlib import suppress
# from typing import Union # from typing import Union
# #
@ -20,94 +20,94 @@ from dis_snek import Scale
# #
# from jarvis.db.models import Lock # from jarvis.db.models import Lock
# from jarvis.utils.permissions import admin_or_permissions # from jarvis.utils.permissions import admin_or_permissions
#
#
class LockCog(Scale): # class LockCog(Scale):
"""J.A.R.V.I.S. LockCog.""" # """J.A.R.V.I.S. LockCog."""
#
# @slash_command(name="lock", description="Lock a channel") # @slash_command(name="lock", description="Lock a channel")
# @slash_option(name="reason", # @slash_option(name="reason",
# description="Lock Reason", # description="Lock Reason",
# opt_type=3, # opt_type=3,
# required=True,) # required=True,)
# @slash_option(name="duration", # @slash_option(name="duration",
# description="Lock duration in minutes (default 10)", # description="Lock duration in minutes (default 10)",
# opt_type=4, # opt_type=4,
# required=False,) # required=False,)
# @slash_option(name="channel", # @slash_option(name="channel",
# description="Channel to lock", # description="Channel to lock",
# opt_type=7, # opt_type=7,
# required=False,) # required=False,)
# @check(admin_or_permissions(Permissions.MANAGE_CHANNELS)) # @check(admin_or_permissions(Permissions.MANAGE_CHANNELS))
# async def _lock( # async def _lock(
# self, # self,
# ctx: InteractionContext, # ctx: InteractionContext,
# reason: str, # reason: str,
# duration: int = 10, # duration: int = 10,
# channel: Union[GuildText, GuildVoice] = None, # channel: Union[GuildText, GuildVoice] = None,
# ) -> None: # ) -> None:
# await ctx.defer(ephemeral=True) # await ctx.defer(ephemeral=True)
# if duration <= 0: # if duration <= 0:
# await ctx.send("Duration must be > 0", ephemeral=True) # await ctx.send("Duration must be > 0", ephemeral=True)
# return # return
# #
# elif duration > 60 * 12: # elif duration > 60 * 12:
# await ctx.send("Duration must be <= 12 hours", ephemeral=True) # await ctx.send("Duration must be <= 12 hours", ephemeral=True)
# return # return
# #
# if len(reason) > 100: # if len(reason) > 100:
# await ctx.send("Reason must be <= 100 characters", ephemeral=True) # await ctx.send("Reason must be <= 100 characters", ephemeral=True)
# return # return
# if not channel: # if not channel:
# channel = ctx.channel # channel = ctx.channel
# #
# # role = ctx.guild.default_role # Uncomment once implemented # # role = ctx.guild.default_role # Uncomment once implemented
# if isinstance(channel, GuildText): # if isinstance(channel, GuildText):
# to_deny = Permissions.SEND_MESSAGES # to_deny = Permissions.SEND_MESSAGES
# elif isinstance(channel, GuildVoice): # elif isinstance(channel, GuildVoice):
# to_deny = Permissions.CONNECT | Permissions.SPEAK # to_deny = Permissions.CONNECT | Permissions.SPEAK
# #
# overwrite = PermissionOverwrite(type=PermissionTypes.ROLE, deny=to_deny) # overwrite = PermissionOverwrite(type=PermissionTypes.ROLE, deny=to_deny)
# # TODO: Get original permissions # # TODO: Get original permissions
# # TODO: Apply overwrite # # TODO: Apply overwrite
# overwrite = overwrite # overwrite = overwrite
# _ = Lock( # _ = Lock(
# channel=channel.id, # channel=channel.id,
# guild=ctx.guild.id, # guild=ctx.guild.id,
# admin=ctx.author.id, # admin=ctx.author.id,
# reason=reason, # reason=reason,
# duration=duration, # duration=duration,
# ) # .save() # Uncomment once implemented # ) # .save() # Uncomment once implemented
# # await ctx.send(f"{channel.mention} locked for {duration} minute(s)") # # await ctx.send(f"{channel.mention} locked for {duration} minute(s)")
# await ctx.send("Unfortunately, this is not yet implemented", hidden=True) # await ctx.send("Unfortunately, this is not yet implemented", hidden=True)
# #
# @cog_ext.cog_slash( # @cog_ext.cog_slash(
# name="unlock", # name="unlock",
# description="Unlocks a channel", # description="Unlocks a channel",
# choices=[ # choices=[
# create_option( # create_option(
# name="channel", # name="channel",
# description="Channel to lock", # description="Channel to lock",
# opt_type=7, # opt_type=7,
# required=False, # required=False,
# ), # ),
# ], # ],
# ) # )
# @check(admin_or_permissions(Permissions.MANAGE_CHANNELS)) # @check(admin_or_permissions(Permissions.MANAGE_CHANNELS))
# async def _unlock( # async def _unlock(
# self, # self,
# ctx: InteractionContext, # ctx: InteractionContext,
# channel: Union[GuildText, GuildVoice] = None, # channel: Union[GuildText, GuildVoice] = None,
# ) -> None: # ) -> None:
# if not channel: # if not channel:
# channel = ctx.channel # channel = ctx.channel
# lock = Lock.objects(guild=ctx.guild.id, channel=channel.id, active=True).first() # lock = Lock.objects(guild=ctx.guild.id, channel=channel.id, active=True).first()
# if not lock: # if not lock:
# await ctx.send(f"{channel.mention} not locked.", ephemeral=True) # await ctx.send(f"{channel.mention} not locked.", ephemeral=True)
# return # return
# for role in ctx.guild.roles: # for role in ctx.guild.roles:
# with suppress(Exception): # with suppress(Exception):
# await self._unlock_channel(channel, role, ctx.author) # await self._unlock_channel(channel, role, ctx.author)
# lock.active = False # lock.active = False
# lock.save() # lock.save()
# await ctx.send(f"{channel.mention} unlocked") # await ctx.send(f"{channel.mention} unlocked")

View file

@ -1,122 +1,122 @@
"""J.A.R.V.I.S. MuteCog.""" """J.A.R.V.I.S. MuteCog."""
from datetime import datetime # from datetime import datetime
#
from dis_snek import InteractionContext, Permissions, Scale, Snake # from dis_snek import InteractionContext, Permissions, Scale, Snake
from dis_snek.models.discord.embed import EmbedField # from dis_snek.models.discord.embed import EmbedField
from dis_snek.models.discord.user import Member # from dis_snek.models.discord.user import Member
from dis_snek.models.snek.application_commands import ( # from dis_snek.models.snek.application_commands import (
OptionTypes, # OptionTypes,
SlashCommandChoice, # SlashCommandChoice,
slash_command, # slash_command,
slash_option, # slash_option,
) # )
from dis_snek.models.snek.command import check # from dis_snek.models.snek.command import check
#
from jarvis.db.models import Mute # from jarvis.db.models import Mute
from jarvis.utils import build_embed # from jarvis.utils import build_embed
from jarvis.utils.permissions import admin_or_permissions # from jarvis.utils.permissions import admin_or_permissions
#
#
class MuteCog(Scale): # class MuteCog(Scale):
"""J.A.R.V.I.S. MuteCog.""" # """J.A.R.V.I.S. MuteCog."""
#
def __init__(self, bot: Snake): # def __init__(self, bot: Snake):
self.bot = bot # self.bot = bot
#
@slash_command(name="mute", description="Mute a user") # @slash_command(name="mute", description="Mute a user")
@slash_option(name="user", description="User to mute", opt_type=OptionTypes.USER, required=True) # @slash_option(name="user", description="User to mute", opt_type=OptionTypes.USER, required=True)
@slash_option( # @slash_option(
name="reason", # name="reason",
description="Reason for mute", # description="Reason for mute",
opt_type=OptionTypes.STRING, # opt_type=OptionTypes.STRING,
required=True, # required=True,
) # )
@slash_option( # @slash_option(
name="time", # name="time",
description="Duration of mute, default 1", # description="Duration of mute, default 1",
opt_type=OptionTypes.INTEGER, # opt_type=OptionTypes.INTEGER,
required=False, # required=False,
) # )
@slash_option( # @slash_option(
name="scale", # name="scale",
description="Time scale, default Hour(s)", # description="Time scale, default Hour(s)",
opt_type=OptionTypes.INTEGER, # opt_type=OptionTypes.INTEGER,
required=False, # required=False,
choices=[ # choices=[
SlashCommandChoice(name="Minute(s)", value=1), # SlashCommandChoice(name="Minute(s)", value=1),
SlashCommandChoice(name="Hour(s)", value=60), # SlashCommandChoice(name="Hour(s)", value=60),
SlashCommandChoice(name="Day(s)", value=3600), # SlashCommandChoice(name="Day(s)", value=3600),
SlashCommandChoice(name="Week(s)", value=604800), # SlashCommandChoice(name="Week(s)", value=604800),
], # ],
) # )
@check( # @check(
admin_or_permissions( # admin_or_permissions(
Permissions.MUTE_MEMBERS, Permissions.BAN_MEMBERS, Permissions.KICK_MEMBERS # Permissions.MUTE_MEMBERS, Permissions.BAN_MEMBERS, Permissions.KICK_MEMBERS
) # )
) # )
async def _timeout( # async def _timeout(
self, ctx: InteractionContext, user: Member, reason: str, time: int = 1, scale: int = 60 # self, ctx: InteractionContext, user: Member, reason: str, time: int = 1, scale: int = 60
) -> None: # ) -> None:
if user == ctx.author: # if user == ctx.author:
await ctx.send("You cannot mute yourself.", ephemeral=True) # await ctx.send("You cannot mute yourself.", ephemeral=True)
return # return
if user == self.bot.user: # if user == self.bot.user:
await ctx.send("I'm afraid I can't let you do that", ephemeral=True) # await ctx.send("I'm afraid I can't let you do that", ephemeral=True)
return # return
if len(reason) > 100: # if len(reason) > 100:
await ctx.send("Reason must be < 100 characters", ephemeral=True) # await ctx.send("Reason must be < 100 characters", ephemeral=True)
return # return
#
# Max 4 weeks (2419200 seconds) per API # # Max 4 weeks (2419200 seconds) per API
duration = time * scale # duration = time * scale
if duration > 2419200: # if duration > 2419200:
await ctx.send("Mute must be less than 4 weeks (2419200 seconds)", ephemeral=True) # await ctx.send("Mute must be less than 4 weeks (2419200 seconds)", ephemeral=True)
return # return
#
await user.timeout(communication_disabled_until=duration, reason=reason) # await user.timeout(communication_disabled_until=duration, reason=reason)
m = Mute( # m = Mute(
user=user.id, # user=user.id,
reason=reason, # reason=reason,
admin=ctx.author.id, # admin=ctx.author.id,
guild=ctx.guild.id, # guild=ctx.guild.id,
duration=duration, # duration=duration,
active=True, # active=True,
) # )
await m.commit() # await m.commit()
#
embed = build_embed( # embed = build_embed(
title="User Muted", # title="User Muted",
description=f"{user.mention} has been muted", # description=f"{user.mention} has been muted",
fields=[EmbedField(name="Reason", value=reason)], # fields=[EmbedField(name="Reason", value=reason)],
) # )
embed.set_author(name=user.display_name, icon_url=user.display_avatar.url) # embed.set_author(name=user.display_name, icon_url=user.display_avatar.url)
embed.set_thumbnail(url=user.display_avatar.url) # embed.set_thumbnail(url=user.display_avatar.url)
embed.set_footer(text=f"{user.username}#{user.discriminator} | {user.id}") # embed.set_footer(text=f"{user.username}#{user.discriminator} | {user.id}")
await ctx.send(embed=embed) # await ctx.send(embed=embed)
#
@slash_command(name="unmute", description="Unmute a user") # @slash_command(name="unmute", description="Unmute a user")
@slash_option( # @slash_option(
name="user", description="User to unmute", opt_type=OptionTypes.USER, required=True # name="user", description="User to unmute", opt_type=OptionTypes.USER, required=True
) # )
@check( # @check(
admin_or_permissions( # admin_or_permissions(
Permissions.MUTE_MEMBERS, Permissions.BAN_MEMBERS, Permissions.KICK_MEMBERS # Permissions.MUTE_MEMBERS, Permissions.BAN_MEMBERS, Permissions.KICK_MEMBERS
) # )
) # )
async def _unmute(self, ctx: InteractionContext, user: Member) -> None: # async def _unmute(self, ctx: InteractionContext, user: Member) -> None:
if ( # if (
not user.communication_disabled_until # not user.communication_disabled_until
or user.communication_disabled_until < datetime.now() # noqa: W503 # or user.communication_disabled_until < datetime.now() # noqa: W503
): # ):
await ctx.send("User is not muted", ephemeral=True) # await ctx.send("User is not muted", ephemeral=True)
return # return
#
embed = build_embed( # embed = build_embed(
title="User Unmuted", # title="User Unmuted",
description=f"{user.mention} has been unmuted", # description=f"{user.mention} has been unmuted",
fields=[], # fields=[],
) # )
embed.set_author(name=user.display_name, icon_url=user.display_avatar.url) # embed.set_author(name=user.display_name, icon_url=user.display_avatar.url)
embed.set_thumbnail(url=user.display_avatar.url) # embed.set_thumbnail(url=user.display_avatar.url)
embed.set_footer(text=f"{user.username}#{user.discriminator} | {user.id}") # embed.set_footer(text=f"{user.username}#{user.discriminator} | {user.id}")
await ctx.send(embed=embed) # await ctx.send(embed=embed)