new file: Dockerfile

new file:   app.py
	new file:   bot.py
	new file:   templates/post.html
This commit is contained in:
SimolZimol
2024-09-03 14:59:08 +02:00
parent 9c99aaab37
commit 8c6b081379
4 changed files with 103 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"]

3
app.py Normal file
View File

@@ -0,0 +1,3 @@
@app.route("/post_form", methods=["GET"])
def post_form():
return render_template("post.html")

64
bot.py Normal file
View File

@@ -0,0 +1,64 @@
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
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
# Channel ID to post messages
CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
# Flask web server for posting updates
app = Flask(__name__)
# Event when bot is ready
@bot.event
async def on_ready():
channel = bot.get_channel(CHANNEL_ID)
await channel.send("Info-Bot is now online!")
# Command to manually post a message in Discord
@bot.command()
async def post(ctx, category: str, *, message: str):
await ctx.send(f"**[{category.upper()}]** {message}")
# Flask route to post updates via Webhook
@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}")
# Start the Flask server in a separate thread
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)

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>