diff --git a/.vscode/settings.json b/.vscode/settings.json index ec17418..3bc96bd 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "rust-analyzer.linkedProjects": [ + ".\\Cargo.toml", ".\\Cargo.toml" ] } \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 3292731..50c9ab3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +rand = "0.8.5" serenity = { version = "0.11.6", default-features = false, features = ["client", "gateway", "rustls_backend", "model"] } tokio = { version = "1.21.1", features = ["full"] } diff --git a/src/main.rs b/src/main.rs index 38ce376..d8de39a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod commands; mod data_loader; +mod utils; use serenity::async_trait; use serenity::model::channel::Message; @@ -11,19 +12,32 @@ use serenity::prelude::*; struct Handler; #[async_trait] -impl EventHandler for Handler { +impl EventHandler for Handler +{ // Set a handler for the `message` event - so that whenever a new message // is received - the closure (or function) passed will be called. // // Event handlers are dispatched through a threadpool, and so multiple // events can be dispatched simultaneously. - async fn message(&self, ctx: Context, msg: Message) { - if msg.content == "!ping" { + async fn message(&self, ctx: Context, msg: Message) + { + if msg.content == ".ping" + { // Sending a message can fail, due to a network error, an // authentication error, or lack of permissions to post in the // channel, so log to stdout when some error happens, with a // description of it. - if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await { + if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await + { + println!("Error sending message: {:?}", why); + } + } + + if msg.content == ".randomizer" + { + let item = utils::get_random_item(); + if let Err(why) = msg.channel_id.say(&ctx.http, format!("You found {}!", item)).await + { println!("Error sending message: {:?}", why); } } diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..064f8a8 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,38 @@ +use rand::prelude::*; + +pub fn get_random_item() -> String +{ + let major_items = vec!["Missiles", "Super Missiles", "E Tank", "Power Bombs" ]; + let minor_items = vec!["Morphing Ball", "Bombs", "Reserve Tank", "Charge Beam", "Spazer", "Ice Beam", "Wave Beam", "Plasma Beam", "Hi-Jump Boots", "Speed Booster", + "Grappling Beam", "Stupi... I mean Spring Ball", "Space Jump", "Screw Attack", + "Varia Suit", "Gravity Suit", "X-Ray Scope"]; + + let major_item_threshold = 75; + let not_missile_threshold = 40; + let missile_idx = 0; + + let mut rng = rand::thread_rng(); + let item_category: i32 = rng.gen_range(0..100); + + if item_category >= major_item_threshold + { + let item: usize = rng.gen_range(0..minor_items.len() - 1); + return minor_items[item].to_string(); + } + else + { + let missile = rng.gen_range(0..100); + if missile >= not_missile_threshold + { + let item: usize = rng.gen_range(0..major_items.len() - 1); + return major_items[item].to_string(); + } + else + { + return major_items[missile_idx].to_string(); + } + } + +} + + \ No newline at end of file