Package: dpy-http-server
dpy-http-serverdpy-http-serverallows for efficient an intuitive creation and management of
an HTTP web server that runs alongside a discord.py bot.Key FeaturesEasy to use, yet powerful API the makes spinning up a web server on the same
event loop as a discord.py bot painlessSupports reloading on cog reloadGetting StartedHere are some examples on how to use this API:Global Scopeimportdiscordimportserverfromaiohttpimportwebbot=discord.Bot(command_prefix="!",description="example",intents=discord.Intents.all())@bot.eventasyncdefon_ready():bot.server=server.HTTPServer(bot=bot,host="0.0.0.0",port="8000",)awaitbot.server.start()@server.add_route(path="/",method="GET")asyncdefhome(request):returnweb.json_response(data={"foo":"bar"},status=200)bot.run(YOUR_TOKEN)Inside Bot SubclassimportdiscordimportserverfromaiohttpimportwebclassBot(discord.Bot):def__init__(*args,**kwargs):super().__init__(*args,**kwargs):self.server=server.HTTPServer(bot=self,host="0.0.0.0",port=8000,)self.loop.create_task(self._start_webserver())asyncdef_start_webserver(self):awaitself.wait_until_ready()awaitself.server.start()@server.add_route(path="/",method="GET")asyncdefhome(self,request):returnweb.json_response(data={"foo":bar},status=200)bot=Bot(command_prefix="!",description="example",intents=discord.Intents.all())bot.run(YOUR_TOKEN)Inside Cogimportserverfromaiohttpimportwebfromdiscord.extimportcommandsclassServerCog(commands.Cog):def__init__(self,bot):self.bot=botself.server=server.HTTPServer(bot=self.bot,host="0.0.0.0",port=8000,)self.bot.loop.create_task(self._start_server())asyncdef_start_server(self):awaitself.bot.wait_until_ready()awaitself.server.start()@server.add_route(path="/",method="GET",cog="ServerCog")asyncdefhome(self,request):returnweb.json_response(data={"foo":"bar"},status=200)Using Checks with Globally Defined Methodsimportdiscordimportserverfromaiohttpimportwebbot=discord.Bot(command_prefix="!",description="example",intents=discord.Intents.all())@bot.eventasyncdefon_ready():bot.server=server.HTTPServer(bot=bot,host="0.0.0.0",port="8000",)awaitbot.server.start()asyncdefchecker(request):returnrequest.headers.get("authorization")=="password"asyncdeffail_handler(request):returnweb.json_response(data={"message":"you are not authorized"},status=401)@server.add_route(path="/",method="GET")@server.check(predicate=checker,fail_handler=fail_handler)asyncdefhome(request):returnweb.json_response(data={"foo":"bar"},status=200)bot.run(YOUR_TOKEN)Using Checks with Cog Defined Methodsimportserverfromaiohttpimportwebfromdiscord.extimportcommandsclassServerCog(commands.Cog):def__init__(self,bot):self.bot=botself.server=server.HTTPServer(bot=self.bot,host="0.0.0.0",port=8000,)self.bot.loop.create_task(self._start_server())asyncdef_start_server(self):awaitself.bot.wait_until_ready()awaitself.server.start()asyncdefchecker(self,request):returnrequest.headers.get("authorization")=="password"asyncdeffail_handler(self,request):returnweb.json_response(data={"message":"you are not authorized"},status=401)@server.add_route(path="/",method="GET",cog="ServerCog")@server.check(predicate="checker",fail_handler="fail_handler")asyncdefhome(self,request):returnweb.json_response(data={"foo":"bar"},status=200)