-
-
Notifications
You must be signed in to change notification settings - Fork 40
feat:(bookmarks) added removing bookmarks #904
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
Changes from all commits
b9c797a
89691c3
38c22a5
56bebaf
217c364
b8a4072
f16d0f9
34798e9
e9119ab
72d3054
b547dd4
0b4d6d9
ba2c381
1c5f20e
bdb6103
66f4df9
b5dcbb7
2ce131a
5d36f15
e0148cd
61d1a22
67b53e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -13,6 +13,16 @@ class Bookmarks(commands.Cog): | |||||||||
def __init__(self, bot: Tux) -> None: | ||||||||||
self.bot = bot | ||||||||||
|
||||||||||
self.valid_add_emojis = CONST.ADD_BOOKMARK | ||||||||||
self.valid_remove_emojis = CONST.REMOVE_BOOKMARK | ||||||||||
self.valid_emojis = CONST.ADD_BOOKMARK + CONST.REMOVE_BOOKMARK | ||||||||||
|
||||||||||
# The linter wants to change this but it breaks when it does that | ||||||||||
def _is_valid_emoji(self, emoji: discord.PartialEmoji, valid_list: str) -> bool: | ||||||||||
if emoji.name in valid_list: # noqa: SIM103 | ||||||||||
sourcery-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
return True | ||||||||||
return False | ||||||||||
sourcery-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
kzndotsh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
@commands.Cog.listener() | ||||||||||
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent) -> None: | ||||||||||
""" | ||||||||||
|
@@ -27,15 +37,30 @@ async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent) -> | |||||||||
------- | ||||||||||
None | ||||||||||
""" | ||||||||||
|
||||||||||
if str(payload.emoji) != "🔖": | ||||||||||
if not self._is_valid_emoji(payload.emoji, self.valid_emojis): | ||||||||||
sourcery-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
return | ||||||||||
|
||||||||||
# Get the user who reacted to the message | ||||||||||
user = self.bot.get_user(payload.user_id) | ||||||||||
if user is None: | ||||||||||
try: | ||||||||||
user = await self.bot.fetch_user(payload.user_id) | ||||||||||
except discord.NotFound: | ||||||||||
logger.error(f"User not found for ID: {payload.user_id}") | ||||||||||
except (discord.Forbidden, discord.HTTPException) as fetch_error: | ||||||||||
logger.error(f"Failed to fetch user: {fetch_error}") | ||||||||||
return | ||||||||||
# Fetch the channel where the reaction was added | ||||||||||
channel = self.bot.get_channel(payload.channel_id) | ||||||||||
if channel is None: | ||||||||||
logger.error(f"Channel not found for ID: {payload.channel_id}") | ||||||||||
try: | ||||||||||
channel = await self.bot.fetch_channel(payload.channel_id) | ||||||||||
except discord.NotFound: | ||||||||||
logger.error(f"Channel not found for ID: {payload.channel_id}") | ||||||||||
except (discord.Forbidden, discord.HTTPException) as fetch_error: | ||||||||||
logger.error(f"Failed to fetch channel: {fetch_error}") | ||||||||||
return | ||||||||||
|
||||||||||
channel = cast(discord.TextChannel | discord.Thread, channel) | ||||||||||
|
||||||||||
# Fetch the message that was reacted to | ||||||||||
|
@@ -48,19 +73,20 @@ async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent) -> | |||||||||
logger.error(f"Failed to fetch message: {fetch_error}") | ||||||||||
return | ||||||||||
|
||||||||||
# Create an embed for the bookmarked message | ||||||||||
embed = self._create_bookmark_embed(message) | ||||||||||
# check for what to do | ||||||||||
if self._is_valid_emoji(payload.emoji, self.valid_add_emojis): | ||||||||||
# Create an embed for the bookmarked message | ||||||||||
embed = await self._create_bookmark_embed(message) | ||||||||||
|
||||||||||
# Get the user who reacted to the message | ||||||||||
user = self.bot.get_user(payload.user_id) | ||||||||||
if user is None: | ||||||||||
logger.error(f"User not found for ID: {payload.user_id}") | ||||||||||
return | ||||||||||
# Send the bookmarked message to the user | ||||||||||
await self._send_bookmark(user, message, embed, payload.emoji) | ||||||||||
|
||||||||||
# Send the bookmarked message to the user | ||||||||||
await self._send_bookmark(user, message, embed, payload.emoji) | ||||||||||
elif self._is_valid_emoji(payload.emoji, self.valid_remove_emojis) and user is not self.bot.user: | ||||||||||
await self._delete_bookmark(message, user) | ||||||||||
else: | ||||||||||
return | ||||||||||
|
||||||||||
def _create_bookmark_embed( | ||||||||||
async def _create_bookmark_embed( | ||||||||||
self, | ||||||||||
message: discord.Message, | ||||||||||
) -> discord.Embed: | ||||||||||
|
@@ -77,13 +103,16 @@ def _create_bookmark_embed( | |||||||||
embed.add_field(name="Author", value=message.author.name, inline=False) | ||||||||||
|
||||||||||
embed.add_field(name="Jump to Message", value=f"[Click Here]({message.jump_url})", inline=False) | ||||||||||
|
||||||||||
if message.attachments: | ||||||||||
attachments_info = "\n".join([attachment.url for attachment in message.attachments]) | ||||||||||
embed.add_field(name="Attachments", value=attachments_info, inline=False) | ||||||||||
|
||||||||||
return embed | ||||||||||
|
||||||||||
async def _delete_bookmark(self, message: discord.Message, user: discord.User) -> None: | ||||||||||
if message.author is not self.bot.user: | ||||||||||
sourcery-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
return | ||||||||||
Comment on lines
+112
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (bug_risk): Using 'is not' for author comparison may not be reliable. Object identity isn't guaranteed; compare author IDs instead for accuracy.
Suggested change
|
||||||||||
await message.delete() | ||||||||||
|
||||||||||
@staticmethod | ||||||||||
async def _send_bookmark( | ||||||||||
user: discord.User, | ||||||||||
|
@@ -107,7 +136,8 @@ async def _send_bookmark( | |||||||||
""" | ||||||||||
|
||||||||||
try: | ||||||||||
await user.send(embed=embed) | ||||||||||
dm_message = await user.send(embed=embed) | ||||||||||
await dm_message.add_reaction(CONST.REMOVE_BOOKMARK) | ||||||||||
Comment on lines
+139
to
+140
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Adding a reaction to the DM message may fail if the bot lacks permissions. Handle or document the potential exception if adding a reaction to a DM fails due to permission issues. |
||||||||||
|
||||||||||
except (discord.Forbidden, discord.HTTPException) as dm_error: | ||||||||||
logger.error(f"Cannot send a DM to {user.name}: {dm_error}") | ||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.