From 364be4f769e5163e47b74246810e2967842d895b Mon Sep 17 00:00:00 2001 From: SimolZimol <70102430+SimolZimol@users.noreply.github.com> Date: Tue, 29 Oct 2024 10:09:44 +0100 Subject: [PATCH] modified: app.py modified: templates/redeem_giveaway.html modified: templates/server_giveaways.html modified: templates/user_giveaways.html --- app.py | 121 +++++++++++++++++++++++++++----- templates/redeem_giveaway.html | 33 ++------- templates/server_giveaways.html | 2 +- templates/user_giveaways.html | 51 +++++++------- 4 files changed, 138 insertions(+), 69 deletions(-) diff --git a/app.py b/app.py index 10bb37a..28579a6 100644 --- a/app.py +++ b/app.py @@ -19,7 +19,6 @@ app.config["SESSION_TYPE"] = "filesystem" # Oder 'redis' für Redis-basierte Sp Session(app) print(f"Session Type: {app.config['SESSION_TYPE']}") -# Verwende Umgebungsvariablen für die Datenbankverbindung DB_HOST = os.getenv("DB_HOST") DB_PORT = os.getenv("DB_PORT") DB_USER = os.getenv("DB_USER") @@ -34,7 +33,6 @@ DISCORD_TOKEN_URL = "https://discord.com/api/oauth2/token" DISCORD_API_URL = "https://discord.com/api/users/@me" os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' -# Speichern der Prozess-ID bot_process = None def bot_status(): @@ -447,28 +445,117 @@ def user_dashboard(guild_id): @app.route("/server_giveaways/") def server_giveaways(guild_id): - """Serverbasiertes Giveaway-Management""" - if g.user_info: - user_id = g.user_info["id"] - - # Überprüfe, ob der Benutzer Admin-Rechte auf diesem Server hat + """Serverbasiertes Giveaway-Management.""" + if is_server_admin(guild_id): connection = get_db_connection() cursor = connection.cursor(dictionary=True) - cursor.execute("SELECT permission FROM user_data WHERE user_id = %s AND guild_id = %s", (user_id, guild_id)) - user_data = cursor.fetchone() + # Hole die Giveaways für den spezifischen Server + cursor.execute("SELECT * FROM giveaway_data WHERE guild_id = %s", (guild_id,)) + giveaways = cursor.fetchall() - if user_data and user_data['permission'] >= 8: - # Hole die Giveaways für diesen Server - cursor.execute("SELECT * FROM giveaway_data WHERE guild_id = %s", (guild_id,)) - giveaways = cursor.fetchall() + cursor.close() + connection.close() - cursor.close() - connection.close() + return render_template("server_giveaways.html", giveaways=giveaways, guild_id=guild_id) + return redirect(url_for("landing_page")) - return render_template("server_giveaways.html", giveaways=giveaways, guild_id=guild_id) +@app.route("/edit_giveaway//", methods=["GET", "POST"]) +def edit_giveaway(guild_id, uuid): + """Bearbeitet ein spezifisches Giveaway für einen bestimmten Server.""" + if is_server_admin(guild_id): + connection = get_db_connection() + cursor = connection.cursor(dictionary=True) - return redirect(url_for("user_landing_page")) + if request.method == "POST": + platform = request.form.get("platform") + name = request.form.get("name") + game_key = request.form.get("game_key") + winner_dc_id = request.form.get("winner_dc_id") + aktiv = bool(request.form.get("aktiv")) + + # Update der Giveaways-Daten + cursor.execute(""" + UPDATE giveaway_data + SET platform = %s, name = %s, game_key = %s, winner_dc_id = %s, aktiv = %s + WHERE guild_id = %s AND uuid = %s + """, (platform, name, game_key, winner_dc_id, aktiv, guild_id, uuid)) + connection.commit() + + flash("Giveaway updated successfully!", "success") + return redirect(url_for("server_giveaways", guild_id=guild_id)) + + # Daten des spezifischen Giveaways laden + cursor.execute("SELECT * FROM giveaway_data WHERE guild_id = %s AND uuid = %s", (guild_id, uuid)) + giveaway = cursor.fetchone() + + cursor.close() + connection.close() + + return render_template("edit_giveaway.html", giveaway=giveaway, guild_id=guild_id) + return redirect(url_for("landing_page")) + +@app.route("/user_giveaways/") +def user_giveaways(guild_id): + """Zeigt dem Benutzer die Giveaways an, die er auf einem bestimmten Server gewonnen hat.""" + if "discord_user" in session: + user_info = session["discord_user"] + user_id = user_info["id"] + + connection = get_db_connection() + cursor = connection.cursor(dictionary=True) + + # Suche nach Giveaways, bei denen der eingeloggte Benutzer der Gewinner ist + cursor.execute(""" + SELECT * FROM giveaway_data WHERE winner_dc_id = %s AND guild_id = %s + """, (user_id, guild_id)) + won_giveaways = cursor.fetchall() + + cursor.close() + connection.close() + + return render_template("user_giveaways.html", won_giveaways=won_giveaways, guild_id=guild_id) + + return redirect(url_for("landing_page")) + +@app.route("/redeem_giveaway//", methods=["GET", "POST"]) +def redeem_giveaway(guild_id, uuid): + """Erlaubt dem Benutzer, einen gewonnenen Giveaway-Code für einen bestimmten Server einzulösen.""" + if "discord_user" in session: + user_info = session["discord_user"] + user_id = user_info["id"] + + connection = get_db_connection() + cursor = connection.cursor(dictionary=True) + + # Überprüfen, ob der eingeloggte Benutzer der Gewinner ist + cursor.execute(""" + SELECT * FROM giveaway_data WHERE guild_id = %s AND uuid = %s AND winner_dc_id = %s + """, (guild_id, uuid, user_id)) + giveaway = cursor.fetchone() + + if giveaway: + if request.method == "POST": + # Wenn der Benutzer den Key einlöst, setze `aktiv` auf TRUE + cursor.execute("UPDATE giveaway_data SET aktiv = TRUE WHERE guild_id = %s AND uuid = %s", (guild_id, uuid)) + connection.commit() + + # Key anzeigen + flash("Giveaway redeemed successfully!", "success") + return render_template("redeem_giveaway.html", giveaway=giveaway, key=giveaway["game_key"]) + + # Seite anzeigen, um den Key einzulösen + return render_template("redeem_giveaway.html", giveaway=giveaway, key=None) + + else: + flash("You are not the winner of this giveaway or the giveaway is no longer available.", "danger") + + cursor.close() + connection.close() + + return redirect(url_for("user_giveaways", guild_id=guild_id)) + + return redirect(url_for("landing_page")) @app.route("/user_landing_page") def user_landing_page(): diff --git a/templates/redeem_giveaway.html b/templates/redeem_giveaway.html index 55c6ae1..ef9f0ac 100644 --- a/templates/redeem_giveaway.html +++ b/templates/redeem_giveaway.html @@ -8,38 +8,19 @@ {% include 'navigation.html' %} -
-

