fixed env var

This commit is contained in:
Faiz Ahmed
2021-07-17 15:50:58 -04:00
parent 3dd4a879df
commit 26970ac780
6 changed files with 86 additions and 33 deletions

View File

@@ -0,0 +1,69 @@
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',
'role_id', '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 = ""
roleid = 0
PLEXUSER = ""
PLEXPASS = ""
PLEX_SERVER_NAME = ""
Plex_LIBS = ""
chan = 0
ownerid = 0
auto_remove_user = ""
switch = 0
try:
if(path.exists(CONFIG_PATH)):
config = configparser.ConfigParser()
config.read(CONFIG_PATH)
Discord_bot_token = config.get(BOT_SECTION, 'discord_bot_token')
roleid = config.get(BOT_SECTION, 'role_id')
PLEXUSER = config.get(BOT_SECTION, 'plex_user')
PLEXPASS = config.get(BOT_SECTION, 'plex_pass')
PLEX_SERVER_NAME = config.get(BOT_SECTION, 'plex_server_name')
Plex_LIBS = config.get(BOT_SECTION, 'plex_libs')
chan = int(config.get(BOT_SECTION, 'channel_id'))
ownerid = int(config.get(BOT_SECTION, 'owner_id'))
auto_remove_user = config.get(BOT_SECTION, 'auto_remove_user') if config.get(BOT_SECTION, 'auto_remove_user') else False
switch = 1
else:
load_dotenv(dotenv_path='bot.env')
Discord_bot_token = environ.get('discord_bot_token')
roleid = int(environ.get('role_id'))
PLEXUSER = environ.get('PLEXUSER')
PLEXPASS = config.get(BOT_SECTION, 'plex_pass')
PLEX_SERVER_NAME = config.get(BOT_SECTION, 'plex_server_name')
Plex_LIBS = config.get(BOT_SECTION, 'plex_libs')
chan = int(config.get(BOT_SECTION, 'channel_id'))
ownerid = int(config.get(BOT_SECTION, 'owner_id'))
auto_remove_user = config.get(BOT_SECTION, 'auto_remove_user') if config.get(BOT_SECTION, 'auto_remove_user') else False
switch = 1
except:
print("Cannot find config/Incomplete config")

83
app/helper/db.py Normal file
View 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