Cleaned log code lines
parent
403ee0aed5
commit
8074fbbef4
|
@ -17,10 +17,10 @@ class DiscordBot(commands.Bot):
|
||||||
self.help_data = None # We'll set this later
|
self.help_data = None # We'll set this later
|
||||||
self.load_commands()
|
self.load_commands()
|
||||||
|
|
||||||
self_log = self.log
|
self.log("Discord bot initiated")
|
||||||
|
|
||||||
self.log("Discord bot initiated", "INFO")
|
cmd_class = str(type(self.commands)).split("'", 2)[1]
|
||||||
log_func(f"DiscordBot.commands type: {type(self.commands)}", "DEBUG")
|
log_func(f"DiscordBot.commands type: {cmd_class}", "DEBUG")
|
||||||
|
|
||||||
def set_db_connection(self, db_conn):
|
def set_db_connection(self, db_conn):
|
||||||
"""
|
"""
|
||||||
|
@ -35,7 +35,7 @@ class DiscordBot(commands.Bot):
|
||||||
try:
|
try:
|
||||||
importlib.reload(cmd_discord)
|
importlib.reload(cmd_discord)
|
||||||
cmd_discord.setup(self)
|
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
|
# Now load the help info from dictionary/help_discord.json
|
||||||
help_json_path = "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")
|
if len(_cmd_args) > 1: self.log(f"!{ctx.command} arguments: {_cmd_args}", "DEBUG")
|
||||||
|
|
||||||
async def on_ready(self):
|
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):
|
async def run(self, token):
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -27,9 +27,10 @@ class TwitchBot(commands.Bot):
|
||||||
initial_channels=config["twitch_channels"]
|
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
|
# 2) Then load commands
|
||||||
self.load_commands()
|
self.load_commands()
|
||||||
|
@ -57,14 +58,14 @@ class TwitchBot(commands.Bot):
|
||||||
await self.handle_commands(message)
|
await self.handle_commands(message)
|
||||||
|
|
||||||
async def event_ready(self):
|
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):
|
async def refresh_access_token(self):
|
||||||
"""
|
"""
|
||||||
Refreshes the Twitch access token using the stored refresh token.
|
Refreshes the Twitch access token using the stored refresh token.
|
||||||
Retries up to 2 times before logging a fatal error.
|
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"
|
url = "https://id.twitch.tv/oauth2/token"
|
||||||
params = {
|
params = {
|
||||||
|
@ -87,7 +88,7 @@ class TwitchBot(commands.Bot):
|
||||||
os.environ["TWITCH_REFRESH_TOKEN"] = self.refresh_token
|
os.environ["TWITCH_REFRESH_TOKEN"] = self.refresh_token
|
||||||
self.update_env_file()
|
self.update_env_file()
|
||||||
|
|
||||||
self.log("Twitch token refreshed successfully.", "INFO")
|
self.log("Twitch token refreshed successfully.")
|
||||||
return # Success, exit function
|
return # Success, exit function
|
||||||
else:
|
else:
|
||||||
self.log(f"Twitch token refresh failed (Attempt {attempt+1}/3): {data}", "WARNING")
|
self.log(f"Twitch token refresh failed (Attempt {attempt+1}/3): {data}", "WARNING")
|
||||||
|
@ -117,7 +118,7 @@ class TwitchBot(commands.Bot):
|
||||||
else:
|
else:
|
||||||
file.write(line)
|
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:
|
except Exception as e:
|
||||||
self.log(f"Failed to update .env file: {e}", "ERROR")
|
self.log(f"Failed to update .env file: {e}", "ERROR")
|
||||||
|
@ -128,7 +129,7 @@ class TwitchBot(commands.Bot):
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
cmd_twitch.setup(self)
|
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
|
# Now load the help info from dictionary/help_twitch.json
|
||||||
help_json_path = "dictionary/help_twitch.json"
|
help_json_path = "dictionary/help_twitch.json"
|
||||||
|
|
10
bots.py
10
bots.py
|
@ -97,6 +97,9 @@ def log(message, level="INFO", exec_info=False):
|
||||||
async def main():
|
async def main():
|
||||||
global discord_bot, twitch_bot, db_conn
|
global discord_bot, twitch_bot, db_conn
|
||||||
|
|
||||||
|
# Log initial start
|
||||||
|
log("--------------- BOT STARTUP ---------------")
|
||||||
|
|
||||||
# Before creating your DiscordBot/TwitchBot, initialize DB
|
# Before creating your DiscordBot/TwitchBot, initialize DB
|
||||||
db_conn = init_db_connection(config_data, log)
|
db_conn = init_db_connection(config_data, log)
|
||||||
if not db_conn:
|
if not db_conn:
|
||||||
|
@ -109,9 +112,8 @@ async def main():
|
||||||
ensure_quotes_table(db_conn, log)
|
ensure_quotes_table(db_conn, log)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log(f"Critical: unable to ensure quotes table: {e}", "FATAL")
|
log(f"Critical: unable to ensure quotes table: {e}", "FATAL")
|
||||||
|
|
||||||
|
|
||||||
log("Initializing bots...", "INFO")
|
log("Initializing bots...")
|
||||||
|
|
||||||
# Create both bots
|
# Create both bots
|
||||||
discord_bot = DiscordBot(config_data, log)
|
discord_bot = DiscordBot(config_data, log)
|
||||||
|
@ -121,11 +123,11 @@ async def main():
|
||||||
try:
|
try:
|
||||||
discord_bot.set_db_connection(db_conn)
|
discord_bot.set_db_connection(db_conn)
|
||||||
twitch_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:
|
except Exception as e:
|
||||||
log(f"Unable to initialize database connection to one or both bots: {e}", "FATAL")
|
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")))
|
discord_task = asyncio.create_task(discord_bot.run(os.getenv("DISCORD_BOT_TOKEN")))
|
||||||
twitch_task = asyncio.create_task(twitch_bot.run())
|
twitch_task = asyncio.create_task(twitch_bot.run())
|
||||||
|
|
|
@ -41,7 +41,7 @@ def init_db_connection(config, log):
|
||||||
port=port
|
port=port
|
||||||
)
|
)
|
||||||
conn.autocommit = False # We'll manage commits manually
|
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
|
return conn
|
||||||
except mariadb.Error as e:
|
except mariadb.Error as e:
|
||||||
log(f"Error connecting to MariaDB: {e}", "WARNING")
|
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")
|
sqlite_path = db_settings.get("sqlite_path", "local_database.sqlite")
|
||||||
try:
|
try:
|
||||||
conn = sqlite3.connect(sqlite_path)
|
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
|
return conn
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
log(f"Could not open local SQLite database '{sqlite_path}': {e}", "WARNING")
|
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
|
return # We can just return
|
||||||
|
|
||||||
# 3) Table does NOT exist => create it
|
# 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:
|
if is_sqlite:
|
||||||
create_table_sql = """
|
create_table_sql = """
|
||||||
|
@ -211,4 +211,4 @@ def ensure_quotes_table(db_conn, log_func):
|
||||||
log_func(error_msg, "ERROR")
|
log_func(error_msg, "ERROR")
|
||||||
raise RuntimeError(error_msg)
|
raise RuntimeError(error_msg)
|
||||||
|
|
||||||
log_func("Successfully created table 'quotes'.", "INFO")
|
log_func("Successfully created table 'quotes'.")
|
||||||
|
|
Loading…
Reference in New Issue