|
|
@ -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;
|
|
|
@ -19,22 +19,22 @@ use slugify::slugify;
|
|
|
|
fn get_challenge_number() -> i32 {
|
|
|
|
fn get_challenge_number() -> i32 {
|
|
|
|
let challenge_dir = format!("{}/content/challenges", env::var("HUGO").unwrap());
|
|
|
|
let challenge_dir = format!("{}/content/challenges", env::var("HUGO").unwrap());
|
|
|
|
let paths = fs::read_dir(challenge_dir).unwrap();
|
|
|
|
let paths = fs::read_dir(challenge_dir).unwrap();
|
|
|
|
let mut max = 0;
|
|
|
|
let mut max = 0;
|
|
|
|
for path in paths {
|
|
|
|
for path in paths {
|
|
|
|
let number = path
|
|
|
|
let number = path
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
.path()
|
|
|
|
.path()
|
|
|
|
.file_stem()
|
|
|
|
.file_stem()
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
.parse::<i32>()
|
|
|
|
.parse::<i32>()
|
|
|
|
.unwrap();
|
|
|
|
.unwrap();
|
|
|
|
if number > max {
|
|
|
|
if number > max {
|
|
|
|
max = number;
|
|
|
|
max = number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
max
|
|
|
|
max
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[command]
|
|
|
|
#[command]
|
|
|
@ -52,127 +52,140 @@ async fn challenge(ctx: &Context, msg: &Message) -> CommandResult {
|
|
|
|
|
|
|
|
|
|
|
|
#[command]
|
|
|
|
#[command]
|
|
|
|
async fn submit(ctx: &Context, msg: &Message) -> CommandResult {
|
|
|
|
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.")
|
|
|
|
return Ok(());
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
let hugo_path = env::var("HUGO").unwrap();
|
|
|
|
}
|
|
|
|
let challenge_number = get_challenge_number();
|
|
|
|
let hugo_path = env::var("HUGO").unwrap();
|
|
|
|
let submission_images_dir = format!("{}/assets/{}", hugo_path, challenge_number);
|
|
|
|
let challenge_number = get_challenge_number();
|
|
|
|
let submission_data_path = format!("{}/data/challenges/{}.json", hugo_path, challenge_number);
|
|
|
|
let submission_images_dir = format!("{}/assets/{}", hugo_path, challenge_number);
|
|
|
|
let submission_data_json = match File::open(&submission_data_path) {
|
|
|
|
let submission_data_path = format!("{}/data/challenges/{}.json", hugo_path, challenge_number);
|
|
|
|
Ok(mut file) => {
|
|
|
|
let submission_data_json = match File::open(&submission_data_path) {
|
|
|
|
let mut json = String::new();
|
|
|
|
Ok(mut file) => {
|
|
|
|
file.read_to_string(&mut json)?;
|
|
|
|
let mut json = String::new();
|
|
|
|
json
|
|
|
|
file.read_to_string(&mut json)?;
|
|
|
|
}
|
|
|
|
json
|
|
|
|
Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => {
|
|
|
|
}
|
|
|
|
let mut file = File::create(&submission_data_path)?;
|
|
|
|
Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => {
|
|
|
|
file.write_all(b"[]")?;
|
|
|
|
let mut file = File::create(&submission_data_path)?;
|
|
|
|
file.flush()?;
|
|
|
|
file.write_all(b"[]")?;
|
|
|
|
String::from("[]")
|
|
|
|
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: 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();
|
|
|
|
let mut existing_submitter = false;
|
|
|
|
let mut existing_submitter = false;
|
|
|
|
let mut invalid_types = false;
|
|
|
|
let mut invalid_types = false;
|
|
|
|
let mut requires_rebuild = false;
|
|
|
|
let mut requires_rebuild = false;
|
|
|
|
for (i, submission) in submission_data.iter_mut().enumerate() {
|
|
|
|
for (i, submission) in submission_data.iter_mut().enumerate() {
|
|
|
|
if &submission["id"].as_str().unwrap() == &msg.author.id.as_u64().to_string() {
|
|
|
|
if &submission["id"].as_str().unwrap() == &msg.author.id.as_u64().to_string() {
|
|
|
|
existing_submitter = true;
|
|
|
|
existing_submitter = true;
|
|
|
|
let mut images = submission["images"].as_array_mut().unwrap().clone();
|
|
|
|
let mut images = submission["images"].as_array_mut().unwrap().clone();
|
|
|
|
for attachment in msg.attachments.iter() {
|
|
|
|
for attachment in msg.attachments.iter() {
|
|
|
|
let extension;
|
|
|
|
let extension;
|
|
|
|
if let Some(content_type) = &attachment.content_type {
|
|
|
|
if let Some(content_type) = &attachment.content_type {
|
|
|
|
if content_type == "image/png" {
|
|
|
|
if content_type == "image/png" {
|
|
|
|
extension = "png";
|
|
|
|
extension = "png";
|
|
|
|
} else if content_type == "image/jpeg" {
|
|
|
|
} else if content_type == "image/jpeg" {
|
|
|
|
extension = "jpg";
|
|
|
|
extension = "jpg";
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
invalid_types = true;
|
|
|
|
invalid_types = true;
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
invalid_types = true;
|
|
|
|
invalid_types = true;
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
requires_rebuild = true;
|
|
|
|
requires_rebuild = true;
|
|
|
|
let file_name = format!("{}-{}-{}-{}.{}",
|
|
|
|
let file_name = format!(
|
|
|
|
i + 1,
|
|
|
|
"{}-{}-{}-{}.{}",
|
|
|
|
slugify!(&msg.author.name),
|
|
|
|
i + 1,
|
|
|
|
msg.author.discriminator,
|
|
|
|
slugify!(&msg.author.name),
|
|
|
|
images.len() + 1,
|
|
|
|
msg.author.discriminator,
|
|
|
|
extension
|
|
|
|
images.len() + 1,
|
|
|
|
);
|
|
|
|
extension
|
|
|
|
images.push(file_name.clone().into());
|
|
|
|
);
|
|
|
|
let image = reqwest::get(&attachment.url)
|
|
|
|
images.push(file_name.clone().into());
|
|
|
|
.await?
|
|
|
|
let image = reqwest::get(&attachment.url).await?.bytes().await?;
|
|
|
|
.bytes()
|
|
|
|
let mut image_file =
|
|
|
|
.await?;
|
|
|
|
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)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
submission["images"] = images.into();
|
|
|
|
submission["images"] = images.into();
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !existing_submitter {
|
|
|
|
if !existing_submitter {
|
|
|
|
let mut submitter_data = Map::new();
|
|
|
|
let mut submitter_data = Map::new();
|
|
|
|
submitter_data.insert(
|
|
|
|
submitter_data.insert(String::from("username"), format!("{}#{}", msg.author.name, msg.author.discriminator).into());
|
|
|
|
String::from("username"),
|
|
|
|
let mut images: Vec<String> = Vec::new();
|
|
|
|
format!("{}#{}", msg.author.name, msg.author.discriminator).into(),
|
|
|
|
for attachment in msg.attachments.iter() {
|
|
|
|
);
|
|
|
|
let extension;
|
|
|
|
let mut images: Vec<String> = Vec::new();
|
|
|
|
if let Some(content_type) = &attachment.content_type {
|
|
|
|
for attachment in msg.attachments.iter() {
|
|
|
|
if content_type == "image/png" {
|
|
|
|
let extension;
|
|
|
|
extension = "png";
|
|
|
|
if let Some(content_type) = &attachment.content_type {
|
|
|
|
} else if content_type == "image/jpeg" {
|
|
|
|
if content_type == "image/png" {
|
|
|
|
extension = "jpg";
|
|
|
|
extension = "png";
|
|
|
|
} else {
|
|
|
|
} else if content_type == "image/jpeg" {
|
|
|
|
invalid_types = true;
|
|
|
|
extension = "jpg";
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
invalid_types = true;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
invalid_types = true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
invalid_types = true;
|
|
|
|
requires_rebuild = true;
|
|
|
|
continue;
|
|
|
|
let file_name = format!("{}-{}-{}{}.{}",
|
|
|
|
}
|
|
|
|
submission_data.len() + 1,
|
|
|
|
requires_rebuild = true;
|
|
|
|
slugify!(&msg.author.name),
|
|
|
|
let file_name = format!(
|
|
|
|
msg.author.discriminator,
|
|
|
|
"{}-{}-{}{}.{}",
|
|
|
|
if images.len() == 0 { String::from("") } else { format!("-{}", images.len() + 1) },
|
|
|
|
submission_data.len() + 1,
|
|
|
|
extension
|
|
|
|
slugify!(&msg.author.name),
|
|
|
|
);
|
|
|
|
msg.author.discriminator,
|
|
|
|
images.push(file_name.clone().into());
|
|
|
|
if images.len() == 0 {
|
|
|
|
let image = reqwest::get(&attachment.url)
|
|
|
|
String::from("")
|
|
|
|
.await?
|
|
|
|
} else {
|
|
|
|
.bytes()
|
|
|
|
format!("-{}", images.len() + 1)
|
|
|
|
.await?;
|
|
|
|
},
|
|
|
|
let mut image_file = File::create(format!("{}/{}", submission_images_dir, file_name))?;
|
|
|
|
extension
|
|
|
|
image_file.write_all(&image)?;
|
|
|
|
);
|
|
|
|
}
|
|
|
|
images.push(file_name.clone().into());
|
|
|
|
submitter_data.insert(String::from("images"), images.into());
|
|
|
|
let image = reqwest::get(&attachment.url).await?.bytes().await?;
|
|
|
|
submitter_data.insert(String::from("id"), msg.author.id.as_u64().to_string().into());
|
|
|
|
let mut image_file = File::create(format!("{}/{}", submission_images_dir, file_name))?;
|
|
|
|
submission_data.push(submitter_data.into());
|
|
|
|
image_file.write_all(&image)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let submission_data: Value = submission_data.into();
|
|
|
|
submitter_data.insert(String::from("images"), images.into());
|
|
|
|
let mut submission_data_file = OpenOptions::new().write(true).truncate(true).open(&submission_data_path)?;
|
|
|
|
submitter_data.insert(
|
|
|
|
submission_data_file.write_all(serde_json::to_string_pretty(&submission_data)?.as_bytes()).unwrap();
|
|
|
|
String::from("id"),
|
|
|
|
let mut message = String::new();
|
|
|
|
msg.author.id.as_u64().to_string().into(),
|
|
|
|
if requires_rebuild {
|
|
|
|
);
|
|
|
|
message.push_str(&format!("Thank you for submitting! You can view your submission at <https://tegakituesday.com/{}>", challenge_number));
|
|
|
|
submission_data.push(submitter_data.into());
|
|
|
|
if invalid_types {
|
|
|
|
}
|
|
|
|
message.push_str("\nSome of your attachments could not be uploaded; only **.png**, **.jpg**, and **.jpeg** files are permitted.");
|
|
|
|
let submission_data: Value = submission_data.into();
|
|
|
|
}
|
|
|
|
let mut submission_data_file = OpenOptions::new()
|
|
|
|
// Currently untested, and thus commented
|
|
|
|
.write(true)
|
|
|
|
// Command::new("hugo").current_dir(&hugo_path).spawn().expect("Failed to rebuild site");
|
|
|
|
.truncate(true)
|
|
|
|
} else if invalid_types {
|
|
|
|
.open(&submission_data_path)?;
|
|
|
|
message.push_str("Sorry, your submission could not be uploaded; only **.png**, **.jpg**, and **.jpeg** files are permitted.");
|
|
|
|
submission_data_file
|
|
|
|
}
|
|
|
|
.write_all(serde_json::to_string_pretty(&submission_data)?.as_bytes())
|
|
|
|
msg.reply(&ctx.http, &message).await?;
|
|
|
|
.unwrap();
|
|
|
|
Ok(())
|
|
|
|
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));
|
|
|
|
|
|
|
|
if invalid_types {
|
|
|
|
|
|
|
|
message.push_str("\nSome of your attachments could not be uploaded; only **.png**, **.jpg**, and **.jpeg** files are permitted.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently untested, and thus commented
|
|
|
|
|
|
|
|
// Command::new("hugo").current_dir(&hugo_path).spawn().expect("Failed to rebuild site");
|
|
|
|
|
|
|
|
} else if invalid_types {
|
|
|
|
|
|
|
|
message.push_str("Sorry, your submission could not be uploaded; only **.png**, **.jpg**, and **.jpeg** files are permitted.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.reply(&ctx.http, &message).await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
}
|
|
|
|