From a9e6185b7ec51a3e58277a6312d38bf6f915c0df Mon Sep 17 00:00:00 2001 From: Zevaryx Date: Mon, 2 Aug 2021 18:23:23 -0600 Subject: [PATCH] Settings management re-write --- jarvis/__init__.py | 2 +- jarvis/cogs/settings.py | 137 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 131 insertions(+), 8 deletions(-) diff --git a/jarvis/__init__.py b/jarvis/__init__.py index dd00c20..185f94d 100644 --- a/jarvis/__init__.py +++ b/jarvis/__init__.py @@ -25,7 +25,7 @@ jarvis = commands.Bot( ) slash = SlashCommand(jarvis, sync_commands=True, sync_on_cog_reload=True) jarvis_self = Process() -__version__ = "1.9.2" +__version__ = "1.9.3" jconfig = get_config() db = DBManager(jconfig.mongo["connect"]).mongo diff --git a/jarvis/cogs/settings.py b/jarvis/cogs/settings.py index 814b101..cce2208 100644 --- a/jarvis/cogs/settings.py +++ b/jarvis/cogs/settings.py @@ -1,9 +1,12 @@ from discord import Role, TextChannel from discord.ext import commands -from discord_slash import cog_ext +from discord.utils import find +from discord_slash import SlashContext, cog_ext from discord_slash.utils.manage_commands import create_option from jarvis.db.types import Setting +from jarvis.utils import build_embed +from jarvis.utils.field import Field from jarvis.utils.permissions import admin_or_permissions @@ -17,8 +20,14 @@ class SettingsCog(commands.Cog): return updated is not None + def delete_settings(self, setting, guild): + return Setting.delete_many(setting=setting, guild=guild) + @cog_ext.cog_subcommand( base="settings", + base_desc="Settings management", + subcommand_group="set", + subcommand_group_description="Set a setting", name="mute", description="Set mute role", options=[ @@ -31,13 +40,14 @@ class SettingsCog(commands.Cog): ], ) @admin_or_permissions(manage_guild=True) - async def _mute(self, ctx, role: Role): + async def _set_mute(self, ctx, role: Role): await ctx.defer() self.update_settings("mute", role.id, ctx.guild.id) await ctx.send(f"Settings applied. New mute role is `{role.name}`") @cog_ext.cog_subcommand( base="settings", + subcommand_group="set", name="modlog", description="Set modlog channel", options=[ @@ -50,7 +60,7 @@ class SettingsCog(commands.Cog): ], ) @admin_or_permissions(manage_guild=True) - async def _modlog(self, ctx, channel: TextChannel): + async def _set_modlog(self, ctx, channel: TextChannel): if not isinstance(channel, TextChannel): await ctx.send("Channel must be a TextChannel", hidden=True) return @@ -61,6 +71,7 @@ class SettingsCog(commands.Cog): @cog_ext.cog_subcommand( base="settings", + subcommand_group="set", name="userlog", description="Set userlog channel", options=[ @@ -73,7 +84,7 @@ class SettingsCog(commands.Cog): ], ) @admin_or_permissions(manage_guild=True) - async def _userlog(self, ctx, channel: TextChannel): + async def _set_userlog(self, ctx, channel: TextChannel): if not isinstance(channel, TextChannel): await ctx.send("Channel must be a TextChannel", hidden=True) return @@ -84,6 +95,7 @@ class SettingsCog(commands.Cog): @cog_ext.cog_subcommand( base="settings", + subcommand_group="set", name="massmention", description="Set massmention amount", options=[ @@ -96,13 +108,14 @@ class SettingsCog(commands.Cog): ], ) @admin_or_permissions(manage_guild=True) - async def _massmention(self, ctx, amount: int): + async def _set_massmention(self, ctx, amount: int): await ctx.defer() self.update_settings("massmention", amount, ctx.guild.id) await ctx.send(f"Settings applied. New massmention limit is {amount}") @cog_ext.cog_subcommand( base="settings", + subcommand_group="set", name="verified", description="Set verified role", options=[ @@ -115,13 +128,14 @@ class SettingsCog(commands.Cog): ], ) @admin_or_permissions(manage_guild=True) - async def _verified(self, ctx, role: Role): + async def _set_verified(self, ctx, role: Role): await ctx.defer() self.update_settings("verified", role.id, ctx.guild.id) await ctx.send(f"Settings applied. New verified role is `{role.name}`") @cog_ext.cog_subcommand( base="settings", + subcommand_group="set", name="unverified", description="Set unverified role", options=[ @@ -134,13 +148,122 @@ class SettingsCog(commands.Cog): ], ) @admin_or_permissions(manage_guild=True) - async def _unverified(self, ctx, role: Role): + async def _set_unverified(self, ctx, role: Role): await ctx.defer() self.update_settings("unverified", role.id, ctx.guild.id) await ctx.send( f"Settings applied. New unverified role is `{role.name}`" ) + @cog_ext.cog_subcommand( + base="settings", + subcommand_group="unset", + subcommand_group_description="Unset a setting", + name="mute", + description="Unset mute role", + ) + @admin_or_permissions(manage_guild=True) + async def _unset_mute(self, ctx): + await ctx.defer() + self.delete_settings("mute", ctx.guild.id) + await ctx.send("Setting removed.") + + @cog_ext.cog_subcommand( + base="settings", + subcommand_group="unset", + name="modlog", + description="Unset modlog channel", + ) + @admin_or_permissions(manage_guild=True) + async def _unset_modlog(self, ctx): + self.delete_settings("modlog", ctx.guild.id) + await ctx.send("Setting removed.") + + @cog_ext.cog_subcommand( + base="settings", + subcommand_group="unset", + name="userlog", + description="Unset userlog channel", + ) + @admin_or_permissions(manage_guild=True) + async def _unset_userlog(self, ctx): + self.delete_settings("userlog", ctx.guild.id) + await ctx.send("Setting removed.") + + @cog_ext.cog_subcommand( + base="settings", + subcommand_group="unset", + name="massmention", + description="Unet massmention amount", + ) + @admin_or_permissions(manage_guild=True) + async def _massmention(self, ctx): + await ctx.defer() + self.delete_settings("massmention", ctx.guild.id) + await ctx.send("Setting removed.") + + @cog_ext.cog_subcommand( + base="settings", + subcommand_group="unset", + name="verified", + description="Unset verified role", + ) + @admin_or_permissions(manage_guild=True) + async def _verified(self, ctx): + await ctx.defer() + self.delete_settings("verified", ctx.guild.id) + await ctx.send("Setting removed.") + + @cog_ext.cog_subcommand( + base="settings", + subcommand_group="unset", + name="unverified", + description="Unset unverified role", + ) + @admin_or_permissions(manage_guild=True) + async def _unverified(self, ctx): + await ctx.defer() + self.delete_settings("unverified", ctx.guild.id) + await ctx.send("Setting removed.") + + @cog_ext.cog_subcommand( + base="settings", name="view", description="View settings" + ) + @admin_or_permissions(manage_guild=True) + async def _view(self, ctx: SlashContext): + settings = Setting.get_many(guild=ctx.guild.id) + + fields = [] + for setting in settings: + value = setting.value + if setting.setting in ["unverified", "verified", "mute"]: + value = find(lambda x: x.id == value, ctx.guild.roles) + if value: + value = value.mention + else: + value = "||`[redacted]`||" + elif setting.setting in ["userlog", "modlog"]: + value = find(lambda x: x.id == value, ctx.guild.text_channels) + if value: + value = value.mention + else: + value = "||`[redacted]`||" + fields.append(Field(name=setting.setting, value=value)) + + embed = build_embed( + title="Current Settings", description="", fields=fields + ) + + await ctx.send(embed=embed) + + @cog_ext.cog_subcommand( + base="settings", name="clear", description="Clear all settings" + ) + @admin_or_permissions(manage_guild=True) + async def _clear(self, ctx: SlashContext): + deleted = Setting.delete_many(guild=ctx.guild.id) + await ctx.send(f"Guild settings cleared: `{deleted}`") + def setup(bot): bot.add_cog(SettingsCog(bot))