feat: implements slash commands
Implements slash commands. task: gh-1
This commit is contained in:
286
run.py
286
run.py
@@ -1,68 +1,59 @@
|
||||
from pydoc import describe
|
||||
import discord
|
||||
import os
|
||||
from discord.ext import commands, tasks
|
||||
from discord.utils import get
|
||||
from discord.ui import Button, View, Select
|
||||
from discord import app_commands
|
||||
import asyncio
|
||||
import sys
|
||||
from app.bot.helper.confighelper import switch, Discord_bot_token, plex_roles, jellyfin_roles
|
||||
from app.bot.helper.confighelper import GUILD_ID, MEMBARR_VERSION, switch, Discord_bot_token, plex_roles, jellyfin_roles
|
||||
import app.bot.helper.confighelper as confighelper
|
||||
import app.bot.helper.jellyfinhelper as jelly
|
||||
from app.bot.helper.message import *
|
||||
maxroles = 10
|
||||
|
||||
print(f"Discord Bot Token: {Discord_bot_token}")
|
||||
|
||||
if plex_roles is None:
|
||||
plex_roles = []
|
||||
else:
|
||||
plex_roles = list(plex_roles.split(','))
|
||||
|
||||
if jellyfin_roles is None:
|
||||
jellyfin_roles = []
|
||||
else:
|
||||
jellyfin_roles = list(jellyfin_roles.split(','))
|
||||
|
||||
if switch == 0:
|
||||
print("Missing Config.")
|
||||
sys.exit()
|
||||
|
||||
print("V 1.1")
|
||||
print(f"V {MEMBARR_VERSION}")
|
||||
|
||||
intents = discord.Intents.default()
|
||||
intents.members = True
|
||||
bot = commands.Bot(command_prefix=".", intents = intents)
|
||||
bot.remove_command('help')
|
||||
class Bot(commands.Bot):
|
||||
def __init__(self) -> None:
|
||||
print("bot init")
|
||||
intents = discord.Intents.default()
|
||||
intents.members = True
|
||||
super().__init__(command_prefix=".", intents=intents)
|
||||
|
||||
@bot.event
|
||||
async def on_ready():
|
||||
print("bot is online.")
|
||||
async def on_ready(self):
|
||||
print("bot is online.")
|
||||
for guild in self.guilds:
|
||||
print("Syncing commands to " + guild.name)
|
||||
self.tree.copy_global_to(guild=guild)
|
||||
await self.tree.sync(guild=guild)
|
||||
|
||||
async def on_guild_join(self, guild):
|
||||
print(f"Joined guild {guild.name}")
|
||||
print(f"Syncing commands to {guild.name}")
|
||||
|
||||
async def setup_hook(self):
|
||||
print("Loading media server connectors")
|
||||
await self.load_extension(f'app.bot.cogs.app')
|
||||
|
||||
bot = Bot()
|
||||
|
||||
@bot.event
|
||||
async def on_message(message):
|
||||
if message.author.id == bot.user.id:
|
||||
return
|
||||
if not message.guild:
|
||||
return
|
||||
await bot.process_commands(message)
|
||||
async def reload():
|
||||
await bot.reload_extension(f'app.bot.cogs.app')
|
||||
|
||||
# these were copied from the app object. They could be made static instead but I'm lazy.
|
||||
async def embederror(author, message):
|
||||
embed1 = discord.Embed(title="ERROR",description=message, color=0xf50000)
|
||||
await author.send(embed=embed1)
|
||||
|
||||
async def embedinfo(author, message):
|
||||
embed1 = discord.Embed(title=message, color=0x00F500)
|
||||
await author.send(embed=embed1)
|
||||
|
||||
def reload():
|
||||
bot.reload_extension(f'app.bot.cogs.app')
|
||||
|
||||
async def getuser(ctx, server, type):
|
||||
async def getuser(interaction, server, type):
|
||||
value = None
|
||||
await ctx.author.send("Please reply with your {} {}:".format(server, type))
|
||||
await interaction.user.send("Please reply with your {} {}:".format(server, type))
|
||||
while(value == None):
|
||||
def check(m):
|
||||
return m.author == ctx.author and not m.guild
|
||||
return m.author == interaction.user and not m.guild
|
||||
try:
|
||||
value = await bot.wait_for('message', timeout=200, check=check)
|
||||
return value.content
|
||||
@@ -70,171 +61,224 @@ async def getuser(ctx, server, type):
|
||||
message = "Timed Out. Try again."
|
||||
return None
|
||||
|
||||
@bot.command()
|
||||
plex_commands = app_commands.Group(name="plexsettings", description="Membarr Plex 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")
|
||||
@commands.has_permissions(administrator=True)
|
||||
async def plexroleadd(ctx, role: discord.Role):
|
||||
async def plexroleadd(interaction: discord.Interaction, role: discord.Role):
|
||||
if len(plex_roles) <= maxroles:
|
||||
# Do not add roles multiple times.
|
||||
if role.name in plex_roles:
|
||||
await embederror(interaction.response, f"Plex role \"{role.name}\" already added.")
|
||||
return
|
||||
|
||||
plex_roles.append(role.name)
|
||||
saveroles = ",".join(plex_roles)
|
||||
plex_button = Button(label = "Plex")
|
||||
view = View()
|
||||
view.add_item(plex_button)
|
||||
confighelper.change_config("plex_roles", saveroles)
|
||||
await ctx.author.send("Updated Plex roles. Bot is restarting. Please wait.")
|
||||
print("Plex roles updated. Restarting bot.")
|
||||
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 ctx.author.send("Bot has been restarted. Give it a few seconds.")
|
||||
print("Bot has been restarted. Give it a few seconds.")
|
||||
|
||||
@bot.command()
|
||||
@plex_commands.command(name="removerole", description="Stop adding users with a role to Plex")
|
||||
@commands.has_permissions(administrator=True)
|
||||
async def setupplex(ctx):
|
||||
username = None
|
||||
password = None
|
||||
servername = None
|
||||
username = await getuser(ctx, "Plex", "username")
|
||||
if username is None:
|
||||
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.")
|
||||
return
|
||||
else:
|
||||
password = await getuser(ctx, "Plex", "password")
|
||||
if password is None:
|
||||
return
|
||||
else:
|
||||
servername = await getuser(ctx, "Plex", "servername")
|
||||
if servername is None:
|
||||
return
|
||||
else:
|
||||
confighelper.change_config("plex_user", str(username))
|
||||
confighelper.change_config("plex_pass", str(password))
|
||||
confighelper.change_config("plex_server_name", str(servername))
|
||||
print("Plex username, password, and plex server name updated. Restarting bot.")
|
||||
await ctx.author.send("Plex username, password, and plex server name updated. Restarting bot. Please wait.")
|
||||
reload()
|
||||
await ctx.author.send("Bot has been restarted. Give it a few seconds. 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. ")
|
||||
print("Bot has been restarted. Give it a few seconds.")
|
||||
plex_roles.remove(role.name)
|
||||
confighelper.change_config("jellyfin_roles", ",".join(plex_roles))
|
||||
await interaction.response.send_message(f"Membarr will stop auto-adding \"{role.name}\" to Plex", ephemeral=True)
|
||||
|
||||
@bot.command()
|
||||
@plex_commands.command(name="listroles", description="List all roles whose members will be automatically added to Plex")
|
||||
@commands.has_permissions(administrator=True)
|
||||
async def jellyroleadd(ctx, role: discord.Role):
|
||||
async def plexrolels(interaction: discord.Interaction):
|
||||
await interaction.response.send_message(
|
||||
"The following roles are being automatically added to Plex:\n" +
|
||||
", ".join(plex_roles), ephemeral=True
|
||||
)
|
||||
|
||||
@plex_commands.command(name="setup", description="Setup Plex integration")
|
||||
@commands.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))
|
||||
confighelper.change_config("plex_server_name", str(server_name))
|
||||
print("Plex username, password, and plex server name updated. Restarting bot.")
|
||||
await interaction.response.send_message(
|
||||
"Plex username, password, and plex server name updated. Restarting bot. Please wait.\n" +
|
||||
"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()
|
||||
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)
|
||||
async def jellyroleadd(interaction: discord.Interaction, role: discord.Role):
|
||||
if len(jellyfin_roles) <= maxroles:
|
||||
# Do not add roles multiple times.
|
||||
if role.name in jellyfin_roles:
|
||||
await embederror(interaction.response, f"Jellyfin role \"{role.name}\" already added.")
|
||||
return
|
||||
|
||||
jellyfin_roles.append(role.name)
|
||||
print (f"new jellyfin roles: {jellyfin_roles}")
|
||||
saveroles = ",".join(jellyfin_roles)
|
||||
print (f"saveroles: {saveroles}")
|
||||
confighelper.change_config("jellyfin_roles", saveroles)
|
||||
await ctx.author.send("Updated Jellyfin roles. Bot is restarting. Please wait.")
|
||||
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 ctx.author.send("Bot has been restarted. Give it a few seconds.")
|
||||
print("Bot has been restarted. Give it a few seconds.")
|
||||
|
||||
@bot.command()
|
||||
@jellyfin_commands.command(name="removerole", description="Stop adding users with a role to Jellyfin")
|
||||
@commands.has_permissions(administrator=True)
|
||||
async def setupjelly(ctx):
|
||||
jellyfin_api_key = None
|
||||
jellyfin_server_url = None
|
||||
|
||||
jellyfin_server_url = await getuser(ctx, "Jellyfin", "Server Url")
|
||||
if jellyfin_server_url is None:
|
||||
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.")
|
||||
return
|
||||
jellyfin_roles.remove(role.name)
|
||||
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)
|
||||
|
||||
jellyfin_api_key = await getuser(ctx, "Jellyfin", "API Key")
|
||||
if jellyfin_api_key is None:
|
||||
return
|
||||
@jellyfin_commands.command(name="listroles", description="List all roles whose members will be automatically added to Jellyfin")
|
||||
@commands.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" +
|
||||
", ".join(jellyfin_roles), ephemeral=True
|
||||
)
|
||||
|
||||
@jellyfin_commands.command(name="setup", description="Setup Jellyfin integration")
|
||||
@commands.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('/')
|
||||
|
||||
try:
|
||||
server_status = jelly.get_status(jellyfin_server_url, jellyfin_api_key)
|
||||
server_status = jelly.get_status(server_url, api_key)
|
||||
if server_status == 200:
|
||||
pass
|
||||
elif server_status == 401:
|
||||
# Unauthorized
|
||||
await embederror(ctx.author, "API key provided is invalid")
|
||||
await embederror(interaction.response, "API key provided is invalid")
|
||||
return
|
||||
elif server_status == 403:
|
||||
# Forbidden
|
||||
await embederror(ctx.author, "API key provided does not have permissions")
|
||||
await embederror(interaction.response, "API key provided does not have permissions")
|
||||
return
|
||||
elif server_status == 404:
|
||||
# page not found
|
||||
await embederror(ctx.author, "Server endpoint provided was not found")
|
||||
await embederror(interaction.response, "Server endpoint provided was not found")
|
||||
return
|
||||
else:
|
||||
await embederror(interaction.response, "Unknown error occurred while connecting to server. Check Membarr logs.")
|
||||
except Exception as e:
|
||||
print("Exception while testing Jellyfin connection")
|
||||
print(e)
|
||||
await embederror(ctx.author, "Could not connect to server. Check logs for more details.")
|
||||
await embederror(interaction.response, "Could not connect to server. Check logs for more details.")
|
||||
return
|
||||
|
||||
|
||||
jellyfin_server_url = jellyfin_server_url.rstrip('/')
|
||||
confighelper.change_config("jellyfin_server_url", str(jellyfin_server_url))
|
||||
confighelper.change_config("jellyfin_api_key", str(jellyfin_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 ctx.author.send("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 ctx.author.send("Bot has been restarted. Give it a few seconds. Please check logs and make sure you see the line: `Connected to Jellyfin`. If not run this command again and make sure you enter the right values. ")
|
||||
print("Bot has been restarted. Give it a few seconds.")
|
||||
|
||||
|
||||
@bot.command()
|
||||
@plex_commands.command(name="setuplibs", description="Setup libraries that new users can access")
|
||||
@commands.has_permissions(administrator=True)
|
||||
async def setupplexlibs(ctx):
|
||||
libs = await getuser(ctx, "Plex", "libs")
|
||||
if libs is None:
|
||||
async def setupplexlibs(interaction: discord.Interaction, libraries:str):
|
||||
if not libraries:
|
||||
await embederror(interaction.response, "libraries string is empty.")
|
||||
return
|
||||
else:
|
||||
confighelper.change_config("plex_libs", str(libs))
|
||||
# Do some fancy python to remove spaces from libraries string, but only where wanted.
|
||||
libraries = ",".join(list(map(lambda lib: lib.strip(),libraries.split(","))))
|
||||
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 ctx.author.send("Bot has been restarted. Give it a few seconds.")
|
||||
print("Bot has been restarted. Give it a few seconds.")
|
||||
|
||||
@bot.command()
|
||||
@jellyfin_commands.command(name="setuplibs", description="Setup libraries that new users can access")
|
||||
@commands.has_permissions(administrator=True)
|
||||
async def setupjellylibs(ctx):
|
||||
libs = await getuser(ctx, "Jellyfin", "libs")
|
||||
if libs is None:
|
||||
async def setupjellylibs(interaction: discord.Interaction, libraries:str):
|
||||
if not libraries is None:
|
||||
await embederror(interaction.response, "libraries string is empty.")
|
||||
return
|
||||
else:
|
||||
confighelper.change_config("jellyfin_libs", str(libs))
|
||||
# Do some fancy python to remove spaces from libraries string, but only where wanted.
|
||||
libraries = ",".join(list(map(lambda lib: lib.strip(),libraries.split(","))))
|
||||
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 ctx.author.send("Bot has been restarted. Give it a few seconds.")
|
||||
print("Bot has been restarted. Give it a few seconds.")
|
||||
|
||||
# Enable / Disable Plex integration
|
||||
@bot.command(aliases=["plexenable"])
|
||||
@plex_commands.command(name="enable", description="Enable auto-adding users to Plex")
|
||||
@commands.has_permissions(administrator=True)
|
||||
async def enableplex(ctx):
|
||||
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 ctx.author.send("Bot has restarted. Give it a few seconds.")
|
||||
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.")
|
||||
|
||||
@bot.command(aliases=["plexdisable"])
|
||||
@plex_commands.command(name="disable", description="Disable adding users to Plex")
|
||||
@commands.has_permissions(administrator=True)
|
||||
async def disableplex(ctx):
|
||||
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 ctx.author.send("Bot has restarted. Give it a few seconds.")
|
||||
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
|
||||
@bot.command(aliases=["jellyenable"])
|
||||
@jellyfin_commands.command(name="enable", description="Enable adding users to Jellyfin")
|
||||
@commands.has_permissions(administrator=True)
|
||||
async def enablejellyfin(ctx):
|
||||
async def enablejellyfin(interaction: discord.Interaction):
|
||||
if confighelper.USE_JELLYFIN:
|
||||
await interaction.response.send_message("Jellyfin already enabled.", ephemeral=True)
|
||||
return
|
||||
confighelper.change_config("jellyfin_enabled", True)
|
||||
print("Jellyfin enabled, reloading server")
|
||||
confighelper.USE_JELLYFIN = True
|
||||
reload()
|
||||
await ctx.author.send("Bot has restarted. Give it a few seconds.")
|
||||
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.")
|
||||
|
||||
@bot.command(aliases=["jellydisable"])
|
||||
@jellyfin_commands.command(name="disable", description = "Disable adding users to Jellyfin")
|
||||
@commands.has_permissions(administrator=True)
|
||||
async def disablejellyfin(ctx):
|
||||
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 ctx.author.send("Bot has restarted. Give it a few seconds.")
|
||||
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.")
|
||||
|
||||
bot.load_extension(f'app.bot.cogs.app')
|
||||
|
||||
bot.tree.add_command(plex_commands)
|
||||
bot.tree.add_command(jellyfin_commands)
|
||||
|
||||
print("running bot")
|
||||
bot.run(Discord_bot_token)
|
||||
Reference in New Issue
Block a user