feat: add roll slash command

This commit is contained in:
Lucien Cartier-Tilet 2025-09-02 15:18:01 +02:00
parent eb32d58341
commit 4b6053fc8f
3 changed files with 76 additions and 1 deletions

View File

@ -2,6 +2,10 @@ use color_eyre::eyre::{Error, Result};
use poise::serenity_prelude::{self as serenity, FullEvent}; use poise::serenity_prelude::{self as serenity, FullEvent};
use tracing::info; use tracing::info;
mod roll;
type Context<'a> = poise::Context<'a, (), Error>;
fn event_handler(_ctx: serenity::Context, event: &FullEvent) { fn event_handler(_ctx: serenity::Context, event: &FullEvent) {
if let FullEvent::Ready { data_about_bot } = event { if let FullEvent::Ready { data_about_bot } = event {
info!("Logged in as {}", data_about_bot.user.name); info!("Logged in as {}", data_about_bot.user.name);
@ -13,7 +17,7 @@ pub async fn make_bot() -> Result<serenity::Client> {
let intents = serenity::GatewayIntents::non_privileged(); let intents = serenity::GatewayIntents::non_privileged();
let framework = poise::Framework::<(), Error>::builder() let framework = poise::Framework::<(), Error>::builder()
.options(poise::FrameworkOptions { .options(poise::FrameworkOptions {
commands: vec![], commands: vec![roll::roll()],
event_handler: |ctx, event, _framework: poise::FrameworkContext<'_, (), _>, _data| { event_handler: |ctx, event, _framework: poise::FrameworkContext<'_, (), _>, _data| {
Box::pin(async move { Box::pin(async move {
event_handler(ctx.clone(), event); event_handler(ctx.clone(), event);

70
src/discord/roll.rs Normal file
View File

@ -0,0 +1,70 @@
use std::str::FromStr;
use super::Context;
use color_eyre::eyre::{Error, Result};
use tracing::info;
enum Advantage {
None,
Beni,
Maudit,
}
impl FromStr for Advantage {
type Err = color_eyre::eyre::Report;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"aucun" => Ok(Self::None),
"béni" => Ok(Self::Beni),
"maudit" => Ok(Self::Maudit),
other => Err(Error::msg(format!("Could not parse {other} into an Advantage enum"))),
}
}
}
impl TryFrom<Option<String>> for Advantage {
type Error = color_eyre::eyre::Report;
fn try_from(value: Option<String>) -> std::result::Result<Self, Self::Error> {
match value {
Some(str) => Advantage::from_str(&str),
None => Ok(Advantage::None)
}
}
}
impl std::fmt::Display for Advantage {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let str = match self {
Self::None => "aucun".to_string(),
Self::Beni => "béni".to_string(),
Self::Maudit => "maudit".to_string(),
};
write!(f, "{str}")
}
}
async fn advantage_autocomplete(_ctx: Context<'_>, _: &str) -> impl Iterator<Item = String> {
[Advantage::None, Advantage::Beni, Advantage::Maudit]
.iter()
.map(std::string::ToString::to_string)
.collect::<Vec<String>>()
.into_iter()
}
#[poise::command(slash_command)]
pub async fn roll(
ctx: Context<'_>,
#[description = "Seuil de réussite"] sr: u32,
#[description = "Maîtrise"] mastery: u32,
#[description = "Avantage"]
#[autocomplete = "advantage_autocomplete"]
advantage: Option<String>,
) -> Result<()> {
info!("Called /roll with following context: {ctx:?}");
let advantage = Advantage::try_from(advantage)?;
info!("Rolling against SR {sr} with mastery {mastery} and advantage {advantage}");
Ok(())
}

View File

@ -1,6 +1,7 @@
#![deny(clippy::all, clippy::pedantic, clippy::nursery)] #![deny(clippy::all, clippy::pedantic, clippy::nursery)]
#![warn(missing_docs)] #![warn(missing_docs)]
#![allow(clippy::module_name_repetitions, clippy::redundant_pub_crate)] #![allow(clippy::module_name_repetitions, clippy::redundant_pub_crate)]
#![allow(clippy::unused_async)]
mod discord; mod discord;
mod utils; mod utils;