late-submissions
Elnu 2 years ago
parent 3c26122a0b
commit 8dde342d97

@ -34,20 +34,19 @@ pub async fn challenge(ctx: Context<'_>) -> Result<(), Error> {
broadcast_typing,
description_localized("en-US", "Submit to the latest handwriting challenge.")
)]
pub async fn submit(
ctx: Context<'_>,
submission: serenity::Attachment
) -> Result<(), Error> {
pub async fn submit(ctx: Context<'_>, submission: serenity::Attachment) -> Result<(), Error> {
// TODO: The code for this command needs to be refactored,
// there are large duplicated sections that need to be merged somehow.
match ctx {
Context::Application(_) => { ctx.defer_ephemeral().await? },
Context::Prefix(ctx) => if ctx.msg.attachments.len() == 0 {
ctx.msg
.reply(&ctx.discord.http, "Please attach at least one image.")
.await?;
return Ok(());
},
Context::Application(_) => ctx.defer_ephemeral().await?,
Context::Prefix(ctx) => {
if ctx.msg.attachments.len() == 0 {
ctx.msg
.reply(&ctx.discord.http, "Please attach at least one image.")
.await?;
return Ok(());
}
}
}
let guild_data = get_guild_data();
let guild = ctx.guild_id().unwrap().as_u64().to_string();
@ -57,23 +56,22 @@ pub async fn submit(
.unwrap()
.contains_key("submissionChannel")
{
ctx.say("Submissions aren't enabled for this server yet.").await?;
ctx.say("Submissions aren't enabled for this server yet.")
.await?;
return Ok(());
}
let current_guild_data = &guild_data[&guild].as_object().unwrap();
let submission_channel = current_guild_data["submissionChannel"].as_str().unwrap();
if submission_channel != ctx.channel_id().as_u64().to_string() {
ctx.defer_ephemeral().await?;
ctx.say(
format!(
"Sorry, submissions aren't permitted here. Please go to <#{}>. Thanks!",
guild
),
)
.await?;
ctx.say(format!(
"Sorry, submissions aren't permitted here. Please go to <#{}>. Thanks!",
guild
))
.await?;
return Ok(());
}
let challenge_number = get_challenge_number();
let submission_images_dir = get_submission_images_dir();
@ -172,10 +170,7 @@ pub async fn submit(
image_file.flush()?;
}
submitter_data.insert(String::from("images"), images.into());
submitter_data.insert(
String::from("id"),
author.id.as_u64().to_string().into(),
);
submitter_data.insert(String::from("id"), author.id.as_u64().to_string().into());
submission_data.push(submitter_data.into());
}
set_submission_data(submission_data);
@ -186,7 +181,7 @@ pub async fn submit(
match ctx {
Context::Application(_) => message.push_str(thank_you),
Context::Prefix(ctx) => match ctx.msg.delete(&ctx.discord.http).await {
Ok(_) => {},
Ok(_) => {}
// don't repost image on this server if no manage messages perm
// (if we can't delete user's image it'll be displayed twice)
Err(_) => {
@ -204,7 +199,9 @@ pub async fn submit(
let data = guild_data[&guild.id.to_string()].as_object().unwrap();
let invite = if data.contains_key("invite") {
Some(data["invite"].as_str().unwrap())
} else { None };
} else {
None
};
for attachment in attachments.iter() {
for (other_guild_id, data) in guild_data.iter() {
let here = other_guild_id.eq(&ctx.guild_id().unwrap().as_u64().to_string());
@ -247,7 +244,7 @@ pub async fn submit(
.icon_url(get_avatar(&author))
.name(username)
.url(format!("https://discord.com/users/{}", author.id));
e.set_author(embed_author);
e.set_author(embed_author);
e.image(&attachment.url);
if let Some(accent_color) = accent_color {
e.color(accent_color);
@ -256,8 +253,8 @@ pub async fn submit(
});
m
}).await?;
};
};
}
}
} else if invalid_types {
message.push_str("Sorry, your submission could not be uploaded; only **.png**, **.jpg**, and **.jpeg** files are permitted.");
}

@ -1,6 +1,6 @@
use crate::serenity;
use crate::Error;
use crate::Context;
use crate::Error;
use rand::seq::IteratorRandom;
use serde_json::Map;
use serde_json::Value;

Loading…
Cancel
Save