use serenity::{
    framework::standard::{macros::command, Args, CommandResult},
    model::channel::Message,
};

// This imports `typemap`'s `Key` as `TypeMapKey`.
use serenity::prelude::*;

use std::process::{Command, Stdio};
use std::str::from_utf8;

#[command]
fn ip(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult {
    let args = ["ifconfig.me"];

    let out = match Command::new("curl")
        .args(&args)
        .stdin(Stdio::null())
        .output()
    {
        Ok(out) => out,
        Err(_) => return Ok(()),
    };

    if !out.status.success() {
        return Ok(());
    }

    match from_utf8(out.stdout.as_slice()) {
        Ok(string) => msg.author.direct_message(&ctx, |m| m.content(string))?,
        Err(_) => msg.channel_id.say(&ctx.http, "error getting IP string")?,
    };

    Ok(())
}
