- 
      
- 
        Save siddAhmed/67ecad8da3d89248f61e1b67816bd17c to your computer and use it in GitHub Desktop. 
    How to run a discord.py bot with FastAPI
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | import asyncio | |
| import discord | |
| from fastapi import FastAPI | |
| app = FastAPI() | |
| client = discord.Client() | |
| # where the magic happens | |
| # register an asyncio.create_task(client.start()) on app's startup event | |
| # ^ note not client.run() | |
| @app.on_event("startup") | |
| async def startup_event(): | |
| asyncio.create_task(client.start('token')) | |
| @app.get("/") | |
| async def read_root(): | |
| return {"Hello": str(client.user)} | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment