2424DEALINGS IN THE SOFTWARE.
2525"""
2626
27- import asyncio
28- #import string
29- #import random
3027import logging
28+ from aiohttp import web
3129
32- from . import __version__ as library_version
3330from .http import HTTPClient
34- #from .errors import InvalidArgument
3531
3632log = logging .getLogger (__name__ )
3733
@@ -56,29 +52,34 @@ class Client:
5652 Defaults to ``bot.loop``.
5753 **session : Optional
5854 The aiohttp session to use for requests to the API.
55+ **webhook_auth: Optional
56+ The string for Authorization you set on the site for verification.
57+ **webhook_path: Optional
58+ The path for the webhook request.
59+ **webhook_port: Optional
60+ The port to run the webhook on. Will activate webhook when set.
5961 """
6062
6163 def __init__ (self , bot , token , ** kwargs ):
6264 self .bot = bot
6365 self .bot_id = None
6466 self .loop = kwargs .get ('loop' ) or bot .loop
67+ self .webhook_port = kwargs .get ('webhook_port' )
68+ self .webhook_auth = kwargs .get ('webhook_auth' ) or ''
69+ self .webhook_path = kwargs .get ('webhook_path' ) or '/dblwebhook'
6570 self .http = HTTPClient (token , loop = self .loop , session = kwargs .get ('session' ))
6671 self ._is_closed = False
67- self .loop .create_task (self .__ainit__ ())
68-
69- self .get_server_count = self .get_guild_count
70- self .post_server_count = self .post_guild_count
72+ self .task1 = self .loop .create_task (self .__ainit__ ())
73+ if self .webhook_port :
74+ self .task2 = self .loop .create_task (self .webhook ())
7175
7276 async def __ainit__ (self ):
7377 await self .bot .wait_until_ready ()
7478 self .bot_id = self .bot .user .id
7579
7680 def guild_count (self ):
7781 """Gets the guild count from the Client/Bot object"""
78- try :
79- return len (self .bot .guilds )
80- except AttributeError :
81- return len (self .bot .servers )
82+ return len (self .bot .guilds )
8283
8384 async def get_weekend_status (self ):
8485 """This function is a coroutine.
@@ -91,7 +92,8 @@ async def get_weekend_status(self):
9192 weekend status: bool
9293 The boolean value of weekend status.
9394 """
94- return await self .http .get_weekend_status ()
95+ data = await self .http .get_weekend_status ()
96+ return data ['is_weekend' ]
9597
9698 async def post_guild_count (
9799 self ,
@@ -348,12 +350,32 @@ async def get_widget_small(self, bot_id: int = None):
348350 url = 'https://discordbots.org/api/widget/lib/{0}.png' .format (bot_id )
349351 return url
350352
353+ async def webhook (self ):
354+ async def vote_handler (request ):
355+ req_auth = request .headers .get ('Authorization' )
356+ if self .webhook_auth == req_auth :
357+ data = await request .json ()
358+ self .bot .dispatch ('dbl_vote' , data )
359+ return web .Response ()
360+ else :
361+ return web .Response (status = 401 )
362+
363+ app = web .Application (loop = self .loop )
364+ app .router .add_post (self .webhook_path , vote_handler )
365+ runner = web .AppRunner (app )
366+ await runner .setup ()
367+ self ._webserver = web .TCPSite (runner , '0.0.0.0' , self .webhook_port )
368+ await self ._webserver .start ()
369+
351370 async def close (self ):
352371 """This function is a coroutine.
353372
354373 Closes all connections."""
355374 if self ._is_closed :
356375 return
357376 else :
377+ await self ._webserver .stop ()
358378 await self .http .close ()
359- self ._is_closed = True
379+ self .task1 .cancel ()
380+ self .task2 .cancel ()
381+ self ._is_closed = True
0 commit comments