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
|
|
|
def channel_create_middleware( |
18
|
|
|
self, |
19
|
|
|
payload: GatewayDispatch |
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
|
|
View Code Duplication |
async def channel_delete_middleware(self, payload: GatewayDispatch): |
|
|
|
|
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): |
|
|
|
|
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", [ |
83
|
|
|
ChannelPinsUpdateEvent.from_dict(payload.data) |
84
|
|
|
] |
85
|
|
|
|
86
|
|
View Code Duplication |
async def channel_update_middleware(self, payload: GatewayDispatch): |
|
|
|
|
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(): |
|
|
|
|
115
|
|
|
return channel_create_middleware, channel_delete_middleware, channel_pins_update_middleware |
116
|
|
|
|