Cleaned log code lines

kami_dev
Kami 2025-02-04 13:35:25 +01:00
parent 403ee0aed5
commit 8074fbbef4
4 changed files with 24 additions and 21 deletions

View File

@ -17,10 +17,10 @@ class DiscordBot(commands.Bot):
self.help_data = None # We'll set this later
self.load_commands()
self_log = self.log
self.log("Discord bot initiated", "INFO")
log_func(f"DiscordBot.commands type: {type(self.commands)}", "DEBUG")
self.log("Discord bot initiated")
cmd_class = str(type(self.commands)).split("'", 2)[1]
log_func(f"DiscordBot.commands type: {cmd_class}", "DEBUG")
def set_db_connection(self, db_conn):
"""
@ -35,7 +35,7 @@ class DiscordBot(commands.Bot):
try:
importlib.reload(cmd_discord)
cmd_discord.setup(self)
self.log("Discord commands loaded successfully.", "INFO")
self.log("Discord commands loaded successfully.")
# Now load the help info from dictionary/help_discord.json
help_json_path = "dictionary/help_discord.json"
@ -57,7 +57,7 @@ class DiscordBot(commands.Bot):
if len(_cmd_args) > 1: self.log(f"!{ctx.command} arguments: {_cmd_args}", "DEBUG")
async def on_ready(self):
self.log(f"Discord bot is online as {self.user}", "INFO")
self.log(f"Discord bot is online as {self.user}")
async def run(self, token):
try:

View File

@ -27,9 +27,10 @@ class TwitchBot(commands.Bot):
initial_channels=config["twitch_channels"]
)
self.log("Twitch bot initiated", "INFO")
self.log("Twitch bot initiated")
log_func(f"TwitchBot._commands type: {type(self._commands)}", "DEBUG")
cmd_class = str(type(self._commands)).split("'", 2)[1]
log_func(f"TwitchBot._commands type: {cmd_class}", "DEBUG")
# 2) Then load commands
self.load_commands()
@ -57,14 +58,14 @@ class TwitchBot(commands.Bot):
await self.handle_commands(message)
async def event_ready(self):
self.log(f"Twitch bot is online as {self.nick}", "INFO")
self.log(f"Twitch bot is online as {self.nick}")
async def refresh_access_token(self):
"""
Refreshes the Twitch access token using the stored refresh token.
Retries up to 2 times before logging a fatal error.
"""
self.log("Attempting to refresh Twitch token...", "INFO")
self.log("Attempting to refresh Twitch token...")
url = "https://id.twitch.tv/oauth2/token"
params = {
@ -87,7 +88,7 @@ class TwitchBot(commands.Bot):
os.environ["TWITCH_REFRESH_TOKEN"] = self.refresh_token
self.update_env_file()
self.log("Twitch token refreshed successfully.", "INFO")
self.log("Twitch token refreshed successfully.")
return # Success, exit function
else:
self.log(f"Twitch token refresh failed (Attempt {attempt+1}/3): {data}", "WARNING")
@ -117,7 +118,7 @@ class TwitchBot(commands.Bot):
else:
file.write(line)
self.log("Updated .env file with new Twitch token.", "INFO")
self.log("Updated .env file with new Twitch token.")
except Exception as e:
self.log(f"Failed to update .env file: {e}", "ERROR")
@ -128,7 +129,7 @@ class TwitchBot(commands.Bot):
"""
try:
cmd_twitch.setup(self)
self.log("Twitch commands loaded successfully.", "INFO")
self.log("Twitch commands loaded successfully.")
# Now load the help info from dictionary/help_twitch.json
help_json_path = "dictionary/help_twitch.json"

10
bots.py
View File

@ -97,6 +97,9 @@ def log(message, level="INFO", exec_info=False):
async def main():
global discord_bot, twitch_bot, db_conn
# Log initial start
log("--------------- BOT STARTUP ---------------")
# Before creating your DiscordBot/TwitchBot, initialize DB
db_conn = init_db_connection(config_data, log)
if not db_conn:
@ -109,9 +112,8 @@ async def main():
ensure_quotes_table(db_conn, log)
except Exception as e:
log(f"Critical: unable to ensure quotes table: {e}", "FATAL")
log("Initializing bots...", "INFO")
log("Initializing bots...")
# Create both bots
discord_bot = DiscordBot(config_data, log)
@ -121,11 +123,11 @@ async def main():
try:
discord_bot.set_db_connection(db_conn)
twitch_bot.set_db_connection(db_conn)
log(f"Initialized database connection to both bots", "INFO")
log(f"Initialized database connection to both bots")
except Exception as e:
log(f"Unable to initialize database connection to one or both bots: {e}", "FATAL")
log("Starting Discord and Twitch bots...", "INFO")
log("Starting Discord and Twitch bots...")
discord_task = asyncio.create_task(discord_bot.run(os.getenv("DISCORD_BOT_TOKEN")))
twitch_task = asyncio.create_task(twitch_bot.run())

View File

@ -41,7 +41,7 @@ def init_db_connection(config, log):
port=port
)
conn.autocommit = False # We'll manage commits manually
log(f"Database connection established using MariaDB (host={host}, db={dbname}).", "INFO")
log(f"Database connection established using MariaDB (host={host}, db={dbname}).")
return conn
except mariadb.Error as e:
log(f"Error connecting to MariaDB: {e}", "WARNING")
@ -55,7 +55,7 @@ def init_db_connection(config, log):
sqlite_path = db_settings.get("sqlite_path", "local_database.sqlite")
try:
conn = sqlite3.connect(sqlite_path)
log(f"Database connection established using local SQLite: {sqlite_path}", "INFO")
log(f"Database connection established using local SQLite: {sqlite_path}")
return conn
except sqlite3.Error as e:
log(f"Could not open local SQLite database '{sqlite_path}': {e}", "WARNING")
@ -175,7 +175,7 @@ def ensure_quotes_table(db_conn, log_func):
return # We can just return
# 3) Table does NOT exist => create it
log_func("Table 'quotes' does not exist; creating now...", "INFO")
log_func("Table 'quotes' does not exist; creating now...")
if is_sqlite:
create_table_sql = """
@ -211,4 +211,4 @@ def ensure_quotes_table(db_conn, log_func):
log_func(error_msg, "ERROR")
raise RuntimeError(error_msg)
log_func("Successfully created table 'quotes'.", "INFO")
log_func("Successfully created table 'quotes'.")