Skip to content

Commit

Permalink
feat: Company share
Browse files Browse the repository at this point in the history
  • Loading branch information
axonasif committed Jul 14, 2023
1 parent b1eb3aa commit 8815aa7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct BotConfig {
pub discord: DiscordConfig,
pub meilisearch: Option<MeilisearchConfig>,
pub openai: Option<OpenaiConfig>,
pub misc: Option<Misc>,
}

#[derive(Debug, Deserialize, Clone)]
Expand Down Expand Up @@ -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<BotConfig, Report> {
// Get executable path and parent dir
let exec_path = std::env::current_exe()?;
Expand Down
28 changes: 21 additions & 7 deletions src/event/interaction_create/getting_started.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -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|{
Expand All @@ -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");
Expand Down Expand Up @@ -329,17 +329,31 @@ 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?;

// 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 = {
Expand Down Expand Up @@ -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| {
Expand Down
38 changes: 38 additions & 0 deletions src/event/interaction_create/mod.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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() {
Expand All @@ -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?
}

_ => {}
}

Expand Down

0 comments on commit 8815aa7

Please sign in to comment.