initial impl
This commit is contained in:
commit
c24a777dcc
6 changed files with 3241 additions and 0 deletions
93
src/main.rs
Normal file
93
src/main.rs
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
use std::fmt::Write;
|
||||
mod dice;
|
||||
mod parsing;
|
||||
|
||||
use std::env;
|
||||
|
||||
use dotenv::dotenv;
|
||||
use poise::CreateReply;
|
||||
use serenity::{client::ClientBuilder, model::prelude::*};
|
||||
|
||||
type Context<'a> = poise::Context<'a, Data, anyhow::Error>;
|
||||
pub struct Data;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
dotenv().ok();
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
|
||||
|
||||
let intents = GatewayIntents::non_privileged();
|
||||
|
||||
let framework = poise::Framework::builder()
|
||||
.options(poise::FrameworkOptions {
|
||||
commands: vec![help(), roll()],
|
||||
..Default::default()
|
||||
})
|
||||
.setup(move |ctx, _ready, framework| {
|
||||
Box::pin(async move {
|
||||
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
|
||||
Ok(Data {})
|
||||
})
|
||||
})
|
||||
.build();
|
||||
|
||||
let client = ClientBuilder::new(token, intents)
|
||||
.framework(framework)
|
||||
.await;
|
||||
|
||||
client.unwrap().start().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[poise::command(slash_command)]
|
||||
pub async fn help(ctx: Context<'_>, command: Option<String>) -> anyhow::Result<()> {
|
||||
let configuration = poise::builtins::HelpConfiguration {
|
||||
..Default::default()
|
||||
};
|
||||
poise::builtins::help(ctx, command.as_deref(), configuration).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[poise::command(slash_command)]
|
||||
pub async fn roll(
|
||||
ctx: Context<'_>,
|
||||
formula: String,
|
||||
#[description = "Roll the formula several times"] multi: Option<u32>,
|
||||
#[description = "Make the roll visible only to you"]
|
||||
#[flag]
|
||||
private: bool,
|
||||
) -> anyhow::Result<()> {
|
||||
let result = parsing::parse(&formula);
|
||||
let expression = match result {
|
||||
Ok(expression) => expression,
|
||||
Err(error) => {
|
||||
ctx.send(
|
||||
CreateReply::default()
|
||||
.content(format!("Dice formula could not be parsed.\n{}", error))
|
||||
.ephemeral(true)
|
||||
.reply(true),
|
||||
)
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
if private {
|
||||
ctx.defer_ephemeral().await?;
|
||||
} else {
|
||||
ctx.defer().await?;
|
||||
}
|
||||
let multi = multi.unwrap_or(1).min(32);
|
||||
let mut content = format!("**{} rolled:**\n", ctx.author().mention());
|
||||
for _ in 0..multi {
|
||||
write!(content, "{}\n", expression.evaluate(rand::rng())?)?;
|
||||
}
|
||||
write!(content, "-# Expected Value: {}", (expression.average(&mut rand::rng())? * 10.0).round() / 10.0)?;
|
||||
let reply = CreateReply::default()
|
||||
.content(content)
|
||||
.ephemeral(private)
|
||||
.reply(true);
|
||||
ctx.send(reply).await?;
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue