Add images command

late-submissions
Elnu 3 years ago
parent c2cc3bdc2a
commit 1849a5c16d

@ -13,6 +13,7 @@ tracing-subscriber = "0.3.8"
serde_json = "1.0.79"
reqwest = "0.11.9"
slug = "0.1.4"
unicode_hfwidth = "0.2.0"
[dependencies.tokio]
version = "1.0"

@ -79,6 +79,21 @@ fn set_submission_data(submission_data: Vec<Value>) {
.unwrap();
}
fn is_matching_submission(submission: &Value, msg: &Message) -> bool {
submission["id"].as_str().unwrap() == msg.author.id.as_u64().to_string()
}
fn to_fullwidth(string: &str) -> String {
let mut fullwidth = String::new();
for character in string.chars() {
fullwidth.push(match unicode_hfwidth::to_fullwidth(character) {
Some(character) => character,
None => character,
});
}
fullwidth
}
#[command]
async fn challenge(ctx: &Context, msg: &Message) -> CommandResult {
msg.reply(
@ -114,7 +129,7 @@ async fn submit(ctx: &Context, msg: &Message) -> CommandResult {
let mut invalid_types = false;
let mut requires_rebuild = false;
for (i, submission) in submission_data.iter_mut().enumerate() {
if &submission["id"].as_str().unwrap() == &msg.author.id.as_u64().to_string() {
if is_matching_submission(&submission, &msg) {
existing_submitter = true;
let mut images = submission["images"].as_array_mut().unwrap().clone();
for attachment in msg.attachments.iter() {
@ -214,3 +229,31 @@ async fn submit(ctx: &Context, msg: &Message) -> CommandResult {
msg.reply(&ctx.http, &message).await?;
Ok(())
}
#[command]
async fn images(ctx: &Context, msg: &Message) -> CommandResult {
let submission_data = get_submission_data();
let images: Vec<String> = {
let mut images = Vec::new();
for submission in submission_data.iter() {
if is_matching_submission(&submission, &msg) {
for image in submission["images"].as_array().unwrap().iter() {
images.push(String::from(image.as_str().unwrap()));
}
break;
}
}
images
};
let challenge_number = get_challenge_number();
if images.len() == 0 {
msg.reply(&ctx.http, format!("You haven't submitted anything for Tegaki Tuesday #{}.", challenge_number)).await?;
return Ok(())
}
let mut message = String::from(format!("Your submission images for Tegaki Tuesday #{}:\n", challenge_number));
for (i, image) in images.iter().enumerate() {
message.push_str(&format!("{}<https://tegakituesday.com/{}/{}>\n", to_fullwidth(&(i + 1).to_string()), challenge_number, image));
}
msg.reply(&ctx.http, message).await?;
Ok(())
}

@ -43,7 +43,7 @@ impl EventHandler for Handler {
}
#[group]
#[commands(joyo, jinmeiyo, kyoiku, jlpt, hyogai, so, challenge, submit, sleep)]
#[commands(joyo, jinmeiyo, kyoiku, jlpt, hyogai, so, challenge, submit, images, sleep)]
struct General;
#[tokio::main]

Loading…
Cancel
Save