59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
import os
|
|
|
|
import uvicorn
|
|
from fastapi import FastAPI
|
|
from git import Repo
|
|
from nextcord.ext import commands
|
|
|
|
app = FastAPI()
|
|
REPO_URL = "https://gitea.cheezers.org/imcompiling/dakiny-messages.git"
|
|
REPO_DIR = "./dakiny-messages"
|
|
|
|
|
|
class ClanMessages(commands.Cog, name="Clan Messages"):
|
|
def __init__(self, bot: commands.Bot):
|
|
self.bot = bot
|
|
self.clone_repo()
|
|
|
|
@commands.Cog.listener()
|
|
async def on_ready(self):
|
|
"""
|
|
Triggers when the bot is in ready state. Adds the webhook listener to FastAPI and serves.
|
|
"""
|
|
app.add_api_route("/message/", self.hello, methods=["POST"])
|
|
config = uvicorn.Config(app=app, port=5000, log_level="info")
|
|
server = uvicorn.Server(config)
|
|
await server.serve()
|
|
|
|
async def hello(self):
|
|
print("we did a webhook")
|
|
await self.update_messages()
|
|
return {"message": "webhook get"}
|
|
|
|
async def update_messages(self):
|
|
# TODO everything
|
|
|
|
# git pull
|
|
# if no change, return
|
|
# for ever directory, sort the files by number then post
|
|
# posting involves checking if there is a message for that particular file then editing it or creating a
|
|
# new one. the message could be deleted, which would cause a new message to be sent and database updated.
|
|
pass
|
|
|
|
def clone_repo(self):
|
|
"""
|
|
Checks if the repo exists and creates repo object, clones if it does not.
|
|
"""
|
|
# check if repo has been cloned. open repo object
|
|
if os.path.isdir(REPO_DIR):
|
|
self.repo = Repo(REPO_DIR)
|
|
else:
|
|
self.repo = Repo.clone_from(REPO_URL, REPO_DIR)
|
|
|
|
|
|
# This function will be called when this extension is loaded.
|
|
# It is necessary to add these functions to the bot.
|
|
def setup(bot: commands.Bot):
|
|
"""Adds function to bot"""
|
|
bot.add_cog(ClanMessages(bot))
|