update bot_core.py for base reaction functionality, create db_helper.py

This commit is contained in:
2025-11-21 12:27:28 -07:00
parent a3c6a080e4
commit 9f7dbc0d45
2 changed files with 69 additions and 1 deletions

44
src/helper/db_helper.py Normal file
View File

@@ -0,0 +1,44 @@
"""
database interaction module of the very-bad bot
@author: Gabriella 'contrastellar' Agathon
"""
import psycopg2
import psycopg2.extensions
from configparser import ConfigParser
import datetime
def load_config(filename='database.ini', section='postgresql'):
parser = ConfigParser()
parser.read(filename)
# get section, default is postgresql
config = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
config[param[0]] = param[1]
else:
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
return config
def connect_config(config) -> psycopg2.extensions.connection:
try:
with psycopg2.connect(**config) as conn:
print('connected to the server')
return conn
except (psycopg2.DatabaseError, Exception) as error:
print(error)
finally:
if conn is None:
raise psycopg2.DatabaseError("Failed to connect!")