Function handle_interaction_button_flex

Source
pub async fn handle_interaction_button_flex(
    ctx: Context,
    message_component_interaction: ComponentInteraction,
    ctx_data: &Data,
) -> Result<(), Box<dyn Error + Send + Sync>>
Expand description

Handles flex interactions with buttons in Discord messages.

This function processes button clicks in Discord related to the flex feature, specifically those with a custom_id starting with flex_user:. It identifies the user and the selected role, updates the embed message to reflect the user’s choice, and manages role assignments based on the presence of existing users.

§Parameters:

  • ctx: The context of the interaction, providing access to the bot and its utilities.
  • message_component_interaction: The interaction object representing the button click event.
  • ctx_data: The application’s shared data, containing the MongoDB client and other configuration details.

§Returns:

  • Result<(), Error>: Returns Ok(()) if the interaction is processed successfully; otherwise, returns an Error.

§⚠️ Notes:

  • The function checks if the custom_id starts with flex_user: to identify flex actions.
  • The custom_id must contain the role as a string after the prefix to parse the selected role.
  • Updates the Discord embed fields based on user selections and ensures only one user is assigned per role.
  • Handles setting and resetting user mentions in the embed fields dynamically.

§Example:

handle_interaction_button_flex(ctx, message_component_interaction, ctx_data).await?;

When a user clicks a “Flex” button for a specific role, the following actions occur:

  1. The selected role is parsed from the custom_id.
  2. The corresponding embed field is updated with the user’s mention if it was previously empty.
  3. If the user was already set in another role, that field is reset to TBD.
  4. If the user is already in the selected role, the field is reset to TBD.
  5. The modified embed is then updated in the original Discord message.

§Errors:

  • If the custom_id is not formatted correctly or the role is unrecognized, the function exits silently.
  • If any database operation with MongoDB fails, an error is returned.
  • If the message cannot be edited on Discord, an error is returned.

§See Also:

  • get_emoji: Retrieves emojis from the MongoDB collection for use in embed fields.
  • Role: Enum representing the different roles available in the flex feature.
  • EmojiId: Represents the structure for storing emoji identifiers in the database.
  • Role: Enum defining Top, Jungle, Mid, ADC, and Support roles.

§Dependencies:

  • Relies on MongoDB for storing and retrieving emoji data.
  • Uses poise::serenity_prelude for interacting with Discord messages and embeds.
  • Parses roles from button custom_id strings using FromStr trait.