diff --git a/app/bot/cogs/app.py b/app/bot/cogs/app.py index 111eaad..51156cf 100644 --- a/app/bot/cogs/app.py +++ b/app/bot/cogs/app.py @@ -18,6 +18,83 @@ BOT_SECTION = 'bot_envs' plex_configured = True jellyfin_configured = True +config = configparser.ConfigParser() +config.read(CONFIG_PATH) + +# Get Plex config +try: + PLEXUSER = config.get(BOT_SECTION, 'plex_user') + 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 +try: + plex_roles = config.get(BOT_SECTION, 'plex_roles') +except: + plex_roles = None +if plex_roles: + plex_roles = list(plex_roles.split(',')) +else: + plex_roles = [] + +# Get Plex libs config +try: + Plex_LIBS = config.get(BOT_SECTION, 'plex_libs') +except: + Plex_LIBS = None +if Plex_LIBS is None: + Plex_LIBS = ["all"] +else: + Plex_LIBS = list(Plex_LIBS.split(',')) + +# Get Jellyfin config +try: + JELLYFIN_SERVER_URL = config.get(BOT_SECTION, 'jellyfin_server_url') + JELLYFIN_API_KEY = config.get(BOT_SECTION, "jellyfin_api_key") +except: + jellyfin_configured = False + +# Get Jellyfin roles config +try: + jellyfin_roles = config.get(BOT_SECTION, 'jellyfin_roles') +except: + jellyfin_roles = None +if jellyfin_roles: + jellyfin_roles = list(jellyfin_roles.split(',')) +else: + jellyfin_roles = [] + +# Get Jellyfin libs config +try: + jellyfin_libs = config.get(BOT_SECTION, 'jellyfin_libs') +except: + jellyfin_libs = None +if jellyfin_libs is None: + jellyfin_libs = ["all"] +else: + jellyfin_libs = list(jellyfin_libs.split(',')) + +# Get Enable config +try: + USE_JELLYFIN = config.get(BOT_SECTION, 'jellyfin_enabled') + USE_JELLYFIN = USE_JELLYFIN.lower() == "true" +except: + USE_JELLYFIN = False + +try: + USE_PLEX = config.get(BOT_SECTION, "plex_enabled") + USE_PLEX = USE_PLEX.lower() == "true" +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......") @@ -252,37 +329,41 @@ class app(commands.Cog): @commands.Cog.listener() async def on_member_remove(self, member): - email = db.get_useremail(member.id) - plexhelper.plexremove(plex,email) - jellyfin_username = db.get_jellyfin_username(member.id) - jelly.remove_user(jellyfin_username) + if USE_PLEX and plex_configured: + email = db.get_useremail(member.id) + plexhelper.plexremove(plex,email) + + if USE_JELLYFIN and jellyfin_configured: + jellyfin_username = db.get_jellyfin_username(member.id) + jelly.remove_user(jellyfin_username) + deleted = db.delete_user(member.id) if deleted: print("Removed {} from db because user left discord server.".format(email)) - @commands.has_permissions(administrator=True) + @app_commands.checks.has_permissions(administrator=True) @plex_commands.command(name="invite", description="Invite a user to Plex") async def plexinvite(self, interaction: discord.Interaction, email: str): await self.addtoplex(email, interaction.response) - @commands.has_permissions(administrator=True) + @app_commands.checks.has_permissions(administrator=True) @plex_commands.command(name="remove", description="Remove a user from Plex") async def plexremove(self, interaction: discord.Interaction, email: str): await self.removefromplex(email, interaction.response) - @commands.has_permissions(administrator=True) + @app_commands.checks.has_permissions(administrator=True) @jellyfin_commands.command(name="invite", description="Invite a user to Jellyfin") async def jellyfininvite(self, interaction: discord.Interaction, username: str): password = jelly.generate_password(16) if await self.addtojellyfin(username, password, interaction.response): await embedcustom(interaction.response, "Jellyfin user created!", {'Username': username, 'Password': f"||{password}||"}) - @commands.has_permissions(administrator=True) + @app_commands.checks.has_permissions(administrator=True) @jellyfin_commands.command(name="remove", description="Remove a user from Jellyfin") async def jellyfinremove(self, interaction: discord.Interaction, username: str): await self.removefromjellyfin(username, interaction.response) - @commands.has_permissions(administrator=True) + @app_commands.checks.has_permissions(administrator=True) @membarr_commands.command(name="dbadd", description="Add a user to the Membarr database") async def dbadd(self, interaction: discord.Interaction, member: discord.Member, email: str = "", jellyfin_username: str = ""): email = email.strip() @@ -300,7 +381,7 @@ class app(commands.Cog): await embedinfo(interaction.response, 'There was an error adding this user to database. Check Membarr logs for more info') print(e) - @commands.has_permissions(administrator=True) + @app_commands.checks.has_permissions(administrator=True) @membarr_commands.command(name="dbls", description="View Membarr database") async def dbls(self, interaction: discord.Interaction): @@ -334,7 +415,7 @@ class app(commands.Cog): await interaction.response.send_message(embed = embed, ephemeral=True) - @commands.has_permissions(administrator=True) + @app_commands.checks.has_permissions(administrator=True) @membarr_commands.command(name="dbrm", description="Remove user from Membarr database") async def dbrm(self, interaction: discord.Interaction, position: int): embed = discord.Embed(title='Membarr Database.') diff --git a/app/bot/helper/confighelper.py b/app/bot/helper/confighelper.py index a89827d..8d6e5b1 100644 --- a/app/bot/helper/confighelper.py +++ b/app/bot/helper/confighelper.py @@ -41,98 +41,103 @@ if(path.exists('bot.env')): except Exception as e: pass - + try: Discord_bot_token = str(os.environ['token']) switch = 1 except Exception as e: pass -if(path.exists(CONFIG_PATH)): - config = configparser.ConfigParser() - config.read(CONFIG_PATH) +if not (path.exists(CONFIG_PATH)): + with open (CONFIG_PATH, 'w') as fp: + pass - # Get Plex config - try: - PLEXUSER = config.get(BOT_SECTION, 'plex_user') - 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 - try: - plex_roles = config.get(BOT_SECTION, 'plex_roles') - except: - print("Could not get Plex roles config") - plex_roles = None - if plex_roles: - plex_roles = list(plex_roles.split(',')) - else: - plex_roles = [] - # Get Plex libs config - try: - Plex_LIBS = config.get(BOT_SECTION, 'plex_libs') - except: - print("Could not get Plex libs config. Defaulting to all libraries.") - Plex_LIBS = None - if Plex_LIBS is None: - Plex_LIBS = ["all"] - else: - Plex_LIBS = list(Plex_LIBS.split(',')) - - # Get Jellyfin config - try: - JELLYFIN_SERVER_URL = config.get(BOT_SECTION, 'jellyfin_server_url') - JELLYFIN_API_KEY = config.get(BOT_SECTION, "jellyfin_api_key") - except: - print("Could not load Jellyfin config") - jellyfin_configured = False +config = configparser.ConfigParser() +config.read(CONFIG_PATH) - # Get Jellyfin roles config - try: - jellyfin_roles = config.get(BOT_SECTION, 'jellyfin_roles') - except: - print("Could not get Jellyfin roles config") - jellyfin_roles = None - if jellyfin_roles: - jellyfin_roles = list(jellyfin_roles.split(',')) - else: - jellyfin_roles = [] +# Get Plex config +try: + PLEXUSER = config.get(BOT_SECTION, 'plex_user') + 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 Jellyfin libs config - try: - jellyfin_libs = config.get(BOT_SECTION, 'jellyfin_libs') - except: - print("Could not get Jellyfin libs config. Defaulting to all libraries.") - jellyfin_libs = None - if jellyfin_libs is None: - jellyfin_libs = ["all"] - else: - jellyfin_libs = list(jellyfin_libs.split(',')) +# Get Plex roles config +try: + plex_roles = config.get(BOT_SECTION, 'plex_roles') +except: + print("Could not get Plex roles config") + plex_roles = None +if plex_roles: + plex_roles = list(plex_roles.split(',')) +else: + plex_roles = [] + +# Get Plex libs config +try: + Plex_LIBS = config.get(BOT_SECTION, 'plex_libs') +except: + print("Could not get Plex libs config. Defaulting to all libraries.") + Plex_LIBS = None +if Plex_LIBS is None: + Plex_LIBS = ["all"] +else: + Plex_LIBS = list(Plex_LIBS.split(',')) - # Get Enable config - try: - USE_JELLYFIN = config.get(BOT_SECTION, 'jellyfin_enabled') - USE_JELLYFIN = USE_JELLYFIN.lower() == "true" - except: - print("Could not get Jellyfin enable config. Defaulting to False") - USE_JELLYFIN = False - - try: - USE_PLEX = config.get(BOT_SECTION, "plex_enabled") - USE_PLEX = USE_PLEX.lower() == "true" - 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 +# Get Jellyfin config +try: + JELLYFIN_SERVER_URL = config.get(BOT_SECTION, 'jellyfin_server_url') + JELLYFIN_API_KEY = config.get(BOT_SECTION, "jellyfin_api_key") +except: + print("Could not load Jellyfin config") + jellyfin_configured = False + +# Get Jellyfin roles config +try: + jellyfin_roles = config.get(BOT_SECTION, 'jellyfin_roles') +except: + print("Could not get Jellyfin roles config") + jellyfin_roles = None +if jellyfin_roles: + jellyfin_roles = list(jellyfin_roles.split(',')) +else: + jellyfin_roles = [] + +# Get Jellyfin libs config +try: + jellyfin_libs = config.get(BOT_SECTION, 'jellyfin_libs') +except: + print("Could not get Jellyfin libs config. Defaulting to all libraries.") + jellyfin_libs = None +if jellyfin_libs is None: + jellyfin_libs = ["all"] +else: + jellyfin_libs = list(jellyfin_libs.split(',')) + +# Get Enable config +try: + USE_JELLYFIN = config.get(BOT_SECTION, 'jellyfin_enabled') + USE_JELLYFIN = USE_JELLYFIN.lower() == "true" +except: + print("Could not get Jellyfin enable config. Defaulting to False") + USE_JELLYFIN = False + +try: + USE_PLEX = config.get(BOT_SECTION, "plex_enabled") + USE_PLEX = USE_PLEX.lower() == "true" +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(): """ diff --git a/run.py b/run.py index ee8b18c..681031f 100644 --- a/run.py +++ b/run.py @@ -65,7 +65,7 @@ plex_commands = app_commands.Group(name="plexsettings", description="Membarr Ple jellyfin_commands = app_commands.Group(name="jellyfinsettings", description="Membarr Jellyfin commands") @plex_commands.command(name="addrole", description="Add a role to automatically add users to Plex") -@commands.has_permissions(administrator=True) +@app_commands.checks.has_permissions(administrator=True) async def plexroleadd(interaction: discord.Interaction, role: discord.Role): if len(plex_roles) <= maxroles: # Do not add roles multiple times. @@ -81,11 +81,11 @@ async def plexroleadd(interaction: discord.Interaction, role: discord.Role): confighelper.change_config("plex_roles", saveroles) await interaction.response.send_message("Updated Plex roles. Bot is restarting. Please wait.", ephemeral=True) print("Plex roles updated. Restarting bot, Give it a few seconds.") - reload() + await reload() print("Bot has been restarted. Give it a few seconds.") @plex_commands.command(name="removerole", description="Stop adding users with a role to Plex") -@commands.has_permissions(administrator=True) +@app_commands.checks.has_permissions(administrator=True) async def plexroleremove(interaction: discord.Interaction, role: discord.Role): if role.name not in plex_roles: await embederror(interaction.response, f"\"{role.name}\" is currently not a Plex role.") @@ -95,7 +95,7 @@ async def plexroleremove(interaction: discord.Interaction, role: discord.Role): await interaction.response.send_message(f"Membarr will stop auto-adding \"{role.name}\" to Plex", ephemeral=True) @plex_commands.command(name="listroles", description="List all roles whose members will be automatically added to Plex") -@commands.has_permissions(administrator=True) +@app_commands.checks.has_permissions(administrator=True) async def plexrolels(interaction: discord.Interaction): await interaction.response.send_message( "The following roles are being automatically added to Plex:\n" + @@ -103,7 +103,7 @@ async def plexrolels(interaction: discord.Interaction): ) @plex_commands.command(name="setup", description="Setup Plex integration") -@commands.has_permissions(administrator=True) +@app_commands.checks.has_permissions(administrator=True) async def setupplex(interaction: discord.Interaction, username: str, password: str, server_name: str): confighelper.change_config("plex_user", str(username)) confighelper.change_config("plex_pass", str(password)) @@ -114,11 +114,11 @@ async def setupplex(interaction: discord.Interaction, username: str, password: s "Please check logs and make sure you see the line: `Logged into plex`. If not run this command again and make sure you enter the right values.", ephemeral=True ) - reload() + await reload() print("Bot has been restarted. Give it a few seconds.") @jellyfin_commands.command(name="addrole", description="Add a role to automatically add users to Jellyfin") -@commands.has_permissions(administrator=True) +@app_commands.checks.has_permissions(administrator=True) async def jellyroleadd(interaction: discord.Interaction, role: discord.Role): if len(jellyfin_roles) <= maxroles: # Do not add roles multiple times. @@ -131,11 +131,11 @@ async def jellyroleadd(interaction: discord.Interaction, role: discord.Role): confighelper.change_config("jellyfin_roles", saveroles) await interaction.response.send_message("Updated Jellyfin roles. Bot is restarting. Please wait a few seconds.", ephemeral=True) print("Jellyfin roles updated. Restarting bot.") - reload() + await reload() print("Bot has been restarted. Give it a few seconds.") @jellyfin_commands.command(name="removerole", description="Stop adding users with a role to Jellyfin") -@commands.has_permissions(administrator=True) +@app_commands.checks.has_permissions(administrator=True) async def jellyroleremove(interaction: discord.Interaction, role: discord.Role): if role.name not in jellyfin_roles: await embederror(interaction.response, f"\"{role.name}\" is currently not a Jellyfin role.") @@ -145,7 +145,7 @@ async def jellyroleremove(interaction: discord.Interaction, role: discord.Role): await interaction.response.send_message(f"Membarr will stop auto-adding \"{role.name}\" to Jellyfin", ephemeral=True) @jellyfin_commands.command(name="listroles", description="List all roles whose members will be automatically added to Jellyfin") -@commands.has_permissions(administrator=True) +@app_commands.checks.has_permissions(administrator=True) async def jellyrolels(interaction: discord.Interaction): await interaction.response.send_message( "The following roles are being automatically added to Jellyfin:\n" + @@ -153,7 +153,7 @@ async def jellyrolels(interaction: discord.Interaction): ) @jellyfin_commands.command(name="setup", description="Setup Jellyfin integration") -@commands.has_permissions(administrator=True) +@app_commands.checks.has_permissions(administrator=True) async def setupjelly(interaction: discord.Interaction, server_url: str, api_key: str): # get rid of training slashes server_url = server_url.rstrip('/') @@ -185,13 +185,13 @@ async def setupjelly(interaction: discord.Interaction, server_url: str, api_key: confighelper.change_config("jellyfin_server_url", str(server_url)) confighelper.change_config("jellyfin_api_key", str(api_key)) print("Jellyfin server URL and API key updated. Restarting bot.") - await interaction.interaction.send_message("Jellyfin server URL and API key updated. Restarting bot.", ephemeral=True) - reload() + await interaction.response.send_message("Jellyfin server URL and API key updated. Restarting bot.", ephemeral=True) + await reload() print("Bot has been restarted. Give it a few seconds.") @plex_commands.command(name="setuplibs", description="Setup libraries that new users can access") -@commands.has_permissions(administrator=True) +@app_commands.checks.has_permissions(administrator=True) async def setupplexlibs(interaction: discord.Interaction, libraries:str): if not libraries: await embederror(interaction.response, "libraries string is empty.") @@ -202,11 +202,11 @@ async def setupplexlibs(interaction: discord.Interaction, libraries:str): confighelper.change_config("plex_libs", str(libraries)) print("Plex libraries updated. Restarting bot. Please wait.") await interaction.response.send_message("Jellyfin libraries updated. Please wait a few seconds for bot to restart.", ephemeral=True) - reload() + await reload() print("Bot has been restarted. Give it a few seconds.") @jellyfin_commands.command(name="setuplibs", description="Setup libraries that new users can access") -@commands.has_permissions(administrator=True) +@app_commands.checks.has_permissions(administrator=True) async def setupjellylibs(interaction: discord.Interaction, libraries:str): if not libraries is None: await embederror(interaction.response, "libraries string is empty.") @@ -217,39 +217,39 @@ async def setupjellylibs(interaction: discord.Interaction, libraries:str): confighelper.change_config("jellyfin_libs", str(libraries)) print("Jellyfin libraries updated. Restarting bot. Please wait.") await interaction.response.send_message("Jellyfin libraries updated. Please wait a few seconds for bot to restart.", ephemeral=True) - reload() + await reload() print("Bot has been restarted. Give it a few seconds.") # Enable / Disable Plex integration @plex_commands.command(name="enable", description="Enable auto-adding users to Plex") -@commands.has_permissions(administrator=True) +@app_commands.checks.has_permissions(administrator=True) async def enableplex(interaction: discord.Interaction): if confighelper.USE_PLEX: await interaction.response.send_message("Plex already enabled.", ephemeral=True) return confighelper.change_config("plex_enabled", True) print("Plex enabled, reloading server") - reload() + await reload() confighelper.USE_PLEX = True await interaction.response.send_message("Plex enabled. Restarting server. Give it a few seconds.", ephemeral=True) print("Bot has restarted. Give it a few seconds.") @plex_commands.command(name="disable", description="Disable adding users to Plex") -@commands.has_permissions(administrator=True) +@app_commands.checks.has_permissions(administrator=True) async def disableplex(interaction: discord.Interaction): if not confighelper.USE_PLEX: await interaction.response.send_message("Plex already disabled.", ephemeral=True) return confighelper.change_config("plex_enabled", False) print("Plex disabled, reloading server") - reload() + await reload() confighelper.USE_PLEX = False await interaction.response.send_message("Plex disabled. Restarting server. Give it a few seconds.", ephemeral=True) print("Bot has restarted. Give it a few seconds.") # Enable / Disable Jellyfin integration @jellyfin_commands.command(name="enable", description="Enable adding users to Jellyfin") -@commands.has_permissions(administrator=True) +@app_commands.checks.has_permissions(administrator=True) async def enablejellyfin(interaction: discord.Interaction): if confighelper.USE_JELLYFIN: await interaction.response.send_message("Jellyfin already enabled.", ephemeral=True) @@ -257,19 +257,19 @@ async def enablejellyfin(interaction: discord.Interaction): confighelper.change_config("jellyfin_enabled", True) print("Jellyfin enabled, reloading server") confighelper.USE_JELLYFIN = True - reload() + await reload() await interaction.response.send_message("Jellyfin enabled. Restarting server. Give it a few seconds.", ephemeral=True) print("Bot has restarted. Give it a few seconds.") @jellyfin_commands.command(name="disable", description = "Disable adding users to Jellyfin") -@commands.has_permissions(administrator=True) +@app_commands.checks.has_permissions(administrator=True) async def disablejellyfin(interaction: discord.Interaction): if not confighelper.USE_JELLYFIN: await interaction.response.send_message("Jellyfin already disabled.", ephemeral=True) return confighelper.change_config("jellyfin_enabled", False) print("Jellyfin disabled, reloading server") - reload() + await reload() confighelper.USE_JELLYFIN = False await interaction.response.send_message("Jellyfin disabled. Restarting server. Give it a few seconds.", ephemeral=True) print("Bot has restarted. Give it a few seconds.")