58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
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)
|