add wager and balance commands

This commit is contained in:
2024-10-10 23:54:13 -04:00
parent 7d591197b0
commit f131815bb2
3 changed files with 38 additions and 5 deletions

1
Cargo.lock generated
View File

@@ -121,6 +121,7 @@ version = "0.1.0"
dependencies = [
"dotenv",
"poise",
"rand",
"tokio",
]

View File

@@ -7,3 +7,4 @@ edition = "2021"
poise = "0.6.1"
tokio = { version = "1", features = ["full"] }
dotenv = "0.15.0"
rand = "0.8.5"

View File

@@ -1,8 +1,12 @@
use std::env;
use std::iter;
use tokio::sync::Mutex;
use poise::{serenity_prelude::{self as serenity, Colour}, CreateReply};
struct Data {}
struct Data {
tokens: Mutex<usize>,
}
type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;
@@ -100,11 +104,38 @@ async fn yeehaw(ctx: Context<'_>,
height: usize) -> Result<(), Error> {
ctx.reply(iter::repeat("\u{1F920}".to_string().repeat(width))
.take(height)
.collect::<Vec<String>>()
.collect::<Vec<_>>()
.join("\n")).await?;
Ok(())
}
#[poise::command(slash_command)]
async fn wager(ctx: Context<'_>, amount: usize) -> Result<(), Error> {
let mut wealth = ctx.data().tokens.lock().await;
if *wealth < amount {
ctx.reply("You do not have enough tokens to wager this amount.").await?;
return Ok(());
}
if rand::random() {
*wealth += amount;
ctx.reply(format!("You just gained {} tokens! You now have **{}**.", amount, *wealth)).await?;
} else {
*wealth -= amount;
ctx.reply(format!("You've lost **{}** tokens, you now have **{}**.", amount, *wealth)).await?;
}
Ok(())
}
#[poise::command(slash_command)]
async fn balance(ctx: Context<'_>) -> Result<(), Error> {
let wealth = ctx.data().tokens.lock().await;
ctx.reply(format!("You have **{}** tokens.", *wealth)).await?;
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Error> {
dotenv::dotenv().ok();
@@ -114,13 +145,13 @@ async fn main() -> Result<(), Error> {
let framework = poise::Framework::builder()
.options(poise::FrameworkOptions {
commands: vec![ping(), dox(), yeehaw()],
commands: vec![ping(), dox(), yeehaw(), wager(), balance()],
..Default::default()
})
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {})
Ok(Data { tokens: Mutex::new(100) })
})
})
.build();