disunity
Advanced tools
| class Attachment: | ||
| def __init__(self, filename: str, content: bytes, description: str = None): | ||
| self._filename = filename | ||
| self._content = content | ||
| self._description = description | ||
| @property | ||
| def filename(self) -> str: | ||
| return self._filename | ||
| @filename.setter | ||
| def filename(self, value: str) -> None: | ||
| self._filename = value | ||
| @property | ||
| def description(self) -> str: | ||
| return self._description | ||
| @description.setter | ||
| def description(self, value: str) -> None: | ||
| self._description = value | ||
| @property | ||
| def content(self) -> bytes: | ||
| return self._content | ||
| @content.setter | ||
| def content(self, value: bytes) -> None: | ||
| self._content = value | ||
| def as_dict(self) -> dict: | ||
| return { | ||
| "filename": self._filename, | ||
| "description": self._description, | ||
| "content": self._content, | ||
| } |
+1
-1
| Metadata-Version: 2.1 | ||
| Name: disunity | ||
| Version: 0.1.9 | ||
| Version: 0.1.10 | ||
| Summary: Python framework for Discord interactions using a web server | ||
@@ -5,0 +5,0 @@ Author: Tadeo Murillo, |
+1
-1
@@ -13,3 +13,3 @@ [build-system] | ||
| name = "disunity" | ||
| version = "0.1.9" | ||
| version = "0.1.10" | ||
| authors = [ | ||
@@ -16,0 +16,0 @@ {name="Tadeo Murillo"}, |
| Metadata-Version: 2.1 | ||
| Name: disunity | ||
| Version: 0.1.9 | ||
| Version: 0.1.10 | ||
| Summary: Python framework for Discord interactions using a web server | ||
@@ -5,0 +5,0 @@ Author: Tadeo Murillo, |
@@ -17,2 +17,3 @@ LICENSE | ||
| src/disunity/models/__init__.py | ||
| src/disunity/models/attachment.py | ||
| src/disunity/models/components.py | ||
@@ -19,0 +20,0 @@ src/disunity/models/context.py |
| from . import utils | ||
| from .embed import Embed | ||
| from .identifiers import SubOption | ||
| from .models import Message | ||
| from .models import Message, Attachment | ||
| from .package import Package | ||
| from .server import DisunityServer |
@@ -22,9 +22,5 @@ from __future__ import annotations | ||
| if self._title is not None: | ||
| self._title = str(self.title) | ||
| self.__json["title"] = self.title | ||
| self.title = title | ||
| self.description = description | ||
| if self._description is not None: | ||
| self.__json["description"] = str(self._description) | ||
| @property | ||
@@ -31,0 +27,0 @@ def description(self) -> None | str: |
@@ -16,1 +16,2 @@ from .components import ( | ||
| from .user import User | ||
| from .attachment import Attachment |
@@ -61,3 +61,3 @@ from __future__ import annotations | ||
| style: int = ButtonStyles.PRIMARY, | ||
| emoji: dict = {}, | ||
| emoji: dict | None = None, | ||
| url: str | None = None, | ||
@@ -71,5 +71,8 @@ disabled: bool = False, | ||
| "style": style, | ||
| "emoji": emoji, | ||
| "disabled": disabled, | ||
| "disabled": disabled | ||
| } | ||
| # Very wonky patch for whatever the hell Discord just did | ||
| if emoji and isinstance(emoji, dict): | ||
| self.dict["emoji"] = emoji | ||
@@ -100,3 +103,3 @@ if self.dict["style"] == ButtonStyles.LINK and url is not None: | ||
| def __init__( | ||
| self, label: str, value: str | int, description: str = "", emoji: dict = {} | ||
| self, label: str, value: str | int, description: str = "", emoji: dict | None = None | ||
| ): | ||
@@ -106,6 +109,8 @@ self.dict = { | ||
| "value": value, | ||
| "description": description, | ||
| "emoji": emoji, | ||
| "description": description | ||
| } | ||
| if emoji and isinstance(emoji, dict): | ||
| self.dict["emoji"] = emoji | ||
| def to_dict(self) -> dict: | ||
@@ -112,0 +117,0 @@ return self.dict |
@@ -5,2 +5,3 @@ from .. import embed, errors, utils | ||
| from .message import Message | ||
| from .attachment import Attachment | ||
@@ -73,2 +74,3 @@ | ||
| components: list[ActionRow] | ActionRow = [], | ||
| attachments: list[Attachment] | Attachment = [], | ||
| ephemeral: bool = False, | ||
@@ -86,2 +88,4 @@ ) -> Message: | ||
| List of components to send with the message | ||
| attachments: List[Attachment] | Attachment | ||
| List of attachments to send with the message | ||
| ephemeral: bool | ||
@@ -100,2 +104,4 @@ Will the message show publically or privately | ||
| components = [components] | ||
| if isinstance(attachments, Attachment): | ||
| attachments = [attachments] | ||
@@ -108,2 +114,7 @@ message_body = { | ||
| ], | ||
| "attachments": [ | ||
| {"id": i, "filename": att.filename, "description": att.description} | ||
| for i, att in enumerate(attachments) | ||
| if isinstance(att, Attachment) | ||
| ], | ||
| } | ||
@@ -114,2 +125,10 @@ | ||
| files = None | ||
| if attachments: | ||
| files = [ | ||
| {"id": i, "filename": att.filename, "content": att.content} | ||
| for i, att in enumerate(attachments) | ||
| if isinstance(att, Attachment) | ||
| ] | ||
| maybe_message = await self._app.make_https_request( | ||
@@ -119,2 +138,3 @@ "POST", | ||
| payload=message_body, | ||
| files=files, | ||
| ) | ||
@@ -121,0 +141,0 @@ |
@@ -32,2 +32,3 @@ from __future__ import annotations | ||
| self.embeds: list | list[dict] = raw_message["embeds"] | ||
| self.attachments: list | list[dict] = raw_message.get("attachements") | ||
| self.content: str = raw_message["content"] | ||
@@ -34,0 +35,0 @@ self.interaction_token: str | None = token |
| import asyncio | ||
| import importlib | ||
| import json | ||
| import mimetypes | ||
| import time | ||
@@ -100,2 +102,3 @@ | ||
| payload: None | dict = None, | ||
| files: None | dict = None, | ||
| override_checks: bool = False, | ||
@@ -109,3 +112,2 @@ ): | ||
| The method to use. Can be either: GET, PUT, PATCH, DELETE, POST | ||
| url: str | ||
@@ -119,17 +121,38 @@ The url to make the request to. If performing a request to discord, do not include | ||
| The payload to send to the url. Only applicable for POST, PUT, and PATCH requests. | ||
| files: Optional[list] | ||
| A list of dictionaries with 'id', 'filename' and 'content'. | ||
| """ | ||
| if not url.startswith("https://"): | ||
| url = "https://discord.com/api/v10/" + url | ||
| if headers is not None: | ||
| _headers = headers | ||
| elif headers is None and self.config["BOT_TOKEN"] is not None: | ||
| _headers = {"Authorization": "Bot " + self.config["BOT_TOKEN"]} | ||
| if headers is None and self.config["BOT_TOKEN"] is not None: | ||
| headers = {"Authorization": "Bot " + self.config["BOT_TOKEN"]} | ||
| elif headers is None: | ||
| headers = self.auth() | ||
| if files: | ||
| data = aiohttp.FormData() | ||
| if payload: | ||
| data.add_field( | ||
| "payload_json", | ||
| json.dumps(payload), | ||
| content_type="application/json", | ||
| ) | ||
| for file in files: | ||
| content_type, _ = mimetypes.guess_type(file["filename"]) | ||
| if content_type is None: | ||
| content_type = "application/octet-stream" | ||
| data.add_field( | ||
| f"files[{file['id']}]", | ||
| file["content"], | ||
| filename=file["filename"], | ||
| content_type=content_type, | ||
| ) | ||
| request_kwargs = {"data": data} | ||
| else: | ||
| _headers = self.auth() | ||
| request_kwargs = {"json": payload} if payload else {} | ||
| async with aiohttp.ClientSession() as session: | ||
| async with session.request( | ||
| method, url, headers=_headers, json=payload | ||
| method, url, headers=headers, **request_kwargs | ||
| ) as maybe_response: | ||
@@ -161,2 +184,23 @@ if override_checks: | ||
| async def global_check(self, context: Context) -> bool: | ||
| """Global check for all application commands, message components and modal submits. | ||
| Args: | ||
| context (Context): Application command context. | ||
| Returns: | ||
| bool: Command is executed only if `True` is returned. | ||
| `True` is returned by default. | ||
| """ | ||
| return True | ||
| async def global_before_interaction(self, context: Context): | ||
| """Called before the execution of any application command, message components and modal submits. | ||
| This is called after global check is handled. | ||
| Args: | ||
| context (Context): Application command or message component context. | ||
| """ | ||
| pass | ||
| async def interactions(self): | ||
@@ -211,2 +255,11 @@ self.verify(request) | ||
| raise errors.CommandNotFound(ctx.command_name) | ||
| check = await self.global_check(ctx) | ||
| if check != True: | ||
| if isinstance(check, dict) and "type" in check: | ||
| return jsonify(check) | ||
| else: | ||
| return jsonify({"type": utils.InteractionCallbackTypes.PONG}) | ||
| await self.global_before_interaction(ctx) | ||
@@ -239,2 +292,11 @@ if coroutine.ack: | ||
| check = await self.global_check(context) | ||
| if check != True: | ||
| if isinstance(check, dict) and "type" in check: | ||
| return jsonify(check) | ||
| else: | ||
| return jsonify({"type": utils.InteractionCallbackTypes.PONG}) | ||
| await self.global_before_interaction(context) | ||
| if component.ack: | ||
@@ -285,3 +347,12 @@ response = { | ||
| check = await self.global_check(context) | ||
| if check != True: | ||
| if isinstance(check, dict) and "type" in check: | ||
| return jsonify(check) | ||
| else: | ||
| return jsonify({"type": utils.InteractionCallbackTypes.PONG}) | ||
| await self.global_before_interaction(context) | ||
| maybe_response = await component(context) | ||
| return jsonify(maybe_response) |
Alert delta unavailable
Currently unable to show alert delta for PyPI packages.
60647
8.4%25
4.17%1361
8.88%