/better-cluster-fastapi

A Better cluster fork made with fastapi

Primary LanguagePythonMIT LicenseMIT

Better Cluster Fastapi

A high-performance inter-process communication library designed to handle communication between multiple shards

This library is made to handle multiple discord clients. If you want something simpler or have only one client, check out better-ipc

Installation

Stable version

For Linux

python3 -m pip install -U git+https://github.com/chredeur0/better-cluster-fastapi

For Windows

py -m pip install -U git+https://github.com/chredeur0/better-cluster-fastapi

Support

You can join the support server here

Examples

Example of a shard

import asyncio
import discord
import logging

from discord.ext import commands
from typing import Optional

from discord.ext.cluster import Shard, ClientPayload
from discord.ext.cluster.errors import ClusterBaseError

logging.basicConfig(level=logging.INFO)

logging.getLogger("discord.http").disabled = True
logging.getLogger("discord.client").disabled = True
logging.getLogger("discord.gateway").disabled = True

endpoints_list = []


class MyBot(commands.Bot):
    def __init__(self) -> None:
        intents = discord.Intents.all()

        super().__init__(
            command_prefix="$.",
            intents=intents
        )

        self.shard = Shard(self, identifier=1, endpoints_list=endpoints_list)

    async def setup_hook(self) -> None:
        await self.shard.connect()

    @staticmethod
    def route(name: Optional[str] = None):
        def decorator(func):
            endpoints_list.append((name or func.__name__, func))
            return func
        return decorator
        
    @route()
    async def get_user_data(self, data: ClientPayload):
        user = self.get_user(data.user_id)
        return user._to_minimal_user_json()
    
    @bot.event
    async def on_shard_error(self, endpoint: str, error: ClusterBaseError):
        raise error

    @bot.event
    async def on_shard_ready(self):
        print(f'shard On')

        
if __name__ == '__main__':
    bot = MyBot()
    asyncio.run(bot.run(...))

Example of web client

from quart import Quart
from discord.ext.client import Client

app = Quart(__name__)
ipc = Client(host="127.0.0.1", secret_key="secret", standard_port=1025)


@app.route('/')
async def main():
    return await ipc.request(endpoint="get_user_data", bot_id=812993088749961236, identifier=1, user_id=383946213629624322)

if __name__ == '__main__':
    app.run(port=8000, debug=True)