From 94726df0903afcee71dbc6fddef0fab439dadd61 Mon Sep 17 00:00:00 2001 From: ByteDice Date: Sat, 1 Feb 2025 00:49:13 +0100 Subject: [PATCH] added /eight_ball command --- Cargo.toml | 1 + TODO.md | 3 ++- data/8-ball_classic.txt | 10 ++++++++++ data/8-ball_quirky.txt | 14 ++++++++++++++ src/cmds.rs | 21 +++++++++++++++++++++ src/events.rs | 6 +++++- src/main.rs | 16 +++++++++++++--- 7 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 data/8-ball_classic.txt create mode 100644 data/8-ball_quirky.txt diff --git a/Cargo.toml b/Cargo.toml index c6d2cc9..e5708a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2021" [dependencies] poise = "0.6.1" +rand = "0.9.0" tokio = { version = "1.43.0", features = ["rt-multi-thread"] } diff --git a/TODO.md b/TODO.md index 7cdd486..f2911d1 100644 --- a/TODO.md +++ b/TODO.md @@ -3,13 +3,14 @@ ### Medium priority: - [ ] JSON -> Rules list + * Structure: /rules [rulename/index] - [ ] Postfix calculator - [ ] Postfic generator - [ ] JSON -> BPS class init - [ ] BPS args -> JSON - [ ] Random tip (from ByteDice.net/data/loadingScreenTips.json) - [ ] A command that just sends my socials -- [ ] Magic 8 ball +- [x] Magic 8 ball ### Low priority: - [ ] Particle of the week diff --git a/data/8-ball_classic.txt b/data/8-ball_classic.txt new file mode 100644 index 0000000..0484f36 --- /dev/null +++ b/data/8-ball_classic.txt @@ -0,0 +1,10 @@ +Yes. +No. +Maybe. +Ask again later. +Cannot predict now. +Outlook not so good. +Signs point to yes. +Very doubtful. +Without a doubt. +Better not tell you now. \ No newline at end of file diff --git a/data/8-ball_quirky.txt b/data/8-ball_quirky.txt new file mode 100644 index 0000000..2e37e0d --- /dev/null +++ b/data/8-ball_quirky.txt @@ -0,0 +1,14 @@ +42 (That’s the answer, right?) +Try turning it off and on again. +Error 404: Answer not found. +Consult your nearest wizard. +Ask your cat. They know everything. +The stars say yes, but the moon disagrees. +You don’t want to know… trust me. +Sure, but don’t quote me on that. +In an alternate universe, yes. +Why are you asking a stupid bot? +Only if you bring snacks. +Flip a coin; I’m on break. +The spirits are buffering... please wait. +You already know the answer. \ No newline at end of file diff --git a/src/cmds.rs b/src/cmds.rs index 35ba743..9d4690a 100644 --- a/src/cmds.rs +++ b/src/cmds.rs @@ -1,6 +1,7 @@ use crate::{send_embed, send_msg, Context, EmbedOptions, Error}; use poise::serenity_prelude::{OnlineStatus, Timestamp}; +use rand::{seq::IteratorRandom, Rng}; #[poise::command(slash_command, prefix_command)] @@ -65,5 +66,25 @@ pub async fn embed( } ).await?; + return Ok(()); +} + + +#[poise::command(slash_command, prefix_command)] +pub async fn eight_ball( + ctx: Context<'_>, + #[description = "Question to ask."] question: String +) -> Result<(), Error> +{ + let is_quirky = rand::rng().random_bool(0.2); + let list = &ctx.data().ball_prompts[if is_quirky { 1 } else { 0 }]; + let rand_item = list.iter().choose(&mut rand::rng()); + + send_msg( + ctx, + format!("Q: {}\nA: {}", question, rand_item.unwrap()), + true + ).await?; + return Ok(()); } \ No newline at end of file diff --git a/src/events.rs b/src/events.rs index 5b443ec..8d5619c 100644 --- a/src/events.rs +++ b/src/events.rs @@ -9,7 +9,7 @@ pub fn event_handler<'a>( ctx: &'a serenity::Context, event: &'a serenity::FullEvent, _framework: poise::FrameworkContext<'a, Data, Error>, - _data: &'a Data, + data: &'a Data, ) -> Pin> + Send + 'a>> { Box::pin(async move { if let serenity::FullEvent::Ready { data_about_bot } = event { @@ -19,6 +19,10 @@ pub fn event_handler<'a>( data_about_bot.user.id ); + if data.dev { + println!("----- DEV MODE ENABLED -----"); + } + let file_text = std::fs::read_to_string("./data/status.txt").unwrap(); let custom_activity = ActivityData::custom(file_text); // TODO: make custom rich presence diff --git a/src/main.rs b/src/main.rs index b6fd4ed..2611a79 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,8 @@ use std::env; use poise::serenity_prelude::{self as serenity, Color, CreateEmbed, Timestamp}; struct Data { - dev: bool + dev: bool, + ball_prompts: [Vec; 2] } type Error = Box; type Context<'a> = poise::Context<'a, Data, Error>; @@ -38,8 +39,16 @@ impl Default for EmbedOptions { #[tokio::main] async fn main() { let args: Vec = env::args().collect(); + + let ball_classic_str = std::fs::read_to_string("./data/8-ball_classic.txt").unwrap(); + let ball_quirk_str = std::fs::read_to_string("./data/8-ball_quirky.txt").unwrap(); + + let ball_classic: Vec = ball_classic_str.lines().map(String::from).collect(); + let ball_quirk: Vec = ball_quirk_str .lines().map(String::from).collect(); + let data = Data { - dev: args.contains(&"--dev".to_string()) + dev: args.contains(&"--dev".to_string()), + ball_prompts: [ball_classic, ball_quirk] }; let token = std::env::var("ASSISTANT_TOKEN").expect("missing ASSISTANT_TOKEN env var"); @@ -56,7 +65,8 @@ async fn main() { commands: vec![ cmds::ping(), cmds::embed(), - cmds::stop() + cmds::stop(), + cmds::eight_ball() ], event_handler: events::event_handler, ..Default::default()