add types of comments, not tested

This commit is contained in:
Victor Kotlin 2021-09-15 21:37:10 +03:00
parent f428c65519
commit 6371ff04a3
4 changed files with 97 additions and 1 deletions

View file

@ -1,2 +1,3 @@
from .client import BoticordClient from .client import BoticordClient
from .webhook import BoticordWebhook from .webhook import BoticordWebhook
from .types import *

View file

@ -1,4 +1,5 @@
from . import exceptions from . import exceptions
from . import types
class Config: class Config:
@ -10,3 +11,8 @@ class Config:
429: exceptions.ToManyRequests, 429: exceptions.ToManyRequests,
500: exceptions.ServerError, 500: exceptions.ServerError,
503: exceptions.ServerError} 503: exceptions.ServerError}
events_list = {
"new_bot_comment": types.Comment,
"edit_bot_comment": types.EditedComment,
"delete_bot_comment": types.EditedComment
}

82
boticordpy/types.py Normal file
View file

@ -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}>'
)

View file

@ -13,6 +13,7 @@ from aiohttp.web_urldispatcher import _WebHandler
from typing import Dict, Union from typing import Dict, Union
from . import BoticordClient from . import BoticordClient
from . import config
class _Webhook(TypedDict): class _Webhook(TypedDict):
@ -72,9 +73,15 @@ class BoticordWebhook:
if auth == self._webhooks["bot"]["hook_key"]: if auth == self._webhooks["bot"]["hook_key"]:
data = (await request.json()) 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: try:
await self.boticord_client.events[data["type"]](data) await self.boticord_client.events[data["type"]](data_for_event)
except: except:
pass pass