Webhooks
Attention
In order for webhooks to work, the port you provide to WebhookManager.run()
must be accessible, meaning your firewall must allow incoming requests to it.
Note
WebhookManager
exposes the internal webserver instance via the WebhookManager.webserver
property.
WebhookManager
- class topgg.WebhookManager(bot: discord.client.Client)[source]
This class is used as a manager for the Top.gg webhook.
Methods
WebhookManager.dbl_webhook()
andWebhookManager.dsl_webhook()
return a modified version of the object, allowing for method chaining.- Parameters
bot (discord.Client) – The Client object that will be utilized by this manager’s webhook(s) to emit events.
- dbl_webhook(route: str = '/dbl', auth_key: str = '') topgg.webhook.WebhookManager [source]
Helper method that configures a route that listens to bot votes.
- Parameters
- Returns
Modified version of the object
- Return type
- dsl_webhook(route: str = '/dsl', auth_key: str = '') topgg.webhook.WebhookManager [source]
Helper method that configures a route that listens to server votes.
- Parameters
- Returns
Modified version of the object
- Return type
- run(port: int) asyncio.Task[None] [source]
Runs the webhook.
- Parameters
port (int) – The port to run the webhook on.
- property webserver: aiohttp.web_app.Application
Returns the internal web application that handles webhook requests.
- Returns
The internal web application.
- Return type
Examples
Helper methods:
import discord
import topgg
bot = discord.Client(...) # Initialize a discord.py client
# WebhookManager helper methods allow method chaining, therefore the lines below are valid
bot.topgg_webhook = topgg.WebhookManager(bot)\
.dbl_webhook("/dbl", "dbl_auth")\
.dsl_webhook("/dsl", "dsl_auth")
Via the webserver
property:
You can utilize the internal aiohttp.web.Application
via the WebhookManager.webserver
property to add custom routes manually.
import discord
import topgg
from aiohttp import web
dbl_auth = "Your DBL Authorization key" # Leave empty to allow all requests
bot = discord.Client(...) # Initialize a discord.py client
async def bot_vote_handler(request):
auth = request.headers.get("Authorization", "") # Default value will be empty string to allow all requests
if auth == dbl_auth:
# Process the vote and return response code 2xx
return web.Response(status=200, text="OK")
# Return code 401 if authorization fails
# 4xx response codes tell Top.gg services not to retry the request
return web.Response(status=401, text="Authorization failed")
bot.topgg_webhook = topgg.WebhookManager(bot)
bot.topgg_webhook.webserver.router.add_post(path="/dbl", handler=bot_vote_handler)