Settings management re-write
This commit is contained in:
parent
fe913f0a3b
commit
a9e6185b7e
2 changed files with 131 additions and 8 deletions
|
@ -25,7 +25,7 @@ jarvis = commands.Bot(
|
||||||
)
|
)
|
||||||
slash = SlashCommand(jarvis, sync_commands=True, sync_on_cog_reload=True)
|
slash = SlashCommand(jarvis, sync_commands=True, sync_on_cog_reload=True)
|
||||||
jarvis_self = Process()
|
jarvis_self = Process()
|
||||||
__version__ = "1.9.2"
|
__version__ = "1.9.3"
|
||||||
|
|
||||||
jconfig = get_config()
|
jconfig = get_config()
|
||||||
db = DBManager(jconfig.mongo["connect"]).mongo
|
db = DBManager(jconfig.mongo["connect"]).mongo
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
from discord import Role, TextChannel
|
from discord import Role, TextChannel
|
||||||
from discord.ext import commands
|
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 discord_slash.utils.manage_commands import create_option
|
||||||
|
|
||||||
from jarvis.db.types import Setting
|
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
|
from jarvis.utils.permissions import admin_or_permissions
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,8 +20,14 @@ class SettingsCog(commands.Cog):
|
||||||
|
|
||||||
return updated is not None
|
return updated is not None
|
||||||
|
|
||||||
|
def delete_settings(self, setting, guild):
|
||||||
|
return Setting.delete_many(setting=setting, guild=guild)
|
||||||
|
|
||||||
@cog_ext.cog_subcommand(
|
@cog_ext.cog_subcommand(
|
||||||
base="settings",
|
base="settings",
|
||||||
|
base_desc="Settings management",
|
||||||
|
subcommand_group="set",
|
||||||
|
subcommand_group_description="Set a setting",
|
||||||
name="mute",
|
name="mute",
|
||||||
description="Set mute role",
|
description="Set mute role",
|
||||||
options=[
|
options=[
|
||||||
|
@ -31,13 +40,14 @@ class SettingsCog(commands.Cog):
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@admin_or_permissions(manage_guild=True)
|
@admin_or_permissions(manage_guild=True)
|
||||||
async def _mute(self, ctx, role: Role):
|
async def _set_mute(self, ctx, role: Role):
|
||||||
await ctx.defer()
|
await ctx.defer()
|
||||||
self.update_settings("mute", role.id, ctx.guild.id)
|
self.update_settings("mute", role.id, ctx.guild.id)
|
||||||
await ctx.send(f"Settings applied. New mute role is `{role.name}`")
|
await ctx.send(f"Settings applied. New mute role is `{role.name}`")
|
||||||
|
|
||||||
@cog_ext.cog_subcommand(
|
@cog_ext.cog_subcommand(
|
||||||
base="settings",
|
base="settings",
|
||||||
|
subcommand_group="set",
|
||||||
name="modlog",
|
name="modlog",
|
||||||
description="Set modlog channel",
|
description="Set modlog channel",
|
||||||
options=[
|
options=[
|
||||||
|
@ -50,7 +60,7 @@ class SettingsCog(commands.Cog):
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@admin_or_permissions(manage_guild=True)
|
@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):
|
if not isinstance(channel, TextChannel):
|
||||||
await ctx.send("Channel must be a TextChannel", hidden=True)
|
await ctx.send("Channel must be a TextChannel", hidden=True)
|
||||||
return
|
return
|
||||||
|
@ -61,6 +71,7 @@ class SettingsCog(commands.Cog):
|
||||||
|
|
||||||
@cog_ext.cog_subcommand(
|
@cog_ext.cog_subcommand(
|
||||||
base="settings",
|
base="settings",
|
||||||
|
subcommand_group="set",
|
||||||
name="userlog",
|
name="userlog",
|
||||||
description="Set userlog channel",
|
description="Set userlog channel",
|
||||||
options=[
|
options=[
|
||||||
|
@ -73,7 +84,7 @@ class SettingsCog(commands.Cog):
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@admin_or_permissions(manage_guild=True)
|
@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):
|
if not isinstance(channel, TextChannel):
|
||||||
await ctx.send("Channel must be a TextChannel", hidden=True)
|
await ctx.send("Channel must be a TextChannel", hidden=True)
|
||||||
return
|
return
|
||||||
|
@ -84,6 +95,7 @@ class SettingsCog(commands.Cog):
|
||||||
|
|
||||||
@cog_ext.cog_subcommand(
|
@cog_ext.cog_subcommand(
|
||||||
base="settings",
|
base="settings",
|
||||||
|
subcommand_group="set",
|
||||||
name="massmention",
|
name="massmention",
|
||||||
description="Set massmention amount",
|
description="Set massmention amount",
|
||||||
options=[
|
options=[
|
||||||
|
@ -96,13 +108,14 @@ class SettingsCog(commands.Cog):
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@admin_or_permissions(manage_guild=True)
|
@admin_or_permissions(manage_guild=True)
|
||||||
async def _massmention(self, ctx, amount: int):
|
async def _set_massmention(self, ctx, amount: int):
|
||||||
await ctx.defer()
|
await ctx.defer()
|
||||||
self.update_settings("massmention", amount, ctx.guild.id)
|
self.update_settings("massmention", amount, ctx.guild.id)
|
||||||
await ctx.send(f"Settings applied. New massmention limit is {amount}")
|
await ctx.send(f"Settings applied. New massmention limit is {amount}")
|
||||||
|
|
||||||
@cog_ext.cog_subcommand(
|
@cog_ext.cog_subcommand(
|
||||||
base="settings",
|
base="settings",
|
||||||
|
subcommand_group="set",
|
||||||
name="verified",
|
name="verified",
|
||||||
description="Set verified role",
|
description="Set verified role",
|
||||||
options=[
|
options=[
|
||||||
|
@ -115,13 +128,14 @@ class SettingsCog(commands.Cog):
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@admin_or_permissions(manage_guild=True)
|
@admin_or_permissions(manage_guild=True)
|
||||||
async def _verified(self, ctx, role: Role):
|
async def _set_verified(self, ctx, role: Role):
|
||||||
await ctx.defer()
|
await ctx.defer()
|
||||||
self.update_settings("verified", role.id, ctx.guild.id)
|
self.update_settings("verified", role.id, ctx.guild.id)
|
||||||
await ctx.send(f"Settings applied. New verified role is `{role.name}`")
|
await ctx.send(f"Settings applied. New verified role is `{role.name}`")
|
||||||
|
|
||||||
@cog_ext.cog_subcommand(
|
@cog_ext.cog_subcommand(
|
||||||
base="settings",
|
base="settings",
|
||||||
|
subcommand_group="set",
|
||||||
name="unverified",
|
name="unverified",
|
||||||
description="Set unverified role",
|
description="Set unverified role",
|
||||||
options=[
|
options=[
|
||||||
|
@ -134,13 +148,122 @@ class SettingsCog(commands.Cog):
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@admin_or_permissions(manage_guild=True)
|
@admin_or_permissions(manage_guild=True)
|
||||||
async def _unverified(self, ctx, role: Role):
|
async def _set_unverified(self, ctx, role: Role):
|
||||||
await ctx.defer()
|
await ctx.defer()
|
||||||
self.update_settings("unverified", role.id, ctx.guild.id)
|
self.update_settings("unverified", role.id, ctx.guild.id)
|
||||||
await ctx.send(
|
await ctx.send(
|
||||||
f"Settings applied. New unverified role is `{role.name}`"
|
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):
|
def setup(bot):
|
||||||
bot.add_cog(SettingsCog(bot))
|
bot.add_cog(SettingsCog(bot))
|
||||||
|
|
Loading…
Add table
Reference in a new issue