Completed
Push — main ( 3a3ea0...93fa25 )
by Yohann
23s queued 12s
created

pincer.objects.message.message   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 58
dl 0
loc 79
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Message.to_dict() 0 23 4
A AllowedMentions.to_dict() 0 12 2
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 typing import Union, List, Optional, TYPE_CHECKING
8
9
from ..app.interaction_base import CallbackType
10
from ..guild.role import Role
11
from ..message.embed import Embed
12
from ..message.user_message import AllowedMentionTypes
13
from ..user import User
14
from ...exceptions import CommandReturnIsEmpty
15
from ...utils.api_object import APIObject
16
from ...utils.snowflake import Snowflake
17
18
if TYPE_CHECKING:
19
    from ..app import InteractionFlags
20
    from .component import MessageComponent
21
22
23
@dataclass
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
24
class AllowedMentions(APIObject):
25
    parse: List[AllowedMentionTypes]
26
    roles: List[Union[Role, Snowflake]]
27
    users: List[Union[User, Snowflake]]
28
    reply: bool = True
29
30
    def to_dict(self):
31
        def get_str_id(obj: Union[Snowflake, User, Role]) -> str:
32
            if hasattr(obj, "id"):
33
                obj = obj.id
34
35
            return str(obj)
36
37
        return {
38
            "parse": self.parse,
39
            "roles": list(map(get_str_id, self.roles)),
40
            "users": list(map(get_str_id, self.users)),
41
            "replied_user": self.reply
42
        }
43
44
45
@dataclass
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
46
class Message:
47
    # TODO: Write docs
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
48
    content: str = ''
49
    tts: Optional[bool] = False
50
    embeds: Optional[List[Embed]] = None
51
    allowed_mentions: Optional[AllowedMentions] = None
52
    components: Optional[List[MessageComponent]] = None
53
    flags: Optional[InteractionFlags] = None
54
    type: Optional[CallbackType] = None
55
56
    def to_dict(self):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
57
        if len(self.content) < 1 and not self.embeds:
58
            raise CommandReturnIsEmpty("Cannot return empty message.")
59
60
        allowed_mentions = (
61
            self.allowed_mentions.to_dict()
62
            if self.allowed_mentions else {}
63
        )
64
65
        resp = {
66
            "content": self.content,
67
            "tts": self.tts,
68
            "flags": self.flags,
69
            "embeds": [embed.to_dict() for embed in (self.embeds or [])],
70
            "allowed_mentions": allowed_mentions,
71
            "components": [
72
                components.to_dict() for components in (self.components or [])
73
            ]
74
        }
75
76
        return {
77
            "type": self.type or CallbackType.MESSAGE,
78
            "data": {k: i for k, i in resp.items() if i}
79
        }
80