Function get_emoji

Source
pub async fn get_emoji(
    collection: Collection<EmojiId>,
    role: &str,
    name: &str,
) -> Result<String, Error>
Expand description

⚙️ Function: Retrieves a custom emoji string based on role and name from a MongoDB collection.

This asynchronous function searches a MongoDB collection for a custom emoji corresponding to a specific role and name. If found, it formats the emoji in a string compatible with Discord. If not found, it returns the provided name as a fallback.

§Parameters:

  • collection: A MongoDB Collection<EmojiId> containing the emoji mappings, where each document maps a role and name to an emoji ID.
  • role: A string slice representing the role of the emoji (e.g., “position”, “champions”).
  • name: A string slice representing the name of the emoji (e.g., “TOP”, “JUNGLE”, champion names).

§Returns:

  • Result<String, Error>: Returns a Result containing the formatted emoji string (if found) or the name as a fallback. In case of errors, it logs the error and returns the name.

§Example:

This function can be used to retrieve custom emojis for roles or champions when creating embeds for Discord:

let emoji = get_emoji(collection_emojis, "position", "TOP").await?;
println!("The emoji for TOP is: {}", emoji);

§Notes:

  • The function creates a MongoDB filter to search for the emoji based on the role and name fields.
  • If an emoji is found, it formats the emoji string in the form <:name:id>, which is recognized by Discord.
  • If no emoji is found or an error occurs, the function returns the name string as a fallback and logs any errors encountered during the search.