Passed
Push — main ( c094fa...1db6e6 )
by
unknown
01:43
created

pincer.objects.message.AllowedMentions.to_dict()   A

Complexity

Conditions 2

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 12
rs 9.9
c 0
b 0
f 0
cc 2
nop 1
1
# -*- coding: utf-8 -*-
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# MIT License
3
#
4
# Copyright (c) 2021 Pincer
5
#
6
# Permission is hereby granted, free of charge, to any person obtaining
7
# a copy of this software and associated documentation files
8
# (the "Software"), to deal in the Software without restriction,
9
# including without limitation the rights to use, copy, modify, merge,
10
# publish, distribute, sublicense, and/or sell copies of the Software,
11
# and to permit persons to whom the Software is furnished to do so,
12
# subject to the following conditions:
13
#
14
# The above copyright notice and this permission notice shall be
15
# included in all copies or substantial portions of the Software.
16
#
17
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
from __future__ import annotations
25
26
from dataclasses import dataclass
27
from typing import Union, List, Optional
28
29
from .embed import Embed
30
from .interaction_base import CallbackType
31
from .interactions import InteractionFlags
32
from .message_component import MessageComponent
33
from .role import Role
34
from .user import User
35
from .user_message import AllowedMentionTypes
36
from ..exceptions import CommandReturnIsEmpty
37
from ..utils import APIObject, Snowflake
38
39
40
@dataclass
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
41
class AllowedMentions(APIObject):
42
    parse: List[AllowedMentionTypes]
43
    roles: List[Union[Role, Snowflake]]
44
    users: List[Union[User, Snowflake]]
45
    reply: bool = True
46
47
    def to_dict(self):
48
        def get_str_id(obj: Union[Snowflake, User, Role]) -> str:
49
            if hasattr(obj, "id"):
50
                obj = obj.id
51
52
            return str(obj)
53
54
        return {
55
            "parse": self.parse,
56
            "roles": list(map(get_str_id, self.roles)),
57
            "users": list(map(get_str_id, self.users)),
58
            "replied_user": self.reply
59
        }
60
61
62
@dataclass
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
63
class Message:
64
    content: str
65
    tts: Optional[bool] = False
66
    embeds: Optional[List[Embed]] = None
67
    allowed_mentions: Optional[AllowedMentions] = None
68
    components: Optional[List[MessageComponent]] = None
69
    flags: Optional[InteractionFlags] = None
70
    type: Optional[CallbackType] = None
71
72
    def to_dict(self):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
73
        if len(self.content) < 1:
74
            raise CommandReturnIsEmpty("Cannot return empty message.")
75
76
        allowed_mentions = self.allowed_mentions.to_dict() \
77
            if self.allowed_mentions \
78
            else {}
79
80
        resp = {
81
            "content": self.content,
82
            "tts": self.tts,
83
            "flags": self.flags,
84
            "embeds": [embed.to_dict() for embed in (self.embeds or [])],
85
            "allowed_mentions": allowed_mentions,
86
            "components": [
87
                components.to_dict() for components in (self.components or [])
88
            ]
89
        }
90
91
        return {
92
            "type": self.type or CallbackType.MESSAGE,
93
            "data": {k: i for k, i in resp.items() if i}
94
        }
95