-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add FastAPI examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters