Skip to main content

Efficiently and intuitively create and manage an HTTP web server running in tandem with a discord.py bot

Project description

dpy-http-server

dpy-http-server allows for efficient an intuitive creation and management of an HTTP web server that runs alongside a discord.py bot.

Key Features

  • Easy to use, yet powerful API the makes spinning up a web server on the same event loop as a discord.py bot painless
  • Supports reloading on cog reload

Getting Started

Here are some examples on how to use this API:

Global Scope

import discord
import server
from aiohttp import web

bot = discord.Bot(command_prefix="!", description="example", intents=discord.Intents.all())

@bot.event
async def on_ready():
    bot.server = server.HTTPServer(
        bot=bot,
        host="0.0.0.0",
        port="8000",
    )
    await bot.server.start()

@server.add_route(path="/", method="GET")
async def home(request):
    return web.json_response(data={"foo": "bar"}, status=200)

bot.run(YOUR_TOKEN)

Inside Bot Subclass

import discord
import server
from aiohttp import web


class Bot(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())

    async def _start_webserver(self):
        await self.wait_until_ready()
        await self.server.start()

    @server.add_route(path="/", method="GET")
    async def home(self, request):
        return web.json_response(data={"foo": bar}, status=200)

bot = Bot(command_prefix="!", description="example", intents=discord.Intents.all())
bot.run(YOUR_TOKEN)

Inside Cog

import server
from aiohttp import web
from discord.ext import commands


class ServerCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.server = server.HTTPServer(
            bot=self.bot,
            host="0.0.0.0",
            port=8000,
        )
        self.bot.loop.create_task(self._start_server())

    async def _start_server(self):
        await self.bot.wait_until_ready()
        await self.server.start()

    @server.add_route(path="/", method="GET", cog="ServerCog")
    async def home(self, request):
        return web.json_response(data={"foo": "bar"}, status=200)

Using Checks with Globally Defined Methods

import discord
import server
from aiohttp import web

bot = discord.Bot(command_prefix="!", description="example", intents=discord.Intents.all())

@bot.event
async def on_ready():
    bot.server = server.HTTPServer(
        bot=bot,
        host="0.0.0.0",
        port="8000",
    )
    await bot.server.start()

async def checker(request):
    return request.headers.get("authorization") == "password"

async def fail_handler(request):
    return web.json_response(data={"message": "you are not authorized"}, status=401)

@server.add_route(path="/", method="GET")
@server.check(predicate=checker, fail_handler=fail_handler)
async def home(request):
    return web.json_response(data={"foo": "bar"}, status=200)

bot.run(YOUR_TOKEN)

Using Checks with Cog Defined Methods

import server
from aiohttp import web
from discord.ext import commands


class ServerCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.server = server.HTTPServer(
            bot=self.bot,
            host="0.0.0.0",
            port=8000,
        )
        self.bot.loop.create_task(self._start_server())

    async def _start_server(self):
        await self.bot.wait_until_ready()
        await self.server.start()

    async def checker(self, request):
        return request.headers.get("authorization") == "password"

    async def fail_handler(self, request):
        return web.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")
    async def home(self, request):
        return web.json_response(data={"foo": "bar"}, status=200)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dpy-http-server-0.1.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

dpy_http_server-0.1.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file dpy-http-server-0.1.0.tar.gz.

File metadata

  • Download URL: dpy-http-server-0.1.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.6

File hashes

Hashes for dpy-http-server-0.1.0.tar.gz
Algorithm Hash digest
SHA256 03f89f5d3793dc47a556857c02f24fb44ff6de9300ef6b2f86e6e85848652f11
MD5 75720c7a977efafffbf4856e9d2e392e
BLAKE2b-256 8a35dbf9af132f6e01e47610d3a2c0b5dd153753ebb5837741aa63887dc1059f

See more details on using hashes here.

File details

Details for the file dpy_http_server-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dpy_http_server-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.6

File hashes

Hashes for dpy_http_server-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 52c3e8403c9b4ae69fbf46071d4d62d66ac76a244f2d4f831fb33a66db6722e1
MD5 8407afd284b95e09a2b70f35c01f73d1
BLAKE2b-256 9c8090bc7171d7723f29da4e94fa1446557298d1510b99b7a80dc96efa0a53ba

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page