Giveaway Prize

-

Congratulations! You have won the giveaway for {{ giveaway.name }} on - {{ giveaway.platform }}.

+

Redeem Giveaway - {{ giveaway.name }}

+

Platform: {{ giveaway.platform }}

{% if key %} - -
-
-
Your Key
-

{{ key }}

+
+ Game Key: {{ key }}
-
- Back to Giveaways {% else %} - -
-
-
Claim Your Key
-

Do you want to reveal your game key? Once revealed, it will be marked as claimed and cannot be undone.

- -
- -
-
-
- Back to Giveaways +
+ +
{% endif %}
- - - diff --git a/templates/server_giveaways.html b/templates/server_giveaways.html index 01296cd..44b6082 100644 --- a/templates/server_giveaways.html +++ b/templates/server_giveaways.html @@ -28,7 +28,7 @@ {{ giveaway.platform }} {{ giveaway.winner_dc_id }} - Edit + Edit Giveaway Delete diff --git a/templates/user_giveaways.html b/templates/user_giveaways.html index c9f711c..980b415 100644 --- a/templates/user_giveaways.html +++ b/templates/user_giveaways.html @@ -3,36 +3,37 @@ - Your Giveaways + Your Won Giveaways {% include 'navigation.html' %} -
-

Your Giveaways

-

Here you can view the giveaways you've won.

- -
- {% if giveaways %} - {% for giveaway in giveaways %} -
-
-
-
{{ giveaway.name }}
-

Platform: {{ giveaway.platform }}

- Claim Your Key -
-
-
- {% endfor %} - {% else %} -

You haven't won any giveaways yet.

- {% endif %} -
+

Your Won Giveaways on Server {{ guild_id }}

+ + + + + + + + + + {% for giveaway in won_giveaways %} + + + + + + {% endfor %} + +
NamePlatformRedeem
{{ giveaway.name }}{{ giveaway.platform }} + {% if giveaway.aktiv %} + Redeemed + {% else %} + Redeem + {% endif %} +
- - -