nomen/nomen/settings.py

145 lines
4.2 KiB
Python

import logging
from typing import Literal
import disnake
from disnake.ext.commands import Cog, group, guild_only
from .utils import confirm
log = logging.getLogger("nomen.settings")
log.setLevel(logging.DEBUG)
class Settings(Cog):
def __init__(self, bot):
self.bot = bot
@group(invoke_without_command=True)
async def nomen(self, ctx):
"""
Settings for Nomen
"""
await ctx.send_help(self.nomen)
@nomen.command()
async def purge(self, ctx):
"""
Deletes all user data stored in Nomen
"""
msg = f"""\
Are you sure you wish to delete **ALL** data across **ALL** servers? (y/N)
NOTE: If you have currently opted-out, this will opt you back in.
You may want to `{ctx.clean_prefix}nomen export` first"""
to_purge = await confirm(ctx, msg)
if to_purge:
# Foreign key constraints will automatically cascade to delete dependent data
await self.bot.db.execute("DELETE FROM users WHERE user_id=?", (ctx.author.id,))
await self.bot.db.commit()
await ctx.send("Deleted all user data.")
else:
await ctx.send("Cancelled.")
@nomen.command(name="import")
@guild_only()
async def _import(self, ctx):
"""
Imports a JSON of all of your user data
"""
schema = {
"disabled": bool,
"keywords": {
"<guild_id>": [
{"keyword": str, "regex": bool, "count": int},
],
},
"ignores": {
"<guild_id>": [int],
},
"blocks": [
int # User ID
],
}
@nomen.command()
@guild_only()
async def export(self, ctx):
"""
Exports a JSON of all of your user data
"""
pass
@nomen.command(name="opt-out")
async def opt_out(self, ctx):
"""
Opt-out of Nomen processing your messages entirely
You will not trigger anyone else's notifications, and you will not receive any notifications
"""
log.debug(f"Opting-out: {ctx.author} ({ctx.author.id})")
await ctx.send(
f"You have now opted-out and will no longer trigger or receive notifications. To opt back in, run `{ctx.clean_prefix}nomen opt-in`"
)
await self.bot.db.execute("REPLACE INTO users (user_id, disabled) VALUES(?, 1)", (ctx.author.id,))
await self.bot.db.commit()
@nomen.command(name="opt-in")
async def opt_in(self, ctx):
"""
Opt-in to Nomen processing your messages
"""
log.debug(f"Opting-in: {ctx.author} ({ctx.author.id})")
await ctx.send(
f"You have opted back in and will now trigger and receive notifications. To opt out, run `{ctx.clean_prefix}nomen opt-out`"
)
await self.bot.db.execute("REPLACE INTO users (user_id, disabled) VALUES(?, 0)", (ctx.author.id,))
await self.bot.db.commit()
@group(invoke_without_command=True)
async def settings(
self,
ctx,
setting: Literal["use-embed", "notify-self", "bots-notify"],
value: bool,
):
"""
Change settings for Nomen
Valid settings are:
- `use-embed`
- `notify-self`
- `bots-notify`
"""
setting_db = setting.replace("-", "_")
await ctx.bot.set_setting(ctx.author.id, setting_db, value)
await self.settings_list(ctx)
@settings.command(name="list")
async def settings_list(self, ctx):
"""
Print all of your current settings
"""
settings = await ctx.bot.db.execute_fetchall(
"SELECT use_embed, notify_self, bots_notify FROM users WHERE user_id=?", (ctx.author.id,)
)
settings = ["Yes" if v else "No" for v in settings[0]]
embed = disnake.Embed(title="Current Settings")
embed.add_field("Use Embeds in Notifications (`use-embed`)", settings[0], inline=False)
embed.add_field("Notify Self (`notify-self`)", settings[1], inline=False)
embed.add_field("Notifications from Bots (`bots-notify`)", settings[2], inline=False)
await ctx.send(embed=embed)