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 |
|
|
|
|
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 |
|
|
|
|
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): |
|
|
|
|
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): |
|
|
|
|
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): |
|
|
|
|
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(): |
|
|
|
|
118
|
|
|
return channel_create_middleware, channel_delete_middleware, channel_pins_update_middleware |
119
|
|
|
|