Skip to content

Commit

Permalink
chore: fix an issue with removing reactions from uncached messages
Browse files Browse the repository at this point in the history
  • Loading branch information
EnokiUN committed Mar 2, 2024
1 parent 9a68c9d commit fa3ee95
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
15 changes: 10 additions & 5 deletions voltage/internals/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ async def handle_messagereact(self, payload: OnMessageReactPayload):
)
await self.dispatch("message_react", message, user_id, emoji_id)

async def handle_messageremovereaction(self, payload: OnMessageRemoveReactionPayload):
async def handle_messageremovereaction(
self, payload: OnMessageRemoveReactionPayload
):
"""
Handles the message remove reaction event.
"""
Expand All @@ -226,9 +228,10 @@ async def handle_messageunreact(self, payload: OnMessageReactPayload):
message = await self.cache.fetch_message(payload["channel_id"], payload["id"])
user_id = payload["user_id"]
emoji_id = payload["emoji_id"]
message.interactions.reactions.setdefault(payload["emoji_id"], []).remove(
self.cache.get_user(payload["user_id"])
)
reactions = message.interactions.reactions.setdefault(payload["emoji_id"], [])
user = self.cache.get_user(payload["user_id"])
if user in reactions:
reactions.remove(user)
await self.dispatch("message_unreact", message, user_id, emoji_id)

async def handle_channelcreate(self, payload: OnChannelCreatePayload):
Expand Down Expand Up @@ -338,7 +341,9 @@ async def handle_servermemberjoin(self, payload: OnServerMemberJoinPayload):
self.cache.get_user(payload["user"])
except KeyError:
self.cache.add_user(await self.http.fetch_user(payload["user"]))
member = self.cache.add_member(payload["id"], {"_id": {"server": payload["id"], "user": payload["user"]}})
member = self.cache.add_member(
payload["id"], {"_id": {"server": payload["id"], "user": payload["user"]}}
)
await self.dispatch("member_join", member)

async def handle_memberleave(self, payload: OnServerMemberLeavePayload):
Expand Down
20 changes: 15 additions & 5 deletions voltage/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def to_dict(self) -> dict:
"""Returns a dictionary representation of the message interactions."""
return {
"reactions": self.reactions if self.reactions else None,
"restrict_reactions": self.restrict_reactions if self.restrict_reactions is not None else None,
"restrict_reactions": self.restrict_reactions
if self.restrict_reactions is not None
else None,
}


Expand Down Expand Up @@ -152,7 +154,9 @@ def __init__(self, data: MessagePayload, cache: CacheHandler):

self.server = self.channel.server
self.author = (
cache.get_member(self.server.id, data["author"]) if self.server else cache.get_user(data["author"])
cache.get_member(self.server.id, data["author"])
if self.server
else cache.get_user(data["author"])
)

if masquerade := data.get("masquerade"):
Expand All @@ -179,7 +183,9 @@ def __init__(self, data: MessagePayload, cache: CacheHandler):
self.mention_ids = data.get("mentions", [])

if interactions := data.get("interactions"):
self.interactions.restrict_reactions = interactions.get("restrict_reactions") or False
self.interactions.restrict_reactions = (
interactions.get("restrict_reactions") or False
)

if reactions := data.get("reactions"):
for emoji_id, users in reactions.items():
Expand Down Expand Up @@ -217,14 +223,18 @@ async def edit(
The new embeds of the message.
"""
if content is None and embed is None and embeds is None:
raise ValueError("You must provide at least one of the following: content, embed, embeds")
raise ValueError(
"You must provide at least one of the following: content, embed, embeds"
)

if embed:
embeds = [embed]

content = str(content) if content else None

await self.cache.http.edit_message(self.channel.id, self.id, content=content, embeds=embeds)
await self.cache.http.edit_message(
self.channel.id, self.id, content=content, embeds=embeds
)

async def delete(self, *, delay: Optional[float] = None):
"""Deletes the message."""
Expand Down

0 comments on commit fa3ee95

Please sign in to comment.