From f0d2111628baa5128376997695b0a563e4c4a207 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 8 Apr 2021 22:51:34 +0200 Subject: [PATCH] Add pekofy command Signed-off-by: trivernis --- src/commands/minecraft/enchantment.rs | 4 +- src/commands/minecraft/item.rs | 4 +- src/commands/minecraft/mod.rs | 2 +- src/commands/misc/mod.rs | 4 +- src/commands/misc/pekofy.rs | 98 +++++++++++++++++++++++++++ src/commands/misc/ping.rs | 3 +- src/commands/misc/shutdown.rs | 3 +- src/commands/misc/stats.rs | 3 +- src/commands/music/clear.rs | 2 +- src/commands/music/current.rs | 2 +- src/commands/music/join.rs | 2 +- src/commands/music/leave.rs | 2 +- src/commands/music/mod.rs | 2 +- src/commands/music/pause.rs | 2 +- src/commands/music/play.rs | 2 +- src/commands/music/play_next.rs | 2 +- src/commands/music/queue.rs | 2 +- src/commands/music/shuffle.rs | 2 +- src/commands/music/skip.rs | 2 +- src/commands/settings/get.rs | 4 +- src/commands/settings/set.rs | 4 +- 21 files changed, 124 insertions(+), 27 deletions(-) create mode 100644 src/commands/misc/pekofy.rs diff --git a/src/commands/minecraft/enchantment.rs b/src/commands/minecraft/enchantment.rs index 1ddf7bc..4b18ccf 100644 --- a/src/commands/minecraft/enchantment.rs +++ b/src/commands/minecraft/enchantment.rs @@ -6,8 +6,8 @@ use crate::utils::store::Store; #[command] #[description("Provides information for a single enchantment")] -#[usage("enchantment ")] -#[example("item unbreaking")] +#[usage("")] +#[example("unbreaking")] #[min_args(1)] #[aliases("ench")] pub(crate) async fn enchantment(ctx: &Context, msg: &Message, args: Args) -> CommandResult { diff --git a/src/commands/minecraft/item.rs b/src/commands/minecraft/item.rs index 95c0183..95af889 100644 --- a/src/commands/minecraft/item.rs +++ b/src/commands/minecraft/item.rs @@ -6,8 +6,8 @@ use crate::utils::store::Store; #[command] #[description("Provides information for a single minecraft item")] -#[usage("item ")] -#[example("item bread")] +#[usage("")] +#[example("bread")] #[min_args(1)] #[aliases("i")] pub(crate) async fn item(ctx: &Context, msg: &Message, args: Args) -> CommandResult { diff --git a/src/commands/minecraft/mod.rs b/src/commands/minecraft/mod.rs index 0b238fb..7bebe31 100644 --- a/src/commands/minecraft/mod.rs +++ b/src/commands/minecraft/mod.rs @@ -8,5 +8,5 @@ mod item; #[group] #[commands(item, enchantment)] -#[prefix("mc")] +#[prefixes("mc", "minecraft")] pub(crate) struct Minecraft; diff --git a/src/commands/misc/mod.rs b/src/commands/misc/mod.rs index e9fe16c..830f38b 100644 --- a/src/commands/misc/mod.rs +++ b/src/commands/misc/mod.rs @@ -1,14 +1,16 @@ use serenity::framework::standard::macros::group; +use pekofy::PEKOFY_COMMAND; use ping::PING_COMMAND; use shutdown::SHUTDOWN_COMMAND; use stats::STATS_COMMAND; pub(crate) mod help; +mod pekofy; mod ping; mod shutdown; mod stats; #[group] -#[commands(ping, stats, shutdown)] +#[commands(ping, stats, shutdown, pekofy)] pub struct Misc; diff --git a/src/commands/misc/pekofy.rs b/src/commands/misc/pekofy.rs new file mode 100644 index 0000000..ad925fb --- /dev/null +++ b/src/commands/misc/pekofy.rs @@ -0,0 +1,98 @@ +use serenity::framework::standard::{Args, CommandError, CommandResult}; +use serenity::model::channel::Message; +use serenity::{framework::standard::macros::command, prelude::*}; + +static MESSAGE_DELIMITERS: &[char] = &['.', '?', '!', '"']; +static MARKDOWN_SPECIAL_CHARACTERS: &[&str] = &["~~", "**", "*"]; + +#[command] +#[description("Pekofy messages")] +#[usage("()")] +#[example("Hello")] +async fn pekofy(ctx: &Context, msg: &Message, args: Args) -> CommandResult { + let mut reference_message = msg.id; + let mut content = args.message().to_string(); + + if args.is_empty() { + if let Some(reference) = &msg.referenced_message { + reference_message = reference.id; + content = reference.content.clone(); + } else { + let messages = msg + .channel_id + .messages(ctx, |ret| ret.before(&msg.id).limit(1)) + .await?; + let reference = messages + .first() + .ok_or(CommandError::from("No message to pekofy"))?; + + reference_message = reference.id; + content = reference.content.clone(); + }; + let _ = msg.delete(ctx).await; + } + if content.is_empty() { + return Err(CommandError::from("Can't pekofy empty message")); + } + + log::debug!("Pekofying message '{}'", content); + let pekofied: String = content + .lines() + .into_iter() + .map(pekofy_line) + .collect::>() + .join("\n"); + let message = ctx + .http + .get_message(msg.channel_id.0, reference_message.0) + .await?; + log::debug!("Pekofied message is '{}'", pekofied); + message.reply(ctx, pekofied).await?; + + Ok(()) +} + +/// Pekofies a single line +fn pekofy_line(mut line: &str) -> String { + log::debug!("Pekofying line '{}'", line); + let original = line; + let mut md_index = None; + + for pattern in MARKDOWN_SPECIAL_CHARACTERS { + if let Some(i) = line.rfind(pattern) { + log::debug!("Found markdown at index {}", i); + md_index = Some(i); + break; + } + } + let mut md = ""; + if let Some(index) = md_index { + let (line_part, md_part) = line.split_at(index); + line = line_part; + md = md_part; + } + if line.ends_with("peko") { + log::debug!("Peko already found in message. Returning original"); + return original.to_string(); + } + + let punctuation_index = line.rfind(MESSAGE_DELIMITERS); + let mut peko = "peko".to_string(); + + if line + .chars() + .filter(|c| c.is_alphabetic()) + .all(char::is_uppercase) + { + log::debug!("Message is all uppercase. Peko will also be uppercase"); + peko = peko.to_uppercase(); + } + + if let Some(index) = punctuation_index { + log::debug!("Found punctuation at index {}", index); + let (before, after) = line.split_at(index); + format!("{} {}{}{}", before, peko, after, md) + } else { + format!("{} {}{}", line, peko, md) + } +} diff --git a/src/commands/misc/ping.rs b/src/commands/misc/ping.rs index 1198939..78a2fcd 100644 --- a/src/commands/misc/ping.rs +++ b/src/commands/misc/ping.rs @@ -5,8 +5,7 @@ use serenity::model::channel::Message; #[command] #[description("Simple ping test command")] -#[usage("ping")] -#[example("ping")] +#[usage("")] async fn ping(ctx: &Context, msg: &Message) -> CommandResult { msg.reply(ctx, "Pong!").await?; diff --git a/src/commands/misc/shutdown.rs b/src/commands/misc/shutdown.rs index 1dab068..7fe3da1 100644 --- a/src/commands/misc/shutdown.rs +++ b/src/commands/misc/shutdown.rs @@ -6,8 +6,7 @@ use std::process; #[command] #[description("Shutdown")] -#[usage("shutdown")] -#[example("shutdown")] +#[usage("")] #[owners_only] async fn shutdown(ctx: &Context, msg: &Message) -> CommandResult { log::info!("Shutting down..."); diff --git a/src/commands/misc/stats.rs b/src/commands/misc/stats.rs index e27e447..2e15e7a 100644 --- a/src/commands/misc/stats.rs +++ b/src/commands/misc/stats.rs @@ -9,8 +9,7 @@ use sysinfo::{ProcessExt, SystemExt}; #[command] #[description("Shows some statistics about the bot")] -#[usage("stats")] -#[example("stats")] +#[usage("")] async fn stats(ctx: &Context, msg: &Message) -> CommandResult { log::debug!("Reading system stats"); let mut system = sysinfo::System::new_all(); diff --git a/src/commands/music/clear.rs b/src/commands/music/clear.rs index e031cdd..aa75c0d 100644 --- a/src/commands/music/clear.rs +++ b/src/commands/music/clear.rs @@ -8,7 +8,7 @@ use crate::commands::music::get_queue_for_guild; #[command] #[only_in(guilds)] #[description("Clears the queue")] -#[usage("clear")] +#[usage("")] #[aliases("cl")] #[allowed_roles("DJ")] async fn clear(ctx: &Context, msg: &Message) -> CommandResult { diff --git a/src/commands/music/current.rs b/src/commands/music/current.rs index 5d078b3..610c760 100644 --- a/src/commands/music/current.rs +++ b/src/commands/music/current.rs @@ -8,7 +8,7 @@ use crate::commands::music::get_queue_for_guild; #[command] #[only_in(guilds)] #[description("Displays the currently playing song")] -#[usage("current")] +#[usage("")] #[aliases("nowplaying", "np")] async fn current(ctx: &Context, msg: &Message) -> CommandResult { let guild = msg.guild(&ctx.cache).await.unwrap(); diff --git a/src/commands/music/join.rs b/src/commands/music/join.rs index b4a5a9d..e6d695c 100644 --- a/src/commands/music/join.rs +++ b/src/commands/music/join.rs @@ -8,7 +8,7 @@ use crate::commands::music::{get_channel_for_author, join_channel}; #[command] #[only_in(guilds)] #[description("Joins a voice channel")] -#[usage("join")] +#[usage("")] async fn join(ctx: &Context, msg: &Message) -> CommandResult { let guild = msg.guild(&ctx.cache).await.unwrap(); let channel_id = get_channel_for_author(&msg.author.id, &guild)?; diff --git a/src/commands/music/leave.rs b/src/commands/music/leave.rs index 53f9210..e305e2b 100644 --- a/src/commands/music/leave.rs +++ b/src/commands/music/leave.rs @@ -8,7 +8,7 @@ use crate::commands::music::{get_queue_for_guild, get_voice_manager}; #[command] #[only_in(guilds)] #[description("Leaves a voice channel")] -#[usage("leave")] +#[usage("")] #[aliases("stop")] #[allowed_roles("DJ")] async fn leave(ctx: &Context, msg: &Message) -> CommandResult { diff --git a/src/commands/music/mod.rs b/src/commands/music/mod.rs index 9a90a1a..7f53ed9 100644 --- a/src/commands/music/mod.rs +++ b/src/commands/music/mod.rs @@ -48,7 +48,7 @@ mod skip; #[commands( join, leave, play, queue, skip, shuffle, current, play_next, clear, pause )] -#[prefix("m")] +#[prefixes("m", "music")] pub struct Music; struct SongEndNotifier { diff --git a/src/commands/music/pause.rs b/src/commands/music/pause.rs index ba836f9..3d49174 100644 --- a/src/commands/music/pause.rs +++ b/src/commands/music/pause.rs @@ -8,7 +8,7 @@ use crate::commands::music::get_queue_for_guild; #[command] #[only_in(guilds)] #[description("Pauses playback")] -#[usage("pause")] +#[usage("")] #[allowed_roles("DJ")] async fn pause(ctx: &Context, msg: &Message) -> CommandResult { let guild = msg.guild(&ctx.cache).await.unwrap(); diff --git a/src/commands/music/play.rs b/src/commands/music/play.rs index 0c41cdc..01c684f 100644 --- a/src/commands/music/play.rs +++ b/src/commands/music/play.rs @@ -13,7 +13,7 @@ use crate::database::guild::SETTING_AUTOSHUFFLE; #[command] #[only_in(guilds)] #[description("Plays a song in a voice channel")] -#[usage("play ")] +#[usage("()")] #[min_args(1)] #[aliases("p")] async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult { diff --git a/src/commands/music/play_next.rs b/src/commands/music/play_next.rs index 1d44a95..cecc74e 100644 --- a/src/commands/music/play_next.rs +++ b/src/commands/music/play_next.rs @@ -11,7 +11,7 @@ use crate::commands::music::{ #[command] #[only_in(guilds)] #[description("Puts a song as the next to play in the queue")] -#[usage("play_next ")] +#[usage("")] #[min_args(1)] #[aliases("pn")] #[allowed_roles("DJ")] diff --git a/src/commands/music/queue.rs b/src/commands/music/queue.rs index b42ba5b..c397d18 100644 --- a/src/commands/music/queue.rs +++ b/src/commands/music/queue.rs @@ -10,7 +10,7 @@ use crate::commands::music::get_queue_for_guild; #[command] #[only_in(guilds)] #[description("Shows the song queue")] -#[usage("queue")] +#[usage("")] #[aliases("q")] async fn queue(ctx: &Context, msg: &Message) -> CommandResult { let guild = msg.guild(&ctx.cache).await.unwrap(); diff --git a/src/commands/music/shuffle.rs b/src/commands/music/shuffle.rs index 6e58caf..e252652 100644 --- a/src/commands/music/shuffle.rs +++ b/src/commands/music/shuffle.rs @@ -8,7 +8,7 @@ use crate::commands::music::get_queue_for_guild; #[command] #[only_in(guilds)] #[description("Shuffles the queue")] -#[usage("shuffle")] +#[usage("")] #[aliases("sh")] #[allowed_roles("DJ")] async fn shuffle(ctx: &Context, msg: &Message) -> CommandResult { diff --git a/src/commands/music/skip.rs b/src/commands/music/skip.rs index bca3dcf..f914ab8 100644 --- a/src/commands/music/skip.rs +++ b/src/commands/music/skip.rs @@ -8,7 +8,7 @@ use crate::commands::music::get_queue_for_guild; #[command] #[only_in(guilds)] #[description("Skips to the next song")] -#[usage("skip")] +#[usage("")] #[aliases("next")] #[allowed_roles("DJ")] async fn skip(ctx: &Context, msg: &Message) -> CommandResult { diff --git a/src/commands/settings/get.rs b/src/commands/settings/get.rs index a037a56..8b193c2 100644 --- a/src/commands/settings/get.rs +++ b/src/commands/settings/get.rs @@ -9,8 +9,8 @@ use crate::database::guild::GUILD_SETTINGS; #[command] #[only_in(guilds)] #[description("Get a guild setting")] -#[usage("get ()")] -#[example("get music.autoshuffle")] +#[usage("()")] +#[example("music.autoshuffle")] #[min_args(0)] #[max_args(1)] #[required_permissions("MANAGE_GUILD")] diff --git a/src/commands/settings/set.rs b/src/commands/settings/set.rs index 0d7cc88..d87b7ca 100644 --- a/src/commands/settings/set.rs +++ b/src/commands/settings/set.rs @@ -8,8 +8,8 @@ use crate::database::get_database_from_context; #[command] #[only_in(guilds)] #[description("Set a guild setting")] -#[usage("set ")] -#[example("set music.autoshuffle true")] +#[usage(" ")] +#[example("music.autoshuffle true")] #[min_args(2)] #[max_args(2)] #[required_permissions("MANAGE_GUILD")]