This commit is contained in:
@@ -1,26 +1,30 @@
|
||||
"""
|
||||
File contains logic for creating a webhook listener, pulling from git, and updating all messages.
|
||||
"""
|
||||
"""File contains logic for creating a webhook listener, pulling from git, and updating all messages."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import nextcord
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
from git import Repo
|
||||
from nextcord.ext import commands
|
||||
|
||||
from classes.gitea_webhook import GiteaWebhook
|
||||
from config import DakinyConfig
|
||||
from utils import REPO_DIR, edit_clan_council_info, update_clan_messages
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import nextcord
|
||||
|
||||
from config import DakinyConfig
|
||||
|
||||
app = FastAPI()
|
||||
REPO_URL = "https://gitea.cheezers.org/imcompiling/dakiny-messages.git"
|
||||
|
||||
|
||||
class ClanMessages(commands.Cog, name="Clan Messages"):
|
||||
"""
|
||||
Cog to be loaded into the bot. Used to be able to have a fastapi post listener and access to the bot at
|
||||
Cog to be loaded into the bot.
|
||||
|
||||
Used to be able to have a fastapi post listener and access to the bot at
|
||||
the same time.
|
||||
"""
|
||||
|
||||
@@ -30,23 +34,24 @@ class ClanMessages(commands.Cog, name="Clan Messages"):
|
||||
self.repo_setup()
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_ready(self):
|
||||
"""
|
||||
Triggers when the bot is in ready state. Adds the webhook listener to FastAPI and serves.
|
||||
"""
|
||||
async def on_ready(self) -> None:
|
||||
"""Triggers when the bot is in ready state. Adds the webhook listener to FastAPI and serves."""
|
||||
app.add_api_route("/messages/", self.update_messages, methods=["POST"])
|
||||
config = uvicorn.Config(app=app, host="0.0.0.0", port=5000, log_level="info")
|
||||
# Bind to all interfaces, don't know the docker interface ahead of time.
|
||||
config = uvicorn.Config(app=app, host="0.0.0.0", port=5000, log_level="info") # noqa: S104
|
||||
server = uvicorn.Server(config)
|
||||
await server.serve()
|
||||
|
||||
async def update_messages(self, hook_info: GiteaWebhook):
|
||||
async def update_messages(self, hook_info: GiteaWebhook) -> dict:
|
||||
"""
|
||||
Triggered by webhook.
|
||||
|
||||
Pulls from git and then calls method to update all the discord messages as described by the
|
||||
files in the git repo.
|
||||
"""
|
||||
# Get all modified/added files
|
||||
modified_files: list[str] = [
|
||||
file for list in [commit.modified + commit.added for commit in hook_info.commits] for file in list
|
||||
file for files in [commit.modified + commit.added for commit in hook_info.commits] for file in files
|
||||
]
|
||||
|
||||
template_updates = {file.split("/")[1] for file in modified_files if "message_templates" in file}
|
||||
@@ -63,12 +68,10 @@ class ClanMessages(commands.Cog, name="Clan Messages"):
|
||||
|
||||
return {"message": "Successfully updated all discord messages."}
|
||||
|
||||
def repo_setup(self):
|
||||
"""
|
||||
Checks if the repo exists and creates repo object, clones if it does not.
|
||||
"""
|
||||
def repo_setup(self) -> None:
|
||||
"""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):
|
||||
if Path(REPO_DIR).is_dir():
|
||||
self.repo = Repo(REPO_DIR)
|
||||
else:
|
||||
self.repo = Repo.clone_from(REPO_URL, REPO_DIR)
|
||||
@@ -85,6 +88,6 @@ class ClanMessages(commands.Cog, name="Clan Messages"):
|
||||
|
||||
# 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"""
|
||||
def setup(bot: commands.Bot) -> None:
|
||||
"""Adds function to bot."""
|
||||
bot.add_cog(ClanMessages(bot))
|
||||
|
||||
Reference in New Issue
Block a user