late-submissions
Elnu 3 years ago
parent e5ed606fa9
commit 11b0995900

@ -5,8 +5,8 @@ use serenity::prelude::*;
use std::env; use std::env;
use std::fs; use std::fs;
use serde_json::Value;
use serde_json::Map; use serde_json::Map;
use serde_json::Value;
use std::fs::File; use std::fs::File;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::Read; use std::io::Read;
@ -55,7 +55,8 @@ async fn submit(ctx: &Context, msg: &Message) -> CommandResult {
// TODO: The code for this command needs to be refactored, // TODO: The code for this command needs to be refactored,
// there are large duplicated sections that need to be merged somehow. // there are large duplicated sections that need to be merged somehow.
if msg.attachments.len() == 0 { if msg.attachments.len() == 0 {
msg.reply(&ctx.http, "Please attach at least one image.").await?; msg.reply(&ctx.http, "Please attach at least one image.")
.await?;
return Ok(()); return Ok(());
} }
let hugo_path = env::var("HUGO").unwrap(); let hugo_path = env::var("HUGO").unwrap();
@ -73,8 +74,8 @@ async fn submit(ctx: &Context, msg: &Message) -> CommandResult {
file.write_all(b"[]")?; file.write_all(b"[]")?;
file.flush()?; file.flush()?;
String::from("[]") String::from("[]")
}, }
Err(_) => panic!("Failed to open submission data file") Err(_) => panic!("Failed to open submission data file"),
}; };
let mut submission_data: Value = serde_json::from_str(&submission_data_json).unwrap(); let mut submission_data: Value = serde_json::from_str(&submission_data_json).unwrap();
let mut submission_data = submission_data.as_array_mut().unwrap().clone(); let mut submission_data = submission_data.as_array_mut().unwrap().clone();
@ -101,7 +102,8 @@ async fn submit(ctx: &Context, msg: &Message) -> CommandResult {
continue; continue;
} }
requires_rebuild = true; requires_rebuild = true;
let file_name = format!("{}-{}-{}-{}.{}", let file_name = format!(
"{}-{}-{}-{}.{}",
i + 1, i + 1,
slugify!(&msg.author.name), slugify!(&msg.author.name),
msg.author.discriminator, msg.author.discriminator,
@ -109,11 +111,9 @@ async fn submit(ctx: &Context, msg: &Message) -> CommandResult {
extension extension
); );
images.push(file_name.clone().into()); images.push(file_name.clone().into());
let image = reqwest::get(&attachment.url) let image = reqwest::get(&attachment.url).await?.bytes().await?;
.await? let mut image_file =
.bytes() File::create(format!("{}/{}", submission_images_dir, file_name))?;
.await?;
let mut image_file = File::create(format!("{}/{}", submission_images_dir, file_name))?;
image_file.write_all(&image)?; image_file.write_all(&image)?;
} }
submission["images"] = images.into(); submission["images"] = images.into();
@ -122,7 +122,10 @@ async fn submit(ctx: &Context, msg: &Message) -> CommandResult {
} }
if !existing_submitter { if !existing_submitter {
let mut submitter_data = Map::new(); let mut submitter_data = Map::new();
submitter_data.insert(String::from("username"), format!("{}#{}", msg.author.name, msg.author.discriminator).into()); submitter_data.insert(
String::from("username"),
format!("{}#{}", msg.author.name, msg.author.discriminator).into(),
);
let mut images: Vec<String> = Vec::new(); let mut images: Vec<String> = Vec::new();
for attachment in msg.attachments.iter() { for attachment in msg.attachments.iter() {
let extension; let extension;
@ -140,28 +143,38 @@ async fn submit(ctx: &Context, msg: &Message) -> CommandResult {
continue; continue;
} }
requires_rebuild = true; requires_rebuild = true;
let file_name = format!("{}-{}-{}{}.{}", let file_name = format!(
"{}-{}-{}{}.{}",
submission_data.len() + 1, submission_data.len() + 1,
slugify!(&msg.author.name), slugify!(&msg.author.name),
msg.author.discriminator, msg.author.discriminator,
if images.len() == 0 { String::from("") } else { format!("-{}", images.len() + 1) }, if images.len() == 0 {
String::from("")
} else {
format!("-{}", images.len() + 1)
},
extension extension
); );
images.push(file_name.clone().into()); images.push(file_name.clone().into());
let image = reqwest::get(&attachment.url) let image = reqwest::get(&attachment.url).await?.bytes().await?;
.await?
.bytes()
.await?;
let mut image_file = File::create(format!("{}/{}", submission_images_dir, file_name))?; let mut image_file = File::create(format!("{}/{}", submission_images_dir, file_name))?;
image_file.write_all(&image)?; image_file.write_all(&image)?;
} }
submitter_data.insert(String::from("images"), images.into()); submitter_data.insert(String::from("images"), images.into());
submitter_data.insert(String::from("id"), msg.author.id.as_u64().to_string().into()); submitter_data.insert(
String::from("id"),
msg.author.id.as_u64().to_string().into(),
);
submission_data.push(submitter_data.into()); submission_data.push(submitter_data.into());
} }
let submission_data: Value = submission_data.into(); let submission_data: Value = submission_data.into();
let mut submission_data_file = OpenOptions::new().write(true).truncate(true).open(&submission_data_path)?; let mut submission_data_file = OpenOptions::new()
submission_data_file.write_all(serde_json::to_string_pretty(&submission_data)?.as_bytes()).unwrap(); .write(true)
.truncate(true)
.open(&submission_data_path)?;
submission_data_file
.write_all(serde_json::to_string_pretty(&submission_data)?.as_bytes())
.unwrap();
let mut message = String::new(); let mut message = String::new();
if requires_rebuild { if requires_rebuild {
message.push_str(&format!("Thank you for submitting! You can view your submission at <https://tegakituesday.com/{}>", challenge_number)); message.push_str(&format!("Thank you for submitting! You can view your submission at <https://tegakituesday.com/{}>", challenge_number));

Loading…
Cancel
Save