|
|
|
@ -2,8 +2,8 @@ use crate::utils::*;
|
|
|
|
|
use serde_json::Map;
|
|
|
|
|
|
|
|
|
|
use crate::serenity;
|
|
|
|
|
use crate::Error;
|
|
|
|
|
use crate::Context;
|
|
|
|
|
use crate::Error;
|
|
|
|
|
use poise::command;
|
|
|
|
|
|
|
|
|
|
use slug::slugify;
|
|
|
|
@ -20,7 +20,8 @@ use std::path::Path;
|
|
|
|
|
)]
|
|
|
|
|
pub async fn challenge(ctx: Context<'_>) -> Result<(), Error> {
|
|
|
|
|
ctx.say(format!(
|
|
|
|
|
"Tegaki Tuesday #{n}: <https://tegakituesday.com/{n}>",
|
|
|
|
|
"Tegaki Tuesday #{n}: <https://{domain}/{n}>",
|
|
|
|
|
domain = get_domain(),
|
|
|
|
|
n = get_challenge_number()
|
|
|
|
|
))
|
|
|
|
|
.await?;
|
|
|
|
@ -40,9 +41,12 @@ pub async fn submit(ctx: Context<'_>, submission: serenity::Attachment) -> Resul
|
|
|
|
|
match ctx {
|
|
|
|
|
Context::Application(_) => ctx.defer_ephemeral().await?,
|
|
|
|
|
Context::Prefix(ctx) => {
|
|
|
|
|
if ctx.msg.attachments.len() == 0 {
|
|
|
|
|
if ctx.msg.attachments.is_empty() {
|
|
|
|
|
ctx.msg
|
|
|
|
|
.reply(&ctx.discord.http, "Please attach at least one image.")
|
|
|
|
|
.reply(
|
|
|
|
|
&ctx.serenity_context.http,
|
|
|
|
|
"Please attach at least one image.",
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
@ -74,7 +78,10 @@ pub async fn submit(ctx: Context<'_>, submission: serenity::Attachment) -> Resul
|
|
|
|
|
|
|
|
|
|
let challenge_number = get_challenge_number();
|
|
|
|
|
let submission_images_dir = get_submission_images_dir();
|
|
|
|
|
let timestamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_millis();
|
|
|
|
|
let timestamp = std::time::SystemTime::now()
|
|
|
|
|
.duration_since(std::time::UNIX_EPOCH)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.as_millis();
|
|
|
|
|
|
|
|
|
|
// Ensure that submission_images_dir exists
|
|
|
|
|
let path = Path::new(&submission_images_dir);
|
|
|
|
@ -92,9 +99,15 @@ pub async fn submit(ctx: Context<'_>, submission: serenity::Attachment) -> Resul
|
|
|
|
|
let author = ctx.author();
|
|
|
|
|
let mut submitted_images = Vec::new();
|
|
|
|
|
for submission in submission_data.iter_mut() {
|
|
|
|
|
if is_matching_submission(&submission, author) {
|
|
|
|
|
if is_matching_submission(submission, author) {
|
|
|
|
|
existing_submitter = true;
|
|
|
|
|
let mut images: Vec<String> = submission["images"].as_array_mut().unwrap().clone().iter().map(|value| value.as_str().unwrap().to_owned()).collect();
|
|
|
|
|
let mut images: Vec<String> = submission["images"]
|
|
|
|
|
.as_array_mut()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.clone()
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|value| value.as_str().unwrap().to_owned())
|
|
|
|
|
.collect();
|
|
|
|
|
for (i, attachment) in attachments.iter().enumerate() {
|
|
|
|
|
let extension;
|
|
|
|
|
if let Some(content_type) = &attachment.content_type {
|
|
|
|
@ -117,10 +130,14 @@ pub async fn submit(ctx: Context<'_>, submission: serenity::Attachment) -> Resul
|
|
|
|
|
slugify(&author.name),
|
|
|
|
|
author.discriminator,
|
|
|
|
|
timestamp,
|
|
|
|
|
if i == 0 { "".to_owned() } else { format!("-{}", i + 1) },
|
|
|
|
|
if i == 0 {
|
|
|
|
|
"".to_owned()
|
|
|
|
|
} else {
|
|
|
|
|
format!("-{}", i + 1)
|
|
|
|
|
},
|
|
|
|
|
extension
|
|
|
|
|
);
|
|
|
|
|
images.push(file_name.clone().into());
|
|
|
|
|
images.push(file_name.clone());
|
|
|
|
|
let image = reqwest::get(&attachment.url).await?.bytes().await?;
|
|
|
|
|
let mut image_file =
|
|
|
|
|
File::create(format!("{}/{}", submission_images_dir, file_name))?;
|
|
|
|
@ -161,10 +178,14 @@ pub async fn submit(ctx: Context<'_>, submission: serenity::Attachment) -> Resul
|
|
|
|
|
slugify(&author.name),
|
|
|
|
|
author.discriminator,
|
|
|
|
|
timestamp,
|
|
|
|
|
if i == 0 { "".to_owned() } else { format!("-{}", i + 1) },
|
|
|
|
|
if i == 0 {
|
|
|
|
|
"".to_owned()
|
|
|
|
|
} else {
|
|
|
|
|
format!("-{}", i + 1)
|
|
|
|
|
},
|
|
|
|
|
extension
|
|
|
|
|
);
|
|
|
|
|
images.push(file_name.clone().into());
|
|
|
|
|
images.push(file_name.clone());
|
|
|
|
|
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)?;
|
|
|
|
@ -178,11 +199,15 @@ pub async fn submit(ctx: Context<'_>, submission: serenity::Attachment) -> Resul
|
|
|
|
|
set_submission_data(submission_data);
|
|
|
|
|
let mut message = String::new();
|
|
|
|
|
if requires_rebuild {
|
|
|
|
|
let thank_you = &format!("Thank you for submitting! You can view your submission at <https://tegakituesday.com/{}>", challenge_number);
|
|
|
|
|
let thank_you = &format!(
|
|
|
|
|
"Thank you for submitting! You can view your submission at <https://{domain}/{}>",
|
|
|
|
|
challenge_number,
|
|
|
|
|
domain = get_domain()
|
|
|
|
|
);
|
|
|
|
|
let mut repost_here = true;
|
|
|
|
|
match ctx {
|
|
|
|
|
Context::Application(_) => message.push_str(thank_you),
|
|
|
|
|
Context::Prefix(ctx) => match ctx.msg.delete(&ctx.discord.http).await {
|
|
|
|
|
Context::Prefix(ctx) => match ctx.msg.delete(&ctx.serenity_context.http).await {
|
|
|
|
|
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)
|
|
|
|
@ -202,7 +227,8 @@ pub async fn submit(ctx: Context<'_>, submission: serenity::Attachment) -> Resul
|
|
|
|
|
let invite = if data.contains_key("invite") {
|
|
|
|
|
Some(data["invite"].as_str().unwrap())
|
|
|
|
|
} else {
|
|
|
|
|
None };
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
for image in submitted_images.iter() {
|
|
|
|
|
for (other_guild_id, data) in guild_data.iter() {
|
|
|
|
|
let here = other_guild_id.eq(&ctx.guild_id().unwrap().as_u64().to_string());
|
|
|
|
@ -221,39 +247,48 @@ pub async fn submit(ctx: Context<'_>, submission: serenity::Attachment) -> Resul
|
|
|
|
|
.unwrap(),
|
|
|
|
|
);
|
|
|
|
|
let accent_color = ctx
|
|
|
|
|
.discord()
|
|
|
|
|
.serenity_context()
|
|
|
|
|
.http
|
|
|
|
|
.get_user(*author.id.as_u64())
|
|
|
|
|
.await
|
|
|
|
|
.unwrap()
|
|
|
|
|
.accent_colour;
|
|
|
|
|
channel.send_message(&ctx.discord().http, |m| {
|
|
|
|
|
m.embed(|e| {
|
|
|
|
|
let username = format!("{}#{}", author.name, author.discriminator);
|
|
|
|
|
let n = get_challenge_number();
|
|
|
|
|
let mut description = format!("New submission to [Tegaki Tuesday #{n}](https://tegakituesday.com/{n})!");
|
|
|
|
|
if !here {
|
|
|
|
|
description.push_str(&if let Some(invite) = invite {
|
|
|
|
|
format!("\nCrossposted from [{}](https://discord.gg/{invite})", guild.name)
|
|
|
|
|
} else {
|
|
|
|
|
format!("\nCrossposted from {}", guild.name)
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
e.description(description);
|
|
|
|
|
let mut embed_author = serenity::builder::CreateEmbedAuthor::default();
|
|
|
|
|
embed_author
|
|
|
|
|
.icon_url(get_avatar(&author))
|
|
|
|
|
.name(username)
|
|
|
|
|
.url(format!("https://discord.com/users/{}", author.id));
|
|
|
|
|
e.set_author(embed_author);
|
|
|
|
|
e.image(format!("https://tegakituesday.com/{n}/{image}#{timestamp}"));
|
|
|
|
|
if let Some(accent_color) = accent_color {
|
|
|
|
|
e.color(accent_color);
|
|
|
|
|
}
|
|
|
|
|
e
|
|
|
|
|
});
|
|
|
|
|
m
|
|
|
|
|
}).await.unwrap();
|
|
|
|
|
channel
|
|
|
|
|
.send_message(&ctx.serenity_context().http, |m| {
|
|
|
|
|
m.embed(|e| {
|
|
|
|
|
let username = format!("{}#{}", author.name, author.discriminator);
|
|
|
|
|
let domain = get_domain();
|
|
|
|
|
let n = get_challenge_number();
|
|
|
|
|
let mut description = format!(
|
|
|
|
|
"New submission to [Tegaki Tuesday #{n}](https://{domain}/{n})!"
|
|
|
|
|
);
|
|
|
|
|
if !here {
|
|
|
|
|
description.push_str(&if let Some(invite) = invite {
|
|
|
|
|
format!(
|
|
|
|
|
"\nCrossposted from [{}](https://discord.gg/{invite})",
|
|
|
|
|
guild.name
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
format!("\nCrossposted from {}", guild.name)
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
e.description(description);
|
|
|
|
|
let mut embed_author = serenity::builder::CreateEmbedAuthor::default();
|
|
|
|
|
embed_author
|
|
|
|
|
.icon_url(get_avatar(author))
|
|
|
|
|
.name(username)
|
|
|
|
|
.url(format!("https://discord.com/users/{}", author.id));
|
|
|
|
|
e.set_author(embed_author);
|
|
|
|
|
e.image(format!("https://{domain}/{n}/{image}"));
|
|
|
|
|
if let Some(accent_color) = accent_color {
|
|
|
|
|
e.color(accent_color);
|
|
|
|
|
}
|
|
|
|
|
e
|
|
|
|
|
});
|
|
|
|
|
m
|
|
|
|
|
})
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if invalid_types {
|
|
|
|
@ -276,7 +311,7 @@ pub async fn images(ctx: Context<'_>) -> Result<(), Error> {
|
|
|
|
|
let images: Vec<String> = {
|
|
|
|
|
let mut images = Vec::new();
|
|
|
|
|
for submission in submission_data.iter() {
|
|
|
|
|
if is_matching_submission(&submission, &ctx.author()) {
|
|
|
|
|
if is_matching_submission(submission, ctx.author()) {
|
|
|
|
|
for image in submission["images"].as_array().unwrap().iter() {
|
|
|
|
|
images.push(String::from(image.as_str().unwrap()));
|
|
|
|
|
}
|
|
|
|
@ -286,7 +321,7 @@ pub async fn images(ctx: Context<'_>) -> Result<(), Error> {
|
|
|
|
|
images
|
|
|
|
|
};
|
|
|
|
|
let challenge_number = get_challenge_number();
|
|
|
|
|
if images.len() == 0 {
|
|
|
|
|
if images.is_empty() {
|
|
|
|
|
ctx.say(format!(
|
|
|
|
|
"You haven't submitted anything for Tegaki Tuesday #{}.",
|
|
|
|
|
challenge_number
|
|
|
|
@ -294,16 +329,17 @@ pub async fn images(ctx: Context<'_>) -> Result<(), Error> {
|
|
|
|
|
.await?;
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
let mut message = String::from(format!(
|
|
|
|
|
let mut message = 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",
|
|
|
|
|
"({})<https://{domain}/{}/{}>\n",
|
|
|
|
|
to_fullwidth(&(i + 1).to_string()),
|
|
|
|
|
challenge_number,
|
|
|
|
|
image
|
|
|
|
|
image,
|
|
|
|
|
domain = get_domain()
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
ctx.say(message).await?;
|
|
|
|
@ -332,7 +368,7 @@ pub async fn imagedelete(ctx: Context<'_>, number: i32) -> Result<(), Error> {
|
|
|
|
|
let challenge_number = get_challenge_number();
|
|
|
|
|
let mut submission_data = get_current_submission_data();
|
|
|
|
|
for (i, submission) in submission_data.iter_mut().enumerate() {
|
|
|
|
|
if !is_matching_submission(&submission, &ctx.author()) {
|
|
|
|
|
if !is_matching_submission(submission, ctx.author()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let mut images = submission["images"].as_array().unwrap().clone();
|
|
|
|
@ -366,7 +402,7 @@ pub async fn imagedelete(ctx: Context<'_>, number: i32) -> Result<(), Error> {
|
|
|
|
|
Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => (),
|
|
|
|
|
Err(_) => panic!("Failed to remove file"),
|
|
|
|
|
};
|
|
|
|
|
let mut message = String::from(format!("Deleted **{}** from your submission.", image));
|
|
|
|
|
let mut message = format!("Deleted **{}** from your submission.", image);
|
|
|
|
|
if images.len() == 1 {
|
|
|
|
|
message.push_str(" As there are no more images left attached to your submission, it has been deleted.");
|
|
|
|
|
submission_data.remove(i);
|
|
|
|
@ -397,7 +433,8 @@ pub async fn imagedelete(ctx: Context<'_>, number: i32) -> Result<(), Error> {
|
|
|
|
|
pub async fn suggest(
|
|
|
|
|
ctx: Context<'_>,
|
|
|
|
|
#[rest]
|
|
|
|
|
#[description = "Suggestion text. Please include passage and source."] suggestion: String,
|
|
|
|
|
#[description = "Suggestion text. Please include passage and source."]
|
|
|
|
|
suggestion: String,
|
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
let guild_data = get_guild_data();
|
|
|
|
|
let channel = serenity::ChannelId(
|
|
|
|
@ -411,14 +448,14 @@ pub async fn suggest(
|
|
|
|
|
// If we just do msg.author.accent_colour here, we will get None
|
|
|
|
|
let author = &ctx.author();
|
|
|
|
|
let accent_color = ctx
|
|
|
|
|
.discord()
|
|
|
|
|
.serenity_context()
|
|
|
|
|
.http
|
|
|
|
|
.get_user(*author.id.as_u64())
|
|
|
|
|
.await
|
|
|
|
|
.unwrap()
|
|
|
|
|
.accent_colour;
|
|
|
|
|
channel
|
|
|
|
|
.send_message(&ctx.discord().http, |m| {
|
|
|
|
|
.send_message(&ctx.serenity_context().http, |m| {
|
|
|
|
|
m.allowed_mentions(|am| {
|
|
|
|
|
am.empty_parse();
|
|
|
|
|
am
|
|
|
|
@ -427,16 +464,13 @@ pub async fn suggest(
|
|
|
|
|
let username = format!("{}#{}", author.name, author.discriminator);
|
|
|
|
|
e.title("New suggestion");
|
|
|
|
|
e.description(if let Context::Prefix(ctx) = ctx {
|
|
|
|
|
format!(
|
|
|
|
|
"{suggestion}\n\n[See original message]({})",
|
|
|
|
|
ctx.msg.link(),
|
|
|
|
|
)
|
|
|
|
|
format!("{suggestion}\n\n[See original message]({})", ctx.msg.link(),)
|
|
|
|
|
} else {
|
|
|
|
|
suggestion
|
|
|
|
|
});
|
|
|
|
|
let mut embed_author = serenity::builder::CreateEmbedAuthor::default();
|
|
|
|
|
embed_author
|
|
|
|
|
.icon_url(get_avatar(&author))
|
|
|
|
|
.icon_url(get_avatar(author))
|
|
|
|
|
.name(username)
|
|
|
|
|
.url(format!("https://discord.com/users/{}", author.id));
|
|
|
|
|
e.set_author(embed_author);
|
|
|
|
|