From 6371ff04a38ed3997b87b8a89b0fa59fe576b052 Mon Sep 17 00:00:00 2001 From: Victor Kotlin Date: Wed, 15 Sep 2021 21:37:10 +0300 Subject: [PATCH] add types of comments, not tested --- boticordpy/__init__.py | 1 + boticordpy/config.py | 6 ++++ boticordpy/types.py | 82 ++++++++++++++++++++++++++++++++++++++++++ boticordpy/webhook.py | 9 ++++- 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 boticordpy/types.py diff --git a/boticordpy/__init__.py b/boticordpy/__init__.py index ed7747c..ffd543a 100644 --- a/boticordpy/__init__.py +++ b/boticordpy/__init__.py @@ -1,2 +1,3 @@ from .client import BoticordClient from .webhook import BoticordWebhook +from .types import * diff --git a/boticordpy/config.py b/boticordpy/config.py index 244e85c..0ccd0b8 100644 --- a/boticordpy/config.py +++ b/boticordpy/config.py @@ -1,4 +1,5 @@ from . import exceptions +from . import types class Config: @@ -10,3 +11,8 @@ class Config: 429: exceptions.ToManyRequests, 500: exceptions.ServerError, 503: exceptions.ServerError} + events_list = { + "new_bot_comment": types.Comment, + "edit_bot_comment": types.EditedComment, + "delete_bot_comment": types.EditedComment + } diff --git a/boticordpy/types.py b/boticordpy/types.py new file mode 100644 index 0000000..f808aea --- /dev/null +++ b/boticordpy/types.py @@ -0,0 +1,82 @@ +from datetime import datetime + + +class CommentData: + """Model that represents edited comment text data. + + Attributes + ----------- + old : :class:`str` or :class:`None` + Old comment text. + new : :class:`str` or :class:`None` + New comment text. + """ + + __slots__ = "old", "new" + + old: str or None + new: str or None + + def __init__(self, raw_data): + self.old = raw_data.get("old") + self.new = raw_data.get("new") + + +class Comment: + """Model that represents information about a comment. + + Attributes + ----------- + raw_data : :class:`dict` + Raw data from the Boticord API. + user_id : :class:`int` + ID of comment author. + comment : :class:`str` + Comment. + at : :class:`datetime.datetime` + The comment creation time. + """ + + __slots__ = "raw_data", "user_id", "comment", "at" + + raw_data: dict + user_id: int + comment: str + at: datetime + + def __init__(self, raw_data): + self.raw_data = raw_data["data"] + self.user_id = int(self.raw_data["user"]) + self.comment = self.raw_data["comment"] + self.at = datetime.fromtimestamp(self.raw_data["at"]) + + def __repr__(self) -> str: + name = self.__class__.__name__ + return ( + f'<{name} user_id={self.user_id} comment={self.comment}>' + ) + + +class EditedComment(Comment): + """Model that represents information about edited comment. + It is inherited from :class:`Comment` + + Attributes + ----------- + comment : :class:`CommentData` + Comment. + """ + + __slots__ = "raw_data", "user_id", "comment", "at" + + comment: CommentData + + def __init__(self, raw_data): + super().__init__(raw_data) + self.comment = CommentData(self.raw_data["comment"]) + + def __repr__(self) -> str: + name = self.__class__.__name__ + return ( + f'<{name} user_id={self.user_id} comment={self.comment.new}>' + ) \ No newline at end of file diff --git a/boticordpy/webhook.py b/boticordpy/webhook.py index 7f76fe1..1c27d4a 100644 --- a/boticordpy/webhook.py +++ b/boticordpy/webhook.py @@ -13,6 +13,7 @@ from aiohttp.web_urldispatcher import _WebHandler from typing import Dict, Union from . import BoticordClient +from . import config class _Webhook(TypedDict): @@ -72,9 +73,15 @@ class BoticordWebhook: if auth == self._webhooks["bot"]["hook_key"]: data = (await request.json()) + event_in_config = config.Config.events_list.get(data["type"]) + + if event_in_config is not None: + data_for_event = event_in_config(data) + else: + data_for_event = data try: - await self.boticord_client.events[data["type"]](data) + await self.boticord_client.events[data["type"]](data_for_event) except: pass