stat_summoner/module/randomchampions/
randomchampions.rs

1use poise::CreateReply;
2
3use crate::embed::schedule_message_deletion;
4use crate::models::data::{Data, EmojiId};
5use crate::models::error::Error;
6use crate::models::role::Role;
7use crate::module::championsinfos::utils::create_embed_champions_info;
8use crate::module::randomchampions::utils::{get_list_champions, get_random_champion};
9use crate::utils::manage_user;
10
11/// Generates a random League of Legends champion embed and sends it as a Discord message.
12///
13/// This command selects a random champion from the available list, optionally filtered by role, and constructs a detailed Discord embed
14/// containing information about that champion. The embed includes roles, winrate, banrate, popularity, runes (with emojis),
15/// and the core item build for the randomly selected champion.
16///
17/// # Parameters:
18/// - `ctx`: The command's context, providing access to the bot, the message, and other utilities.
19/// - `role`: An optional parameter specifying the role of the champion. If provided, the champion list will be filtered accordingly.
20///
21/// # Returns:
22/// - `Result<(), Error>`: Returns `Ok(())` if the command executes successfully, otherwise returns an `Error`.
23///
24/// # ⚠️ Notes:
25/// - The function calls `get_list_champions` to retrieve a list of champions, optionally filtered by role.
26/// - It uses `get_random_champion` to randomly select a champion from the filtered list.
27/// - `create_embed_champions_info` is called to construct a richly formatted embed with the champion's details.
28/// - After sending the embed, the message is scheduled for deletion after 60 seconds to keep the chat clean.
29///
30/// # Example:
31/// ```rust
32/// randomchampions(ctx, Some(Role::Top)).await?;
33/// ```
34///
35/// This command produces an embed displaying information such as:
36/// ```text
37/// 📝 Information about Jhin
38/// Roles: AD Carry
39/// Winrate: 51.74%   Banrate: 17.61%   Popularity: 27.57%
40///
41/// Runes:
42/// **Primary Rune:** <FleetFootwork Emoji>
43/// <PresenceOfMind Emoji> <LegendBloodline Emoji> <CoupDeGrace Emoji>
44///
45/// **Secondary Runes:**
46/// <Celerity Emoji> <GatheringStorm Emoji>
47///
48/// **Shards:** <AdaptiveForce Emoji> <AdaptiveForce Emoji> <HealthScale Emoji>
49///
50/// Item Build:
51/// <StatikkShiv Emoji> <RapidFirecannon Emoji> <InfinityEdge Emoji>
52/// ```
53///
54/// # Errors:
55/// - If the retrieval of the champion list fails (`get_list_champions`), the function returns an `Error`.
56/// - If there is an issue constructing the embed (`create_embed_champions_info`), an `Error` will be returned.
57/// - If the message deletion fails, it will log the error, but the command will still complete successfully.
58///
59/// # See Also:
60/// - `get_list_champions`: Retrieves the list of champions, filtered by role if specified.
61/// - `get_random_champion`: Selects a random champion from the provided list.
62/// - `create_embed_champions_info`: Constructs the champion information embed to be sent.
63/// - `schedule_message_deletion`: Schedules a message for deletion after a specific time interval to maintain chat cleanliness.
64///
65/// # Related Structures:
66/// - `ChampionData`: Contains the champion's details used to construct the embed.
67/// - `EmojiId`: Represents the mapping of rune or item names to their corresponding emoji IDs.
68///
69/// # Dependencies:
70/// - This function relies on a MongoDB collection for retrieving emojis.
71/// - The embed includes images fetched from the Data Dragon API.
72#[poise::command(slash_command)]
73pub async fn randomchampions(
74    ctx: poise::ApplicationContext<'_, Data, Error>,
75    #[description = "Select a role (optional)"] role: Option<Role>,
76) -> Result<(), Error> {
77    manage_user(
78        ctx.author().id.to_string(),
79        ctx.author().name.clone(),
80        &ctx.data().mongo_client,
81        false,
82    )
83    .await?;
84
85    let champions_list = get_list_champions(ctx, role).await?;
86    let mongo_client = &ctx.data().mongo_client;
87    let collection_emoji = mongo_client
88        .database("stat-summoner")
89        .collection::<EmojiId>("emojis_id");
90    let champion_data = get_random_champion(champions_list);
91    let embed = create_embed_champions_info(champion_data, &collection_emoji).await?;
92    let reply = CreateReply {
93        embeds: vec![embed],
94        ..Default::default()
95    };
96    let sent_message = ctx.send(reply).await?;
97    if let Err(e) = schedule_message_deletion(sent_message, ctx).await {
98        log::error!("Failed to schedule message deletion: {}", e);
99    }
100    Ok(())
101}