add modules (not community cogs) n fix resource checking

This commit is contained in:
WhatDidYouExpect 2025-04-26 15:03:16 +02:00
parent f74b310b47
commit c891457121
4 changed files with 59 additions and 0 deletions

13
bot.py
View file

@ -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}")

View file

@ -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...",

View file

@ -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...",

42
modules/grabtemplate.py Normal file
View file

@ -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))