cargo fmt
This commit is contained in:
parent
269e4f77c9
commit
1131e1692b
4 changed files with 63 additions and 27 deletions
|
@ -2,9 +2,9 @@ use serenity::framework::standard::{macros::command, Args, CommandResult};
|
|||
use serenity::model::prelude::*;
|
||||
use serenity::prelude::*;
|
||||
|
||||
use serde_json::Value;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use serde_json::Value;
|
||||
|
||||
use rand::seq::IteratorRandom;
|
||||
|
||||
|
@ -13,7 +13,8 @@ fn random_from_string(string: &str) -> char {
|
|||
}
|
||||
|
||||
async fn display_kanji(ctx: &Context, msg: &Message, kanji: char, comment: &str) -> CommandResult {
|
||||
msg.reply(&ctx.http, format!("{}{}", kanji, comment)).await?;
|
||||
msg.reply(&ctx.http, format!("{}{}", kanji, comment))
|
||||
.await?;
|
||||
// Not all character codes are covered in mistval/kanji_images.
|
||||
// The current implementation of 404-checking requires
|
||||
// the loading of whatever content is hosted on the URL even though
|
||||
|
@ -27,22 +28,34 @@ async fn display_kanji(ctx: &Context, msg: &Message, kanji: char, comment: &str)
|
|||
// (not desirable, since the remote repository may change)
|
||||
// - Find a way to make requests but only get the status code
|
||||
const VALIDATE_LINKS: bool = false;
|
||||
let url = format!("https://raw.githubusercontent.com/mistval/kanji_images/master/gifs/{}.gif", format!("{:x}", kanji as i32));
|
||||
let url = format!(
|
||||
"https://raw.githubusercontent.com/mistval/kanji_images/master/gifs/{}.gif",
|
||||
format!("{:x}", kanji as i32)
|
||||
);
|
||||
let mut link_validated = true;
|
||||
if VALIDATE_LINKS {
|
||||
let response = reqwest::get(&url)
|
||||
.await?
|
||||
.text()
|
||||
.await?;
|
||||
let response = reqwest::get(&url).await?.text().await?;
|
||||
link_validated = response != "404: Not Found";
|
||||
}
|
||||
msg.channel_id.say(&ctx.http, if link_validated { &url } else {
|
||||
"The stroke order diagram for this kanji is unavailable."
|
||||
}).await?;
|
||||
msg.channel_id
|
||||
.say(
|
||||
&ctx.http,
|
||||
if link_validated {
|
||||
&url
|
||||
} else {
|
||||
"The stroke order diagram for this kanji is unavailable."
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn random_kanji(category: &str, ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||
async fn random_kanji(
|
||||
category: &str,
|
||||
ctx: &Context,
|
||||
msg: &Message,
|
||||
mut args: Args,
|
||||
) -> CommandResult {
|
||||
let mut lists_file = File::open("kanji_lists.json").unwrap();
|
||||
let mut lists_json = String::new();
|
||||
lists_file.read_to_string(&mut lists_json).unwrap();
|
||||
|
@ -54,7 +67,7 @@ async fn random_kanji(category: &str, ctx: &Context, msg: &Message, mut args: Ar
|
|||
Some(string) => {
|
||||
let kanji = random_from_string(string);
|
||||
display_kanji(&ctx, &msg, kanji, "").await?;
|
||||
},
|
||||
}
|
||||
None => {
|
||||
let subcategory = args.single::<String>();
|
||||
let subcategories = list.as_object().unwrap();
|
||||
|
@ -70,26 +83,36 @@ async fn random_kanji(category: &str, ctx: &Context, msg: &Message, mut args: Ar
|
|||
total_count += count;
|
||||
string.push_str(&format!("**{}** ({} kanji),\n", subcategory, count));
|
||||
}
|
||||
string.push_str(&format!("or **all** ({} kanji total) for all subcategories", total_count));
|
||||
string.push_str(&format!(
|
||||
"or **all** ({} kanji total) for all subcategories",
|
||||
total_count
|
||||
));
|
||||
string
|
||||
};
|
||||
match subcategory {
|
||||
Ok(string) => {
|
||||
let string = string.to_uppercase();
|
||||
if string == "ALL" {
|
||||
let subcategory_key = subcategories.keys().choose(&mut rand::thread_rng()).unwrap();
|
||||
let subcategory_key = subcategories
|
||||
.keys()
|
||||
.choose(&mut rand::thread_rng())
|
||||
.unwrap();
|
||||
let list = subcategories[subcategory_key].as_str().unwrap();
|
||||
let kanji = random_from_string(&list);
|
||||
display_kanji(&ctx, &msg, kanji, &format!(", **{}**", subcategory_key)).await?;
|
||||
display_kanji(&ctx, &msg, kanji, &format!(", **{}**", subcategory_key))
|
||||
.await?;
|
||||
} else if subcategories.contains_key(&string) {
|
||||
let list = list[&string].as_str().unwrap();
|
||||
let kanji = random_from_string(&list);
|
||||
display_kanji(&ctx, &msg, kanji, "").await?;
|
||||
} else {
|
||||
let message = format!("That is an invalid subcategory. Please use {}.", &subcategory_list);
|
||||
let message = format!(
|
||||
"That is an invalid subcategory. Please use {}.",
|
||||
&subcategory_list
|
||||
);
|
||||
msg.reply(&ctx.http, message).await?;
|
||||
}
|
||||
},
|
||||
}
|
||||
Err(_) => {
|
||||
let mut message = String::from("Please specify a subcategory: ");
|
||||
message.push_str(&subcategory_list);
|
||||
|
@ -131,14 +154,23 @@ async fn so(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
|||
const MAX_CHARS: i32 = 4;
|
||||
let text = args.rest();
|
||||
if text.is_empty() {
|
||||
msg.reply(&ctx.http, "Please provide some text you want the stroke order for.").await?;
|
||||
msg.reply(
|
||||
&ctx.http,
|
||||
"Please provide some text you want the stroke order for.",
|
||||
)
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
let mut displayed_characters: Vec<char> = Vec::new();
|
||||
let mut displayed_character_count = 0;
|
||||
for character in text.chars() {
|
||||
if displayed_character_count >= MAX_CHARS {
|
||||
msg.channel_id.say(&ctx.http, ":warning: Maximum number of stroke order diagrams per command reached.").await?;
|
||||
msg.channel_id
|
||||
.say(
|
||||
&ctx.http,
|
||||
":warning: Maximum number of stroke order diagrams per command reached.",
|
||||
)
|
||||
.await?;
|
||||
break;
|
||||
}
|
||||
if character.is_whitespace() || displayed_characters.contains(&character) {
|
||||
|
@ -150,4 +182,4 @@ async fn so(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
|||
display_kanji(&ctx, &msg, character, "").await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
pub mod kanji;
|
||||
pub mod owner;
|
||||
pub mod owner;
|
||||
|
|
|
@ -13,10 +13,11 @@ async fn sleep(ctx: &Context, msg: &Message) -> CommandResult {
|
|||
msg.reply(ctx, "Good night!").await?;
|
||||
manager.lock().await.shutdown_all().await;
|
||||
} else {
|
||||
msg.reply(ctx, "There was a problem getting the shard manager").await?;
|
||||
msg.reply(ctx, "There was a problem getting the shard manager")
|
||||
.await?;
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
11
src/main.rs
11
src/main.rs
|
@ -70,13 +70,14 @@ async fn main() {
|
|||
owners.insert(info.owner.id);
|
||||
|
||||
(owners, info.id)
|
||||
},
|
||||
}
|
||||
Err(why) => panic!("Could not access application info: {:?}", why),
|
||||
};
|
||||
|
||||
// Create the framework
|
||||
let framework =
|
||||
StandardFramework::new().configure(|c| c.owners(owners).prefix(prefix)).group(&GENERAL_GROUP);
|
||||
let framework = StandardFramework::new()
|
||||
.configure(|c| c.owners(owners).prefix(prefix))
|
||||
.group(&GENERAL_GROUP);
|
||||
|
||||
let mut client = Client::builder(&token)
|
||||
.framework(framework)
|
||||
|
@ -92,7 +93,9 @@ async fn main() {
|
|||
let shard_manager = client.shard_manager.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
tokio::signal::ctrl_c().await.expect("Could not register ctrl+c handler");
|
||||
tokio::signal::ctrl_c()
|
||||
.await
|
||||
.expect("Could not register ctrl+c handler");
|
||||
shard_manager.lock().await.shutdown_all().await;
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue