From ee29c47387e007685268aa0c73105a7561f9ccec Mon Sep 17 00:00:00 2001 From: Vicky Date: Tue, 29 Oct 2024 15:59:12 +0100 Subject: [PATCH] First draft of modal vote --- Cargo.toml | 3 ++ src/.token | 1 + src/main.rs | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 src/.token diff --git a/Cargo.toml b/Cargo.toml index bdb547b..8b2bd28 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,9 @@ version = "0.1.0" edition = "2021" [dependencies] +poise = "0.6.1" +poise_macros = "0.6.1" +tokio = { version = "1.41.0", features = ["rt-multi-thread"] } [profile.dev] debug = 0 diff --git a/src/.token b/src/.token new file mode 100644 index 0000000..2e8ed40 --- /dev/null +++ b/src/.token @@ -0,0 +1 @@ +MTMwMDc2ODk2MTgwNTI5MTUzMA.GlMP_L.PU1953SsJ-8naI2rXSRLZuZw1jzV7CgckYtOSk diff --git a/src/main.rs b/src/main.rs index bfd1c1d..5e1bf68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,119 @@ mod server; mod votes; mod watchdog; -#[cfg(not(tarpaulin_include))] -fn main() { - println!("Hello, world!"); +use poise::serenity_prelude as serenity; +// use poise::{serenity_prelude as serenity, CreateReply}; + +struct Data {} // User data, which is stored and accessible in all command invocations +type Error = Box; +type Context<'a> = poise::Context<'a, Data, Error>; + +/// Displays your or another user's account creation date +#[poise::command(slash_command, prefix_command)] +async fn how_old( + ctx: Context<'_>, + #[description = "Selected user"] user: Option, +) -> Result<(), Error> { + let u = user.as_ref().unwrap_or_else(|| ctx.author()); + let response = format!("{}'s account was created at {}", u.name, u.created_at()); + ctx.say(response).await?; + Ok(()) +} + +#[derive(Debug, poise::Modal)] +#[allow(dead_code)] // fields only used for Debug print +#[name = "Créer un nouveau Vote"] +struct VoteCreation { + #[name = "Titre"] + #[placeholder = "Titre de votre événement"] + title: String, + #[name = "Nom du jeu"] + #[placeholder = "'FFXIV', 'MHW', 'D&D'..."] + short_name: Option, + #[name = "Groupe"] + #[placeholder = "groupe (sans le @)"] + group: Option, + #[paragraph] + #[name = "Description"] + #[placeholder = "petite description de votre événement"] + description: String, + #[name = "Horaires"] + #[placeholder = "'21h - 23h' par défaut"] + hours: Option, +} + +#[poise::command(slash_command)] +pub async fn modal(ctx: poise::ApplicationContext<'_, Data, Error>) -> Result<(), Error> { + use poise::Modal as _; + + let data = VoteCreation::execute(ctx).await?; + println!("Got data: {:?}", data); + if let Some(unwrapped_data) = data { + let group = unwrapped_data.group.unwrap_or("".into()); + let title = unwrapped_data.title; + let description = unwrapped_data.description; + let short_name = unwrapped_data.short_name.unwrap_or("".into()); + let hours = unwrapped_data.hours.unwrap_or("".into()); + + let reply = poise::CreateReply::default().content(format!( + "@{} **{}**\n{}\n{}, {}", + group, title, description, short_name, hours + )); + ctx.send(reply).await?; + }; + + Ok(()) +} + +#[poise::command(prefix_command, slash_command)] +pub async fn component_modal(ctx: crate::Context<'_>) -> Result<(), Error> { + let reply = { + let components = vec![serenity::CreateActionRow::Buttons(vec![ + serenity::CreateButton::new("open_modal") + .label("Open modal") + .style(poise::serenity_prelude::ButtonStyle::Success), + ])]; + + poise::CreateReply::default() + .content("Click the button below to open the modal") + .components(components) + }; + + ctx.send(reply).await?; + + while let Some(mci) = serenity::ComponentInteractionCollector::new(ctx.serenity_context()) + .timeout(std::time::Duration::from_secs(120)) + .filter(move |mci| mci.data.custom_id == "open_modal") + .await + { + let data = + poise::execute_modal_on_component_interaction::(ctx, mci, None, None) + .await?; + println!("Got data: {:?}", data); + } + Ok(()) +} + +#[tokio::main] +async fn main() { + let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN"); + let intents = serenity::GatewayIntents::non_privileged(); + + let framework = poise::Framework::builder() + .options(poise::FrameworkOptions { + commands: vec![how_old(), modal(), component_modal()], + ..Default::default() + }) + .setup(|ctx, _ready, framework| { + Box::pin(async move { + poise::builtins::register_globally(ctx, &framework.options().commands).await?; + Ok(Data {}) + }) + }) + .build(); + + let client = serenity::ClientBuilder::new(token, intents) + .framework(framework) + .await; + client.unwrap().start().await.unwrap(); }