stat_summoner/module/suggestions/interaction_black_list.rs
1use poise::serenity_prelude::ComponentInteraction;
2
3use crate::models::{
4 data::{Data, User},
5 error::Error,
6};
7
8use super::utils::add_user_to_blacklist;
9
10/// Handles blacklist interactions with buttons in Discord messages.
11///
12/// This function processes button clicks in Discord, specifically those with a `custom_id`
13/// starting with `blacklist_user:`. It extracts the user ID and username from the `custom_id`,
14/// adds the user to a MongoDB blacklist collection, and deletes the original message containing
15/// the button.
16///
17/// # Parameters:
18/// - `ctx`: The context of the interaction, providing access to the bot and its utilities.
19/// - `interaction`: The interaction object representing the button click.
20/// - `ctx_data`: The application's shared data, containing the MongoDB client and other configuration.
21///
22/// # Returns:
23/// - `Result<(), Error>`: Returns `Ok(())` if the interaction is handled successfully; otherwise, returns an `Error`.
24///
25/// # ⚠️ Notes:
26/// - The function checks if the `custom_id` starts with `blacklist_user:` to identify blacklist actions.
27/// - The `custom_id` must be formatted as `blacklist_user:{user_id}|{username}` to extract the required data.
28/// - After adding the user to the blacklist, the message containing the button is deleted.
29///
30/// # Example:
31/// ```rust
32/// handle_interaction_button_black_list(ctx, message_component_interaction, ctx_data).await?;
33/// ```
34///
35/// When a moderator clicks the "Blacklist User" button on a suggestion, the following actions occur:
36/// 1. The user who submitted the suggestion is added to the blacklist in MongoDB.
37/// 2. The original suggestion message is deleted.
38/// 3. Errors during these actions are logged but do not stop the execution.
39///
40/// # Errors:
41/// - If the user cannot be added to the blacklist, an error is logged.
42/// - If the original message cannot be deleted, an error is logged.
43/// - If the `custom_id` does not have the correct format, the function exits without doing anything.
44///
45/// # See Also:
46/// - `add_user_to_blacklist`: Adds a user to the MongoDB blacklist collection.
47/// - `BlackList`: Represents the structure of a blacklisted user in the database.
48///
49/// # Related Structures:
50/// - `Interaction`: Represents the interaction object from Discord.
51/// - `BlackList`: Contains information about blacklisted users.
52///
53/// # Dependencies:
54/// - Relies on MongoDB for storing blacklisted users.
55/// - Uses `custom_id` from Discord buttons to identify actions and extract relevant data.
56pub async fn handle_interaction_button_black_list(
57 ctx: poise::serenity_prelude::Context,
58 message_component_interaction: ComponentInteraction,
59 ctx_data: &Data,
60) -> Result<(), Error> {
61 let custom_id = &message_component_interaction.data.custom_id;
62 if let Some(data) = custom_id.strip_prefix("blacklist_user:") {
63 let user_id = data;
64 let mongo_client = ctx_data.mongo_client.clone();
65 let collection = mongo_client
66 .database("stat-summoner")
67 .collection::<User>("users");
68
69 if let Err(e) = add_user_to_blacklist(&collection, user_id).await {
70 log::error!("Erreur lors de l'ajout à la blacklist : {:?}", e);
71 }
72
73 if let Err(e) = ctx
74 .http
75 .delete_message(
76 message_component_interaction.channel_id,
77 message_component_interaction.message.id,
78 None,
79 )
80 .await
81 {
82 log::error!("Erreur lors de la suppression du message : {:?}", e);
83 }
84 }
85 Ok(())
86}