use std::sync::LazyLock; use twilight_model::{ application::{ command::{Command, CommandType}, interaction::Interaction, }, channel::message::MessageFlags, http::interaction::{InteractionResponse, InteractionResponseType}, }; use twilight_util::builder::{InteractionResponseDataBuilder, command::CommandBuilder}; use crate::{command::State, user_capnp::user::Consent}; const NAME: &str = "opt-in"; const DESCRIPTION: &str = "Opt in to being recorded"; pub static COMMAND: LazyLock = LazyLock::new(|| { CommandBuilder::new(NAME, DESCRIPTION, CommandType::ChatInput) .validate() .expect("command wasn't correct") .build() }); #[tracing::instrument] pub async fn handle(state: State, interaction: Interaction) { let user_id = interaction .member .as_ref() .and_then(|member| member.user.as_ref().map(|user| user.id)); let user_id = match user_id { Some(user_id) => user_id, None => { state .discord_client .interaction(state.discord_application_id) .create_response( interaction.id, &interaction.token, &InteractionResponse { kind: InteractionResponseType::ChannelMessageWithSource, data: Some( InteractionResponseDataBuilder::new() .content("TODO") .build(), ), }, ) .await .expect("TODO"); return; } }; let previous_consent = state .user_data_manager .update(user_id, |mut user_data| { let previous_consent = user_data .reborrow() .get_voice_recording_consent() .expect("TODO"); user_data.set_voice_recording_consent(Consent::Granted); previous_consent }) .await .expect("TODO"); state .discord_client .interaction(state.discord_application_id) .create_response( interaction.id, &interaction.token, &InteractionResponse { kind: InteractionResponseType::ChannelMessageWithSource, data: Some( InteractionResponseDataBuilder::new() .content(format!( "opted you in, your previous consent was {previous_consent:?}" )) .flags(MessageFlags::EPHEMERAL) .build(), ), }, ) .await .expect("TODO"); }