Function update_follower_if_new_match

Source
async fn update_follower_if_new_match(
    collection: &Collection<SummonerFollowedData>,
    followed_summoner: &SummonerFollowedData,
    riot_api_key: &str,
    http: Arc<Http>,
    collection_emojis: Collection<EmojiId>,
) -> Result<(), Box<dyn Error + Send + Sync>>
Expand description

⚙️ Function: Updates a followed summoner’s last match ID and sends a Discord update if a new match is detected.

This asynchronous function checks if a followed summoner has played a new match. If a new match is detected, it updates the summoner’s last match ID in the MongoDB collection and sends a match update to the appropriate Discord channel.

§Parameters:

  • collection: A reference to a MongoDB Collection<SummonerFollowedData> that stores the followed summoners’ data.
  • followed_summoner: A reference to a SummonerFollowedData struct containing the summoner’s information, including their PUUID, summoner ID, and last match ID.
  • riot_api_key: A string slice containing the Riot Games API key for authenticating the API request.
  • http: An Arc<Http> object used to send messages via the Discord API.
  • collection_emojis: A MongoDB Collection containing emoji mappings, used to enhance the Discord embed with custom emojis for roles and champions.

§Returns:

  • Result<(), Error>: Returns Ok(()) if the last match ID was successfully updated and the match update was sent to Discord, or an error if something went wrong.

§Example:

This function is typically called periodically to check if a followed summoner has played a new match:

let result = update_follower_if_new_match(collection, &followed_summoner, riot_api_key, http.clone(), collection_emojis).await;
if result.is_err() {
    // Handle error (e.g., log failure or retry)
}

§Notes:

  • The function begins by creating an HTTP client using reqwest and fetching the latest match ID for the summoner using the get_latest_match_id function.
  • If the new match ID is different from the stored last_match_id, the function updates the MongoDB collection with the new match ID.
  • Once the database is updated, the function calls send_match_update_to_discord to send a match update to the Discord channel associated with the summoner.
  • This function ensures that the Discord server is notified whenever the summoner completes a new match, keeping followers updated in real time.