Compare commits

5 Commits
main ... Bot

Author SHA1 Message Date
SimolZimol
d0f4381a59 modified: bot.py 2025-01-17 15:03:10 +01:00
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
SimolZimol
530e6def77 modified: app.py
new file:   templates/index.html
2024-09-03 15:22:57 +02:00
SimolZimol
40924e10c0 new file: requirements.txt 2024-09-03 15:04:59 +02:00
SimolZimol
8c6b081379 new file: Dockerfile
new file:   app.py
	new file:   bot.py
	new file:   templates/post.html
2024-09-03 14:59:08 +02:00
7 changed files with 234 additions and 0 deletions

14
Dockerfile Normal file
View File

@@ -0,0 +1,14 @@
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
ENV DISCORD_TOKEN=$DISCORD_TOKEN
ENV DISCORD_CHANNEL_ID=$DISCORD_CHANNEL_ID
ENV PORT=$PORT
CMD ["python", "bot.py"]

76
app.py Normal file
View File

@@ -0,0 +1,76 @@
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)

57
bot.py Normal file
View File

@@ -0,0 +1,57 @@
import discord
import os
from discord.ext import commands
from flask import Flask, request, jsonify
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"))
app = Flask(__name__)
@bot.event
async def on_ready():
channel = bot.get_channel(CHANNEL_ID)
await channel.send("Info-Bot is now online!")
@bot.command()
async def post(ctx, category: str, *, message: str):
await ctx.send(f"**[{category.upper()}]** {message}")
@app.route("/webhook", methods=["POST"])
def webhook():
data = request.json
if 'commits' in data:
commits = data['commits']
for commit in commits:
message = f"New commit by {commit['author']['name']}: {commit['message']} - {commit['url']}"
bot.loop.create_task(post_commit(message))
return jsonify({"status": "success"}), 200
async def post_commit(message):
channel = bot.get_channel(CHANNEL_ID)
await channel.send(message)
# Flask route for manual posts
@app.route("/post", methods=["POST"])
def manual_post():
category = request.form.get("category")
message = request.form.get("message")
bot.loop.create_task(post_manual(category, message))
return jsonify({"status": "success"}), 200
async def post_manual(category, message):
channel = bot.get_channel(CHANNEL_ID)
await channel.send(f"**[{category.upper()}]** {message}")
def start_flask():
app.run(host="0.0.0.0", port=os.getenv("PORT"))
if __name__ == "__main__":
from threading import Thread
flask_thread = Thread(target=start_flask)
flask_thread.start()
bot.run(DISCORD_TOKEN)

4
requirements.txt Normal file
View File

@@ -0,0 +1,4 @@
discord.py
flask
requests
psutil

36
templates/index.html Normal file
View File

@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Info-Bot Admin Panel</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<h1 class="text-center">Info-Bot Admin Panel</h1>
<h2 class="mt-4">Bot Status</h2>
<p class="lead">Other Bot Status: <strong>{{ bot_status }}</strong></p>
<h2 class="mt-4">Post a Message to Discord</h2>
<form action="/post_message" method="post">
<div class="form-group">
<label for="category">Category:</label>
<select class="form-control" id="category" name="category">
<option value="Important">Important</option>
<option value="Info">Info</option>
<option value="Update">Update</option>
</select>
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea class="form-control" id="message" name="message" rows="3" required></textarea>
</div>
<button type="submit" class="btn btn-success">Post Message</button>
</form>
<a href="/logout" class="btn btn-secondary btn-block mt-4">Logout</a>
</div>
</body>
</html>

25
templates/login.html Normal file
View File

@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<h1 class="text-center">Login</h1>
<form action="/login" method="post">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<button type="submit" class="btn btn-primary btn-block">Login</button>
</form>
</div>
</body>
</html>

22
templates/post.html Normal file
View File

@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Info-Bot Post</title>
</head>
<body>
<h1>Post a Message to Discord</h1>
<form action="/post" method="post">
<label for="category">Category:</label>
<select name="category" id="category">
<option value="Important">Important</option>
<option value="Info">Info</option>
<option value="Update">Update</option>
</select><br><br>
<label for="message">Message:</label><br>
<textarea name="message" id="message" rows="4" cols="50"></textarea><br><br>
<input type="submit" value="Post">
</form>
</body>
</html>