~phate/rsPhate

2589f3e37623d3e5a650c513afd2b221c2c6bff8 — ValleyKnight 9 months ago 78c7442
add workaround for incorrectly named jpe files
1 files changed, 21 insertions(+), 11 deletions(-)

M src/commands/noice.rs
M src/commands/noice.rs => src/commands/noice.rs +21 -11
@@ 8,6 8,17 @@ use serenity::{
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 {


@@ 22,17 33,16 @@ fn noice(ctx: &mut Context, msg: &Message) -> CommandResult {
        .output()
        .expect("Could not get base name.");
    let new_file = format!("images/just_yes/SPOILER_{}", String::from_utf8_lossy(&name.stdout).trim());
    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());
    let path2 = new_file2;
    
    let msg = msg.channel_id.send_message(&ctx.http, |m| {
        m.add_file(AttachmentType::Path(Path::new(&path2)));
        m
    });

    if let Err(why) = msg {
        error!("Error sending message: {:?}", why);
    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(())