Skip to content

tux.cogs.services.bookmarks

Classes:

Name Description
Bookmarks

Classes

Bookmarks(bot: Tux)

Bases: Cog

Methods:

Name Description
on_raw_reaction_add

Handle the addition of a reaction to a message.

Source code in tux/cogs/services/bookmarks.py
Python
def __init__(self, bot: Tux) -> None:
    self.bot = bot

Functions

on_raw_reaction_add(payload: discord.RawReactionActionEvent) -> None async

Handle the addition of a reaction to a message.

Parameters:

Name Type Description Default
payload RawReactionActionEvent

The payload of the reaction event.

required

Returns:

Type Description
None
Source code in tux/cogs/services/bookmarks.py
Python
@commands.Cog.listener()
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent) -> None:
    """
    Handle the addition of a reaction to a message.

    Parameters
    ----------
    payload : discord.RawReactionActionEvent
        The payload of the reaction event.

    Returns
    -------
    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
    channel = cast(discord.TextChannel | discord.Thread, channel)

    # 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)
_send_bookmark(user: discord.User, message: discord.Message, embed: discord.Embed, emoji: discord.PartialEmoji) -> None async staticmethod

Send a bookmarked message to the user.

Parameters:

Name Type Description Default
user User

The user to send the bookmarked message to.

required
message Message

The message that was bookmarked.

required
embed Embed

The embed to send to the user.

required
emoji str

The emoji that was reacted to the message.

required
Source code in tux/cogs/services/bookmarks.py
Python
@staticmethod
async def _send_bookmark(
    user: discord.User,
    message: discord.Message,
    embed: discord.Embed,
    emoji: discord.PartialEmoji,
) -> 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)

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

        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)