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