From 11b42cff538ab95bc2fe9d8c1bcd613caf6c6bc6 Mon Sep 17 00:00:00 2001 From: Yoruio Date: Sat, 16 Jul 2022 14:18:04 -0600 Subject: [PATCH 1/2] feat: Adds jellyfin external URL and timeout gh-13 - Adds Jellyfin external URL config option - URL to send to users if different from server URL. - Adds Jellyfin server setup timeout after 5 seconds, and response delaying so discord bot doesn't timeout task: gh-13 --- app/bot/cogs/app.py | 10 +++++++++- app/bot/helper/confighelper.py | 10 +++++++++- app/bot/helper/jellyfinhelper.py | 4 ++-- app/bot/helper/message.py | 4 ++-- run.py | 24 +++++++++++++++++------- 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/app/bot/cogs/app.py b/app/bot/cogs/app.py index bcbe928..cbdd7b9 100644 --- a/app/bot/cogs/app.py +++ b/app/bot/cogs/app.py @@ -90,6 +90,14 @@ try: except: USE_PLEX = False +try: + JELLYFIN_EXTERNAL_URL = config.get(BOT_SECTION, "jellyfin_external_url") + if not JELLYFIN_EXTERNAL_URL: + JELLYFIN_EXTERNAL_URL = JELLYFIN_SERVER_URL +except: + JELLYFIN_EXTERNAL_URL = JELLYFIN_SERVER_URL + print("Could not get Jellyfin external url. Defaulting to server url.") + if USE_PLEX and plex_configured: try: print("Connecting to Plex......") @@ -296,7 +304,7 @@ class app(commands.Cog): db.save_user_jellyfin(str(after.id), username) await asyncio.sleep(5) await embedcustom(after, "You have been added to Jellyfin!", {'Username': username, 'Password': f"||{password}||"}) - await embedinfo(after, f"Go to {JELLYFIN_SERVER_URL} to log in!") + await embedinfo(after, f"Go to {JELLYFIN_EXTERNAL_URL} to log in!") else: await embedinfo(after, 'There was an error adding this user to Jellyfin. Message Server Admin.') jellyfin_processed = True diff --git a/app/bot/helper/confighelper.py b/app/bot/helper/confighelper.py index 45ebff9..25115f1 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'] + 'jellyfin_libs', 'plex_enabled', 'jellyfin_enabled', 'jellyfin_external_url'] # settings Discord_bot_token = "" @@ -96,6 +96,14 @@ except: print("Could not load Jellyfin config") jellyfin_configured = False +try: + JELLYFIN_EXTERNAL_URL = config.get(BOT_SECTION, "jellyfin_external_url") + if not JELLYFIN_EXTERNAL_URL: + JELLYFIN_EXTERNAL_URL = JELLYFIN_SERVER_URL +except: + JELLYFIN_EXTERNAL_URL = JELLYFIN_SERVER_URL + print("Could not get Jellyfin external url. Defaulting to server url.") + # Get Jellyfin roles config try: jellyfin_roles = config.get(BOT_SECTION, 'jellyfin_roles') diff --git a/app/bot/helper/jellyfinhelper.py b/app/bot/helper/jellyfinhelper.py index f598d38..e7f7317 100644 --- a/app/bot/helper/jellyfinhelper.py +++ b/app/bot/helper/jellyfinhelper.py @@ -164,12 +164,12 @@ def get_config(jellyfin_url, jellyfin_api_key): url = f"{jellyfin_url}/System/Configuration" querystring = {"api_key":jellyfin_api_key} - response = requests.request("GET", url, params=querystring) + response = requests.request("GET", url, params=querystring, timeout=5) return response.json() def get_status(jellyfin_url, jellyfin_api_key): url = f"{jellyfin_url}/System/Configuration" querystring = {"api_key":jellyfin_api_key} - response = requests.request("GET", url, params=querystring) + response = requests.request("GET", url, params=querystring, timeout=5) return response.status_code \ No newline at end of file diff --git a/app/bot/helper/message.py b/app/bot/helper/message.py index 3846b47..fc92cda 100644 --- a/app/bot/helper/message.py +++ b/app/bot/helper/message.py @@ -18,11 +18,11 @@ async def embedcustom(recipient, title, fields, ephemeral=True): async def send_info(recipient, message, ephemeral=True): if isinstance(recipient, discord.InteractionResponse): await recipient.send_message(message, ephemeral=ephemeral) - elif isinstance(recipient, discord.User) or isinstance(recipient, discord.member.Member): + elif isinstance(recipient, discord.User) or isinstance(recipient, discord.member.Member) or isinstance(recipient, discord.Webhook): await recipient.send(message) async def send_embed(recipient, embed, ephemeral=True): - if isinstance(recipient, discord.User) or isinstance(recipient, discord.member.Member): + if isinstance(recipient, discord.User) or isinstance(recipient, discord.member.Member) or isinstance(recipient, discord.Webhook): await recipient.send(embed=embed) elif isinstance(recipient, discord.InteractionResponse): await recipient.send_message(embed=embed, ephemeral = ephemeral) \ No newline at end of file diff --git a/run.py b/run.py index a145a78..3f15a4c 100644 --- a/run.py +++ b/run.py @@ -11,6 +11,7 @@ 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 * +from requests import ConnectTimeout maxroles = 10 @@ -153,7 +154,8 @@ async def jellyrolels(interaction: discord.Interaction): @jellyfin_commands.command(name="setup", description="Setup Jellyfin integration") @app_commands.checks.has_permissions(administrator=True) -async def setupjelly(interaction: discord.Interaction, server_url: str, api_key: str): +async def setupjelly(interaction: discord.Interaction, server_url: str, api_key: str, external_url: str = None): + await interaction.response.defer() # get rid of training slashes server_url = server_url.rstrip('/') @@ -163,28 +165,36 @@ async def setupjelly(interaction: discord.Interaction, server_url: str, api_key: pass elif server_status == 401: # Unauthorized - await embederror(interaction.response, "API key provided is invalid") + await embederror(interaction.followup, "API key provided is invalid") return elif server_status == 403: # Forbidden - await embederror(interaction.response, "API key provided does not have permissions") + await embederror(interaction.followup, "API key provided does not have permissions") return elif server_status == 404: # page not found - await embederror(interaction.response, "Server endpoint provided was not found") + await embederror(interaction.followup, "Server endpoint provided was not found") return else: - await embederror(interaction.response, "Unknown error occurred while connecting to server. Check Membarr logs.") + await embederror(interaction.followup, "Unknown error occurred while connecting to Jellyfin. Check Membarr logs.") + except ConnectTimeout as e: + await embederror(interaction.followup, "Connection to server timed out. Check that Jellyfin is online and reachable.") + return except Exception as e: print("Exception while testing Jellyfin connection") + print(type(e).__name__) print(e) - await embederror(interaction.response, "Could not connect to server. Check logs for more details.") + await embederror(interaction.followup, "Unknown exception while connecting to Jellyfin. Check Membarr logs") return confighelper.change_config("jellyfin_server_url", str(server_url)) confighelper.change_config("jellyfin_api_key", str(api_key)) + if external_url is not None: + confighelper.change_config("jellyfin_external_url", str(external_url)) + else: + confighelper.change_config("jellyfin_external_url", "") print("Jellyfin server URL and API key updated. Restarting bot.") - await interaction.response.send_message("Jellyfin server URL and API key updated. Restarting bot.", ephemeral=True) + await interaction.followup.send("Jellyfin server URL and API key updated. Restarting bot.", ephemeral=True) await reload() print("Bot has been restarted. Give it a few seconds.") From 7bf0360015209891a6901fcdcbbff2345085fc1b Mon Sep 17 00:00:00 2001 From: Roy Du Date: Sat, 16 Jul 2022 14:42:36 -0600 Subject: [PATCH 2/2] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 846a70f..81d4ab7 100644 --- a/README.md +++ b/README.md @@ -91,8 +91,8 @@ This command disables the Plex integration (currently only disables auto-add / a # Jellyfin Setup Commands: ``` -/jellyfinsettings setup -This command is used to setup the Jellyfin server +/jellyfinsettings setup +This command is used to setup the Jellyfin server. The external server URL is the URL that is sent to users to log into your Jellyfin server. /jellyfinsettings addrole <@role> These role(s) will be used as the role(s) to automatically invite user to Jellyfin /jellyfinsettings removerole <@role>