Plex setup and roles setup now thru bot
This commit is contained in:
10
Dockerfile
Normal file
10
Dockerfile
Normal file
@@ -0,0 +1,10 @@
|
||||
FROM python:3.6-slim
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
RUN apt-get clean \
|
||||
&& apt-get -y update
|
||||
RUN apt-get -y install nginx \
|
||||
&& apt-get -y install python3-dev \
|
||||
&& apt-get -y install build-essential
|
||||
RUN pip install -Ur requirements.txt
|
||||
CMD ["python", "Run.py"]
|
||||
@@ -1,22 +1,48 @@
|
||||
import logging
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
import asyncio
|
||||
from plexapi.myplex import MyPlexAccount
|
||||
from discord import Webhook, AsyncWebhookAdapter
|
||||
from app.bot.helper.confighelper import roles, PLEXUSER, PLEXPASS, PLEX_SERVER_NAME, Plex_LIBS
|
||||
logging.basicConfig(filename="app/config/invitarr.log", filemode='a', level=logging.ERROR)
|
||||
import app.bot.helper.db as db
|
||||
import app.bot.helper.plexhelper as plexhelper
|
||||
import texttable
|
||||
import os
|
||||
from os import path
|
||||
import configparser
|
||||
CONFIG_PATH = 'app/config/config.ini'
|
||||
BOT_SECTION = 'bot_envs'
|
||||
|
||||
# settings
|
||||
roles = None
|
||||
PLEXUSER = ""
|
||||
PLEXPASS = ""
|
||||
PLEX_SERVER_NAME = ""
|
||||
Plex_LIBS = ["all"]
|
||||
|
||||
if(path.exists('app/config/config.ini')):
|
||||
try:
|
||||
config = configparser.ConfigParser()
|
||||
config.read(CONFIG_PATH)
|
||||
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:
|
||||
pass
|
||||
if(path.exists('app/config/config.ini')):
|
||||
try:
|
||||
roles = config.get(BOT_SECTION, 'roles')
|
||||
except:
|
||||
pass
|
||||
|
||||
try:
|
||||
account = MyPlexAccount(PLEXUSER, PLEXPASS)
|
||||
plex = account.resource(PLEX_SERVER_NAME).connect() # returns a PlexServer instance
|
||||
logging.info('Logged into plex!')
|
||||
print('Logged into plex!')
|
||||
except:
|
||||
logging.error('Error with plex login. Please check username and password and Plex server name.')
|
||||
print('Error with plex login. Please check username and password and Plex server name or setup plex in the bot.')
|
||||
|
||||
if roles is not None:
|
||||
roles = list(roles.split(','))
|
||||
|
||||
class app(commands.Cog):
|
||||
|
||||
@@ -25,9 +51,11 @@ class app(commands.Cog):
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_ready(self):
|
||||
logging.info('Made by Sleepingpirate https://github.com/Sleepingpirates/')
|
||||
logging.info(f'Logged in as {self.bot.user} (ID: {self.bot.user.id})')
|
||||
logging.info('------')
|
||||
print('Made by Sleepingpirate https://github.com/Sleepingpirates/')
|
||||
print(f'Logged in as {self.bot.user} (ID: {self.bot.user.id})')
|
||||
print('------')
|
||||
if roles is None:
|
||||
print('Configure roles to enable auto invite after a role is assigned.')
|
||||
|
||||
async def embederror(self, author, message):
|
||||
embed1 = discord.Embed(title="ERROR",description=message, color=0xf50000)
|
||||
@@ -58,10 +86,11 @@ class app(commands.Cog):
|
||||
await self.embederror(after, message)
|
||||
return None
|
||||
|
||||
|
||||
async def addtoplex(self, email, channel):
|
||||
if(plexhelper.verifyemail(email)):
|
||||
if plexhelper.plexadd(plex,email):
|
||||
await self.embedinfo(channel, 'There was an error adding this email address. Message Server Admin.')
|
||||
await self.embedinfo(channel, 'This email address has been added to plex')
|
||||
return True
|
||||
else:
|
||||
await self.embederror(channel, 'There was an error adding this email address. Check logs.')
|
||||
@@ -73,7 +102,7 @@ class app(commands.Cog):
|
||||
async def removefromplex(self, email, channel):
|
||||
if(plexhelper.verifyemail(email)):
|
||||
if plexhelper.plexadd(plex,email):
|
||||
await self.embedinfo(channel, 'There was an error removing this email address. Message Server Admin.')
|
||||
await self.embedinfo(channel, 'This email address has been removed from plex.')
|
||||
return True
|
||||
else:
|
||||
await self.embederror(channel, 'There was an error removing this email address. Check logs.')
|
||||
@@ -81,11 +110,11 @@ class app(commands.Cog):
|
||||
else:
|
||||
await self.embederror(channel, 'Invalid email.')
|
||||
return False
|
||||
|
||||
#Auto add or remove user from plex if role is given or taken.
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_member_update(self, before, after):
|
||||
if roles is None:
|
||||
return
|
||||
roles_in_guild = after.guild.roles
|
||||
role = None
|
||||
for role_for_app in roles:
|
||||
@@ -112,25 +141,23 @@ class app(commands.Cog):
|
||||
plexremove(email)
|
||||
deleted = db.delete_user(user_id)
|
||||
if deleted:
|
||||
logging.info("Removed {} from db".format(email))
|
||||
print("Removed {} from db".format(email))
|
||||
#await secure.send(plexname + ' ' + after.mention + ' was removed from plex')
|
||||
else:
|
||||
logging.error("Cannot remove this user from db.")
|
||||
print("Cannot remove this user from db.")
|
||||
except:
|
||||
logging.error("Cannot remove this user from plex.")
|
||||
print("Cannot remove this user from plex.")
|
||||
return
|
||||
|
||||
|
||||
@commands.has_permissions(administrator=True)
|
||||
@commands.command()
|
||||
async def plexinvite(self, ctx):
|
||||
email = str(ctx.content)
|
||||
async def plexinvite(self, ctx, email):
|
||||
await self.addtoplex(email, ctx.channel)
|
||||
|
||||
@commands.has_permissions(administrator=True)
|
||||
@commands.command()
|
||||
async def plexremove(self, ctx):
|
||||
email = str(ctx.content)
|
||||
async def plexremove(self, ctx, email):
|
||||
await self.removefromplex(email, ctx.channel)
|
||||
|
||||
@commands.has_permissions(administrator=True)
|
||||
@@ -143,7 +170,7 @@ class app(commands.Cog):
|
||||
await self.embedinfo(ctx.channel,'email and user were added to the database.')
|
||||
except Exception as e:
|
||||
await self.embedinfo(ctx.channel, 'There was an error adding this email address to database.')
|
||||
logging.error(e)
|
||||
print(e)
|
||||
else:
|
||||
await self.embederror(ctx.channel, 'Invalid email.')
|
||||
|
||||
@@ -209,12 +236,12 @@ class app(commands.Cog):
|
||||
email = db.get_useremail(id)
|
||||
deleted = db.delete_user(id)
|
||||
if deleted:
|
||||
logging.info("Removed {} from db".format(email))
|
||||
print("Removed {} from db".format(email))
|
||||
await self.embedinfo(ctx.channel,"Removed {} from db".format(email))
|
||||
else:
|
||||
await self.embederror(ctx.channel,"Cannot remove this user from db.")
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
print(e)
|
||||
|
||||
def setup(bot):
|
||||
bot.add_cog(app(bot))
|
||||
@@ -1,14 +1,65 @@
|
||||
import configparser
|
||||
import os
|
||||
from os import environ, path
|
||||
from dotenv import load_dotenv
|
||||
config = configparser.ConfigParser()
|
||||
|
||||
CONFIG_PATH = 'app/config/config.ini'
|
||||
BOT_SECTION = 'bot_envs'
|
||||
config = configparser.ConfigParser()
|
||||
|
||||
CONFIG_KEYS = ['username', 'password', 'discord_bot_token', 'plex_user', 'plex_pass',
|
||||
'roles', 'plex_server_name', 'plex_libs', 'owner_id', 'channel_id',
|
||||
'auto_remove_user']
|
||||
|
||||
# settings
|
||||
Discord_bot_token = ""
|
||||
roles = None
|
||||
PLEXUSER = ""
|
||||
PLEXPASS = ""
|
||||
PLEX_SERVER_NAME = ""
|
||||
Plex_LIBS = ["all"]
|
||||
switch = 0
|
||||
|
||||
|
||||
if(path.exists('bot.env')):
|
||||
try:
|
||||
load_dotenv(dotenv_path='bot.env')
|
||||
# settings
|
||||
Discord_bot_token = environ.get('discord_bot_token')
|
||||
switch = 1
|
||||
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
elif(path.exists('app/config/config.ini')):
|
||||
try:
|
||||
config = configparser.ConfigParser()
|
||||
config.read(CONFIG_PATH)
|
||||
Discord_bot_token = config.get(BOT_SECTION, 'discord_bot_token')
|
||||
except:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
Discord_bot_token = str(os.environ['token'])
|
||||
switch = 1
|
||||
except Exception as e:
|
||||
print("ERROR. No config found.")
|
||||
|
||||
if(path.exists('app/config/config.ini')):
|
||||
try:
|
||||
config = configparser.ConfigParser()
|
||||
config.read(CONFIG_PATH)
|
||||
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:
|
||||
pass
|
||||
|
||||
if(path.exists('app/config/config.ini')):
|
||||
try:
|
||||
roles = config.get(BOT_SECTION, 'roles')
|
||||
except:
|
||||
pass
|
||||
|
||||
def get_config():
|
||||
"""
|
||||
Function to return current config
|
||||
@@ -22,40 +73,26 @@ def get_config():
|
||||
return None
|
||||
|
||||
|
||||
CONFIG_PATH = 'app/config/config.ini'
|
||||
BOT_SECTION = 'bot_envs'
|
||||
def change_config(key, value):
|
||||
"""
|
||||
Function to change the key, value pair in config
|
||||
"""
|
||||
try:
|
||||
config = configparser.ConfigParser()
|
||||
config.read(CONFIG_PATH)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print("Cannot Read config.")
|
||||
|
||||
# settings
|
||||
Discord_bot_token = ""
|
||||
roles = ""
|
||||
PLEXUSER = ""
|
||||
PLEXPASS = ""
|
||||
PLEX_SERVER_NAME = ""
|
||||
Plex_LIBS = ""
|
||||
chan = 0
|
||||
ownerid = 0
|
||||
auto_remove_user = ""
|
||||
|
||||
switch = 0
|
||||
|
||||
try:
|
||||
load_dotenv(dotenv_path='bot.env')
|
||||
|
||||
# settings
|
||||
Discord_bot_token = environ.get('discord_bot_token')
|
||||
roles = (environ.get('roles')) # Role Id, right click the role and copy id.
|
||||
PLEXUSER = environ.get('plex_user') # Plex Username
|
||||
PLEXPASS = environ.get('plex_pass') # plex password
|
||||
PLEX_SERVER_NAME = environ.get('plex_server_name') # Name of plex server
|
||||
Plex_LIBS = environ.get('plex_libs') #name of the libraries you want the user to have access to.
|
||||
chan = int(environ.get('channel_id'))
|
||||
ownerid = int(environ.get('owner_id'))
|
||||
auto_remove_user = environ.get('auto_remove_user') if environ.get('auto_remove_user') else False # auto remove user from plex and db if removed from the role
|
||||
switch = 1
|
||||
if switch == 1:
|
||||
Plex_LIBS = list(Plex_LIBS.split(','))
|
||||
roles = list(roles.split(','))
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
try:
|
||||
config.set(BOT_SECTION, key, str(value))
|
||||
except Exception as e:
|
||||
config.add_section(BOT_SECTION)
|
||||
config.set(BOT_SECTION, key, str(value))
|
||||
|
||||
try:
|
||||
with open(CONFIG_PATH, 'w') as configfile:
|
||||
config.write(configfile)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print("Cannot write to config.")
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
from plexapi.myplex import MyPlexAccount
|
||||
import re
|
||||
from app.bot.helper.confighelper import Plex_LIBS
|
||||
import logging
|
||||
logging.basicConfig(filename="app/config/plex.log", filemode='a', level=logging.ERROR)
|
||||
|
||||
def plexadd(plex, plexname):
|
||||
global Plex_LIBS
|
||||
@@ -12,25 +10,25 @@ def plexadd(plex, plexname):
|
||||
plex.myPlexAccount().inviteFriend(user=plexname, server=plex, sections=Plex_LIBS, allowSync=False,
|
||||
allowCameraUpload=False, allowChannels=False, filterMovies=None,
|
||||
filterTelevision=None, filterMusic=None)
|
||||
logging.info(plexname +' has been added to plex')
|
||||
print(plexname +' has been added to plex')
|
||||
return True
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
print(e)
|
||||
return False
|
||||
|
||||
def plexremove(plex, plexname):
|
||||
try:
|
||||
plex.myPlexAccount().removeFriend(user=plexname)
|
||||
logging.info(plexname +' has been removed from plex')
|
||||
print(plexname +' has been removed from plex')
|
||||
return True
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
print(e)
|
||||
return False
|
||||
'''
|
||||
|
||||
plex python api has no tools to remove unaccepted invites...
|
||||
|
||||
logging.info("Trying to remove invite...")
|
||||
print("Trying to remove invite...")
|
||||
removeinvite = plexremoveinvite(plex, plexname)
|
||||
if removeinvite:
|
||||
return True
|
||||
@@ -40,10 +38,10 @@ def plexremove(plex, plexname):
|
||||
def plexremoveinvite(plex, plexname):
|
||||
try:
|
||||
plex.myPlexAccount().removeFriend(user=plexname)
|
||||
logging.info(plexname +' has been removed from plex')
|
||||
print(plexname +' has been removed from plex')
|
||||
return True
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
print(e)
|
||||
return False
|
||||
'''
|
||||
def verifyemail(addressToVerify):
|
||||
|
||||
9
bot.env
9
bot.env
@@ -1,8 +1 @@
|
||||
discord_bot_token=
|
||||
plex_username=
|
||||
plex_password=
|
||||
plex_server_name=
|
||||
plex_libs=
|
||||
owner_id=
|
||||
role_id=
|
||||
channel_id=
|
||||
discord_bot_token=
|
||||
@@ -0,0 +1,27 @@
|
||||
aiohttp
|
||||
async-timeout==3.0.1
|
||||
attrs==19.3.0
|
||||
bcrypt==3.1.7
|
||||
certifi==2020.4.5.2
|
||||
cffi==1.14.0
|
||||
chardet==3.0.4
|
||||
click==7.1.2
|
||||
discord.py
|
||||
idna==2.9
|
||||
idna-ssl==1.1.0
|
||||
itsdangerous==1.1.0
|
||||
Jinja2
|
||||
MarkupSafe==1.1.1
|
||||
multidict==4.7.6
|
||||
plex.py==0.9.0
|
||||
PlexAPI==4.0.0
|
||||
pycparser==2.20
|
||||
python-dotenv==0.13.0
|
||||
requests==2.23.0
|
||||
six==1.15.0
|
||||
typing-extensions==3.7.4.2
|
||||
urllib3
|
||||
websockets
|
||||
WTForms==2.3.1
|
||||
yarl==1.4.2
|
||||
texttable
|
||||
100
run.py
100
run.py
@@ -4,7 +4,14 @@ from discord.ext import commands, tasks
|
||||
from discord.utils import get
|
||||
import asyncio
|
||||
import sys
|
||||
from app.bot.helper.confighelper import switch, Discord_bot_token
|
||||
from app.bot.helper.confighelper import switch, Discord_bot_token, roles
|
||||
import app.bot.helper.confighelper as confighelper
|
||||
maxroles = 10
|
||||
|
||||
if roles is None:
|
||||
roles = []
|
||||
else:
|
||||
roles = list(roles.split(','))
|
||||
|
||||
if switch == 0:
|
||||
print("Missing Config.")
|
||||
@@ -20,28 +27,6 @@ async def on_ready():
|
||||
print("bot is online.")
|
||||
|
||||
|
||||
@bot.command()
|
||||
@commands.has_permissions(administrator=True)
|
||||
async def load(ctx, name):
|
||||
bot.load_extension(f'app.bot.cogs.{name}')
|
||||
print(f"The {name} cog has been loaded successfully.")
|
||||
|
||||
|
||||
@bot.command()
|
||||
@commands.has_permissions(administrator=True)
|
||||
async def unload(ctx, name):
|
||||
bot.unload_extension(f'app.bot.cogs.{name}')
|
||||
print(f"The {name} cog has been unloaded successfully.")
|
||||
|
||||
|
||||
@bot.command()
|
||||
@commands.has_permissions(administrator=True)
|
||||
async def reload(ctx, name):
|
||||
bot.unload_extension(f'app.bot.cogs.{name}')
|
||||
bot.load_extension(f'app.bot.cogs.{name}')
|
||||
print(f"The {name} cog has been reloaded successfully.")
|
||||
|
||||
|
||||
@bot.event
|
||||
async def on_message(message):
|
||||
if message.author.id == bot.user.id:
|
||||
@@ -50,20 +35,61 @@ async def on_message(message):
|
||||
return
|
||||
await bot.process_commands(message)
|
||||
|
||||
def reload():
|
||||
bot.reload_extension(f'app.bot.cogs.app')
|
||||
|
||||
async def getplex(ctx, type):
|
||||
username = None
|
||||
await ctx.author.send("Please reply with your Plex {}:".format(type))
|
||||
while(username == None):
|
||||
def check(m):
|
||||
return m.author == ctx.author and not m.guild
|
||||
try:
|
||||
username = await bot.wait_for('message', timeout=200, check=check)
|
||||
return username.content
|
||||
except asyncio.TimeoutError:
|
||||
message = "Timed Out. Try again."
|
||||
return None
|
||||
|
||||
@bot.command()
|
||||
@commands.has_permissions(administrator=True)
|
||||
async def roleadd(ctx, role: discord.Role):
|
||||
if len(roles) <= maxroles:
|
||||
roles.append(role.name)
|
||||
saveroles = ",".join(roles)
|
||||
confighelper.change_config("roles", saveroles)
|
||||
await ctx.author.send("Updated roles. Bot is restarting.")
|
||||
print("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()
|
||||
@commands.has_permissions(administrator=True)
|
||||
async def all(ctx):
|
||||
for filename in os.listdir("app/bot/cogs/"):
|
||||
if filename.endswith('.py'):
|
||||
bot.unload_extension(f'app.bot.cogs.{filename[:-3]}')
|
||||
for filename in os.listdir("app/bot/cogs/"):
|
||||
if filename.endswith('.py'):
|
||||
bot.load_extension(f'app.bot.cogs.{filename[:-3]}')
|
||||
print("All cogs has been reloaded.")
|
||||
|
||||
|
||||
for filename in os.listdir("app/bot/cogs/"):
|
||||
if filename.endswith('.py'):
|
||||
bot.load_extension(f'app.bot.cogs.{filename[:-3]}')
|
||||
|
||||
async def setupplex(ctx):
|
||||
username = ""
|
||||
pasword = ""
|
||||
servername = ""
|
||||
username = await getplex(ctx, "username")
|
||||
if username is None:
|
||||
return
|
||||
else:
|
||||
password = await getplex(ctx, "password")
|
||||
if password is None:
|
||||
return
|
||||
else:
|
||||
servername = await getplex(ctx, "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.")
|
||||
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.")
|
||||
|
||||
bot.load_extension(f'app.bot.cogs.app')
|
||||
bot.run(Discord_bot_token)
|
||||
Reference in New Issue
Block a user