idlefox/src/io.rs

152 lines
5.1 KiB
Rust

//! All IO related functions, include all Discord IO functions
//!
//! Features:
//! All recv and send functions to discord
//! Helper function for advanced checks
use poise::serenity_prelude as serenity;
/// User data, which is stored and accessible in all command invocations
pub struct Data {}
/// A generic temporary serenity error
type GenericError = Box<dyn std::error::Error + Send + Sync>;
/// The context (in a discord sence) of a message
type Context<'a> = poise::Context<'a, Data, GenericError>;
// // Displays your or another user's account creation date
// // Dummy command from poise documentation
// #[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(())
// }
/// The result of a vote creation
#[derive(Debug, poise::Modal)]
#[allow(dead_code)] // fields only used for Debug print
#[name = "Créer un nouveau Vote"]
struct VoteCreation {
/// The title of the event
#[name = "Titre"]
#[placeholder = "Titre de votre événement"]
title: String,
/// The name of the event
#[name = "Nom du jeu"]
#[placeholder = "'FFXIV', 'MHW', 'D&D'..."]
short_name: Option<String>,
/// The group that shall be tagged by the bot
#[name = "Groupe"]
#[placeholder = "groupe (sans le @)"]
group: Option<String>,
/// The description of the event
#[paragraph]
#[name = "Description"]
#[placeholder = "petite description de votre événement"]
description: String,
/// The time a which the event shall take place
#[name = "Horaires"]
#[placeholder = "'21h - 23h' par défaut"]
hours: Option<String>,
// Max fields is 5 so the two below will have to be command-exclusive
// #[name = "Date de début du vote"]
// #[placeholder = "'ven', '10', '21 jan', vide pour la prochaine période de votes"]
// start_date: Option<String>,
// #[name = "Jours non dispos"]
// #[placeholder = "'lun', '30', '2 dec'"]
// non_days: Option<String>,
}
#[poise::command(slash_command)]
pub async fn vote_modal(
ctx: poise::ApplicationContext<'_, Data, GenericError>,
) -> Result<(), GenericError> {
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(())
}
/// List of choices for all the roles
#[derive(Debug, poise::ChoiceParameter)]
pub enum ListOfRoles {
// TODO: This needs to be a list of all guildRoles under Users (included)
#[name = "hunters"]
Hunters,
}
// Since this is a discord command and not a real rust command, this many arguments is "normal"
#[allow(clippy::too_many_arguments)]
#[poise::command(prefix_command, slash_command)]
pub async fn vote_command(
ctx: Context<'_>,
#[description = "Titre"] title: String,
#[description = "Nom du jeu"] short_name: Option<String>,
#[description = "Groupe"] group: ListOfRoles,
#[description = "Description"] description: String,
#[description = "Horaires"] hours: Option<String>,
#[description = "Date de début du vote"] start_date: Option<String>,
#[description = "Jours non dispos"] non_days: Option<String>,
) -> Result<(), GenericError> {
ctx.say(format!(
"{:?} ; {:?} ; {:?} ; {:?} ; {:?} ; {:?} ; {:?}",
title, short_name, group, description, hours, start_date, non_days
))
.await?;
Ok(())
}
/// TODO: Fill doc
#[poise::command(prefix_command, slash_command)]
pub async fn component_modal(ctx: Context<'_>) -> Result<(), GenericError> {
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(())
}
#[cfg(test)]
mod tests {
// use super::*;
}