Passed
Pull Request — main (#195)
by
unknown
01:35
created

pincer.middleware.channel   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 118
Duplicated Lines 43.22 %

Importance

Changes 0
Metric Value
wmc 11
eloc 38
dl 51
loc 118
rs 10
c 0
b 0
f 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A channel_delete_middleware() 25 25 4
A channel_update_middleware() 26 26 4
A channel_pins_update_middleware() 0 17 1
A export() 0 5 1
A channel_create_middleware() 0 19 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
from __future__ import annotations
4
5
"""Channel Middlewares"""
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
6
7
from typing import TYPE_CHECKING
0 ignored issues
show
introduced by
Import "from typing import TYPE_CHECKING" should be placed at the top of the module
Loading history...
8
9
from ..core.dispatch import GatewayDispatch
0 ignored issues
show
introduced by
Import "from ..core.dispatch import GatewayDispatch" should be placed at the top of the module
Loading history...
10
from ..objects.guild.channel import Channel
0 ignored issues
show
Bug introduced by
The name channel does not seem to exist in module pincer.objects.guild.
Loading history...
introduced by
Cannot import 'objects.guild.channel' due to syntax error 'invalid syntax (<unknown>, line 249)'
Loading history...
introduced by
Import "from ..objects.guild.channel import Channel" should be placed at the top of the module
Loading history...
11
from ..utils.conversion import construct_client_dict
0 ignored issues
show
introduced by
Import "from ..utils.conversion import construct_client_dict" should be placed at the top of the module
Loading history...
12
from ..objects.events.channel import ChannelPinsUpdateEvent
0 ignored issues
show
introduced by
Import "from ..objects.events.channel import ChannelPinsUpdateEvent" should be placed at the top of the module
Loading history...
13
14
if TYPE_CHECKING:
15
    from typing import List, Tuple
16
17
def channel_create_middleware(
18
    self, payload: GatewayDispatch
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
19
) -> Tuple[str, List[Channel]]:
20
    """|coro|
21
22
    Middleware for ``on_channel_creation`` event.
23
24
    Parameters
25
    ----------
26
    payload : :class:`~pincer.core.dispatch.GatewayDispatch`
27
        The data received from the ready event.
28
29
    Returns
30
    -------
31
    Tuple[:class:`str`, List[:class:`~pincer.objects.guild.channel.Channel`]]
32
        ``"on_channel_creation"`` and a channel.
33
    """
34
    return "on_channel_creation", [
35
        Channel.from_dict(construct_client_dict(self, payload.data))
36
    ]
37
38
39 View Code Duplication
async def channel_delete_middleware(self, payload: GatewayDispatch):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
40
    """|coro|
41
42
    Middleware for ``on_channel_delete``,
43
44
    Parameters
45
    ----------
46
    payload : :class:`pincer.core.dispatch.GatewayDispatch`
47
        The data received from the channel delete event.
48
49
    Returns
50
    -------
51
    Tuple[:class:`str`, List[:class:`~pincer.objects.guild.channel.Channel`]]
52
        ``on_channel_delete`` and a ``Channel``
53
    """
54
55
    channel = Channel.from_dict(construct_client_dict(self, payload.data))
56
57
    if channel.guild_id in self.guilds:
58
        guild = self.guilds[channel.guild_id]
59
        old = filter(lambda c: c.id == channel.id, guild.channels)
60
        if old:
61
            guild.channels.remove(old)
62
63
    return "on_channel_delete", [channel]
64
65
66
async def channel_pins_update_middleware(self, payload: GatewayDispatch):
0 ignored issues
show
Unused Code introduced by
The argument self seems to be unused.
Loading history...
67
    """|coro|
68
69
    Middleware for ``on_channel_pins_update``,
70
71
    Parameters
72
    ----------
73
    payload : :class:`pincer.core.dispatch.GatewayDispatch`
74
        The data received from the channel pins update event.
75
76
    Returns
77
    -------
78
    Tuple[:class:`str`, List[:class:`~pincer.objects.guild.channel.Channel`]]
79
        ``on_channel_pins_update`` and a ``Channel``
80
    """
81
82
    return "on_channel_pins_update", [ChannelPinsUpdateEvent.from_dict(payload.data)]
83
84
85 View Code Duplication
async def channel_update_middleware(self, payload: GatewayDispatch):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
86
    """|coro|
87
88
    Middleware for ``on_channel_update`` event.
89
90
    Parameters
91
    ----------
92
    payload : :class:`GatewayDispatch`
93
        The data received from the channel update event.
94
95
    Returns
96
    -------
97
    Tuple[:class:`str`, List[:class:`~pincer.objects.channel.channel.Channel`]]
98
        ``on_channel_update`` and a ``Channel``
99
    """
100
101
    channel = Channel.from_dict(construct_client_dict(self, payload.data))
102
103
    if channel.guild_id in self.guilds.keys():
104
        guild = self.guilds[channel.guild_id]
105
        old = filter(lambda c: c.id == channel.id, guild.channels)
106
        if old:
107
            guild.channels.remove(old)
108
        guild.channels.append(channel)
109
110
    return "on_channel_update", [channel]
111
112
113
def export():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
114
    return (
115
        channel_create_middleware,
116
        channel_delete_middleware,
117
        channel_pins_update_middleware,
118
    )
119