From 2d46d5c35de0453acef2e6f94736ad03abe24a31 Mon Sep 17 00:00:00 2001 From: pixeldeee Date: Thu, 3 Aug 2023 20:14:45 +0300 Subject: [PATCH] reply method --- pytecord/client.py | 2 +- pytecord/guild.py | 10 ++++++++-- pytecord/utils.py | 12 ++++++++++-- pytecord/web.py | 6 +++--- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/pytecord/client.py b/pytecord/client.py index 427b198..8cbdf0e 100644 --- a/pytecord/client.py +++ b/pytecord/client.py @@ -19,7 +19,7 @@ def __init__(self, token: str, debug: bool = False) -> None: @property def user(self) -> User: - return User(self.webhook.get_user(self.__user_id)) + return User(self.webhook.get_user(self.__user_id), self.token) def listen(self): def decorator(func_to_decorate: Callable[..., Coroutine[Any, Any, Any]]): diff --git a/pytecord/guild.py b/pytecord/guild.py index c49435a..10ced1b 100644 --- a/pytecord/guild.py +++ b/pytecord/guild.py @@ -105,7 +105,7 @@ def __init__(self, data: dict[str, Any], token: str): self.bitrate = data.get('bitrate') self.user_limit = data.get('user_limit') self.rate_limit_per_user = data.get('rate_limit_per_user') - self.recipients = [User(i) for i in x] if (x := data.get('recipients')) else None + self.recipients = [User(i, token) for i in x] if (x := data.get('recipients')) else None self.icon = data.get('icon') self.owner_id = int(x) if (x := data.get('owner_id')) else None self.application_id = int(x) if (x := data.get('application_id')) else None @@ -210,7 +210,7 @@ def __init__(self, data: dict[str, Any], token: str) -> None: self.edited_timestamp = data.get('edited_timestamp') self.tts = data.get('tts') self.mention_everyone = data.get('mention_everyone') - self.mentions = [User(i) for i in x] if (x := data.get('mentions')) else None + self.mentions = [User(i, token) for i in x] if (x := data.get('mentions')) else None self.mention_roles = data.get('mention_roles') self.mention_channels = data.get('mention_channels') self.attachments = data.get('attachments') @@ -279,6 +279,12 @@ def eval(self) -> dict[str, Any]: ``` """ return self.__data + + async def reply(self, content: str) -> 'Message': + payload = MessagePayload(content) + payload.make_reply(self.id) + data = await apost(f'/channels/{self.__channel_id}/messages', self.__token, data=payload.eval()) + return Message(data, self.__token) class MessageDeleteEvent: def __init__(self, data: dict[str, Any], token: str) -> None: diff --git a/pytecord/utils.py b/pytecord/utils.py index 1530aa4..d9e1b8a 100644 --- a/pytecord/utils.py +++ b/pytecord/utils.py @@ -1,5 +1,6 @@ from requests import get, post from aiohttp import ClientSession +import json from typing import Literal, Any @@ -10,11 +11,18 @@ def __init__(self, content: str) -> None: self.json = { 'content': content } + + def make_reply(self, message_id: int) -> None: + self.json['message_reference'] = { + 'message_id': message_id + } + def eval(self) -> dict[str, Any]: - return self.json + return json.dumps(self.json) + def get_headers(token: str): - return {'Authorization': f'Bot {token}'} + return {'Authorization': f'Bot {token}', 'Content-Type': 'application/json'} def rget(endpoint: str, token: str = None, headers: dict[str, Any] = None): if token: diff --git a/pytecord/web.py b/pytecord/web.py index b7a2e1b..56660ea 100644 --- a/pytecord/web.py +++ b/pytecord/web.py @@ -7,7 +7,7 @@ from asyncio import gather, create_task from asyncio import sleep as asleep from .config import GATEWAY_VERSION -from .utils import rget +from .utils import get_headers, rget if TYPE_CHECKING: from .guild import Guild, GuildChannel @@ -136,7 +136,7 @@ async def check_events(self): else: continue if self.debug: - print(f'DEBUG op:{data.op} s:{data.s} t:{data.t} d:{data.d}') + print(f'DEBUG op:{data.op} s:{data.s} t:{data.t} d:{data.d}', end='\n' * 2) await self.listener.listen(data) async def run(self, intents: int = None): @@ -163,7 +163,7 @@ class BaseWebhook: def __init__(self, token: str, debug: bool) -> None: self.token = token self.debug = debug - self.headers = {'Authorization': f'Bot {self.token}',} + self.headers = get_headers(token) self.listener = DataStreamListener() self.stream = DataStream(self.listener, self.headers, self.token, self.debug)