From c891457121e39e76d65265ebe380d916e1aea804 Mon Sep 17 00:00:00 2001 From: WhatDidYouExpect Date: Sat, 26 Apr 2025 15:03:16 +0200 Subject: [PATCH] add modules (not community cogs) n fix resource checking --- bot.py | 13 +++++++++++++ locales/en.json | 2 ++ locales/it.json | 2 ++ modules/grabtemplate.py | 42 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 modules/grabtemplate.py diff --git a/bot.py b/bot.py index 4a946f1..a2c48be 100644 --- a/bot.py +++ b/bot.py @@ -39,6 +39,7 @@ def check_resources(): print(f"{resource} is not installed. Downloading now...") download(resource) +check_resources() from nltk.sentiment.vader import SentimentIntensityAnalyzer from nltk.tokenize import word_tokenize @@ -179,6 +180,17 @@ async def load_cogs_from_folder(bot, folder_name="cogs"): print(f"{RED}{get_translation(LOCALE, 'cog_fail')} {cog_name} {e}{RESET}") traceback.print_exc() +async def load_modules(bot, folder_name="modules"): + for filename in os.listdir(folder_name): + if filename.endswith(".py") and not filename.startswith("_"): + cog_name = filename[:-3] + try: + await bot.load_extension(f"{folder_name}.{cog_name}") + print(f"{GREEN}{get_translation(LOCALE, 'loaded_cog2')} {cog_name}{RESET}") + except Exception as e: + print(f"{RED}{get_translation(LOCALE, 'cog_fail2')} {cog_name} {e}{RESET}") + traceback.print_exc() + currenthash = "" def generate_sha256_of_current_file(): global currenthash @@ -312,6 +324,7 @@ async def on_ready(): else: print(f"{DEBUG}{get_translation(LOCALE, 'folder_exists').format(folder_name=folder_name)}{RESET}") markov_model = train_markov_model(memory) + await load_modules(bot) await load_cogs_from_folder(bot) global slash_commands_enabled print(f"{GREEN}{get_translation(LOCALE, 'logged_in')} {bot.user}{RESET}") diff --git a/locales/en.json b/locales/en.json index d7e7961..57d4208 100644 --- a/locales/en.json +++ b/locales/en.json @@ -5,7 +5,9 @@ "not_found": "is not found!", "version_error": "Unable to fetch version info. Status code", "loaded_cog": "Loaded cog:", + "loaded_cog2": "Loaded module:", "cog_fail": "Failed to load cog:", + "cog_fail2": "Failed to load module:", "no_model": "No saved Markov model found. Starting from scratch.", "folder_created": "Folder '{folder_name}' created.", "folder_exists": "Folder '{folder_name}' already exists. skipping...", diff --git a/locales/it.json b/locales/it.json index 703625b..36c2f69 100644 --- a/locales/it.json +++ b/locales/it.json @@ -6,6 +6,8 @@ "version_error": "Impossibile recuperare le informazioni sulla versione. Codice di stato", "loaded_cog": "Cog caricato:", "cog_fail": "Impossibile caricare il cog:", + "loaded_cog2": "Module caricato:", + "cog_fail2": "Impossibile caricare il module:", "no_model": "Nessun modello Markov salvato trovato. Iniziamo da zero.", "folder_created": "Cartella '{folder_name}' creata.", "folder_exists": "La cartella '{folder_name}' esiste giĆ . Saltando...", diff --git a/modules/grabtemplate.py b/modules/grabtemplate.py new file mode 100644 index 0000000..891db0b --- /dev/null +++ b/modules/grabtemplate.py @@ -0,0 +1,42 @@ +import discord +from discord.ext import commands +import os +import requests +import ast + +def get_version_url(config_file_path): + with open(config_file_path, "r") as file: + file_content = file.read() + tree = ast.parse(file_content) + for node in ast.walk(tree): + if isinstance(node, ast.Assign): + for target in node.targets: + if isinstance(target, ast.Name) and target.id == "VERSION_URL": + if isinstance(node.value, ast.Str): + return node.value.s + return None +config_file_path = "config.py" +VERSION_URL = get_version_url(config_file_path) + + +class grabTemplate(commands.Cog): + def __init__(self, bot): + self.bot = bot + + def download_json(): + response = requests.get(f"{VERSION_URL}/goob/template.json") + if response.status_code == 200: + if os.path.exists("memory.json"): + return + else: + userinput = input("Do you want to download the template json instead of starting from scratch?\n(Y/N)\n") + if userinput.lower() == "y": + with open("memory.json", "w", encoding="utf-8") as file: + file.write(response.text) + else: + print("Starting from scratch...") + elif response.status_code == 404: + print("File not found on goober central!!") + download_json() +async def setup(bot): + await bot.add_cog(grabTemplate(bot))