I’ve got what I would consider a fairly basic attempt at starting discord.py under FastAPI.
It just crashes with errno -3 temporary failure in name resolution
I’ve included most of the relevant code, in case I’m overlooking something simple.
@typechecked
class UnfilteredBot(Bot):
async def process_commands(self, message:Message)->None:
""" /questions/68581659/i-want-my-bot-to-process-commands-sent-by-other-bots """
ctx:Context = await self.get_context(message)
await self.invoke(ctx)
@typechecked
class MyBot(UnfilteredBot):
def __init__(self, command_prefix:str='!')->None:
""" /questions/30633889/typeerror-super-does-not-take-key-word-arguments """
intents: Intents = Intents.default()
#intents.members = True
# allow to get commands from GC
intents.message_content = True # v2
super().__init__(intents=intents, command_prefix=command_prefix)
@typechecked
def get_app(token:str, guild:str, channel:int, rest_key:str)->FastAPI:
shutdown :Event = Event() # from threading import Event
#complete :Event = Event()
app :FastAPI = FastAPI()
bot :Bot = MyBot()
@app.on_event("startup")
async def startup_event()->Coroutine[None]: # this function will run before the main API starts
""" /questions/65460672/expose-discord-bot-to-api-flask-fastapi """
#await bot.add_cog(BasicCog (bot))
#await bot.add_cog(MessageCog(bot, channel, ai_bot_pipe))
bot_start:Coroutine[None] = bot.start(token)
create_task( bot_start )
@app.on_event("shutdown")
async def shutdown_event()->Coroutine[None]:
""" /questions/75975807/how-to-stop-a-loop-on-shutdown-in-fastapi """
shutdown.set()
#while not complete.wait(1): # gently stop a different bot's loop
# print("waiting")
return app
@typechecked
def main()->None:
load_dotenv () # from dotenv import load_dotenv
token :str = environ['DISCORD_TOKEN']
guild :str = environ['DISCORD_GUILD']
channel :int = int(environ['DISCORD_CHANNEL'])
host :str = getenv('HOST', 'locahost') # <-- nevermind, guys. I found it
port :int = getenv('PORT', 5000)
setup_logging() # from discord.utils import setup_logging
app :FastAPI = get_app(token, guild, channel, rest_key)
urun(app, host=host, port=port) # from uvicorn import run as urun
if __name__ == '__main__':
exit(main())
pre-post update:
Solved. I’ll go ahead and post anyway, since I couldn’t find an example with cogs and graceful termination
Reading the ‘similar question’ here:
FastAPI, asyncpg (postgres) and Docker: wsocket.gaierror: [Errno -3] Temporary failure in name resolution
Made me wonder why in the world “localhost” would cause a problem… and then I noticed the typo.