Move the webhook listener into a cog so that I have access to the bot for the action method.

This commit is contained in:
2024-05-14 16:04:52 -04:00
parent 3fec5fab41
commit 4ae31159dc
3 changed files with 38 additions and 19 deletions
-9
View File
@@ -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"}
+38
View File
@@ -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))
-10
View File
@@ -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)