Skip to content

Commit

Permalink
Add FastAPI examples (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
No767 authored Dec 9, 2024
1 parent f3b2a62 commit f238cb0
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/57.doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add FastAPI examples
44 changes: 44 additions & 0 deletions examples/fastapi/bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import logging

import discord
from discord.ext import commands, ipcx


class MyBot(commands.Bot):
def __init__(self, intents: discord.Intents, *args, **kwargs):
super().__init__(command_prefix="!", intents=intents, *args, **kwargs)
self.ipc = ipcx.Server(self, secret_key="my_secret_key") # nosec
self.log = logging.getLogger("discord.ext.ipcx")

async def setup_hook(self):
"""One time setup hook"""
await self.ipc.start()

async def on_ipc_ready(self):
"""Called when the IPC server is starting up"""
self.log.info("Starting IPC Server")

async def on_ipc_error(self, endpoint, error):
"""Called upon an error being raised within an IPC route"""
self.log.error("Error in %s: %s", endpoint, error)


intents = discord.Intents.default()
intents.message_content = True

TOKEN = "INSERT YOUR TOKEN HERE" # nosec
bot = MyBot(intents=intents)


# This route returns the member count of the guild with the ID provided
@bot.ipc.route()
async def get_member_count(data):
guild = bot.get_guild(data.guild_id)

if guild is None:
return 0
return guild.member_count


if __name__ == "__main__":
bot.run(TOKEN)
40 changes: 40 additions & 0 deletions examples/fastapi/webserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from __future__ import annotations

from contextlib import asynccontextmanager
from typing import TYPE_CHECKING

import uvicorn
from fastapi import FastAPI

from discord.ext import ipcx

if TYPE_CHECKING:
from typing_extensions import Self


class MyApp(FastAPI):
client: ipcx.Client

def __init__(self, *args, **kwargs):
super().__init__(lifespan=self.lifespan, *args, **kwargs)

@asynccontextmanager
async def lifespan(self, app: Self):
async with ipcx.Client(secret_key="my_secret_key") as app.client: # nosec # secret_key must be the same as your server
yield


app = MyApp()


@app.get("/")
async def index():
member_count = await app.client.request(
"get_member_count", guild_id=12345678
) # get the member count of server with ID 12345678

return str(member_count) # display member count


if __name__ == "__main__":
uvicorn.run(app)
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ dev = [
"pyright>=1.1.386,<2",
"ruff>=0.7.1,<1",
"tox>=4.23.2,<5",
"quart>=0.19.8,<1",
"towncrier>=24.8.0,<25"
]

examples = [
"quart>=0.19.8,<1",
"fastapi[standard]>=0.115.6,<1"
]

build = [
"build>=1.2.2.post1,<2"
]
Expand Down

0 comments on commit f238cb0

Please sign in to comment.