forked from Pycord-Development/pycord
-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: ConnectionState & Event Handling #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
VincentRPS
wants to merge
17
commits into
master
Choose a base branch
from
state-rewrite
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9c4c3b6
feat: first batch of changes
VincentRPS 5904f0a
fix: await prevent_view_updates_for
VincentRPS 0d0ac28
refactor!: bulk of the work
VincentRPS 97dac81
refactor!: another bulk of changes
VincentRPS d0c2eb5
feat: message events
VincentRPS a824153
feat: InteractionCreate
VincentRPS 8a2bd59
refactor!: remove View and Model Store & make `Guild._from_data` into…
VincentRPS f0554c3
chore: Type -> type
VincentRPS b689c54
Apply suggestions from code review
VincentRPS 3a43db0
Merge branch 'master' into state-rewrite
VincentRPS 56fdd91
feat: add more events
VincentRPS 2a715f4
Merge https://github.com/Pycord-Test/pycord into state-rewrite
VincentRPS a2015f9
chore: temporary fix
VincentRPS f15035b
Merge branch 'master' into state-rewrite
plun1331 79c6bf4
Update discord/app/event_emitter.py
VincentRPS 79883a0
refactor!: miscellaneous
VincentRPS 160830e
feat: more thread events
VincentRPS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,306 @@ | ||
""" | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2021-present Pycord Development | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a | ||
copy of this software and associated documentation files (the "Software"), | ||
to deal in the Software without restriction, including without limitation | ||
the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
and/or sell copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
DEALINGS IN THE SOFTWARE. | ||
""" | ||
|
||
from collections import OrderedDict, deque | ||
from typing import Deque, Protocol | ||
|
||
from discord.app.state import ConnectionState | ||
from discord.message import Message | ||
|
||
from ..abc import PrivateChannel | ||
from ..channel import DMChannel | ||
from ..emoji import AppEmoji, GuildEmoji | ||
from ..guild import Guild | ||
from ..poll import Poll | ||
from ..sticker import GuildSticker, Sticker | ||
from ..ui.modal import Modal, ModalStore | ||
from ..ui.view import View, ViewStore | ||
from ..user import User | ||
from ..types.user import User as UserPayload | ||
from ..types.emoji import Emoji as EmojiPayload | ||
from ..types.sticker import GuildSticker as GuildStickerPayload | ||
from ..types.channel import DMChannel as DMChannelPayload | ||
|
||
class Cache(Protocol): | ||
# users | ||
async def get_all_users(self) -> list[User]: | ||
... | ||
|
||
async def store_user(self, payload: UserPayload) -> User: | ||
... | ||
|
||
async def delete_user(self, user_id: int) -> None: | ||
... | ||
|
||
async def get_user(self, user_id: int) -> User | None: | ||
... | ||
|
||
# stickers | ||
|
||
async def get_all_stickers(self) -> list[GuildSticker]: | ||
... | ||
|
||
async def get_sticker(self, sticker_id: int) -> GuildSticker: | ||
... | ||
|
||
async def store_sticker(self, guild: Guild, data: GuildStickerPayload) -> GuildSticker: | ||
... | ||
|
||
# interactions | ||
|
||
async def store_view(self, view: View, message_id: int | None) -> None: | ||
... | ||
|
||
async def delete_view_on(self, message_id: int) -> None: | ||
... | ||
|
||
async def get_all_views(self) -> list[View]: | ||
... | ||
|
||
async def store_modal(self, modal: Modal) -> None: | ||
... | ||
|
||
async def get_all_modals(self) -> list[Modal]: | ||
... | ||
|
||
# guilds | ||
|
||
async def get_all_guilds(self) -> list[Guild]: | ||
... | ||
|
||
async def get_guild(self, id: int) -> Guild: | ||
... | ||
|
||
async def add_guild(self, guild: Guild) -> None: | ||
... | ||
|
||
async def delete_guild(self, guild: Guild) -> None: | ||
... | ||
|
||
# emojis | ||
|
||
async def store_guild_emoji(self, guild: Guild, data: EmojiPayload) -> GuildEmoji: | ||
... | ||
|
||
async def store_app_emoji( | ||
self, application_id: int, data: EmojiPayload | ||
) -> AppEmoji: | ||
... | ||
|
||
async def get_all_emojis(self) -> list[GuildEmoji | AppEmoji]: | ||
... | ||
|
||
async def get_emoji(self, emoji_id: int | None) -> GuildEmoji | AppEmoji | None: | ||
... | ||
|
||
async def delete_emoji(self, emoji: GuildEmoji | AppEmoji) -> None: | ||
... | ||
|
||
# polls | ||
|
||
async def get_all_polls(self) -> list[Poll]: | ||
VincentRPS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
... | ||
|
||
async def get_poll(self, message_id: int) -> Poll: | ||
... | ||
|
||
async def store_poll(self, poll: Poll, message_id: int) -> None: | ||
... | ||
|
||
# private channels | ||
|
||
async def get_private_channels(self) -> list[PrivateChannel]: | ||
... | ||
|
||
async def get_private_channel(self, channel_id: int) -> PrivateChannel: | ||
... | ||
|
||
async def store_private_channel(self, channel: PrivateChannel, channel_id: int) -> None: | ||
... | ||
|
||
# dm channels | ||
|
||
async def get_dm_channels(self) -> list[DMChannel]: | ||
... | ||
|
||
async def get_dm_channel(self, channel_id: int) -> DMChannel: | ||
... | ||
|
||
async def store_dm_channel(self, channel: DMChannelPayload, channel_id: int) -> DMChannel: | ||
... | ||
|
||
def clear(self, views: bool = True) -> None: | ||
... | ||
|
||
class MemoryCache(Cache): | ||
def __init__(self, max_messages: int | None = None, *, state: ConnectionState): | ||
self._state = state | ||
self.max_messages = max_messages | ||
self.clear() | ||
|
||
def clear(self, views: bool = True) -> None: | ||
self._users: dict[int, User] = {} | ||
self._guilds: dict[int, Guild] = {} | ||
self._polls: dict[int, Poll] = {} | ||
self._stickers: dict[int, list[GuildSticker]] = {} | ||
if views: | ||
self._views: dict[str, View] = {} | ||
self._modals: dict[str, Modal] = {} | ||
self._messages: Deque[Message] = deque(maxlen=self.max_messages) | ||
|
||
self._emojis = dict[str, GuildEmoji | AppEmoji] = {} | ||
|
||
self._private_channels: OrderedDict[int, PrivateChannel] = OrderedDict() | ||
self._private_channels_by_user: dict[int, DMChannel] = {} | ||
|
||
# users | ||
async def get_all_users(self) -> list[User]: | ||
return list(self._users.values()) | ||
|
||
async def store_user(self, payload: UserPayload) -> User: | ||
user_id = int(payload["id"]) | ||
try: | ||
return self._users[user_id] | ||
except KeyError: | ||
user = User(state=self, data=payload) | ||
if user.discriminator != "0000": | ||
self._users[user_id] = user | ||
user._stored = True | ||
return user | ||
|
||
async def delete_user(self, user_id: int) -> None: | ||
self._users.pop(user_id, None) | ||
|
||
async def get_user(self, user_id: int) -> User: | ||
return self._users.get(user_id) | ||
|
||
# stickers | ||
|
||
async def get_all_stickers(self) -> list[GuildSticker]: | ||
return list(self._stickers.values()) | ||
|
||
async def get_sticker(self, sticker_id: int) -> GuildSticker: | ||
return self._stickers.get(sticker_id) | ||
|
||
async def store_sticker(self, guild: Guild, data: GuildStickerPayload) -> GuildSticker: | ||
sticker = GuildSticker(state=self._state, data=data) | ||
try: | ||
self._stickers[guild.id].append(sticker) | ||
except KeyError: | ||
self._stickers[guild.id] = sticker | ||
VincentRPS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return sticker | ||
|
||
# interactions | ||
|
||
async def delete_view_on(self, message_id: int) -> View | None: | ||
return self._views.pop(message_id, None) | ||
|
||
async def store_view(self, view: View, message_id: int) -> None: | ||
self._views[message_id or view.id] = view | ||
|
||
async def get_all_views(self) -> list[View]: | ||
return list(self._views.values()) | ||
|
||
async def store_modal(self, modal: Modal) -> None: | ||
self._modals[modal.custom_id] = modal | ||
|
||
async def get_all_modals(self) -> list[Modal]: | ||
return list(self._modals.values()) | ||
|
||
# guilds | ||
|
||
async def get_all_guilds(self) -> list[Guild]: | ||
return list(self._guilds.values()) | ||
|
||
async def get_guild(self, id: int) -> Guild | None: | ||
return self._guilds.get(id) | ||
|
||
async def add_guild(self, guild: Guild) -> None: | ||
self._guilds[guild.id] = guild | ||
|
||
async def delete_guild(self, guild: Guild) -> None: | ||
self._guilds.pop(guild.id, None) | ||
|
||
# emojis | ||
|
||
async def store_guild_emoji(self, guild: Guild, data: EmojiPayload) -> GuildEmoji: | ||
emoji = GuildEmoji(guild=guild, state=self._state, data=data) | ||
try: | ||
self._emojis[guild.id].append(emoji) | ||
except KeyError: | ||
self._emojis[guild.id] = [emoji] | ||
return emoji | ||
|
||
async def store_app_emoji( | ||
self, application_id: int, data: EmojiPayload | ||
) -> AppEmoji: | ||
emoji = AppEmoji(application_id=application_id, state=self._state, data=data) | ||
try: | ||
self._emojis[application_id].append(emoji) | ||
except KeyError: | ||
self._emojis[application_id] = [emoji] | ||
return emoji | ||
|
||
async def get_all_emojis(self) -> list[GuildEmoji | AppEmoji]: | ||
return list(self._emojis.values()) | ||
|
||
async def get_emoji(self, emoji_id: int | None) -> GuildEmoji | AppEmoji | None: | ||
return self._emojis.get(emoji_id) | ||
|
||
async def delete_emoji(self, emoji: GuildEmoji | AppEmoji) -> None: | ||
if isinstance(emoji, AppEmoji): | ||
self._emojis[emoji.application_id].remove(emoji) | ||
else: | ||
self._emojis[emoji.guild_id].remove(emoji) | ||
|
||
# polls | ||
|
||
async def get_all_polls(self) -> list[Poll]: | ||
return list(self._polls.values()) | ||
|
||
async def get_poll(self, message_id: int) -> Poll | None: | ||
return self._polls.get(message_id) | ||
|
||
async def store_poll(self, poll: Poll, message_id: int) -> None: | ||
self._polls[message_id] = poll | ||
|
||
# private channels | ||
|
||
async def get_private_channels(self) -> list[PrivateChannel]: | ||
return list(self._private_channels.values()) | ||
|
||
async def get_private_channel(self, channel_id: int) -> PrivateChannel | None: | ||
return self._private_channels.get(channel_id) | ||
|
||
async def store_private_channel(self, channel: PrivateChannel) -> None: | ||
channel_id = channel.id | ||
self._private_channels[channel_id] = channel | ||
|
||
if len(self._private_channels) > 128: | ||
_, to_remove = self._private_channels.popitem(last=False) | ||
if isinstance(to_remove, DMChannel) and to_remove.recipient: | ||
self._private_channels_by_user.pop(to_remove.recipient.id, None) | ||
|
||
if isinstance(channel, DMChannel) and channel.recipient: | ||
self._private_channels_by_user[channel.recipient.id] = channel |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.