Files
Discord-infobot/app.py
SimolZimol d200d3d1d5 modified: app.py
modified:   requirements.txt
	modified:   templates/index.html
	new file:   templates/login.html
2024-09-03 15:54:48 +02:00

77 lines
2.3 KiB
Python

from flask import Flask, render_template, request, redirect, url_for, session, jsonify
import os
import requests
import discord
from discord.ext import commands
# Discord bot setup
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
# Flask app setup
app = Flask(__name__)
app.secret_key = os.urandom(24)
# Function to check the status of the other bot
def check_bot_status():
try:
response = requests.get("https://nww8w44sgg0c8okcc8wg8soc.simolzimol.net/")
if response.status_code == 200:
return "Online"
else:
return "Offline"
except requests.exceptions.RequestException:
return "Offline"
@app.route("/")
def index():
if "username" in session:
bot_status = check_bot_status()
return render_template("index.html", bot_status=bot_status)
return redirect(url_for("login"))
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
# Simple auth check, replace with a proper method
if username == "admin" and password == "password":
session["username"] = username
return redirect(url_for("index"))
else:
return "Login Failed"
return render_template("login.html")
@app.route("/logout")
def logout():
session.pop("username", None)
return redirect(url_for("login"))
@app.route("/post_message", methods=["POST"])
def post_message():
if "username" in session:
category = request.form.get("category")
message = request.form.get("message")
bot.loop.create_task(post_to_discord(category, message))
return redirect(url_for("index"))
return redirect(url_for("login"))
async def post_to_discord(category, message):
channel = bot.get_channel(CHANNEL_ID)
await channel.send(f"**[{category.upper()}]** {message}")
# Run the Flask app and Discord bot
def start_bot():
bot.run(DISCORD_TOKEN)
if __name__ == "__main__":
from threading import Thread
bot_thread = Thread(target=start_bot)
bot_thread.start()
app.run(host="0.0.0.0", port=5000)