~phate/rsPhate

ref: 2589f3e37623d3e5a650c513afd2b221c2c6bff8 rsPhate/src/commands/noice.rs -rw-r--r-- 1.8 KiB
2589f3e3ValleyKnight add workaround for incorrectly named jpe files 2 years 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
use log::error;
use serenity::{
    framework::standard::{macros::command, CommandResult},
    http::AttachmentType,
    model::channel::Message,
    prelude::*,
};
use std::{fs, path::Path};
use std::process::Command;

fn send(ctx: &Context, msg: &Message, path: &String) {
    let msg = msg.channel_id.send_message(&ctx.http, |m| {
        m.add_file(AttachmentType::Path(Path::new(&path)));
        m
    });

    if let Err(why) = msg {
        error!("Error sending message: {:?}", why);
    }
}

#[command]
#[description = "Bot will post a random picture from Phate's \"just yes\" folder. Images will be spoiler'd, have fun!"]
fn noice(ctx: &mut Context, msg: &Message) -> CommandResult {
    let file = Command::new("scripts/noice")
        .arg("file")
        .output()
        .expect("Could not obtain random image");
    let path = String::from_utf8_lossy(&file.stdout).trim().to_string();
    let name = Command::new("scripts/noice")
        .arg("name")
        .arg(path)
        .output()
        .expect("Could not get base name.");
    let new_file = format!("images/just_yes/SPOILER_{}", String::from_utf8_lossy(&name.stdout).trim());
    let new_file3 = format!("images/just_yes/SPOILER_{}", String::from_utf8_lossy(&name.stdout).trim());
    if new_file.contains(".jpe") {
        let new_file2 = [new_file, ".jpg".to_string()].concat();
        fs::copy(String::from_utf8_lossy(&file.stdout).trim(), new_file2).expect("could not copy file");
        let path2 = new_file3 + &".jpg".to_string();
        send(ctx, &msg, &path2);
    } else {
        fs::copy(String::from_utf8_lossy(&file.stdout).trim(), new_file).expect("could not copy file");
        let new_file2 = format!("images/just_yes/SPOILER_{}", String::from_utf8_lossy(&name.stdout).trim());
        send(ctx, &msg, &new_file2);
    }

    Ok(())
}