Compare commits

...

9 Commits

@ -1,3 +1,4 @@
DOMAIN=tegakituesday.com
DISCORD_TOKEN=
PREFIX="-h "
HUGO=/path/to/hugo

@ -0,0 +1 @@
use nix

834
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -15,7 +15,7 @@ reqwest = "0.11"
slug = "0.1"
unicode_hfwidth = "0.2"
fs_extra = "1.2"
poise = "0.4"
poise = "0.5.5"
[dependencies.tokio]
version = "1.0"

@ -0,0 +1,21 @@
# <shell.nix>
{ pkgs ? import <nixpkgs> {}}:
let
rust_overlay = import (builtins.fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz");
pkgs = import <nixpkgs> { overlays = [ rust_overlay ]; };
ruststable = (pkgs.rust-bin.stable.latest.default.override {
extensions = [
"rust-src"
];
});
in
pkgs.mkShell {
buildInputs = with pkgs; [
ruststable
rust-analyzer
bacon
pkg-config
openssl
];
}

@ -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);

@ -21,7 +21,7 @@ pub async fn i(
}
let kanji_info = get_kanji_info(character);
covered_chars.push(character);
if kanji_info.len() == 0 {
if kanji_info.is_empty() {
skipped_chars += 1;
continue;
}
@ -34,7 +34,7 @@ pub async fn i(
message = format!(
"Found {} kanji{}\n{}",
found_chars.len(),
if found_chars.len() == 0 { "." } else { ":" },
if found_chars.is_empty() { "." } else { ":" },
message
);
if skipped_chars > 0 {
@ -54,9 +54,9 @@ pub async fn i(
e.description(message);
let mut author = serenity::builder::CreateEmbedAuthor::default();
author
.icon_url("https://raw.githubusercontent.com/ElnuDev/ji-chan/main/ji-chan.png")
.icon_url("https://git.elnu.com/tegakituesday/ji-chan/raw/branch/main/ji-chan.png")
.name("字ちゃん")
.url("https://github.com/ElnuDev/ji-chan");
.url("https://git.elnu.com/tegakituesday/ji-chan");
e.set_author(author);
e.color(serenity::utils::Colour::from_rgb(251, 73, 52));
if found_chars.len() == 1 {
@ -142,7 +142,7 @@ pub async fn so(
if displayed_character_count >= MAX_CHARS {
ctx.channel_id()
.say(
&ctx.discord().http,
&ctx.serenity_context().http,
":warning: Maximum number of stroke order diagrams per command reached.",
)
.await?;

@ -1,3 +1,4 @@
use crate::utils::get_domain;
use crate::Context;
use crate::Error;
use poise::command;
@ -13,10 +14,9 @@ use std::env;
description_localized("en-US", "Get help for the 字ちゃん Tegaki Tuesday bot")
)]
pub async fn help(ctx: Context<'_>) -> Result<(), Error> {
let p = env::var("PREFIX").unwrap();
let message = format!(
"<:jichan:943336845637480478> Hello! I'm (Ji-chan), the Tegaki Tuesday bot (and mascot!).
For more information about the challenge, check out the website at <https://tegakituesday.com>
For more information about the challenge, check out the website at <https://{domain}>
__**Tegaki Tuesday **__
:ballot_box: `{p}submit` Submit to the latest handwriting challenge.
@ -32,7 +32,9 @@ __**Kanji 漢字**__
:game_die: `{p}joyo` Random Jōyō kanji
:game_die: `{p}kyoiku <grade|all>` Random Kyōiku kanji
:game_die: `{p}jlpt <level|all>` Random JLPT kanji
:game_die: `{p}hyogai <group|all>` Random Hyōgai kanji"
:game_die: `{p}hyogai <group|all>` Random Hyōgai kanji",
p = env::var("PREFIX").unwrap(),
domain = get_domain(),
);
ctx.say(message).await?;
Ok(())

@ -97,9 +97,10 @@ pub async fn announce(
#[command(prefix_command, hide_in_help, ephemeral, owners_only)]
pub async fn announcechallenge(ctx: Context<'_>) -> Result<(), Error> {
let challenge_number = get_challenge_number();
let message = format!("Welcome to the **{n}{th}** weekly **Tegaki Tuesday** () handwriting challenge! :pen_fountain: The prompt is available in both Japanese and English on the website at <https://tegakituesday.com/{n}>.
let message = format!("Welcome to the **{n}{th}** weekly **Tegaki Tuesday** () handwriting challenge! :pen_fountain: The prompt is available in both Japanese and English on the website at <https://{domain}/{n}>.
You can make submissions in both languages, but please submit in your target language first. Submissions can be submitted by uploading the image to this channel along with the `{p}submit` command. By submitting, you agree to having your work posted to the website under the Attribution-ShareAlike 4.0 Unported (CC BY-SA 4.0) license, attributed to your Discord account. (<https://creativecommons.org/licenses/by-sa/4.0>).",
domain = get_domain(),
n = challenge_number,
th = match challenge_number % 10 {
1 => "ˢᵗ",

@ -71,7 +71,7 @@ async fn main() {
| GatewayIntents::MESSAGE_CONTENT
| GatewayIntents::GUILDS,
)
.user_data_setup(move |_ctx, _ready, _framework| Box::pin(async move { Ok(Data {}) }));
.setup(move |_ctx, _ready, _framework| Box::pin(async move { Ok(Data {}) }));
framework.run().await.unwrap();
}

@ -34,6 +34,10 @@ pub fn get_challenge_number() -> i32 {
max
}
pub fn get_domain() -> String {
env::var("DOMAIN").unwrap()
}
pub fn get_hugo_path() -> String {
env::var("HUGO").unwrap()
}
@ -175,7 +179,7 @@ pub async fn send(ctx: Context<'_>, message: &str, ping: bool, pin: bool) -> Res
));
}
message_to_send.push_str(message);
let ctx = ctx.discord();
let ctx = ctx.serenity_context();
let sent_message = channel
.send_message(&ctx.http, |e| {
e.content(message_to_send);
@ -186,10 +190,7 @@ pub async fn send(ctx: Context<'_>, message: &str, ping: bool, pin: bool) -> Res
if pin {
// No need to do anything on error,
// it just means we don't have pin permissions
match sent_message.pin(&ctx.http).await {
Ok(_) => (),
Err(_) => (),
};
let _ = sent_message.pin(&ctx.http).await;
}
// announcements_count += 1;
}
@ -210,8 +211,8 @@ pub fn random_from_string(string: &str) -> char {
pub fn get_so_diagram(kanji: char) -> String {
format!(
"https://raw.githubusercontent.com/mistval/kanji_images/master/gifs/{}.gif",
format!("{:x}", kanji as i32)
"https://raw.githubusercontent.com/mistval/kanji_images/master/gifs/{:x}.gif",
kanji as i32
)
}
@ -224,7 +225,7 @@ pub async fn display_kanji(ctx: Context<'_>, kanji: char, comment: &str) -> Resu
let link_validated = response != reqwest::StatusCode::NOT_FOUND;
ctx.channel_id()
.say(
&ctx.discord().http,
&ctx.serenity_context().http,
if link_validated {
&url
} else {
@ -328,11 +329,11 @@ pub async fn random_kanji(
.choose(&mut rand::thread_rng())
.unwrap();
let list = subcategories[subcategory_key].as_str().unwrap();
let kanji = random_from_string(&list);
let kanji = random_from_string(list);
display_kanji(ctx, kanji, &format!(", **{}**", subcategory_key)).await?;
} else if subcategories.contains_key(&subcategory) {
let list = list[&subcategory].as_str().unwrap();
let kanji = random_from_string(&list);
let kanji = random_from_string(list);
display_kanji(ctx, kanji, "").await?;
} else {
let message = format!(
@ -373,7 +374,7 @@ pub async fn leaderboard(ctx: &Context<'_>) -> Result<(), Error> {
for (i, (id, count)) in top_submitters[0..LENGTH].iter().enumerate() {
let place = i + 1;
let user = serenity::UserId(id.parse::<u64>().unwrap())
.to_user(&ctx.discord().http)
.to_user(&ctx.serenity_context().http)
.await?;
let avatar = get_avatar(&user);
let profile = format!("https://discord.com/users/{id}");

Loading…
Cancel
Save