modified: bot.py

This commit is contained in:
SimolZimol
2024-10-22 13:15:21 +02:00
parent a5ca5c7fff
commit 7e5732ae79

71
bot.py
View File

@@ -34,11 +34,6 @@ OPENAI_BASE_URL = os.getenv("OPENAI_BASE_URL")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
OWNER_ID = int(os.getenv("OWNER_ID")) OWNER_ID = int(os.getenv("OWNER_ID"))
GIVEAWAY_DB_HOST = os.getenv("GIVEAWAY_DB_HOST")
GIVEAWAY_DB_PORT = os.getenv("GIVEAWAY_DB_PORT")
GIVEAWAY_DB_USER = os.getenv("GIVEAWAY_DB_USER")
GIVEAWAY_DB_PASSWORD = os.getenv("GIVEAWAY_DB_PASSWORD")
GIVEAWAY_DB_DATABASE = os.getenv("GIVEAWAY_DB_DATABASE")
GIVEAWAY_WEBSITE_URL = os.getenv("GIVEAWAY_WEBSITE_URL") GIVEAWAY_WEBSITE_URL = os.getenv("GIVEAWAY_WEBSITE_URL")
features = { features = {
@@ -46,36 +41,6 @@ features = {
"vision": bool(int(os.getenv("VISION_ENABLED", 0))) "vision": bool(int(os.getenv("VISION_ENABLED", 0)))
} }
def connect_to_giveaway_db():
return mysql.connector.connect(
host=GIVEAWAY_DB_HOST,
port=GIVEAWAY_DB_PORT,
user=GIVEAWAY_DB_USER,
password=GIVEAWAY_DB_PASSWORD,
database=GIVEAWAY_DB_DATABASE
)
def create_giveaway_table():
connection = connect_to_giveaway_db()
cursor = connection.cursor()
create_table_query = """
CREATE TABLE IF NOT EXISTS giveaways (
id INT AUTO_INCREMENT PRIMARY KEY,
platform VARCHAR(255),
name VARCHAR(255),
uuid CHAR(36) NOT NULL UNIQUE,
game_key VARCHAR(255),
winner_dc_id BIGINT,
aktiv BOOLEAN DEFAULT FALSE
);
"""
cursor.execute(create_table_query)
connection.commit()
cursor.close()
connection.close()
create_giveaway_table()
giveaways = {} giveaways = {}
# Erstelle einen Ordner für die Logs, wenn er noch nicht existiert # Erstelle einen Ordner für die Logs, wenn er noch nicht existiert
@@ -146,25 +111,8 @@ db_connection = mysql.connector.connect(
# Cursor erstellen # Cursor erstellen
db_cursor = db_connection.cursor() db_cursor = db_connection.cursor()
# SQL-Befehl für die Erstellung der Tabelle, falls sie noch nicht existiert def close_database_connection(connection):
create_table_query = """ connection.close()
CREATE TABLE IF NOT EXISTS user_data (
user_id BIGINT,
guild_id BIGINT,
permission INT,
points INT,
ban INT,
askmultus INT,
filter_value INT,
rank INT,
chat_history JSON,
xp INT,
level INT,
PRIMARY KEY (user_id, guild_id)
);
"""
db_cursor.execute(create_table_query)
db_connection.commit()
def insert_user_data(user_id, guild_id, permission, points, ban, askmultus, filter_value, chat_history, xp=0, level=1): def insert_user_data(user_id, guild_id, permission, points, ban, askmultus, filter_value, chat_history, xp=0, level=1):
insert_query = """ insert_query = """
@@ -308,27 +256,26 @@ def load_user_data(user_id, guild_id):
return user_data return user_data
#----------------------------------------------------------------------------------------------------------- #-----------------------------------------------------------------------------------------------------------
def save_giveaway_to_db(platform, name, prize_uuid, game_key, winner_dc_id=None): def save_giveaway_to_db(guild_id, platform, name, prize_uuid, game_key, winner_dc_id=None):
connection = connect_to_giveaway_db() connection = connect_to_database()
cursor = connection.cursor() cursor = connection.cursor()
insert_query = """ insert_query = """
INSERT INTO giveaways (platform, name, uuid, game_key, winner_dc_id, aktiv) INSERT INTO giveaway_data (guild_id, platform, name, uuid, game_key, winner_dc_id, aktiv)
VALUES (%s, %s, %s, %s, %s, %s) VALUES (%s, %s, %s, %s, %s, %s)
""" """
data = (platform, name, str(prize_uuid), game_key, winner_dc_id, False) data = (guild_id, platform, name, str(prize_uuid), game_key, winner_dc_id, False)
cursor.execute(insert_query, data) cursor.execute(insert_query, data)
connection.commit() connection.commit()
cursor.close() cursor.close()
connection.close() connection.close()
def update_winner_in_db(prize_uuid, winner_dc_id): def update_winner_in_db(prize_uuid, winner_dc_id):
connection = connect_to_giveaway_db() connection = connect_to_database()
cursor = connection.cursor() cursor = connection.cursor()
update_query = """ update_query = """
UPDATE giveaways SET winner_dc_id = %s WHERE uuid = %s UPDATE giveaway_data SET winner_dc_id = %s WHERE uuid = %s
""" """
data = (winner_dc_id, str(prize_uuid)) data = (winner_dc_id, str(prize_uuid))
cursor.execute(update_query, data) cursor.execute(update_query, data)
@@ -351,7 +298,7 @@ class Giveaway:
self.game_key = f"PREDEFINED_GAME_KEY" # Platzhalter für den tatsächlichen Game-Key self.game_key = f"PREDEFINED_GAME_KEY" # Platzhalter für den tatsächlichen Game-Key
# Speichern des Giveaways in der Datenbank # Speichern des Giveaways in der Datenbank
save_giveaway_to_db(self.platform, self.title, self.prize_uuid, self.game_key) save_giveaway_to_db(ctx.guild.id, self.platform, self.title, self.prize_uuid, self.game_key)
def add_participant(self, user): def add_participant(self, user):
if user not in self.participants: if user not in self.participants: