Skip to content

Instantly share code, notes, and snippets.

@haykkh
Created June 24, 2020 10:09
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save haykkh/49ed16a9c3bbe23491139ee6225d6d09 to your computer and use it in GitHub Desktop.
Save haykkh/49ed16a9c3bbe23491139ee6225d6d09 to your computer and use it in GitHub Desktop.
How to run a discord.py bot with FastAPI
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)}
@alanoitaliano
Copy link

alanoitaliano commented Mar 26, 2023

@kovalbogdan95 thanks for Your input, but still the same :(
What webserver are You using?
I'm running Uvicorn and there's one thing I can't wrap my head around:

If I run uvicorn like this:
uvicorn.run("main:app", host="0.0.0.0", port=5000, reload=True)
No problems occur, when I try to run it in "production" mode:
uvicorn.run(app, host="0.0.0.0", port=5000, log_level="info")
That loop error appears. Any ideas maybe?

This is what I tried:

@app.get("/isdcvip/{discordID}")
async def isdcvip(discordID: str):
    print(discordID)
    asyncio.create_task(client.start(DISCORD_TOKEN))
    await asyncio.sleep(1)
    guild = client.get_guild(XXXXXXX)
    vip = discord.utils.get(guild.roles, name="VIP")
    hvip = discord.utils.get(guild.roles, name="Honorable Member")
    member = guild.get_member_named(discordID)

    if member not in guild.members:
        await client.close()
        return 'User not found!'

    if vip in member.roles:
        await client.close()
        return True

    if hvip in member.roles:
        await client.close()
        return "Honorable Member"

    await client.close()
    return False

@titouanfreville
Copy link

@djalo you can try to setup and initialize the async loop before starting anything so they would most likely run on the same loop. If FastAPI creates its own loop it will not solve any thing though. This helped me to go through the same issue when running FastAPI with a mix between sync and async methods and google Firebase client in async mode.

        loop = new_event_loop()
        set_event_loop(loop)

@alanoitaliano
Copy link

@titouanfreville
Thanks for Your input! Gonna save this for another occasion.
I solved that pretty easily. Switched to discord.py library just because was curious.
No more errors, maybe that will help somebody too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment