Adds .randomizer command

main
Joey Pollack 2 years ago
parent b976591e7a
commit 7936b24d28

@ -1,5 +1,6 @@
{ {
"rust-analyzer.linkedProjects": [ "rust-analyzer.linkedProjects": [
".\\Cargo.toml",
".\\Cargo.toml" ".\\Cargo.toml"
] ]
} }

@ -6,5 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
rand = "0.8.5"
serenity = { version = "0.11.6", default-features = false, features = ["client", "gateway", "rustls_backend", "model"] } serenity = { version = "0.11.6", default-features = false, features = ["client", "gateway", "rustls_backend", "model"] }
tokio = { version = "1.21.1", features = ["full"] } tokio = { version = "1.21.1", features = ["full"] }

@ -2,6 +2,7 @@
mod commands; mod commands;
mod data_loader; mod data_loader;
mod utils;
use serenity::async_trait; use serenity::async_trait;
use serenity::model::channel::Message; use serenity::model::channel::Message;
@ -11,19 +12,32 @@ use serenity::prelude::*;
struct Handler; struct Handler;
#[async_trait] #[async_trait]
impl EventHandler for Handler { impl EventHandler for Handler
{
// Set a handler for the `message` event - so that whenever a new message // Set a handler for the `message` event - so that whenever a new message
// is received - the closure (or function) passed will be called. // is received - the closure (or function) passed will be called.
// //
// Event handlers are dispatched through a threadpool, and so multiple // Event handlers are dispatched through a threadpool, and so multiple
// events can be dispatched simultaneously. // events can be dispatched simultaneously.
async fn message(&self, ctx: Context, msg: Message) { async fn message(&self, ctx: Context, msg: Message)
if msg.content == "!ping" { {
if msg.content == ".ping"
{
// Sending a message can fail, due to a network error, an // Sending a message can fail, due to a network error, an
// authentication error, or lack of permissions to post in the // authentication error, or lack of permissions to post in the
// channel, so log to stdout when some error happens, with a // channel, so log to stdout when some error happens, with a
// description of it. // 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); println!("Error sending message: {:?}", why);
} }
} }

@ -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();
}
}
}
Loading…
Cancel
Save