~phate/rsPhate

ref: 78c74426c01fbbc73264d1178ee2dfecd911fce2 rsPhate/src/commands/noice.rs -rw-r--r-- 1.3 KiB
78c74426ValleyKnight symlink 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
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;

#[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());
    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);
    }

    Ok(())
}