diff --git a/src/main.rs b/src/main.rs index cfb2b8b..406a951 100755 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,8 @@ use serenity::{ model::{channel::Message, gateway::Ready, id::UserId, user::OnlineStatus}, prelude::*, }; +use std::collections::hash_map::DefaultHasher; +use std::hash::{Hash, Hasher}; use std::{env, process}; struct Handler; @@ -33,11 +35,18 @@ impl EventHandler for Handler { #[group] #[commands( - init, ping, halt, list_srv, host, ship, headpat, uwu, gayculator, waffle, sausage, help, embed, - what, owo, info + init, ping, halt, list_srv, host, ship, bottom_rng, headpat, uwu, gayculator, waffle, sausage, + help, embed, what, owo, info )] struct General; +// Calculates hash of a type that implements Hash +fn calculate_hash(t: &T) -> u64 { + let mut s = DefaultHasher::new(); + t.hash(&mut s); + s.finish() +} + fn main() { let mut client = Client::new(&env::var("DISCORD_TOKEN").expect("Invalid token"), Handler) .expect("Error creating client"); @@ -255,18 +264,38 @@ fn embed(ctx: &mut Context, message: &Message, args: Args) -> CommandResult { Ok(()) } +#[command] +fn bottom_rng(ctx: &mut Context, message: &Message) -> CommandResult { + use rand::{rngs::StdRng, SeedableRng}; + + let messages = message + .channel_id + .messages(&ctx.http, |get| get.before(message.id).limit(20)); + if let Err(e) = messages { + let _ = message.channel_id.say(&ctx.http, format!("Error: {}", e)); + } else { + let mut messages = messages.unwrap(); + messages.retain(|v| v.author != message.mentions[0]); + let mut input = String::new(); + for msg in messages { + input.push_str(&format!("{} ", msg.content)); + } + let result: u64 = StdRng::seed_from_u64(calculate_hash(&input)).gen_range(0, 100); + let _ = message.channel_id.send_message(&ctx.http, |m| { + m.embed(|e| { + e.title("Bottom RNG") + .description(format!("Result: {}", result)) + .color(0x800869) + }) + }); + } + + Ok(()) +} + #[command] fn ship(ctx: &mut Context, message: &Message, args: Args) -> CommandResult { use rand::{rngs::StdRng, SeedableRng}; - use std::collections::hash_map::DefaultHasher; - use std::hash::{Hash, Hasher}; - - // Calculates hash of a type that implements Hash - fn calculate_hash(t: &T) -> u64 { - let mut s = DefaultHasher::new(); - t.hash(&mut s); - s.finish() - } // Get input names let names: String = args.rest().trim().to_string();