Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

✨ Guild & Channel properties for APIObjects #298

Merged
merged 37 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
83bd54e
:sparkles: created properties
Lunarmagpie Dec 9, 2021
fb6a3ef
:sparkles: added properties to UserMessage
Lunarmagpie Dec 9, 2021
8bd5b67
Merge remote-tracking branch 'upstream/main' into properties
Lunarmagpie Dec 9, 2021
c85071c
:construction: started updates
Lunarmagpie Dec 9, 2021
424c365
:sparkles: sticker upadate middleware
Lunarmagpie Dec 9, 2021
3d3356d
:bug: added construct client dict to utils __init__.py
Lunarmagpie Dec 10, 2021
bc4417c
:bug: prevented middleware from raising exception if guild isn't regi…
Lunarmagpie Dec 10, 2021
f5f027d
:speech_balloon: fix typo
Lunarmagpie Dec 10, 2021
560400f
:sparkles: thread and channel updates
Lunarmagpie Dec 11, 2021
33bf97c
:sparkles: caching voice_states
Lunarmagpie Dec 11, 2021
95dc347
:memo: add docs
Lunarmagpie Dec 11, 2021
6b3b2c4
:art: changed properties to classes
Lunarmagpie Dec 11, 2021
ad7727a
:sparkles: added channel properties
Lunarmagpie Dec 11, 2021
8b4ebf0
:sparkles: added guild properties
Lunarmagpie Dec 11, 2021
568668c
:sparkles: added guild_ids property to Client
Lunarmagpie Dec 11, 2021
62c96b4
:art: codacity changes
Lunarmagpie Dec 11, 2021
ce0f3ec
:memo: updated docs
Lunarmagpie Dec 11, 2021
ef999b5
:speech_balloon: fix typo
Lunarmagpie Dec 11, 2021
c68e9a1
:technologist: made Intents easier to use
Lunarmagpie Dec 11, 2021
3542492
:art: ran black on middleware
Lunarmagpie Dec 11, 2021
dbf1c63
:memo: maked guild_ids return type as List[Snowflake]
Lunarmagpie Dec 12, 2021
c622563
Merge branch 'main' into properties
Sigmanificient Dec 12, 2021
5293c72
Update pincer/middleware/interaction_create.py
Lunarmagpie Dec 12, 2021
aac1ec9
:art: fixed formatting to work with 88 char line length
Lunarmagpie Dec 12, 2021
bee85ea
Merge branch 'properties' of https://github.com/Lunarmagpie/Pincer in…
Lunarmagpie Dec 12, 2021
b490aa7
:recycle: Update pincer/middleware/stage_instance_delete.py
Lunarmagpie Dec 12, 2021
9795c13
:recycle: Update pincer/middleware/thread_delete.py
Lunarmagpie Dec 12, 2021
21be6f7
:art: fixed formatting to work with 88 char line length
Lunarmagpie Dec 12, 2021
1fd59d5
:art: fixed formatting to work with 88 char line length
Lunarmagpie Dec 12, 2021
eed6893
:recycle: Update pincer/middleware/channel_delete.py
Lunarmagpie Dec 12, 2021
97b09ce
:recycle: Update pincer/middleware/guild_role_delete.py
Lunarmagpie Dec 12, 2021
f138503
:memo: added ctx to examples
Lunarmagpie Dec 12, 2021
913ba15
:bug: APIObject _client is now correctly propogated to children.
Lunarmagpie Dec 13, 2021
cbac5da
:bug: ThreadMember now has correct args
Lunarmagpie Dec 13, 2021
267ea16
:memo: ctx.channel.send is now awaited
Lunarmagpie Dec 13, 2021
0c32152
:art: added missing await
Lunarmagpie Dec 13, 2021
954f6bf
:art: removed unused import
Lunarmagpie Dec 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions pincer/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,11 @@ def __init__(
self.http = HTTPClient(token)
self.throttler = throttler
self.event_mgr = EventMgr()
# TODO: Document guild prop
# The guild value is only registered if the GUILD_MEMBERS
# intent is enabled.

# The guild and channel value is only registered if the GUILD intent.
self.guilds: Dict[Snowflake, Optional[Guild]] = {}
self.channels: Dict[Snowflake, Optional[Channel]] = {}

ChatCommandHandler.managers[self.__module__] = self

@property
Expand Down Expand Up @@ -497,7 +498,7 @@ async def handle_middleware(
raise RuntimeError(f"Middleware `{key}` has not been registered.")

if next_call.startswith("on_"):
return next_call, ret_object
return (next_call, ret_object)
Lunarmagpie marked this conversation as resolved.
Show resolved Hide resolved

return await self.handle_middleware(
payload, next_call, *arguments, **params
Expand Down Expand Up @@ -565,7 +566,7 @@ async def event_handler(self, _, payload: GatewayDispatch):
----------
_ :
Socket param, but this isn't required for this handler. So
it's just a filler parameter, doesn't matter what is passed.
its just a filler parameter, doesn't matter what is passed.
payload : :class:`~pincer.core.dispatch.GatewayDispatch`
The payload sent from the Discord gateway, this contains the
required data for the client to know what event it is and
Expand All @@ -582,7 +583,7 @@ async def payload_event_handler(self, _, payload: GatewayDispatch):
----------
_ :
Socket param, but this isn't required for this handler. So
it's just a filler parameter, doesn't matter what is passed.
its just a filler parameter, doesn't matter what is passed.
Lunarmagpie marked this conversation as resolved.
Show resolved Hide resolved
payload : :class:`~pincer.core.dispatch.GatewayDispatch`
The payload sent from the Discord gateway, this contains the
required data for the client to know what event it is and
Expand Down Expand Up @@ -857,5 +858,4 @@ async def get_webhook(
"""
return await Webhook.from_id(self, id, token)


Bot = Client
13 changes: 8 additions & 5 deletions pincer/middleware/channel_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from ..core.dispatch import GatewayDispatch


def channel_create_middleware(
async def channel_create_middleware(
self,
payload: GatewayDispatch
) -> Tuple[str, Channel]:
Expand All @@ -32,10 +32,13 @@ def channel_create_middleware(
Tuple[:class:`str`, List[:class:`~pincer.objects.guild.channel.Channel`]]
``on_channel_creation`` and a channel.
"""
return (
"on_channel_creation",
Channel.from_dict(construct_client_dict(self, payload.data))
)

channel: Channel = Channel.from_dict(construct_client_dict(self, payload.data))
self.guilds[channel.guild_id].channels.append(channel)
Lunarmagpie marked this conversation as resolved.
Show resolved Hide resolved

self.channels[channel.id] = channel

return "on_channel_creation", channel


def export():
Expand Down
9 changes: 4 additions & 5 deletions pincer/middleware/channel_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ async def channel_delete_middleware(self, payload: GatewayDispatch):

channel = Channel.from_dict(construct_client_dict(self, payload.data))

if channel.guild_id in self.guilds:
guild = self.guilds[channel.guild_id]
old = filter(lambda c: c.id == channel.id, guild.channels)
if old:
guild.channels.remove(old)
guild = self.guilds[channel.guild_id]
guild.channels = list(filter(lambda c: c.id != channel.id, guild.channels))

self.channels.pop(channel.id)

return "on_channel_delete", channel

Expand Down
5 changes: 4 additions & 1 deletion pincer/middleware/channel_pins_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ async def channel_pins_update_middleware(self, payload: GatewayDispatch):
``on_channel_pins_update`` and a ``Channel``
"""

event = ChannelPinsUpdateEvent.from_dict(payload.data)
self.channels[event.channel_id].last_pin_timestamp = event.last_pin_timestamp

return (
"on_channel_pins_update",
ChannelPinsUpdateEvent.from_dict(payload.data)
event
)


Expand Down
8 changes: 1 addition & 7 deletions pincer/middleware/channel_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ async def channel_update_middleware(self, payload: GatewayDispatch):
"""

channel = Channel.from_dict(construct_client_dict(self, payload.data))

if channel.guild_id in self.guilds.keys():
guild = self.guilds[channel.guild_id]
old = filter(lambda c: c.id == channel.id, guild.channels)
if old:
guild.channels.remove(old)
guild.channels.append(channel)
self.channels[channel.id] = channel

return "on_channel_update", channel

Expand Down
3 changes: 3 additions & 0 deletions pincer/middleware/guild_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ async def guild_create_middleware(self, payload: GatewayDispatch):
"""
guild = Guild.from_dict(construct_client_dict(self, payload.data))
self.guilds[guild.id] = guild
for channel in guild.channels:
self.channels[channel.id] = channel

return "on_guild_create", guild


Expand Down
8 changes: 4 additions & 4 deletions pincer/middleware/guild_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ async def guild_delete_middleware(self, payload: GatewayDispatch):
Tuple[:class:`str`, :class:`~pincer.objects.guild.guild.UnavailableGuild`]
``on_guild_delete`` and an ``UnavailableGuild``
"""
# TODO: Fix docs on line 23 (three lines above)
# http://docs.pincer.dev/pincer.middleware#pincer.middleware.guild_delete.guild_delete_middleware

guild = UnavailableGuild.from_dict(
construct_client_dict(self, payload.data)
)

if guild.id in self.guilds.key():
self.guilds.pop(guild.id)
self.guilds.pop(guild.id, None)

for channel in self.guild.channels:
self.channels.pop(channel.id)

return "on_guild_delete", guild

Expand Down
10 changes: 7 additions & 3 deletions pincer/middleware/guild_emojis_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ async def guild_emojis_update_middleware(self, payload: GatewayDispatch):
``on_guild_emoji_update`` and a ``GuildEmojisUpdateEvent``
""" # noqa: E501

event = GuildEmojisUpdateEvent.from_dict(
construct_client_dict(self, payload.data)
)

self.guild[event.guild_id] = event.emojis

return (
"on_guild_emojis_update",
GuildEmojisUpdateEvent.from_dict(
construct_client_dict(self, payload.data)
)
event
)


Expand Down
2 changes: 1 addition & 1 deletion pincer/middleware/guild_member_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async def guild_member_add_middleware(self, payload: GatewayDispatch):
-------
Tuple[:class:`str`, :class:`~pincer.objects.events.guild.GuildMemberAddEvent`]
``on_guild_member_add`` and a ``GuildMemberAddEvent``
""" # noqa: E501
"""

return (
"on_guild_member_add",
Expand Down
2 changes: 1 addition & 1 deletion pincer/middleware/guild_member_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def guild_member_remove_middleware(self, payload: GatewayDispatch):
-------
Tuple[:class:`str`, :class:`~pincer.objects.events.guild.GuildMemberRemoveEvent`]
``on_guild_member_remove`` and a ``GuildMemberRemoveEvent``
""" # noqa: E501
"""

return (
"on_guild_member_remove",
Expand Down
2 changes: 1 addition & 1 deletion pincer/middleware/guild_member_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async def guild_member_update_middleware(self, payload: GatewayDispatch):
-------
Tuple[:class:`str`, :class:`~pincer.objects.events.guild.GuildMemberUpdateEvent`]
``on_guild_member_update`` and a ``GuildMemberUpdateEvent``
""" # noqa: E501
"""

return (
"on_guild_member_update",
Expand Down
8 changes: 5 additions & 3 deletions pincer/middleware/guild_role_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ async def guild_role_create_middleware(self, payload: GatewayDispatch):
``on_guild_role_create`` and a ``GuildRoleCreateEvent``
""" # noqa: E501

event = GuildRoleCreateEvent.from_dict(construct_client_dict(self, payload.data))

self.guilds[event.guild_id].roles.append(event.role)

return (
"on_guild_role_create",
GuildRoleCreateEvent.from_dict(
construct_client_dict(self, payload.data)
)
event
)


Expand Down
10 changes: 7 additions & 3 deletions pincer/middleware/guild_role_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ async def guild_role_delete_middleware(self, payload: GatewayDispatch):
``on_guild_role_delete`` and a ``GuildRoleDeleteEvent``
""" # noqa: E501

event = GuildRoleDeleteEvent.from_dict(construct_client_dict(self, payload.data))

self.guilds[event.guild_id].roles = list(filter(
lambda role: role.id != event.role_id, self.guilds[event.guild_id].roles
))

return (
"on_guild_role_delete",
GuildRoleDeleteEvent.from_dict(
construct_client_dict(self, payload.data)
),
event
)


Expand Down
12 changes: 8 additions & 4 deletions pincer/middleware/guild_role_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ async def guild_role_update_middleware(self, payload: GatewayDispatch):
-------
Tuple[:class:`str`, :class:`~pincer.objects.events.guild.GuildRoleUpdateEvent`]
``on_guild_role_update`` and a ``GuildRoleUpdateEvent``
""" # noqa: E501
"""

event = GuildRoleUpdateEvent.from_dict(construct_client_dict(self, payload.data))
self.guilds[event.guild_id].roles = [
role if role.id != event.role.id else event.role
for role in self.guilds[event.guild_id].roles
]

return (
"on_guild_role_update",
GuildRoleUpdateEvent.from_dict(
construct_client_dict(self, payload.data)
),
event
)


Expand Down
15 changes: 4 additions & 11 deletions pincer/middleware/guild_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,12 @@ async def guild_update_middleware(self, payload: GatewayDispatch):
``on_guild_Update`` and an ``Guild``
"""

channel_list = payload.data.pop("channels", [])

channels: List[Channel] = [
Channel.from_dict(construct_client_dict(self, channel))
for channel in channel_list
]

guild = Guild.from_dict(construct_client_dict(
self,
{"channels": channels, **payload.data}
))
guild = Guild.from_dict(construct_client_dict(self, payload.data))
self.guild[guild.id] = guild

for channel in guild.channels:
self.channels[channel.id] = channel

return "on_guild_update", guild


Expand Down
1 change: 0 additions & 1 deletion pincer/objects/events/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ class ChannelPinsUpdateEvent(APIObject):

guild_id: APINullable[Snowflake] = MISSING
last_pin_timestamp: APINullable[Timestamp] = MISSING