fixes plex-removerole removing jellyfin roles instead (#47)

And some style fixes
This commit is contained in:
Roy Du
2023-08-27 20:17:48 -04:00
committed by GitHub
parent 6b0dcbd80e
commit fab423b3e3
2 changed files with 55 additions and 30 deletions

View File

@@ -69,9 +69,9 @@ except:
# Get Plex config # Get Plex config
try: try:
PLEX_SERVER_NAME = config.get(BOT_SECTION, 'plex_server_name')
PLEXUSER = config.get(BOT_SECTION, 'plex_user') PLEXUSER = config.get(BOT_SECTION, 'plex_user')
PLEXPASS = config.get(BOT_SECTION, 'plex_pass') PLEXPASS = config.get(BOT_SECTION, 'plex_pass')
PLEX_SERVER_NAME = config.get(BOT_SECTION, 'plex_server_name')
except: except:
print("No Plex login info found") print("No Plex login info found")
if not plex_token_configured: if not plex_token_configured:

83
run.py
View File

@@ -20,6 +20,7 @@ if switch == 0:
print("Missing Config.") print("Missing Config.")
sys.exit() sys.exit()
class Bot(commands.Bot): class Bot(commands.Bot):
def __init__(self) -> None: def __init__(self) -> None:
print("Initializing Discord bot") print("Initializing Discord bot")
@@ -34,28 +35,32 @@ class Bot(commands.Bot):
print("Syncing commands to " + guild.name) print("Syncing commands to " + guild.name)
self.tree.copy_global_to(guild=guild) self.tree.copy_global_to(guild=guild)
await self.tree.sync(guild=guild) await self.tree.sync(guild=guild)
async def on_guild_join(self, guild): async def on_guild_join(self, guild):
print(f"Joined guild {guild.name}") print(f"Joined guild {guild.name}")
print(f"Syncing commands to {guild.name}") print(f"Syncing commands to {guild.name}")
self.tree.copy_global_to(guild=guild) self.tree.copy_global_to(guild=guild)
await self.tree.sync(guild=guild) await self.tree.sync(guild=guild)
async def setup_hook(self): async def setup_hook(self):
print("Loading media server connectors") print("Loading media server connectors")
await self.load_extension(f'app.bot.cogs.app') await self.load_extension(f'app.bot.cogs.app')
bot = Bot() bot = Bot()
async def reload(): async def reload():
await bot.reload_extension(f'app.bot.cogs.app') await bot.reload_extension(f'app.bot.cogs.app')
async def getuser(interaction, server, type): async def getuser(interaction, server, type):
value = None value = None
await interaction.user.send("Please reply with your {} {}:".format(server, type)) await interaction.user.send("Please reply with your {} {}:".format(server, type))
while(value == None): while (value == None):
def check(m): def check(m):
return m.author == interaction.user and not m.guild return m.author == interaction.user and not m.guild
try: try:
value = await bot.wait_for('message', timeout=200, check=check) value = await bot.wait_for('message', timeout=200, check=check)
return value.content return value.content
@@ -63,9 +68,11 @@ async def getuser(interaction, server, type):
message = "Timed Out. Try again." message = "Timed Out. Try again."
return None return None
plex_commands = app_commands.Group(name="plexsettings", description="Membarr Plex commands") plex_commands = app_commands.Group(name="plexsettings", description="Membarr Plex commands")
jellyfin_commands = app_commands.Group(name="jellyfinsettings", description="Membarr Jellyfin commands") 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") @plex_commands.command(name="addrole", description="Add a role to automatically add users to Plex")
@app_commands.checks.has_permissions(administrator=True) @app_commands.checks.has_permissions(administrator=True)
async def plexroleadd(interaction: discord.Interaction, role: discord.Role): async def plexroleadd(interaction: discord.Interaction, role: discord.Role):
@@ -77,15 +84,13 @@ async def plexroleadd(interaction: discord.Interaction, role: discord.Role):
plex_roles.append(role.name) plex_roles.append(role.name)
saveroles = ",".join(plex_roles) saveroles = ",".join(plex_roles)
plex_button = Button(label = "Plex")
view = View()
view.add_item(plex_button)
confighelper.change_config("plex_roles", saveroles) confighelper.change_config("plex_roles", saveroles)
await interaction.response.send_message("Updated Plex roles. Bot is restarting. Please wait.", ephemeral=True) 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.") print("Plex roles updated. Restarting bot, Give it a few seconds.")
await reload() await reload()
print("Bot has been restarted. Give it a few seconds.") print("Bot has been restarted. Give it a few seconds.")
@plex_commands.command(name="removerole", description="Stop adding users with a role to Plex") @plex_commands.command(name="removerole", description="Stop adding users with a role to Plex")
@app_commands.checks.has_permissions(administrator=True) @app_commands.checks.has_permissions(administrator=True)
async def plexroleremove(interaction: discord.Interaction, role: discord.Role): async def plexroleremove(interaction: discord.Interaction, role: discord.Role):
@@ -93,9 +98,10 @@ async def plexroleremove(interaction: discord.Interaction, role: discord.Role):
await embederror(interaction.response, f"\"{role.name}\" is currently not a Plex role.") await embederror(interaction.response, f"\"{role.name}\" is currently not a Plex role.")
return return
plex_roles.remove(role.name) plex_roles.remove(role.name)
confighelper.change_config("jellyfin_roles", ",".join(plex_roles)) confighelper.change_config("plex_roles", ",".join(plex_roles))
await interaction.response.send_message(f"Membarr will stop auto-adding \"{role.name}\" to Plex", ephemeral=True) 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") @plex_commands.command(name="listroles", description="List all roles whose members will be automatically added to Plex")
@app_commands.checks.has_permissions(administrator=True) @app_commands.checks.has_permissions(administrator=True)
async def plexrolels(interaction: discord.Interaction): async def plexrolels(interaction: discord.Interaction):
@@ -104,9 +110,11 @@ async def plexrolels(interaction: discord.Interaction):
", ".join(plex_roles), ephemeral=True ", ".join(plex_roles), ephemeral=True
) )
@plex_commands.command(name="setup", description="Setup Plex integration") @plex_commands.command(name="setup", description="Setup Plex integration")
@app_commands.checks.has_permissions(administrator=True) @app_commands.checks.has_permissions(administrator=True)
async def setupplex(interaction: discord.Interaction, username: str, password: str, server_name: str, base_url: str = "", save_token: bool = True): async def setupplex(interaction: discord.Interaction, username: str, password: str, server_name: str,
base_url: str = "", save_token: bool = True):
await interaction.response.defer() await interaction.response.defer()
try: try:
account = MyPlexAccount(username, password) account = MyPlexAccount(username, password)
@@ -115,11 +123,11 @@ async def setupplex(interaction: discord.Interaction, username: str, password: s
if str(e).startswith("(429)"): if str(e).startswith("(429)"):
await embederror(interaction.followup, "Too many requests. Please try again later.") await embederror(interaction.followup, "Too many requests. Please try again later.")
return return
await embederror(interaction.followup, "Could not connect to Plex server. Please check your credentials.") await embederror(interaction.followup, "Could not connect to Plex server. Please check your credentials.")
return return
if (save_token): if (save_token):
# Save new config entries # Save new config entries
confighelper.change_config("plex_base_url", plex._baseurl if base_url == "" else base_url) confighelper.change_config("plex_base_url", plex._baseurl if base_url == "" else base_url)
confighelper.change_config("plex_token", plex._token) confighelper.change_config("plex_token", plex._token)
@@ -138,7 +146,6 @@ async def setupplex(interaction: discord.Interaction, username: str, password: s
confighelper.change_config("plex_base_url", "") confighelper.change_config("plex_base_url", "")
confighelper.change_config("plex_token", "") confighelper.change_config("plex_token", "")
print("Plex authentication details updated. Restarting bot.") print("Plex authentication details updated. Restarting bot.")
await interaction.followup.send( await interaction.followup.send(
"Plex authentication details updated. Restarting bot. Please wait.\n" + "Plex authentication details updated. Restarting bot. Please wait.\n" +
@@ -148,7 +155,8 @@ async def setupplex(interaction: discord.Interaction, username: str, password: s
await reload() await reload()
print("Bot has been restarted. Give it a few seconds.") 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")
@jellyfin_commands.command(name="addrole", description="Add a role to automatically add users to Jellyfin")
@app_commands.checks.has_permissions(administrator=True) @app_commands.checks.has_permissions(administrator=True)
async def jellyroleadd(interaction: discord.Interaction, role: discord.Role): async def jellyroleadd(interaction: discord.Interaction, role: discord.Role):
if len(jellyfin_roles) <= maxroles: if len(jellyfin_roles) <= maxroles:
@@ -160,11 +168,13 @@ async def jellyroleadd(interaction: discord.Interaction, role: discord.Role):
jellyfin_roles.append(role.name) jellyfin_roles.append(role.name)
saveroles = ",".join(jellyfin_roles) saveroles = ",".join(jellyfin_roles)
confighelper.change_config("jellyfin_roles", saveroles) confighelper.change_config("jellyfin_roles", saveroles)
await interaction.response.send_message("Updated Jellyfin roles. Bot is restarting. Please wait a few seconds.", ephemeral=True) await interaction.response.send_message("Updated Jellyfin roles. Bot is restarting. Please wait a few seconds.",
ephemeral=True)
print("Jellyfin roles updated. Restarting bot.") print("Jellyfin roles updated. Restarting bot.")
await reload() await reload()
print("Bot has been restarted. Give it a few seconds.") print("Bot has been restarted. Give it a few seconds.")
@jellyfin_commands.command(name="removerole", description="Stop adding users with a role to Jellyfin") @jellyfin_commands.command(name="removerole", description="Stop adding users with a role to Jellyfin")
@app_commands.checks.has_permissions(administrator=True) @app_commands.checks.has_permissions(administrator=True)
async def jellyroleremove(interaction: discord.Interaction, role: discord.Role): async def jellyroleremove(interaction: discord.Interaction, role: discord.Role):
@@ -173,9 +183,12 @@ async def jellyroleremove(interaction: discord.Interaction, role: discord.Role):
return return
jellyfin_roles.remove(role.name) jellyfin_roles.remove(role.name)
confighelper.change_config("jellyfin_roles", ",".join(jellyfin_roles)) confighelper.change_config("jellyfin_roles", ",".join(jellyfin_roles))
await interaction.response.send_message(f"Membarr will stop auto-adding \"{role.name}\" to Jellyfin", ephemeral=True) 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")
@jellyfin_commands.command(name="listroles",
description="List all roles whose members will be automatically added to Jellyfin")
@app_commands.checks.has_permissions(administrator=True) @app_commands.checks.has_permissions(administrator=True)
async def jellyrolels(interaction: discord.Interaction): async def jellyrolels(interaction: discord.Interaction):
await interaction.response.send_message( await interaction.response.send_message(
@@ -183,6 +196,7 @@ async def jellyrolels(interaction: discord.Interaction):
", ".join(jellyfin_roles), ephemeral=True ", ".join(jellyfin_roles), ephemeral=True
) )
@jellyfin_commands.command(name="setup", description="Setup Jellyfin integration") @jellyfin_commands.command(name="setup", description="Setup Jellyfin integration")
@app_commands.checks.has_permissions(administrator=True) @app_commands.checks.has_permissions(administrator=True)
async def setupjelly(interaction: discord.Interaction, server_url: str, api_key: str, external_url: str = None): async def setupjelly(interaction: discord.Interaction, server_url: str, api_key: str, external_url: str = None):
@@ -207,9 +221,11 @@ async def setupjelly(interaction: discord.Interaction, server_url: str, api_key:
await embederror(interaction.followup, "Server endpoint provided was not found") await embederror(interaction.followup, "Server endpoint provided was not found")
return return
else: else:
await embederror(interaction.followup, "Unknown error occurred while connecting to Jellyfin. Check Membarr logs.") await embederror(interaction.followup,
"Unknown error occurred while connecting to Jellyfin. Check Membarr logs.")
except ConnectTimeout as e: except ConnectTimeout as e:
await embederror(interaction.followup, "Connection to server timed out. Check that Jellyfin is online and reachable.") await embederror(interaction.followup,
"Connection to server timed out. Check that Jellyfin is online and reachable.")
return return
except Exception as e: except Exception as e:
print("Exception while testing Jellyfin connection") print("Exception while testing Jellyfin connection")
@@ -217,7 +233,7 @@ async def setupjelly(interaction: discord.Interaction, server_url: str, api_key:
print(e) print(e)
await embederror(interaction.followup, "Unknown exception while connecting to Jellyfin. Check Membarr logs") await embederror(interaction.followup, "Unknown exception while connecting to Jellyfin. Check Membarr logs")
return return
confighelper.change_config("jellyfin_server_url", str(server_url)) confighelper.change_config("jellyfin_server_url", str(server_url))
confighelper.change_config("jellyfin_api_key", str(api_key)) confighelper.change_config("jellyfin_api_key", str(api_key))
if external_url is not None: if external_url is not None:
@@ -232,34 +248,38 @@ async def setupjelly(interaction: discord.Interaction, server_url: str, api_key:
@plex_commands.command(name="setuplibs", description="Setup libraries that new users can access") @plex_commands.command(name="setuplibs", description="Setup libraries that new users can access")
@app_commands.checks.has_permissions(administrator=True) @app_commands.checks.has_permissions(administrator=True)
async def setupplexlibs(interaction: discord.Interaction, libraries:str): async def setupplexlibs(interaction: discord.Interaction, libraries: str):
if not libraries: if not libraries:
await embederror(interaction.response, "libraries string is empty.") await embederror(interaction.response, "libraries string is empty.")
return return
else: else:
# Do some fancy python to remove spaces from libraries string, but only where wanted. # Do some fancy python to remove spaces from libraries string, but only where wanted.
libraries = ",".join(list(map(lambda lib: lib.strip(),libraries.split(",")))) libraries = ",".join(list(map(lambda lib: lib.strip(), libraries.split(","))))
confighelper.change_config("plex_libs", str(libraries)) confighelper.change_config("plex_libs", str(libraries))
print("Plex libraries updated. Restarting bot. Please wait.") print("Plex libraries updated. Restarting bot. Please wait.")
await interaction.response.send_message("Plex libraries updated. Please wait a few seconds for bot to restart.", ephemeral=True) await interaction.response.send_message("Plex libraries updated. Please wait a few seconds for bot to restart.",
ephemeral=True)
await reload() await reload()
print("Bot has been restarted. Give it a few seconds.") print("Bot has been restarted. Give it a few seconds.")
@jellyfin_commands.command(name="setuplibs", description="Setup libraries that new users can access") @jellyfin_commands.command(name="setuplibs", description="Setup libraries that new users can access")
@app_commands.checks.has_permissions(administrator=True) @app_commands.checks.has_permissions(administrator=True)
async def setupjellylibs(interaction: discord.Interaction, libraries:str): async def setupjellylibs(interaction: discord.Interaction, libraries: str):
if not libraries: if not libraries:
await embederror(interaction.response, "libraries string is empty.") await embederror(interaction.response, "libraries string is empty.")
return return
else: else:
# Do some fancy python to remove spaces from libraries string, but only where wanted. # Do some fancy python to remove spaces from libraries string, but only where wanted.
libraries = ",".join(list(map(lambda lib: lib.strip(),libraries.split(",")))) libraries = ",".join(list(map(lambda lib: lib.strip(), libraries.split(","))))
confighelper.change_config("jellyfin_libs", str(libraries)) confighelper.change_config("jellyfin_libs", str(libraries))
print("Jellyfin libraries updated. Restarting bot. Please wait.") 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) await interaction.response.send_message(
"Jellyfin libraries updated. Please wait a few seconds for bot to restart.", ephemeral=True)
await reload() await reload()
print("Bot has been restarted. Give it a few seconds.") print("Bot has been restarted. Give it a few seconds.")
# Enable / Disable Plex integration # Enable / Disable Plex integration
@plex_commands.command(name="enable", description="Enable auto-adding users to Plex") @plex_commands.command(name="enable", description="Enable auto-adding users to Plex")
@app_commands.checks.has_permissions(administrator=True) @app_commands.checks.has_permissions(administrator=True)
@@ -274,6 +294,7 @@ async def enableplex(interaction: discord.Interaction):
await interaction.response.send_message("Plex enabled. Restarting server. Give it a few seconds.", ephemeral=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.") print("Bot has restarted. Give it a few seconds.")
@plex_commands.command(name="disable", description="Disable adding users to Plex") @plex_commands.command(name="disable", description="Disable adding users to Plex")
@app_commands.checks.has_permissions(administrator=True) @app_commands.checks.has_permissions(administrator=True)
async def disableplex(interaction: discord.Interaction): async def disableplex(interaction: discord.Interaction):
@@ -287,6 +308,7 @@ async def disableplex(interaction: discord.Interaction):
await interaction.response.send_message("Plex disabled. Restarting server. Give it a few seconds.", ephemeral=True) 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.") print("Bot has restarted. Give it a few seconds.")
# Enable / Disable Jellyfin integration # Enable / Disable Jellyfin integration
@jellyfin_commands.command(name="enable", description="Enable adding users to Jellyfin") @jellyfin_commands.command(name="enable", description="Enable adding users to Jellyfin")
@app_commands.checks.has_permissions(administrator=True) @app_commands.checks.has_permissions(administrator=True)
@@ -298,10 +320,12 @@ async def enablejellyfin(interaction: discord.Interaction):
print("Jellyfin enabled, reloading server") print("Jellyfin enabled, reloading server")
confighelper.USE_JELLYFIN = True confighelper.USE_JELLYFIN = True
await reload() await reload()
await interaction.response.send_message("Jellyfin enabled. Restarting server. Give it a few seconds.", ephemeral=True) 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.") print("Bot has restarted. Give it a few seconds.")
@jellyfin_commands.command(name="disable", description = "Disable adding users to Jellyfin")
@jellyfin_commands.command(name="disable", description="Disable adding users to Jellyfin")
@app_commands.checks.has_permissions(administrator=True) @app_commands.checks.has_permissions(administrator=True)
async def disablejellyfin(interaction: discord.Interaction): async def disablejellyfin(interaction: discord.Interaction):
if not confighelper.USE_JELLYFIN: if not confighelper.USE_JELLYFIN:
@@ -311,11 +335,12 @@ async def disablejellyfin(interaction: discord.Interaction):
print("Jellyfin disabled, reloading server") print("Jellyfin disabled, reloading server")
await reload() await reload()
confighelper.USE_JELLYFIN = False confighelper.USE_JELLYFIN = False
await interaction.response.send_message("Jellyfin disabled. Restarting server. Give it a few seconds.", ephemeral=True) 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.") print("Bot has restarted. Give it a few seconds.")
bot.tree.add_command(plex_commands) bot.tree.add_command(plex_commands)
bot.tree.add_command(jellyfin_commands) bot.tree.add_command(jellyfin_commands)
bot.run(Discord_bot_token) bot.run(Discord_bot_token)