pincer.utils.convert_message.convert_message()   C
last analyzed

Complexity

Conditions 10

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 38
rs 5.9999
c 0
b 0
f 0
cc 10
nop 2

How to fix   Complexity   

Complexity

Complex classes like pincer.utils.convert_message.convert_message() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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 collections import defaultdict
7
from collections.abc import Iterable
8
from typing import TYPE_CHECKING, Tuple, List
9
from typing import Union
10
11
from ..objects.app.interaction_flags import InteractionFlags
12
from ..objects.message.component import MessageComponent
13
from ..objects.message.embed import Embed
14
from ..objects.message.file import File
15
from ..objects.message.message import Message
16
17
PILLOW_IMPORT = True
18
19
try:
20
    from PIL.Image import Image
21
except (ModuleNotFoundError, ImportError):
22
    PILLOW_IMPORT = False
23
24
if TYPE_CHECKING:
25
    from pincer import Client
26
27
MessageConvertable = Union[Embed, Message, str, Tuple, List]
28
29
30
def convert_message(client: Client, message: MessageConvertable) -> Message:
31
    """
32
    Converts a message to a Message object.
33
34
    Parameters
35
    ----------
36
    client : :class:`~pincer.client.Client`
37
        The Client object for the bot
38
    message : MessageConvertable
39
        A value to be converted to a message
40
41
    Returns
42
    -------
43
    :class:`~pincer.objects.message.message.Message`
44
        The message object to be sent
45
    """
46
    if (
47
        message
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
48
        and isinstance(message, Iterable)
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
49
        and not isinstance(message, str)
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
50
    ):
51
        kwargs = defaultdict(list)
52
        for item in message:
53
            list_to_message_dict(item, kwargs)
54
        message = Message(**kwargs)
55
    elif isinstance(message, Embed):
56
        message = Message(embeds=[message])
57
    elif PILLOW_IMPORT and isinstance(message, (File, Image)):
58
        message = Message(attachments=[message])
59
    elif not isinstance(message, Message):
60
        message = (
61
            Message(message)
62
            if message
63
            else Message(
64
                client.received_message, flags=InteractionFlags.EPHEMERAL
65
            )
66
        )
67
    return message
68
69
70
def list_to_message_dict(item, kwargs):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
71
    if isinstance(item, Embed):
72
        kwargs["embeds"].append(item)
73
    elif isinstance(item, File) or (PILLOW_IMPORT and isinstance(item, Image)):
74
        kwargs["attachments"].append(item)
75
    elif isinstance(item, MessageComponent):
76
        kwargs["components"].append(item)
77
    elif isinstance(item, str):
78
        kwargs["content"] = item
79
    elif isinstance(item, InteractionFlags):
80
        kwargs["flags"] = item
81