modified: app.py
modified: bot.py
This commit is contained in:
4
app.py
4
app.py
@@ -1,3 +1,7 @@
|
|||||||
|
__version__ = "dev-0.4.6"
|
||||||
|
__all__ = ["Discordbot-chatai-webpanel (Discord)"]
|
||||||
|
__author__ = "SimolZimol"
|
||||||
|
|
||||||
from flask import Flask, render_template, redirect, url_for, request, session, jsonify, send_file
|
from flask import Flask, render_template, redirect, url_for, request, session, jsonify, send_file
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|||||||
72
bot.py
72
bot.py
@@ -1,4 +1,4 @@
|
|||||||
__version__ = "dev-0.7.9"
|
__version__ = "dev-0.8.2"
|
||||||
__all__ = ["Discordbot-chatai (Discord)"]
|
__all__ = ["Discordbot-chatai (Discord)"]
|
||||||
__author__ = "SimolZimol"
|
__author__ = "SimolZimol"
|
||||||
|
|
||||||
@@ -116,14 +116,45 @@ CREATE TABLE IF NOT EXISTS user_data (
|
|||||||
db_cursor.execute(create_table_query)
|
db_cursor.execute(create_table_query)
|
||||||
db_connection.commit()
|
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):
|
def insert_user_data(user_id, permission, points, ban, askmultus, filter_value, chat_history):
|
||||||
insert_query = """
|
insert_query = """
|
||||||
INSERT INTO user_data (user_id, permission, points, ban, askmultus, filter_value, rank, chat_history)
|
INSERT INTO user_data (user_id, permission, points, ban, askmultus, filter_value, rank, chat_history)
|
||||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
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)
|
serialized_chat_history = json.dumps(chat_history)
|
||||||
data = (user_id, permission, points, ban, askmultus, filter_value, 0, serialized_chat_history) # Setze den Rang initial auf 0
|
data = (user_id, permission, points, ban, askmultus, filter_value, 0, serialized_chat_history)
|
||||||
try:
|
try:
|
||||||
db_cursor.execute(insert_query, data)
|
db_cursor.execute(insert_query, data)
|
||||||
db_connection.commit()
|
db_connection.commit()
|
||||||
@@ -134,15 +165,11 @@ def insert_user_data(user_id, permission, points, ban, askmultus, filter_value,
|
|||||||
|
|
||||||
def update_user_data(user_id, field, value):
|
def update_user_data(user_id, field, value):
|
||||||
update_query = f"UPDATE user_data SET {field} = %s WHERE user_id = %s"
|
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':
|
if field == 'chat_history':
|
||||||
# Serialize the chat history list to a JSON string
|
|
||||||
serialized_chat_history = json.dumps(value)
|
serialized_chat_history = json.dumps(value)
|
||||||
db_cursor.execute(update_query, (serialized_chat_history, user_id))
|
db_cursor.execute(update_query, (serialized_chat_history, user_id))
|
||||||
else:
|
else:
|
||||||
db_cursor.execute(update_query, (value, user_id))
|
db_cursor.execute(update_query, (value, user_id))
|
||||||
|
|
||||||
db_connection.commit()
|
db_connection.commit()
|
||||||
|
|
||||||
def load_user_data_from_mysql(user_id):
|
def load_user_data_from_mysql(user_id):
|
||||||
@@ -162,19 +189,16 @@ def load_user_data_from_mysql(user_id):
|
|||||||
"chat_history": json.loads(result[7]) if result[7] else []
|
"chat_history": json.loads(result[7]) if result[7] else []
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
# Wenn keine Benutzerdaten vorhanden sind, erstelle neue Daten
|
|
||||||
user_data = {
|
user_data = {
|
||||||
"user_id": user_id,
|
"user_id": user_id,
|
||||||
"permission": 0, # Standardberechtigung
|
"permission": 0,
|
||||||
"points": 0, # Standardpunkte
|
"points": 0,
|
||||||
"ban": 0, # Standardbannstatus
|
"ban": 0,
|
||||||
"askmultus": 0, # Standardwert für askmultus
|
"askmultus": 0,
|
||||||
"filter_value": 0, # Standardwert für Filter
|
"filter_value": 0,
|
||||||
"rank": 0, # Standardrang
|
"rank": 0,
|
||||||
"chat_history": [] # Leerer Chatverlauf
|
"chat_history": []
|
||||||
}
|
}
|
||||||
|
|
||||||
# Fügen Sie die neuen Benutzerdaten zur Datenbank hinzu
|
|
||||||
insert_user_data(
|
insert_user_data(
|
||||||
user_data["user_id"],
|
user_data["user_id"],
|
||||||
user_data["permission"],
|
user_data["permission"],
|
||||||
@@ -185,6 +209,8 @@ def load_user_data_from_mysql(user_id):
|
|||||||
user_data["chat_history"]
|
user_data["chat_history"]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Speichern Sie die Benutzerdaten lokal
|
||||||
|
local_user_data[user_id] = user_data
|
||||||
return user_data
|
return user_data
|
||||||
|
|
||||||
def save_user_data_to_mysql(user_data):
|
def save_user_data_to_mysql(user_data):
|
||||||
@@ -212,6 +238,16 @@ def save_user_data_to_mysql(user_data):
|
|||||||
db_cursor.execute(update_query, data)
|
db_cursor.execute(update_query, data)
|
||||||
db_connection.commit()
|
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():
|
def read_introduction():
|
||||||
@@ -247,6 +283,8 @@ background_data = read_background_data("background_data.txt")
|
|||||||
@client.event
|
@client.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
client.loop.create_task(process_ai_queue())
|
client.loop.create_task(process_ai_queue())
|
||||||
|
connect_to_db()
|
||||||
|
client.loop.create_task(periodic_cleanup())
|
||||||
logger.info("Bot is ready!")
|
logger.info("Bot is ready!")
|
||||||
logger.info(f"Logged in as: {client.user.name}")
|
logger.info(f"Logged in as: {client.user.name}")
|
||||||
logger.info(f"Client ID: {client.user.id}")
|
logger.info(f"Client ID: {client.user.id}")
|
||||||
|
|||||||
Reference in New Issue
Block a user