From 4ae31159dce4fbdb8ab3a1ff57184ae534d83afb Mon Sep 17 00:00:00 2001 From: imcompiling Date: Tue, 14 May 2024 16:04:52 -0400 Subject: [PATCH] Move the webhook listener into a cog so that I have access to the bot for the action method. --- classes/clan_message.py | 9 -------- cogs/clan_messages/__init__.py | 38 ++++++++++++++++++++++++++++++++++ main.py | 10 --------- 3 files changed, 38 insertions(+), 19 deletions(-) delete mode 100644 classes/clan_message.py create mode 100644 cogs/clan_messages/__init__.py diff --git a/classes/clan_message.py b/classes/clan_message.py deleted file mode 100644 index ef7c376..0000000 --- a/classes/clan_message.py +++ /dev/null @@ -1,9 +0,0 @@ -from fastapi import APIRouter - -router = APIRouter() - - -@router.post("/message/") -async def hello(): - print("we did a webhook") - return {"message": "webhook get"} diff --git a/cogs/clan_messages/__init__.py b/cogs/clan_messages/__init__.py new file mode 100644 index 0000000..9d479a9 --- /dev/null +++ b/cogs/clan_messages/__init__.py @@ -0,0 +1,38 @@ +import uvicorn +from fastapi import FastAPI +from nextcord.ext import commands + +app = FastAPI() + + +class ClanMessages(commands.Cog, name="Clan Messages"): + def __init__(self, bot: commands.Bot): + self.bot = bot + + @commands.Cog.listener() + async def on_ready(self): + 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") + 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 + + +# 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)) diff --git a/main.py b/main.py index 0e9d034..b1679fb 100644 --- a/main.py +++ b/main.py @@ -4,12 +4,9 @@ import sys import hydra import nextcord -import uvicorn -from fastapi import FastAPI from hydra.core.config_store import ConfigStore from nextcord.ext import commands -import classes.clan_message as clan_message import database_setup.create_database as create_database from classes.nominate_highcouncil_views import NominateHighCouncilView from classes.nominate_views import NominateView @@ -30,9 +27,6 @@ handler.setFormatter( ) logger.addHandler(handler) -app = FastAPI() -app.include_router(clan_message.router) - @hydra.main(config_path="conf", config_name="config") def main(cfg: DakinyConfig): @@ -67,10 +61,6 @@ def main(cfg: DakinyConfig): bot.add_view(VoteHighcouncilView(cfg, bot.get_guild(cfg.params.guild_id))) print(f"{bot.user.name} has connected to Discord!") - config = uvicorn.Config("main:app", port=5000, log_level="info") - server = uvicorn.Server(config) - await server.serve() - # Run Discord bot bot.run(bot.config.params.discord_token)