modified: bot.py
This commit is contained in:
70
bot.py
70
bot.py
@@ -116,45 +116,14 @@ CREATE TABLE IF NOT EXISTS user_data (
|
||||
db_cursor.execute(create_table_query)
|
||||
db_connection.commit()
|
||||
|
||||
local_user_data = {}
|
||||
|
||||
def check_mysql_connection():
|
||||
global db_connection
|
||||
try:
|
||||
if db_connection is not None:
|
||||
db_connection.ping(reconnect=True, attempts=3, delay=2)
|
||||
logger.info("MySQL connection is active.")
|
||||
else:
|
||||
logger.info("MySQL connection is None, reconnecting...")
|
||||
connect_to_db()
|
||||
except mysql.connector.Error as err:
|
||||
logger.error(f"MySQL connection lost: {err}. Reconnecting...")
|
||||
connect_to_db()
|
||||
|
||||
def connect_to_db():
|
||||
global db_connection, db_cursor
|
||||
if db_connection is not None:
|
||||
db_connection.close()
|
||||
try:
|
||||
db_connection = mysql.connector.connect(
|
||||
host=DB_HOST,
|
||||
port=DB_PORT,
|
||||
user=DB_USER,
|
||||
password=DB_PASSWORD,
|
||||
database=DB_DATABASE
|
||||
)
|
||||
db_cursor = db_connection.cursor()
|
||||
logger.info("Connected to the MySQL database.")
|
||||
except mysql.connector.Error as err:
|
||||
logger.error(f"Error connecting to MySQL: {err}")
|
||||
|
||||
def insert_user_data(user_id, permission, points, ban, askmultus, filter_value, chat_history):
|
||||
insert_query = """
|
||||
INSERT INTO user_data (user_id, permission, points, ban, askmultus, filter_value, rank, chat_history)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
||||
"""
|
||||
# Serialize the chat_history list to a JSON string
|
||||
serialized_chat_history = json.dumps(chat_history)
|
||||
data = (user_id, permission, points, ban, askmultus, filter_value, 0, serialized_chat_history)
|
||||
data = (user_id, permission, points, ban, askmultus, filter_value, 0, serialized_chat_history) # Setze den Rang initial auf 0
|
||||
try:
|
||||
db_cursor.execute(insert_query, data)
|
||||
db_connection.commit()
|
||||
@@ -165,11 +134,15 @@ def insert_user_data(user_id, permission, points, ban, askmultus, filter_value,
|
||||
|
||||
def update_user_data(user_id, field, value):
|
||||
update_query = f"UPDATE user_data SET {field} = %s WHERE user_id = %s"
|
||||
|
||||
# Überprüfen, ob das Feld 'chat_history' aktualisiert wird
|
||||
if field == 'chat_history':
|
||||
# Serialize the chat history list to a JSON string
|
||||
serialized_chat_history = json.dumps(value)
|
||||
db_cursor.execute(update_query, (serialized_chat_history, user_id))
|
||||
else:
|
||||
db_cursor.execute(update_query, (value, user_id))
|
||||
|
||||
db_connection.commit()
|
||||
|
||||
def load_user_data_from_mysql(user_id):
|
||||
@@ -189,16 +162,19 @@ def load_user_data_from_mysql(user_id):
|
||||
"chat_history": json.loads(result[7]) if result[7] else []
|
||||
}
|
||||
else:
|
||||
# Wenn keine Benutzerdaten vorhanden sind, erstelle neue Daten
|
||||
user_data = {
|
||||
"user_id": user_id,
|
||||
"permission": 0,
|
||||
"points": 0,
|
||||
"ban": 0,
|
||||
"askmultus": 0,
|
||||
"filter_value": 0,
|
||||
"rank": 0,
|
||||
"chat_history": []
|
||||
"permission": 0, # Standardberechtigung
|
||||
"points": 0, # Standardpunkte
|
||||
"ban": 0, # Standardbannstatus
|
||||
"askmultus": 0, # Standardwert für askmultus
|
||||
"filter_value": 0, # Standardwert für Filter
|
||||
"rank": 0, # Standardrang
|
||||
"chat_history": [] # Leerer Chatverlauf
|
||||
}
|
||||
|
||||
# Fügen Sie die neuen Benutzerdaten zur Datenbank hinzu
|
||||
insert_user_data(
|
||||
user_data["user_id"],
|
||||
user_data["permission"],
|
||||
@@ -209,8 +185,6 @@ def load_user_data_from_mysql(user_id):
|
||||
user_data["chat_history"]
|
||||
)
|
||||
|
||||
# Speichern Sie die Benutzerdaten lokal
|
||||
local_user_data[user_id] = user_data
|
||||
return user_data
|
||||
|
||||
def save_user_data_to_mysql(user_data):
|
||||
@@ -238,16 +212,6 @@ def save_user_data_to_mysql(user_data):
|
||||
db_cursor.execute(update_query, data)
|
||||
db_connection.commit()
|
||||
|
||||
async def periodic_cleanup():
|
||||
while True:
|
||||
try:
|
||||
logger.info("Performing periodic cleanup.")
|
||||
local_user_data.clear() # Lokale Benutzerdaten löschen
|
||||
check_mysql_connection() # MySQL-Verbindung prüfen
|
||||
except Exception as e:
|
||||
logger.error(f"Error during periodic cleanup: {e}")
|
||||
await asyncio.sleep(30) # Alle 30 Sekunden ausführen
|
||||
|
||||
#-----------------------------------------------------------------------------------------------------------
|
||||
|
||||
def read_introduction():
|
||||
@@ -283,8 +247,6 @@ background_data = read_background_data("background_data.txt")
|
||||
@client.event
|
||||
async def on_ready():
|
||||
client.loop.create_task(process_ai_queue())
|
||||
connect_to_db()
|
||||
client.loop.create_task(periodic_cleanup())
|
||||
logger.info("Bot is ready!")
|
||||
logger.info(f"Logged in as: {client.user.name}")
|
||||
logger.info(f"Client ID: {client.user.id}")
|
||||
|
||||
Reference in New Issue
Block a user