From 19dab96ef4825fce6d723b04a0768c0fc5bcc390 Mon Sep 17 00:00:00 2001 From: Sabi <120003982+cyanogus@users.noreply.github.com> Date: Sat, 1 Jul 2023 04:44:53 +0000 Subject: [PATCH 1/2] Add framework module to modify user settings --- config/settings.json | 1 + framework/isobot/settings.py | 53 ++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 config/settings.json create mode 100644 framework/isobot/settings.py diff --git a/config/settings.json b/config/settings.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/config/settings.json @@ -0,0 +1 @@ +{} diff --git a/framework/isobot/settings.py b/framework/isobot/settings.py new file mode 100644 index 00000000..6172d27a --- /dev/null +++ b/framework/isobot/settings.py @@ -0,0 +1,53 @@ +"""The isobot module to configure internal user/server settings.""" + +# Imports +import json +from typing import Union, Literal + +# Classes and Functions +class Colors: + """Contains general stdout colors.""" + cyan = '\033[96m' + red = '\033[91m' + green = '\033[92m' + end = '\033[0m' + +# Functions +class Configurator(Colors): + """The class used to initialize the settings module.""" + def __init__(self): + print(f"[framework/Configurator] {Colors.green}Module initialized.{Colors.end}") + + def generate(self, user_id: int) -> Literal[0, 1]: + """Generates a new settings configuration for the specified user. + Does not do anything if a configuration already exists.\n + Returns 0 if the request was successful, returns 1 if the configuration already exists.""" + with open("config/settings.json", 'r', encoding="utf-8") as f: db = json.load(f) + if str(user_id) in db.keys(): return 1 + template = { + "levelup_messages": True + } + db[str(user_id)] = template + with open("config/settings.json", 'w+', encoding="utf-8") as f: json.dump(db, f, indent=4) + return 0 + + def fetch_setting(self, user_id: int, setting: str) -> Union[int, str, bool]: + """Fetches the current value of a user setting.""" + with open("config/settings.json", 'r', encoding="utf-8") as f: db = json.load(f) + return db[str(user_id)][setting] + + def edit_setting(self, user_id: int, setting: str, value) -> Literal[0]: + """Modifies the value of a user setting.""" + with open("config/settings.json", 'r', encoding="utf-8") as f: db = json.load(f) + db[str(user_id)][setting] = value + return 0 + def reset(self, user_id: int): + """Completely resets the specified user's configuration.""" + with open("config/settings.json", 'r', encoding="utf-8") as f: db = json.load(f) + template = { + "levelup_messages": True + } + db[str(user_id)] = template + with open("config/settings.json", 'w+', encoding="utf-8") as f: json.dump(db, f, indent=4) + return 0 + \ No newline at end of file From 24a6549dcf84d9664272695bdd9fb9154c39537c Mon Sep 17 00:00:00 2001 From: Sabi <120003982+cyanogus@users.noreply.github.com> Date: Sat, 1 Jul 2023 04:54:36 +0000 Subject: [PATCH 2/2] Add command to let users change settings for level up messages --- main.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 9ab4dd68..c6445241 100644 --- a/main.py +++ b/main.py @@ -10,7 +10,7 @@ from utils import logger, ping from math import floor from random import randint -from framework.isobot import currency, colors, embedengine +from framework.isobot import currency, colors, embedengine, settings from framework.isobank import authorize, manager from discord import ApplicationContext, option from discord.ext import commands @@ -57,6 +57,7 @@ def save(): #Framework Module Loader colors = colors.Colors() currency = currency.CurrencyAPI("database/currency.json", "logs/currency.log") +settings = settings.Configurator() # Theme Loader themes = False # True: enables themes; False: disables themes; @@ -95,6 +96,7 @@ async def on_message(ctx): currency.new_bank(ctx.author.id) create_isocoin_key(ctx.author.id) new_userdat(ctx.author.id) + settings.generate(ctx.author.id) if str(ctx.author.id) not in items: items[str(ctx.author.id)] = {} if str(ctx.author.id) not in levels: levels[str(ctx.author.id)] = {"xp": 0, "level": 0} if str(ctx.guild.id) not in automod_config: @@ -135,7 +137,8 @@ async def on_message(ctx): if levels[str(ctx.author.id)]["xp"] >= xpreq: levels[str(ctx.author.id)]["xp"] = 0 levels[str(ctx.author.id)]["level"] += 1 - await ctx.author.send(f"{ctx.author.mention}, you just ranked up to **level {levels[str(ctx.author.id)]['level']}**. Nice!") + if settings.fetch_setting(ctx.author.id, "levelup_messages") == True: + await ctx.author.send(f"{ctx.author.mention}, you just ranked up to **level {levels[str(ctx.author.id)]['level']}**. Nice!") save() if automod_config[str(ctx.guild.id)]["swear_filter"]["enabled"] == True: if automod_config[str(ctx.guild.id)]["swear_filter"]["keywords"]["use_default"] and any(x in ctx.content.lower() for x in automod_config[str(ctx.guild.id)]["swear_filter"]["keywords"]["default"]): @@ -256,6 +259,23 @@ async def reload(ctx: ApplicationContext, cog: str): ) ) +# Settings commands +config = client.create_group("settings", "Commands used to change bot settings.") + +@config.command( + name="levelup_messages", + description="Configure whether you want to be notified for level ups or not." +) +@option(name="enabled", description="Do you want this setting enabled?", type=bool) +async def levelup_messages(ctx: ApplicationContext, enabled: bool): + if settings.fetch_setting(ctx.author.id, "levelup_messages") == enabled: return await ctx.respond("This is already done.", ephemeral=True) + settings.edit_setting(ctx.author.id, "levelup_messages", enabled) + localembed = discord.Embed( + description="Setting successfully updated.", + color=discord.Color.green() + ) + await ctx.respond(embed=localembed) + # Initialization active_cogs = [ "economy",