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

@ -5,8 +5,8 @@ use serenity::prelude::*;
use std::env;
use std::fs;
use serde_json::Value;
use serde_json::Map;
use serde_json::Value;
use std::fs::File;
use std::fs::OpenOptions;
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,
// there are large duplicated sections that need to be merged somehow.
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(());
}
let hugo_path = env::var("HUGO").unwrap();
@ -73,8 +74,8 @@ async fn submit(ctx: &Context, msg: &Message) -> CommandResult {
file.write_all(b"[]")?;
file.flush()?;
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 = submission_data.as_array_mut().unwrap().clone();
@ -101,7 +102,8 @@ async fn submit(ctx: &Context, msg: &Message) -> CommandResult {
continue;
}
requires_rebuild = true;
let file_name = format!("{}-{}-{}-{}.{}",
let file_name = format!(
"{}-{}-{}-{}.{}",
i + 1,
slugify!(&msg.author.name),
msg.author.discriminator,
@ -109,11 +111,9 @@ async fn submit(ctx: &Context, msg: &Message) -> CommandResult {
extension
);
images.push(file_name.clone().into());
let image = reqwest::get(&attachment.url)
.await?
.bytes()
.await?;
let mut image_file = File::create(format!("{}/{}", submission_images_dir, file_name))?;
let image = reqwest::get(&attachment.url).await?.bytes().await?;
let mut image_file =
File::create(format!("{}/{}", submission_images_dir, file_name))?;
image_file.write_all(&image)?;
}
submission["images"] = images.into();
@ -122,7 +122,10 @@ async fn submit(ctx: &Context, msg: &Message) -> CommandResult {
}
if !existing_submitter {
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();
for attachment in msg.attachments.iter() {
let extension;
@ -140,28 +143,38 @@ async fn submit(ctx: &Context, msg: &Message) -> CommandResult {
continue;
}
requires_rebuild = true;
let file_name = format!("{}-{}-{}{}.{}",
let file_name = format!(
"{}-{}-{}{}.{}",
submission_data.len() + 1,
slugify!(&msg.author.name),
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
);
images.push(file_name.clone().into());
let image = reqwest::get(&attachment.url)
.await?
.bytes()
.await?;
let image = reqwest::get(&attachment.url).await?.bytes().await?;
let mut image_file = File::create(format!("{}/{}", submission_images_dir, file_name))?;
image_file.write_all(&image)?;
}
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());
}
let submission_data: Value = submission_data.into();
let mut submission_data_file = OpenOptions::new().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 submission_data_file = OpenOptions::new()
.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();
if requires_rebuild {
message.push_str(&format!("Thank you for submitting! You can view your submission at <https://tegakituesday.com/{}>", challenge_number));

Loading…
Cancel
Save