discord-havoc/api.py

54 lines
1.2 KiB
Python
Raw Normal View History

2025-02-13 14:51:35 -05:00
#!/usr/bin/env python3.12
import importlib
from typing import Optional
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import util
2025-04-17 14:35:56 -04:00
2025-02-13 14:51:35 -05:00
class ValidSendMsgRequest(BaseModel):
"""
- **guild**: optional, guild id in case multiple channels match (normally first result would be used)
- **channel**: channel to target
- **message**: message to send
"""
guild: Optional[int] = None
channel: str
message: str
2025-04-17 14:35:56 -04:00
2025-02-13 14:51:35 -05:00
class API:
"""API [FastAPI Instance] for Havoc"""
2025-04-17 14:35:56 -04:00
2025-02-13 14:51:35 -05:00
def __init__(self, discord_bot):
api_app = FastAPI(title="Havoc API")
self.bot = discord_bot
self.api_app = api_app
@api_app.get("/{any:path}")
def block_get():
raise HTTPException(status_code=403, detail="Invalid request")
2025-04-17 14:35:56 -04:00
2025-02-13 14:51:35 -05:00
@api_app.post("/send_msg")
async def send_msg_handler(data: ValidSendMsgRequest):
await util.discord_helpers.send_message(
bot=self.bot,
guild=data.guild,
channel=data.channel,
message=data.message,
)
return {
2025-04-17 14:35:56 -04:00
"result": "presumed_success",
2025-02-13 14:51:35 -05:00
}
2025-04-17 14:35:56 -04:00
2025-02-13 14:51:35 -05:00
def __init__():
2025-03-12 09:37:44 -04:00
import util
2025-04-17 14:35:56 -04:00
2025-02-13 14:51:35 -05:00
importlib.reload(util)
2025-04-17 14:35:56 -04:00
__init__()