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

channel_pins_update_middleware()   A

Complexity

Conditions 1

Size

Total Lines 18
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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