stat_summoner/module/whoisfollowed/
whoisfollowed.rs

1use crate::embed::schedule_message_deletion;
2use crate::models::data::{Data, SummonerFollowedData};
3use crate::models::error::Error;
4use crate::module::whoisfollowed::utils::{
5    create_embed_followed_summoner, get_data_followed_summoner,
6};
7use crate::utils::manage_user;
8use mongodb::bson::doc;
9
10/// Retrieves and displays the list of summoners followed in the current Discord guild.
11///
12/// This slash command fetches the summoners being followed within the Discord guild where the command is invoked.
13/// It queries the MongoDB collection for follow data and creates an embed message that lists all tracked summoners, along with the time remaining for each follow.
14/// The message is set to automatically delete after 60 seconds.
15///
16/// # Parameters:
17/// - `ctx`: The context of the command, which includes information about the current Discord guild, channel, and bot data.
18///   The `ctx` is used to access the MongoDB client, retrieve the guild's ID, and send the resulting message.
19///
20/// # Returns:
21/// - `Result<(), Error>`: Returns `Ok(())` on success, or an `Error` if something goes wrong during database access or message creation.
22///
23/// # ⚠️ Notes:
24/// - The function retrieves the guild's ID and queries the `follower_summoner` collection for summoners being tracked in that guild.
25/// - It uses the `get_data_followed_summoner` function to gather the data and the `create_embed_followed_summoner` function to construct the embed message.
26/// - The message is automatically deleted after 60 seconds using the `schedule_message_deletion` function.
27/// - The command can only be used in a Discord server (guild), not in direct messages.
28///
29/// # Example:
30/// ```rust
31/// #[poise::command(slash_command)]
32/// pub async fn whoisfollowed(
33///     ctx: poise::ApplicationContext<'_, Data, Error>,
34/// ) -> Result<(), Error> {
35///     let mongo_client = &ctx.data().mongo_client;
36///     let collection = mongo_client
37///         .database("stat-summoner")
38///         .collection::<SummonerFollowedData>("follower_summoner");
39///
40///     let guild_id = ctx.guild_id().map(|id| id.get()).unwrap_or(0).to_string();
41///
42///     let followed_data = get_data_followed_summoner(collection, guild_id).await?;
43///
44///     let reply = ctx.send(create_embed_followed_summoner(followed_data)).await?;
45///     schedule_message_deletion(reply, ctx).await?;
46///     return Ok(());
47/// }
48/// ```
49///
50/// This command will create an embed showing all followed summoners in the guild where the command is run, along with their remaining follow time.
51#[poise::command(slash_command)]
52pub async fn whoisfollowed(ctx: poise::ApplicationContext<'_, Data, Error>) -> Result<(), Error> {
53    manage_user(
54        ctx.author().id.to_string(),
55        ctx.author().name.clone(),
56        &ctx.data().mongo_client,
57        false,
58    )
59    .await?;
60
61    let mongo_client = &ctx.data().mongo_client;
62    let collection = mongo_client
63        .database("stat-summoner")
64        .collection::<SummonerFollowedData>("follower_summoner");
65
66    let guild_id = ctx.guild_id().map(|id| id.get()).unwrap_or(0).to_string();
67    let followed_data = get_data_followed_summoner(collection, guild_id).await?;
68    let reply = ctx
69        .send(create_embed_followed_summoner(followed_data))
70        .await?;
71    schedule_message_deletion(reply, ctx).await?;
72    return Ok(());
73}