Plex setup and roles setup now thru bot

This commit is contained in:
Faiz Ahmed
2021-07-26 12:19:37 -04:00
parent ceb8bc32a3
commit 49c9e68706
8 changed files with 231 additions and 113 deletions

View File

@@ -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.")

View File

@@ -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):