forked from top-gg-community/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook.py
More file actions
99 lines (80 loc) · 3.39 KB
/
webhook.py
File metadata and controls
99 lines (80 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
"""
The MIT License (MIT)
Copyright (c) 2020 Assanali Mukhanov
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
import logging
from typing import Callable, Dict, Union
import aiohttp
import discord
from aiohttp import web
log = logging.getLogger(__name__)
class WebhookManager:
"""
This class is used as a manager for top.gg webhooks.
Parameters
----------
bot: discord.Client
The Client object that will be utilized by this manager's webhook(s) to emit events.
"""
__app: web.Application
_webhooks: Dict[str, Dict[str, Union[str, Callable]]]
_webserver: web.TCPSite
_is_closed: bool
def __init__(self, bot: discord.Client):
self.bot = bot
self._webhooks = {}
self.__app = web.Application()
self._is_closed = False
def dbl_webhook(self, path: str, auth_key: str):
self._webhooks["dbl"] = {
"path": path or "/dbl",
"auth": auth_key or "",
"func": self._bot_vote_handler
}
def dsl_webhook(self, path: str, auth_key: str):
self._webhooks["dsl"] = {
"path": path or "/dsl",
"auth": auth_key or "",
"func": self._guild_vote_handler
}
async def _bot_vote_handler(self, request: aiohttp.web.Request):
data = await request.json()
auth = request.headers.get("Authorization", "")
if auth == self._webhooks["dbl"]["auth"]:
self.bot.dispatch("dbl_vote", data)
return web.Response(status=200, text="OK")
return web.Response(status=401, text="Unauthorized")
async def _guild_vote_handler(self, request: aiohttp.web.Request):
data = await request.json()
auth = request.headers.get("Authorization", "")
if auth == self._webhooks["dsl"]["auth"]:
self.bot.dispatch("dsl_vote", data)
return web.Response(status=200, text="OK")
return web.Response(status=401, text="Unauthorized")
async def run(self, port: int):
for webhook in self._webhooks:
self.__app.router.add_post(self._webhooks[webhook]["path"], self._webhooks[webhook]["func"])
runner = web.AppRunner(self.__app)
await runner.setup()
self._webserver = web.TCPSite(runner, '0.0.0.0', port)
await self._webserver.start()
self._is_closed = False
async def close(self):
await self._webserver.stop()
self._is_closed = True