First draft of modal vote

This commit is contained in:
Vicky 2024-10-29 15:59:12 +01:00
parent f870f41e31
commit ee29c47387
3 changed files with 119 additions and 3 deletions

View File

@ -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

1
src/.token Normal file
View File

@ -0,0 +1 @@
MTMwMDc2ODk2MTgwNTI5MTUzMA.GlMP_L.PU1953SsJ-8naI2rXSRLZuZw1jzV7CgckYtOSk

View File

@ -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<dyn std::error::Error + Send + Sync>;
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<serenity::User>,
) -> 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<String>,
#[name = "Groupe"]
#[placeholder = "groupe (sans le @)"]
group: Option<String>,
#[paragraph]
#[name = "Description"]
#[placeholder = "petite description de votre événement"]
description: String,
#[name = "Horaires"]
#[placeholder = "'21h - 23h' par défaut"]
hours: Option<String>,
}
#[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::<VoteCreation>(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();
}