added funcs
This commit is contained in:
61
app/bot/helper/confighelper.py
Normal file
61
app/bot/helper/confighelper.py
Normal file
@@ -0,0 +1,61 @@
|
||||
import configparser
|
||||
from os import environ, path
|
||||
from dotenv import load_dotenv
|
||||
config = configparser.ConfigParser()
|
||||
|
||||
CONFIG_PATH = 'app/config/config.ini'
|
||||
BOT_SECTION = 'bot_envs'
|
||||
CONFIG_KEYS = ['username', 'password', 'discord_bot_token', 'plex_user', 'plex_pass',
|
||||
'roles', 'plex_server_name', 'plex_libs', 'owner_id', 'channel_id',
|
||||
'auto_remove_user']
|
||||
|
||||
def get_config():
|
||||
"""
|
||||
Function to return current config
|
||||
"""
|
||||
try:
|
||||
config.read(CONFIG_PATH)
|
||||
return config
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print('error in reading config')
|
||||
return None
|
||||
|
||||
|
||||
CONFIG_PATH = 'app/config/config.ini'
|
||||
BOT_SECTION = 'bot_envs'
|
||||
|
||||
# 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)
|
||||
|
||||
83
app/bot/helper/db.py
Normal file
83
app/bot/helper/db.py
Normal file
@@ -0,0 +1,83 @@
|
||||
import sqlite3
|
||||
|
||||
DB_URL = 'app/config/app.db'
|
||||
|
||||
def create_connection(db_file):
|
||||
""" create a database connection to a SQLite database """
|
||||
conn = None
|
||||
try:
|
||||
conn = sqlite3.connect(db_file)
|
||||
print("Connected to db")
|
||||
except Error as e:
|
||||
print("error in connecting to db")
|
||||
finally:
|
||||
if conn:
|
||||
return conn
|
||||
|
||||
def checkTableExists(dbcon, tablename):
|
||||
dbcur = dbcon.cursor()
|
||||
dbcur.execute("""SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='{0}';""".format(tablename.replace('\'', '\'\'')))
|
||||
if dbcur.fetchone()[0] == 1:
|
||||
dbcur.close()
|
||||
return True
|
||||
dbcur.close()
|
||||
return False
|
||||
|
||||
conn = create_connection(DB_URL)
|
||||
|
||||
# Checking if table exists
|
||||
if checkTableExists(conn, 'clients'):
|
||||
print('Table exists.')
|
||||
else:
|
||||
conn.execute('''
|
||||
CREATE TABLE "clients" (
|
||||
"id" INTEGER NOT NULL UNIQUE,
|
||||
"discord_username" TEXT NOT NULL UNIQUE,
|
||||
"email" TEXT NOT NULL,
|
||||
PRIMARY KEY("id" AUTOINCREMENT)
|
||||
);
|
||||
''')
|
||||
|
||||
def save_user(username, email):
|
||||
if username and email:
|
||||
conn.execute("INSERT INTO clients (discord_username, email) VALUES ('"+ username +"', '" + email + "')")
|
||||
conn.commit()
|
||||
print("User added to db.")
|
||||
else:
|
||||
return "Username or email cannot be empty"
|
||||
|
||||
def get_useremail(username):
|
||||
if username:
|
||||
try:
|
||||
cursor = conn.execute('SELECT discord_username, email from clients where discord_username="{}";'.format(username))
|
||||
for row in cursor:
|
||||
email = row[1]
|
||||
if email:
|
||||
return email
|
||||
else:
|
||||
return "No users found"
|
||||
except:
|
||||
return "error in fetching from db"
|
||||
else:
|
||||
return "username cannot be empty"
|
||||
|
||||
def delete_user(username):
|
||||
if username:
|
||||
try:
|
||||
conn.execute('DELETE from clients where discord_username="{}";'.format(username))
|
||||
conn.commit()
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
else:
|
||||
return "username cannot be empty"
|
||||
|
||||
def read_useremail():
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT * FROM clients")
|
||||
rows = cur.fetchall()
|
||||
all = []
|
||||
for row in rows:
|
||||
#print(row[1]+' '+row[2])
|
||||
all.append(row)
|
||||
return all
|
||||
55
app/bot/helper/plexhelper.py
Normal file
55
app/bot/helper/plexhelper.py
Normal file
@@ -0,0 +1,55 @@
|
||||
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
|
||||
try:
|
||||
if Plex_LIBS[0] == "all":
|
||||
Plex_LIBS = plex.library.sections()
|
||||
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')
|
||||
return True
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
return False
|
||||
|
||||
def plexremove(plex, plexname):
|
||||
try:
|
||||
plex.myPlexAccount().removeFriend(user=plexname)
|
||||
logging.info(plexname +' has been removed from plex')
|
||||
return True
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
return False
|
||||
'''
|
||||
|
||||
plex python api has no tools to remove unaccepted invites...
|
||||
|
||||
logging.info("Trying to remove invite...")
|
||||
removeinvite = plexremoveinvite(plex, plexname)
|
||||
if removeinvite:
|
||||
return True
|
||||
'''
|
||||
|
||||
'''
|
||||
def plexremoveinvite(plex, plexname):
|
||||
try:
|
||||
plex.myPlexAccount().removeFriend(user=plexname)
|
||||
logging.info(plexname +' has been removed from plex')
|
||||
return True
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
return False
|
||||
'''
|
||||
def verifyemail(addressToVerify):
|
||||
regex = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$'
|
||||
match = re.match(regex, addressToVerify)
|
||||
if match == None:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
Reference in New Issue
Block a user