modified: Dockerfile
modified: app.py modified: bot.py new file: templates/admin_giveaways.html new file: templates/edit_giveaway.html new file: templates/user_giveaways.html
This commit is contained in:
110
app.py
110
app.py
@@ -2,7 +2,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, flash
|
||||
from requests_oauthlib import OAuth2Session
|
||||
import os
|
||||
import subprocess
|
||||
@@ -393,5 +393,113 @@ def download_logs():
|
||||
return send_file(LOG_FILE_PATH, as_attachment=True)
|
||||
return redirect(url_for("landing_page"))
|
||||
|
||||
@app.route("/admin/giveaways", methods=["GET", "POST"])
|
||||
def admin_giveaways():
|
||||
"""Zeigt eine Liste aller Giveaways an und ermöglicht das Bearbeiten und Sortieren."""
|
||||
if is_admin():
|
||||
connection = get_db_connection()
|
||||
cursor = connection.cursor(dictionary=True)
|
||||
|
||||
# Sortierung nach bestimmten Feldern
|
||||
sort_field = request.args.get("sort", "id") # Standardmäßig nach 'id' sortieren
|
||||
order = request.args.get("order", "asc") # Standardmäßig aufsteigend sortieren
|
||||
|
||||
# Holen aller Giveaways aus der Datenbank
|
||||
cursor.execute(f"SELECT * FROM giveaways ORDER BY {sort_field} {order}")
|
||||
giveaways = cursor.fetchall()
|
||||
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
return render_template("admin_giveaways.html", giveaways=giveaways, sort_field=sort_field, order=order)
|
||||
return redirect(url_for("login"))
|
||||
|
||||
@app.route("/admin/giveaways/edit/<int:giveaway_id>", methods=["GET", "POST"])
|
||||
def edit_giveaway(giveaway_id):
|
||||
"""Bearbeitet ein spezifisches Giveaway."""
|
||||
if is_admin():
|
||||
connection = get_db_connection()
|
||||
cursor = connection.cursor(dictionary=True)
|
||||
|
||||
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 giveaways
|
||||
SET platform = %s, name = %s, game_key = %s, winner_dc_id = %s, aktiv = %s
|
||||
WHERE id = %s
|
||||
""", (platform, name, game_key, winner_dc_id, aktiv, giveaway_id))
|
||||
connection.commit()
|
||||
|
||||
flash("Giveaway updated successfully!", "success")
|
||||
return redirect(url_for("admin_giveaways"))
|
||||
|
||||
# Daten des spezifischen Giveaways laden
|
||||
cursor.execute("SELECT * FROM giveaways WHERE id = %s", (giveaway_id,))
|
||||
giveaway = cursor.fetchone()
|
||||
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
return render_template("edit_giveaway.html", giveaway=giveaway)
|
||||
return redirect(url_for("login"))
|
||||
|
||||
@app.route("/user/giveaways", methods=["GET"])
|
||||
def user_giveaways():
|
||||
"""Zeigt dem Benutzer die Giveaways, die er 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 giveaways WHERE winner_dc_id = %s AND aktiv = TRUE
|
||||
""", (user_id,))
|
||||
won_giveaways = cursor.fetchall()
|
||||
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
return render_template("user_giveaways.html", user_info=user_info, giveaways=won_giveaways)
|
||||
|
||||
return redirect(url_for("login"))
|
||||
|
||||
@app.route("/user/giveaway/redeem/<int:giveaway_id>", methods=["POST"])
|
||||
def redeem_giveaway(giveaway_id):
|
||||
"""Erlaubt dem Benutzer, seinen Giveaway-Preis abzuholen."""
|
||||
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üfe, ob der eingeloggte Benutzer der Gewinner ist
|
||||
cursor.execute("SELECT * FROM giveaways WHERE id = %s AND winner_dc_id = %s AND aktiv = TRUE", (giveaway_id, user_id))
|
||||
giveaway = cursor.fetchone()
|
||||
|
||||
if giveaway:
|
||||
# Setze das Giveaway auf inaktiv, damit es nicht erneut eingelöst werden kann
|
||||
cursor.execute("UPDATE giveaways SET aktiv = FALSE WHERE id = %s", (giveaway_id,))
|
||||
connection.commit()
|
||||
flash("You have successfully redeemed your prize!", "success")
|
||||
else:
|
||||
flash("You are not the winner of this giveaway or the giveaway is no longer active.", "danger")
|
||||
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
return redirect(url_for("user_giveaways"))
|
||||
|
||||
return redirect(url_for("login"))
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=5000, debug=True)
|
||||
|
||||
Reference in New Issue
Block a user