|
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, payload: GatewayDispatch |
|
|
|
|
|
|
19
|
|
|
) -> Tuple[str, List[Channel]]: |
|
20
|
|
|
"""|coro| |
|
21
|
|
|
|
|
22
|
|
|
Middleware for ``on_channel_creation`` event. |
|
23
|
|
|
|
|
24
|
|
|
Parameters |
|
25
|
|
|
---------- |
|
26
|
|
|
payload : :class:`~pincer.core.dispatch.GatewayDispatch` |
|
27
|
|
|
The data received from the ready event. |
|
28
|
|
|
|
|
29
|
|
|
Returns |
|
30
|
|
|
------- |
|
31
|
|
|
Tuple[:class:`str`, List[:class:`~pincer.objects.guild.channel.Channel`]] |
|
32
|
|
|
``"on_channel_creation"`` and a channel. |
|
33
|
|
|
""" |
|
34
|
|
|
return "on_channel_creation", [ |
|
35
|
|
|
Channel.from_dict(construct_client_dict(self, payload.data)) |
|
36
|
|
|
] |
|
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", [ChannelPinsUpdateEvent.from_dict(payload.data)] |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
View Code Duplication |
async def channel_update_middleware(self, payload: GatewayDispatch): |
|
|
|
|
|
|
86
|
|
|
"""|coro| |
|
87
|
|
|
|
|
88
|
|
|
Middleware for ``on_channel_update`` event. |
|
89
|
|
|
|
|
90
|
|
|
Parameters |
|
91
|
|
|
---------- |
|
92
|
|
|
payload : :class:`GatewayDispatch` |
|
93
|
|
|
The data received from the channel update event. |
|
94
|
|
|
|
|
95
|
|
|
Returns |
|
96
|
|
|
------- |
|
97
|
|
|
Tuple[:class:`str`, List[:class:`~pincer.objects.channel.channel.Channel`]] |
|
98
|
|
|
``on_channel_update`` and a ``Channel`` |
|
99
|
|
|
""" |
|
100
|
|
|
|
|
101
|
|
|
channel = Channel.from_dict(construct_client_dict(self, payload.data)) |
|
102
|
|
|
|
|
103
|
|
|
if channel.guild_id in self.guilds.keys(): |
|
104
|
|
|
guild = self.guilds[channel.guild_id] |
|
105
|
|
|
old = filter(lambda c: c.id == channel.id, guild.channels) |
|
106
|
|
|
if old: |
|
107
|
|
|
guild.channels.remove(old) |
|
108
|
|
|
guild.channels.append(channel) |
|
109
|
|
|
|
|
110
|
|
|
return "on_channel_update", [channel] |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
def export(): |
|
|
|
|
|
|
114
|
|
|
return ( |
|
115
|
|
|
channel_create_middleware, |
|
116
|
|
|
channel_delete_middleware, |
|
117
|
|
|
channel_pins_update_middleware, |
|
118
|
|
|
) |
|
119
|
|
|
|