Function check_and_add_in_db

Source
pub async fn check_and_add_in_db(
    collection: Collection<SummonerFollowedData>,
    ctx: ApplicationContext<'_, Data, Box<dyn Error + Send + Sync>>,
    modal_data: FollowGamesModal,
    region_str: String,
    puuid: String,
    match_id: String,
    time_end_follow: String,
) -> Result<(), Box<dyn Error + Send + Sync>>
Expand description

⚙️ Function: Adds a summoner to the database for game follow-up if they are not already being followed.

This asynchronous function checks if a summoner is already being followed by querying the MongoDB collection using their puuid. If they are not followed yet, it adds the summoner’s data to the database and returns a success message. If the summoner is already followed, an error message is returned.

§Parameters:

  • collection: A MongoDB collection (mongodb::Collection<SummonerFollowedData>) where the summoner’s follow data is stored.
  • ctx: The poise::ApplicationContext provides the context for the Discord interaction, including the ability to send responses.
  • modal_data: A FollowGamesModal struct containing the user’s input data from the modal (game name, tag line, etc.).
  • region_str: A string representing the summoner’s region (e.g., “NA”, “EUW”).
  • puuid: A string containing the summoner’s unique PUUID (player unique identifier from Riot’s API).
  • guild_id: An integer representing the ID of the Discord guild.
  • summoner_id: A string containing the summoner’s unique Summoner ID from Riot’s API.
  • match_id: A string representing the summoner’s latest match ID.
  • time_end_follow: A string representing the timestamp for when the follow period ends.

§Returns:

  • Result<(), Error>: Returns an empty result if the operation is successful, or an error if any part of the process fails.

§Example:

This function is used internally to add a summoner to the follow list after a successful interaction with the /followgames command:

check_and_add_in_db(collection, ctx, modal_data, region_str, puuid, summoner_id, match_id, time_end_follow).await?;

§Notes:

  • If the user is already being followed, an error message is sent to the Discord channel using create_embed_error.
  • If the user is successfully added to the database, a success message is sent using create_embed_success.
  • The function makes sure to handle errors from both MongoDB operations and Discord message sending by logging appropriate error messages.