~phate/rsPhate

ref: 15191e34eee9b6d0b5cd4b50ba7153e3482b330e rsPhate/src/commands/rng.rs -rw-r--r-- 2.3 KiB
15191e34ValleyKnight add neko command, remove rofl dependency 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use log::error;
use rand::Rng;
use serenity::{
    framework::standard::{macros::command, Args, CommandResult},
    model::channel::Message,
    prelude::*,
};

#[command]
#[description = "Bot will generate a random number between min and max and reply with the result."]
#[usage = "min max"]
#[example = "-999 999"]
fn rng(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
    fn min_and_max(mut a: Args) -> (i64, i64) {
        let min = a.single::<i64>().unwrap_or(9223372036854775807);
        let max = a.single::<i64>().unwrap_or(9223372036854775807);
        (min, max)
    }

    let (min, max) = min_and_max(args);
    let message: String = "Could not generate a random number, please try again.".to_string();
    if min == 9223372036854775807 {
        let message_specific: String = "The min value supplied is invalid.".to_string();
        error!("{}", message_specific);
        msg.channel_id.say(&ctx.http, message_specific)?;
        msg.channel_id.say(&ctx.http, message)?;
        return Ok(());
    } else if max == 9223372036854775807 {
        let message_specific: String = "The max value supplied is invalid.".to_string();
        error!("{}", message_specific);
        msg.channel_id.say(&ctx.http, message_specific)?;
        msg.channel_id.say(&ctx.http, message)?;
        return Ok(());
    } else if min == max {
        let message_specific: String =
            "The min value must not be equal to, or greater than, the max value.".to_string();
        error!("{}", message_specific);
        msg.channel_id.say(&ctx.http, message_specific)?;
        msg.channel_id.say(&ctx.http, message)?;
        return Ok(());
    } else if min > max {
        let message_specific: String =
            "The min value must be lower than the max value.".to_string();
        error!("{}", message_specific);
        msg.channel_id.say(&ctx.http, message_specific)?;
        msg.channel_id.say(&ctx.http, message)?;
        return Ok(());
    }

    let random_number = rand::thread_rng().gen_range(min..max);

    if let Err(why) = msg.channel_id.say(&ctx.http, &random_number.to_string()) {
        error!("Could not send randomly generated number because: {}", why);
        msg.channel_id.say(
            &ctx.http,
            "Could not generate a random number, please try again.",
        )?;
    }

    Ok(())
}