From 22c9bd1da9a8e46c5d8e779958f516575f1df908 Mon Sep 17 00:00:00 2001 From: Yoruio Date: Thu, 14 Jul 2022 01:08:32 -0600 Subject: [PATCH] feat: Implements auto-updating database table - Automatically updates database table from old versions / Invitarr - Fancy text formatting to boost my ego - Removes synced variable from config, as we now sync always (guild-specific sync is automatic.) task: gh-9 --- app/bot/cogs/app.py | 15 ++++---- app/bot/helper/confighelper.py | 8 +---- app/bot/helper/db.py | 7 +++- app/bot/helper/dbupdater.py | 65 ++++++++++++++++++++++++++++++++++ app/bot/helper/textformat.py | 14 ++++++++ run.py | 3 +- 6 files changed, 93 insertions(+), 19 deletions(-) create mode 100644 app/bot/helper/dbupdater.py create mode 100644 app/bot/helper/textformat.py diff --git a/app/bot/cogs/app.py b/app/bot/cogs/app.py index 862b537..af86962 100644 --- a/app/bot/cogs/app.py +++ b/app/bot/cogs/app.py @@ -1,5 +1,6 @@ from pickle import FALSE import app.bot.helper.jellyfinhelper as jelly +from app.bot.helper.textformat import bcolors import discord from discord.ext import commands from discord import app_commands @@ -27,7 +28,6 @@ try: PLEXPASS = config.get(BOT_SECTION, 'plex_pass') PLEX_SERVER_NAME = config.get(BOT_SECTION, 'plex_server_name') except: - print("Could not load plex config") plex_configured = False # Get Plex roles config @@ -90,11 +90,6 @@ try: except: USE_PLEX = False -try: - synced = not (float(config.get(BOT_SECTION, "sync_version")) < MEMBARR_VERSION) -except: - synced = False - if USE_PLEX and plex_configured: try: print("Connecting to Plex......") @@ -120,9 +115,11 @@ class app(commands.Cog): @commands.Cog.listener() async def on_ready(self): - print('Made by Yoruio https://github.com/Yoruio/') - print('Forked from Invitarr https://github.com/Sleepingpirates/Invitarr') - print('Named by lordfransie') + print('------') + print(bcolors.AUTHOR + "{:^41}".format(f"MEMBARR V {MEMBARR_VERSION}") + bcolors.ENDC) + print(f'{bcolors.AUTHOR}Made by Yoruio https://github.com/Yoruio/{bcolors.ENDC}') + print(f'Forked from Invitarr https://github.com/Sleepingpirates/Invitarr') + print(f'Named by lordfransie') print(f'Logged in as {self.bot.user} (ID: {self.bot.user.id})') print('------') diff --git a/app/bot/helper/confighelper.py b/app/bot/helper/confighelper.py index 8d6e5b1..45ebff9 100644 --- a/app/bot/helper/confighelper.py +++ b/app/bot/helper/confighelper.py @@ -12,7 +12,7 @@ config = configparser.ConfigParser() CONFIG_KEYS = ['username', 'password', 'discord_bot_token', 'plex_user', 'plex_pass', 'plex_roles', 'plex_server_name', 'plex_libs', 'owner_id', 'channel_id', 'auto_remove_user', 'jellyfin_api_key', 'jellyfin_server_url', 'jellyfin_roles', - 'jellyfin_libs', 'plex_enabled', 'jellyfin_enabled', 'sync_version'] + 'jellyfin_libs', 'plex_enabled', 'jellyfin_enabled'] # settings Discord_bot_token = "" @@ -133,12 +133,6 @@ except: print("Could not get Plex enable config. Defaulting to False") USE_PLEX = False -try: - synced = not (float(config.get(BOT_SECTION, "sync_version")) < MEMBARR_VERSION) -except: - print("Could not find previously synced version. Setting synced to false.") - synced = False - def get_config(): """ Function to return current config diff --git a/app/bot/helper/db.py b/app/bot/helper/db.py index 550ba4a..6610d58 100644 --- a/app/bot/helper/db.py +++ b/app/bot/helper/db.py @@ -1,6 +1,9 @@ import sqlite3 +from app.bot.helper.dbupdater import check_table_version, update_table + DB_URL = 'app/config/app.db' +DB_TABLE = 'clients' def create_connection(db_file): """ create a database connection to a SQLite database """ @@ -26,7 +29,7 @@ def checkTableExists(dbcon, tablename): conn = create_connection(DB_URL) # Checking if table exists -if checkTableExists(conn, 'clients'): +if checkTableExists(conn, DB_TABLE): print('Table exists.') else: conn.execute( @@ -38,6 +41,8 @@ else: PRIMARY KEY("id" AUTOINCREMENT) );''') +update_table(conn, DB_TABLE) + def save_user_email(username, email): if username and email: conn.execute(f""" diff --git a/app/bot/helper/dbupdater.py b/app/bot/helper/dbupdater.py new file mode 100644 index 0000000..da32b58 --- /dev/null +++ b/app/bot/helper/dbupdater.py @@ -0,0 +1,65 @@ +import sqlite3 + +CURRENT_VERSION = 'Membarr V1.1' + +table_history = { + 'Invitarr V1.0': [ + (0, 'id', 'INTEGER', 1, None, 1), + (1, 'discord_username', 'TEXT', 1, None, 0), + (2, 'email', 'TEXT', 1, None, 0) + ], + 'Membarr V1.1': [ + (0, 'id', 'INTEGER', 1, None, 1), + (1, 'discord_username', 'TEXT', 1, None, 0), + (2, 'email', 'TEXT', 0, None, 0), + (3, 'jellyfin_username', 'TEXT', 0, None, 0) + ] +} + +def check_table_version(conn, tablename): + dbcur = conn.cursor() + dbcur.execute(f"PRAGMA table_info({tablename})") + table_format = dbcur.fetchall() + for app_version in table_history: + if table_history[app_version] == table_format: + return app_version + raise ValueError("Could not identify database table version.") + +def update_table(conn, tablename): + version = check_table_version(conn, tablename) + print('------') + print(f'DB table version: {version}') + if version == CURRENT_VERSION: + print('DB table up to date!') + print('------') + return + + # Table NOT up to date. + # Update to Membarr V1.1 table + if version == 'Invitarr V1.0': + print("Upgrading DB table from Invitarr v1.0 to Membarr V1.1") + # Create temp table + conn.execute( + '''CREATE TABLE "membarr_temp_upgrade_table" ( + "id" INTEGER NOT NULL UNIQUE, + "discord_username" TEXT NOT NULL UNIQUE, + "email" TEXT, + "jellyfin_username" TEXT, + PRIMARY KEY("id" AUTOINCREMENT) + );''') + conn.execute(f''' + INSERT INTO membarr_temp_upgrade_table(id, discord_username, email) + SELECT id, discord_username, email + FROM {tablename}; + ''') + conn.execute(f''' + DROP TABLE {tablename}; + ''') + conn.execute(f''' + ALTER TABLE membarr_temp_upgrade_table RENAME TO {tablename} + ''') + conn.commit() + version = 'Membarr V1.1' + + print('------') + \ No newline at end of file diff --git a/app/bot/helper/textformat.py b/app/bot/helper/textformat.py new file mode 100644 index 0000000..d4f5da4 --- /dev/null +++ b/app/bot/helper/textformat.py @@ -0,0 +1,14 @@ +from multiprocessing import AuthenticationError + + +class bcolors: + HEADER = '\033[95m' + OKBLUE = '\033[94m' + OKCYAN = '\033[96m' + OKGREEN = '\033[92m' + WARNING = '\033[93m' + FAIL = '\033[91m' + ENDC = '\033[0m' + BOLD = '\033[1m' + UNDERLINE = '\033[4m' + AUTHOR = '\033[1;45;96m' \ No newline at end of file diff --git a/run.py b/run.py index 681031f..a145a78 100644 --- a/run.py +++ b/run.py @@ -11,14 +11,13 @@ from app.bot.helper.confighelper import MEMBARR_VERSION, switch, Discord_bot_tok import app.bot.helper.confighelper as confighelper import app.bot.helper.jellyfinhelper as jelly from app.bot.helper.message import * + maxroles = 10 if switch == 0: print("Missing Config.") sys.exit() -print(f"V {MEMBARR_VERSION}") - class Bot(commands.Bot): def __init__(self) -> None: print("Initializing Discord bot")