Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
09dc200
docs(contributor): contrib-readme-action has updated readme
github-actions[bot] Aug 27, 2024
632eb57
new file: tux/cogs/services/bookmarks.py
meatsnails Aug 28, 2024
60f6b11
docs(contributor): contrib-readme-action has updated readme
github-actions[bot] Aug 28, 2024
b9f355e
modified: tux/cogs/services/bookmarks.py
meatsnails Aug 28, 2024
1fa450b
Merge branch 'main' of https://github.com/GabberBalls/tux
meatsnails Aug 28, 2024
508bbb5
modified: tux/cogs/services/bookmarks.py
meatsnails Aug 28, 2024
086cb92
Merge branch 'allthingslinux:main' into main
meatsnails Aug 28, 2024
39bd877
modified: tux/cogs/services/bookmarks.py
meatsnails Aug 28, 2024
b561061
Merge branch 'main' of https://github.com/GabberBalls/tux
meatsnails Aug 28, 2024
f777d08
modified: tux/cogs/services/bookmarks.py
meatsnails Aug 28, 2024
cecd4ba
modified: tux/cogs/services/bookmarks.py
meatsnails Aug 29, 2024
b7eb0ee
docs(contributor): contrib-readme-action has updated readme
github-actions[bot] Aug 29, 2024
59129f1
Merge branch 'main' into main
electron271 Aug 29, 2024
7d65c3e
docs(contributor): contrib-readme-action has updated readme
github-actions[bot] Aug 29, 2024
5ad2cfc
updated and moved bookmarks.py to utils
meatsnails Aug 29, 2024
ecc22e0
docs(contributor): contrib-readme-action has updated readme
github-actions[bot] Aug 29, 2024
608682b
Merge branch 'main' of https://github.com/GabberBalls/tux
meatsnails Aug 29, 2024
0568e48
feat(bookmarks.py): add new bookmarks service in cogs/services
kzndotsh Aug 29, 2024
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
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,10 @@ See [LICENSE](LICENSE.md) for details.
<tbody>
<tr>
<td align="center">
<a href="https://github.com/0x4248">
<img src="https://avatars.githubusercontent.com/u/60709927?v=4" width="100;" alt="0x4248"/>
<br />
<sub><b>0x4248</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/wlinator">
<img src="https://avatars.githubusercontent.com/u/75494059?v=4" width="100;" alt="wlinator"/>
<a href="https://github.com/targzballs">
<img src="https://avatars.githubusercontent.com/u/102343489?v=4" width="100;" alt="targzballs"/>
<br />
<sub><b>wlinator</b></sub>
<sub><b>targzballs</b></sub>
</a>
</td>
<td align="center">
Expand Down Expand Up @@ -213,22 +206,15 @@ See [LICENSE](LICENSE.md) for details.
<sub><b>ExploitDemon</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/jwe66">
<img src="https://avatars.githubusercontent.com/u/142009905?v=4" width="100;" alt="jwe66"/>
<br />
<sub><b>jwe66</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/targzballs">
<img src="https://avatars.githubusercontent.com/u/102343489?v=4" width="100;" alt="targzballs"/>
<br />
<sub><b>targzballs</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/doomed-neko">
<img src="https://avatars.githubusercontent.com/u/79870712?v=4" width="100;" alt="doomed-neko"/>
Expand All @@ -243,6 +229,13 @@ See [LICENSE](LICENSE.md) for details.
<sub><b>HikariNeee</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/0x4248">
<img src="https://avatars.githubusercontent.com/u/60709927?v=4" width="100;" alt="0x4248"/>
<br />
<sub><b>0x4248</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/AuraPy">
<img src="https://avatars.githubusercontent.com/u/119633142?v=4" width="100;" alt="AuraPy"/>
Expand Down Expand Up @@ -270,6 +263,13 @@ See [LICENSE](LICENSE.md) for details.
<br />
<sub><b>CapnRyna</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/wlinator">
<img src="https://avatars.githubusercontent.com/u/75494059?v=4" width="100;" alt="wlinator"/>
<br />
<sub><b>wlinator</b></sub>
</a>
</td>
</tr>
<tr>
Expand Down
102 changes: 102 additions & 0 deletions tux/cogs/services/bookmarks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import discord
from discord.ext import commands
from loguru import logger

from tux.utils.embeds import EmbedCreator


class Bookmarks(commands.Cog):
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot

@commands.Cog.listener()
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent) -> None:
if str(payload.emoji) != "🔖":
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}")
return

# Fetch the message that was reacted to
try:
message = await channel.fetch_message(payload.message_id)
except discord.NotFound:
logger.error(f"Message not found for ID: {payload.message_id}")
return
except (discord.Forbidden, discord.HTTPException) as fetch_error:
logger.error(f"Failed to fetch message: {fetch_error}")
return

# Create an embed for the bookmarked message
embed = 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)

def _create_bookmark_embed(
self,
message: discord.Message,
) -> discord.Embed:
embed = EmbedCreator.create_info_embed(
title="Message Bookmarked",
description=f"> {message.content}",
)

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 _send_bookmark(
self,
user: discord.User,
message: discord.Message,
embed: discord.Embed,
emoji: str,
) -> None:
"""
Send a bookmarked message to the user.

Parameters
----------
user : discord.User
The user to send the bookmarked message to.
message : discord.Message
The message that was bookmarked.
embed : discord.Embed
The embed to send to the user.
emoji : str
The emoji that was reacted to the message.
"""

try:
await user.send(embed=embed)
await message.remove_reaction(emoji, user)

except (discord.Forbidden, discord.HTTPException) as dm_error:
logger.error(f"Cannot send a DM to {user.name}: {dm_error}")

await message.remove_reaction(emoji, user)

notify_message = await message.channel.send(
f"{user.mention}, I couldn't send you a DM. Please make sure your DMs are open for bookmarks to work.",
)
await notify_message.delete(delay=30)


async def setup(bot: commands.Bot) -> None:
await bot.add_cog(Bookmarks(bot))