Passed
Pull Request — main (#158)
by
unknown
01:43
created

pincer.objects.message.message.Message.isempty()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
# Copyright Pincer 2021-Present
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# Full MIT License can be found in `LICENSE` at the project root.
3
4
from __future__ import annotations
5
6
from dataclasses import dataclass
7
from json import dumps
8
from typing import Dict, Tuple, Union, List, Optional, TYPE_CHECKING
9
10
from aiohttp import FormData, Payload
11
12
from ..guild.role import Role
13
from ..message.embed import Embed
14
from ..message.file import File
15
from ..message.user_message import AllowedMentionTypes
16
from ..user import User
17
from ...exceptions import CommandReturnIsEmpty
18
from ...utils.api_object import APIObject
0 ignored issues
show
introduced by
Cannot import 'utils.api_object' due to syntax error 'invalid syntax (<unknown>, line 69)'
Loading history...
Bug introduced by
The name api_object does not seem to exist in module pincer.utils.
Loading history...
19
from ...utils.snowflake import Snowflake
20
21
PILLOW_IMPORT = True
22
23
try:
24
    from PIL.Image import Image
25
except (ModuleNotFoundError, ImportError):
26
    PILLOW_IMPORT = False
27
28
if TYPE_CHECKING:
29
    from ..app import InteractionFlags
30
    from .component import MessageComponent
31
32
33
@dataclass
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
34
class AllowedMentions(APIObject):
35
    parse: List[AllowedMentionTypes]
36
    roles: List[Union[Role, Snowflake]]
37
    users: List[Union[User, Snowflake]]
38
    reply: bool = True
39
40
    def to_dict(self):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
41
        def get_str_id(obj: Union[Snowflake, User, Role]) -> str:
42
            if hasattr(obj, "id"):
43
                obj = obj.id
44
45
            return str(obj)
46
47
        return {
48
            "parse": self.parse,
49
            "roles": list(map(get_str_id, self.roles)),
50
            "users": list(map(get_str_id, self.users)),
51
            "replied_user": self.reply
52
        }
53
54
55
@dataclass
0 ignored issues
show
best-practice introduced by
Too many instance attributes (8/7)
Loading history...
56
class Message:
57
    # TODO: Docs for tts, allowed_mentions, components, flags, and type.
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
58
59
    """
60
    A discord message that will be send to discord
61
62
    :param content:
63
        The text in the message.
64
65
    :param attachments:
66
        Attachments on the message. This is a File object. You can also attach
67
        a Pillow Image or string. Pillow images will be converted to PNGs. They
68
        will use the naming sceme ``image%`` where % is the images index in the
69
        attachments array. Strings will be read as a filepath. The name of the
70
        file that the string points to will be used as the name.
71
72
    :param embeds:
73
        Embed attached to the message. This is an Embed object.
74
    """
75
    content: str = ''
76
    attachments: Optional[List[File]] = None
77
    tts: Optional[bool] = False
78
    embeds: Optional[List[Embed]] = None
79
    allowed_mentions: Optional[AllowedMentions] = None
80
    components: Optional[List[MessageComponent]] = None
81
    flags: Optional[InteractionFlags] = None
82
    delete_after: Optional[float] = None
83
84
    def __post_init__(self):
85
86
        if self.delete_after and self.delete_after < 0:
87
            raise ValueError(
88
                "Message can not be deleted after a negative amount of "
89
                "seconds!"
90
            )
91
92
        if not self.attachments:
93
            return
94
95
        attch = []
96
97
        for count, value in enumerate(self.attachments):
98
            if isinstance(value, File):
99
                attch.append(value)
100
            elif PILLOW_IMPORT and isinstance(value, Image):
101
                attch.append(File.from_pillow_image(
102
                    value,
103
                    f"image{count}.png",
104
                ))
105
            elif isinstance(value, str):
106
                attch.append(File.from_file(value))
107
            else:
108
                raise ValueError(f"Attachment {count} is invalid type.")
109
110
        self.attachments = attch
111
112
    @property
113
    def isempty(self) -> bool:
114
        """
115
        :return:
116
            Returns true if a message is empty.
117
        """
118
119
        return (
120
                len(self.content) < 1
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (remove 4 spaces).
Loading history...
121
                and not self.embeds
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (remove 4 spaces).
Loading history...
122
                and not self.attachments
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (remove 4 spaces).
Loading history...
123
        )
124
125
    def to_dict(self):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
126
127
        allowed_mentions = (
128
            self.allowed_mentions.to_dict()
129
            if self.allowed_mentions else {}
130
        )
131
132
        # Attachments aren't serialized
133
        # because they are not sent as part of the json
134
        resp = {
135
            "content": self.content,
136
            "tts": self.tts,
137
            "flags": self.flags,
138
            "embeds": [embed.to_dict() for embed in (self.embeds or [])],
139
            "allowed_mentions": allowed_mentions,
140
            "components": [
141
                components.to_dict() for components in (self.components or [])
142
            ]
143
        }
144
145
        # IDE does not recognise return type of filter properly.
146
        # noinspection PyTypeChecker
147
        return dict(filter(
148
            lambda kv: kv[1],
149
            resp.items()
150
        ))
151
152
    def serialize(self) -> Tuple[str, Union[Payload, Dict]]:
153
        """
154
        Creates the data that the discord API wants for the message object
155
156
        :return: (content_type, data)
157
158
        :raises CommandReturnIsEmpty:
159
            Command does not have content, an embed, or attachment.
160
        """
161
162
        if self.isempty:
163
            raise CommandReturnIsEmpty("Cannot return empty message.")
164
165
        if not self.attachments:
166
            return "application/json", self.to_dict()
167
168
        form = FormData()
169
        form.add_field("payload_json", dumps(self.to_dict()))
170
171
        for file in self.attachments:
172
            form.add_field("file", file.content, filename=file.filename)
173
174
        payload = form()
175
        return payload.headers["Content-Type"], payload
176