From ce6819a9f53b1026688e88e79231f2b13d9ad25a Mon Sep 17 00:00:00 2001 From: AXON Date: Fri, 14 Jul 2023 11:46:19 -0700 Subject: [PATCH] feat: Company share (#52) --- src/config.rs | 6 +++ .../interaction_create/getting_started.rs | 28 ++++++++++---- src/event/interaction_create/mod.rs | 38 +++++++++++++++++++ 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/config.rs b/src/config.rs index 1e2e18b..01339a2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -14,6 +14,7 @@ pub struct BotConfig { pub discord: DiscordConfig, pub meilisearch: Option, pub openai: Option, + pub misc: Option, } #[derive(Debug, Deserialize, Clone)] @@ -51,6 +52,11 @@ pub struct OpenaiConfig { pub api_key: String, } +#[derive(Debug, Deserialize)] +pub struct Misc { + pub company_share_endpoint: String, +} + pub fn read(toml_path: &Path) -> Result { // Get executable path and parent dir let exec_path = std::env::current_exe()?; diff --git a/src/event/interaction_create/getting_started.rs b/src/event/interaction_create/getting_started.rs index 9e31b76..f5e12ce 100644 --- a/src/event/interaction_create/getting_started.rs +++ b/src/event/interaction_create/getting_started.rs @@ -210,7 +210,7 @@ pub async fn responder(mci: &MessageComponentInteraction, ctx: &Context) -> Resu mci.create_interaction_response(&ctx.http, |r| { r.kind(InteractionResponseType::ChannelMessageWithSource); r.interaction_response_data(|d| { - d.content("**[1/4]:** Which additional channels would you like to have access to?"); + d.content("**[1/5]:** Which additional channels would you like to have access to?"); d.components(|c| { c.create_action_row(|a| { a.create_select_menu(|s| { @@ -260,7 +260,7 @@ pub async fn responder(mci: &MessageComponentInteraction, ctx: &Context) -> Resu "channel_choice" => { interaction.create_interaction_response(&ctx.http, |r| { r.kind(InteractionResponseType::UpdateMessage).interaction_response_data(|d|{ - d.content("**[2/4]:** Would you like to get notified for announcements and community events?"); + d.content("**[2/5]:** Would you like to get notified for announcements and community events?"); d.components(|c| { c.create_action_row(|a| { a.create_button(|b|{ @@ -287,7 +287,7 @@ pub async fn responder(mci: &MessageComponentInteraction, ctx: &Context) -> Resu "subscribed" | "not_subscribed" => { interaction.create_interaction_response(&ctx.http, |r| { r.kind(InteractionResponseType::UpdateMessage).interaction_response_data(|d| { - d.content("**[3/4]:** Why did you join our community?\nI will point you to the correct channels with this info.").components(|c| { + d.content("**[3/5]:** Why did you join our community?\nI will point you to the correct channels with this info.").components(|c| { c.create_action_row(|a| { a.create_button(|b|{ b.label("To hangout with others"); @@ -329,10 +329,23 @@ pub async fn responder(mci: &MessageComponentInteraction, ctx: &Context) -> Resu "hangout" | "gitpodio_help" | "selfhosted_help" => { interaction .create_interaction_response(&ctx.http, |r| { - r.kind(InteractionResponseType::UpdateMessage) + r.kind(InteractionResponseType::Modal) .interaction_response_data(|d| { - d.content("**[3/4]**: You have personalized the server, congrats!") - .components(|c| c) + d.custom_id("company_name_submitted") + .title("[4/5] Share your company name") + .components(|c| { + c.create_action_row(|a| { + a.create_input_text(|t| { + t.custom_id("company_submitted") + .label("Company (optional)") + .placeholder("Type in your company name") + .max_length(50) + .min_length(2) + .required(false) + .style(component::InputTextStyle::Short) + }) + }) + }) }) }) .await?; @@ -340,6 +353,7 @@ pub async fn responder(mci: &MessageComponentInteraction, ctx: &Context) -> Resu // Save join reason join_reason.push_str(interaction.data.custom_id.as_str()); + // Fetch member let mut member = mci.member.clone().context("Can't fetch member")?; let member_role = get_role(mci, ctx, "Member").await?; let never_introduced = { @@ -371,7 +385,7 @@ pub async fn responder(mci: &MessageComponentInteraction, ctx: &Context) -> Resu let followup = interaction .create_followup_message(&ctx.http, |d| { - d.content("**[4/4]:** How did you find Gitpod?"); + d.content("**[5/5]:** How did you find Gitpod?"); d.components(|c| { c.create_action_row(|a| { a.create_select_menu(|s| { diff --git a/src/event/interaction_create/mod.rs b/src/event/interaction_create/mod.rs index 6e0fb21..3d283bb 100644 --- a/src/event/interaction_create/mod.rs +++ b/src/event/interaction_create/mod.rs @@ -1,4 +1,6 @@ use anyhow::Result; +use serde_json::json; +use serenity::model::application::component::ActionRowComponent; use serenity::{client::Context, model::application::interaction::Interaction}; // Internals @@ -7,6 +9,38 @@ mod getting_started; mod question_thread_suggestions; mod slash_commands; +use serenity::model::application::interaction::modal::ModalSubmitInteraction; + +use crate::BOT_CONFIG; +async fn company_name_submitted_response( + mci: &ModalSubmitInteraction, + ctx: &Context, +) -> Result<()> { + mci.create_interaction_response(&ctx.http, |r| { + r.kind(serenity::model::prelude::InteractionResponseType::UpdateMessage) + .interaction_response_data(|d| { + d.content("**[4/4]**: You have personalized the server, congrats!") + .components(|c| c) + }) + }) + .await?; + if let Some(component) = &mci.data.components.get(0) + && let Some(input_field) = component.components.get(0) + && let ActionRowComponent::InputText(it) = input_field + && let Some(config) = BOT_CONFIG.get() && let Some(misc) = &config.misc + { + reqwest::Client::new() + .post(&misc.company_share_endpoint) + .json(&json!( + { + "username": mci.user.name, + "company": it.value + } + )).send().await?; + } + Ok(()) +} + pub async fn responder(ctx: &Context, interaction: Interaction) -> Result<()> { match interaction { Interaction::MessageComponent(mci) => match mci.data.custom_id.as_str() { @@ -23,6 +57,10 @@ pub async fn responder(ctx: &Context, interaction: Interaction) -> Result<()> { _ => {} }, + Interaction::ModalSubmit(mci) => if mci.data.custom_id.as_str() == "company_name_submitted" { + company_name_submitted_response(&mci, ctx).await? + } + _ => {} }