Passed
Pull Request — main (#195)
by
unknown
02:29 queued 01:00
created

pincer.middleware.channel   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 119
Duplicated Lines 42.86 %

Importance

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

5 Functions

Rating   Name   Duplication   Size   Complexity  
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
A channel_delete_middleware() 25 25 4

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