1
Fork 0
mirror of https://github.com/allthingslinux/tux.git synced 2024-10-02 16:43:12 +00:00

Merge pull request #485 from targzballs/main

This commit is contained in:
kzndotsh 2024-08-29 04:02:06 -04:00 committed by GitHub
commit a57dbdfcfc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 121 additions and 19 deletions

View file

@ -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"/>
<a href="https://github.com/targzballs">
<img src="https://avatars.githubusercontent.com/u/102343489?v=4" width="100;" alt="targzballs"/>
<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"/>
<br />
<sub><b>wlinator</b></sub>
<sub><b>targzballs</b></sub>
</a>
</td>
<td align="center">
@ -213,8 +206,6 @@ 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"/>
@ -222,13 +213,8 @@ See [LICENSE](LICENSE.md) for details.
<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"/>
@ -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"/>
@ -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>

View file

@ -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))