forked from Raiza.dev/EliteBot
Add db support (W.I.P.), cookie.py and removed UseSASL requirement.
This commit is contained in:
parent
a3312cffda
commit
3ca7e9cf36
6 changed files with 111 additions and 2 deletions
|
@ -21,6 +21,7 @@ class Bot:
|
|||
def __init__(self, config_file):
|
||||
self.config = self.load_config(config_file)
|
||||
self.validate_config(self.config)
|
||||
self.connection_string = self.config['Database'].get('ConnectionString')
|
||||
self.channel_manager = ChannelManager()
|
||||
self.logger = Logger('logs/elitebot.log')
|
||||
self.connected = False
|
||||
|
@ -36,7 +37,8 @@ class Bot:
|
|||
["Connection", "Nick"],
|
||||
["Connection", "Ident"],
|
||||
["Connection", "Name"],
|
||||
["SASL", "UseSASL"]
|
||||
# ["SASL", "UseSASL"],
|
||||
["Database", "ConnectionString"]
|
||||
]
|
||||
|
||||
for field in required_fields:
|
||||
|
|
39
src/db.py
Normal file
39
src/db.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
from sqlalchemy import create_engine, Table, MetaData, inspect, update, select
|
||||
from sqlalchemy_utils import database_exists, create_database
|
||||
|
||||
|
||||
# TODO: Load ConnectionString from config
|
||||
class Database:
|
||||
def __init__(self, table: Table, meta: MetaData):
|
||||
self.engine = create_engine(r'sqlite:///.\data\database.db')
|
||||
self.table = table
|
||||
self.meta = meta
|
||||
|
||||
if not database_exists(self.engine.url):
|
||||
create_database(self.engine.url)
|
||||
|
||||
def create_table(self, table_name):
|
||||
if not inspect(self.engine).has_table(table_name):
|
||||
self.meta.create_all(self.engine)
|
||||
|
||||
def set(self, user: str, values: dict):
|
||||
with self.engine.connect() as conn:
|
||||
stmt = select(self.table).where(self.table.c.name == user)
|
||||
cnt = len(conn.execute(stmt).fetchall())
|
||||
|
||||
if cnt == 1:
|
||||
conn.execute((
|
||||
update(self.table).
|
||||
values(values)
|
||||
))
|
||||
conn.commit()
|
||||
|
||||
def get(self, user: str, index: int):
|
||||
with self.engine.connect() as conn:
|
||||
stmt = select(self.table).where(self.table.c.name == user)
|
||||
cnt = len(conn.execute(stmt).fetchall())
|
||||
|
||||
if cnt == 1:
|
||||
return conn.execute(select(self.table).where(self.table.c.name == user)).fetchone()[index]
|
||||
else:
|
||||
return -1
|
Loading…
Add table
Add a link
Reference in a new issue