initial push
This commit is contained in:
49
api.py
Normal file
49
api.py
Normal file
@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env python3.12
|
||||
|
||||
import importlib
|
||||
from typing import Optional
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from pydantic import BaseModel
|
||||
import util
|
||||
|
||||
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
|
||||
|
||||
class API:
|
||||
"""API [FastAPI Instance] for Havoc"""
|
||||
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")
|
||||
|
||||
@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 {
|
||||
'result': "presumed_success",
|
||||
}
|
||||
|
||||
|
||||
def __init__():
|
||||
import util # pylint: disable=redefined-outer-name, reimported, import-outside-toplevel
|
||||
importlib.reload(util)
|
||||
|
||||
__init__()
|
Reference in New Issue
Block a user