Skip to content

emoji_manager

Emoji Manager Service for Tux Bot.

This module provides comprehensive emoji management functionality for the Tux Discord bot, including creating, updating, and managing custom emojis across guilds. It supports bulk operations, file-based emoji storage, and automatic cleanup.

Classes:

  • EmojiManager

    Manages application emojis, caching, and synchronization from local files.

Classes

EmojiManager

Python
EmojiManager(
    bot: Bot, emojis_path: Path | None = None, create_delay: float | None = None
)

Manages application emojis, caching, and synchronization from local files.

Initialize the EmojiManager.

Parameters:

  • bot (Bot) –

    The discord bot instance.

  • emojis_path (Optional[Path], default: None ) –

    Path to the directory containing local emoji files. Defaults to DEFAULT_EMOJI_ASSETS_PATH.

  • create_delay (Optional[float], default: None ) –

    Delay in seconds before creating an emoji to mitigate rate limits. Defaults to DEFAULT_EMOJI_CREATE_DELAY.

Methods:

  • init

    Initialize the emoji cache by fetching application emojis.

  • get

    Retrieve an emoji from the cache.

  • sync_emojis

    Synchronize emojis from the local assets directory to the application.

  • resync_emoji

    Resync a specific emoji: Deletes existing, finds local file, creates new.

  • delete_all_emojis

    Delete all application emojis that match names from the emoji assets directory.

Functions

init async
Python
init() -> bool

Initialize the emoji cache by fetching application emojis.

Ensures the cache reflects the current state of application emojis on Discord. This method is locked to prevent concurrent initialization attempts.

Returns:

  • bool

    True if initialization was successful or already done, False otherwise.

get
Python
get(name: str) -> Emoji | None

Retrieve an emoji from the cache.

Ensures initialization before attempting retrieval.

Parameters:

  • name (str) –

    The name of the emoji to retrieve.

Returns:

  • Emoji | None

    The discord.Emoji object if found, None otherwise.

_create_discord_emoji async
Python
_create_discord_emoji(name: str, image_bytes: bytes) -> Emoji | None

Create a Discord emoji with error handling and delay.

Parameters:

  • name (str) –

    The name of the emoji to create.

  • image_bytes (bytes) –

    The image bytes of the emoji to create.

Returns:

  • Emoji | None

    The newly created emoji if successful, otherwise None.

_process_emoji_file async
Python
_process_emoji_file(file_path: Path) -> tuple[Emoji | None, Path | None]

Process a single emoji file.

Parameters:

  • file_path (Path) –

    The path to the emoji file to process

Returns:

  • tuple[Emoji | None, Path | None]

    A tuple where the first element is the newly created emoji (if created) and the second element is the file_path if processing failed or was skipped.

sync_emojis async
Python
sync_emojis() -> tuple[list[Emoji], list[Path]]

Synchronize emojis from the local assets directory to the application.

Ensures the cache is initialized, then iterates through local emoji files. If an emoji with the same name doesn't exist in the cache, it attempts to create it.

Returns:

  • tuple[list[Emoji], list[Path]]

    A tuple containing: - A list of successfully created discord.Emoji objects. - A list of file paths for emojis that already existed or failed.

_ensure_initialized async
Python
_ensure_initialized() -> bool

Check if cache is initialized, logs warning if not.

Returns:

  • bool

    True if initialized, False otherwise.

_delete_discord_emoji async
Python
_delete_discord_emoji(name: str) -> bool

Delete an existing Discord emoji by name and updates cache.

Parameters:

  • name (str) –

    The name of the emoji to delete.

Returns:

  • bool

    True if the emoji was deleted, False otherwise.

resync_emoji async
Python
resync_emoji(name: str) -> Emoji | None

Resync a specific emoji: Deletes existing, finds local file, creates new.

Parameters:

  • name (str) –

    The name of the emoji to resync.

Returns:

  • Optional[Emoji]

    The newly created emoji if successful, otherwise None.

delete_all_emojis async
Python
delete_all_emojis() -> tuple[list[str], list[str]]

Delete all application emojis that match names from the emoji assets directory.

This method: 1. Ensures the emoji cache is initialized 2. Finds all potential emoji names from the assets directory 3. Deletes any matching emojis from Discord and updates the cache

Returns:

  • tuple[list[str], list[str]]

    A tuple containing: - A list of successfully deleted emoji names - A list of emoji names that failed to delete or weren't found

Functions

_is_valid_emoji_name

Python
_is_valid_emoji_name(name: str) -> bool

Check if an emoji name meets basic validity criteria.

Returns:

  • bool

    True if the name is valid, False otherwise.

_find_emoji_file

Python
_find_emoji_file(base_path: Path, name: str) -> Path | None

Find the local file corresponding to an emoji name within a base path.

Returns:

  • Path | None

    The path to the emoji file if found, None otherwise.

_read_emoji_file

Python
_read_emoji_file(file_path: Path) -> bytes | None

Read image bytes from a file path, handling errors.

Returns:

  • bytes | None

    The file contents as bytes if successful, None otherwise.