From 19e0ecc50050e96aecc70d560c9a903a6e51fd85 Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Tue, 4 May 2021 18:25:12 +0200 Subject: [PATCH] restructuring --- src/commands.rs | 112 +++++++++++++++++++++++++++++++++++++++++++++ src/general.rs | 4 +- src/main.rs | 119 ++++-------------------------------------------- 3 files changed, 122 insertions(+), 113 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index e69de29..5a33de6 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -0,0 +1,112 @@ +use std::collections::HashSet; + +use lazy_static::lazy_static; +use rand::Rng; +use serenity::client::Context; +use serenity::framework::standard::{Args, CommandGroup, CommandResult, help_commands, HelpOptions, macros::{command, group, help}}; +use serenity::model::{channel::Message}; +use serenity::model::id::UserId; +use serenity::utils::{content_safe, ContentSafeOptions}; +use toml::Value; +use uwuifier::uwuify_str_sse; + +use crate::general::{CONFIG, CONFIG_ERR, reply}; +use crate::LastMessageInChannel; + +#[group] +#[commands(say)] +#[description = "General widetom commands"] +struct General; + +#[group] +#[commands(uwuify, xp)] +#[description = "meme commands"] +struct Meme; + +#[group] +#[commands(shutdown)] +#[owners_only] +#[description = "bot admin commands"] +struct Admin; + + +#[command] +#[description("say something")] +async fn say(ctx: &Context, msg: &Message, args: Args) -> CommandResult { + let settings = if let Some(guild_id) = msg.guild_id { + ContentSafeOptions::default() + .clean_channel(false) + .display_as_member_from(guild_id) + } else { + ContentSafeOptions::default() + .clean_channel(false) + .clean_role(false) + }; + + let content = content_safe(&ctx.cache, &args.rest(), &settings).await; + msg.channel_id.say(&ctx.http, &content).await?; + Ok(()) +} + +#[command] +#[description("uwuifies the arguments, or the last message in the channel if no args are supplied")] +async fn uwuify(ctx: &Context, msg: &Message, args: Args) -> CommandResult { + if args.is_empty() { + let mut data = ctx.data.write().await; + let map = data.get_mut::().expect("No LastMessageInChannel in TypeMap"); + let old_message = map.get(&msg.channel_id); + match old_message { + Some(s) => { + let uwu = uwuify_str_sse(s); + msg.channel_id.say(&ctx.http, uwu).await?; + } + None => { + msg.channel_id.say(&ctx.http, "Could not find last message.").await?; + } + } + } else { + let uwu = uwuify_str_sse(args.rest()); + msg.channel_id.say(&ctx.http, uwu).await?; + } + Ok(()) +} + +#[command] +#[description("end tom")] +async fn shutdown(ctx: &Context, msg: &Message, _: Args) -> CommandResult { + reply("<:tom:811324632082415626> bye <:tom:811324632082415626>", &msg, &ctx).await; + std::process::exit(0); +} + + +#[command] +#[description("display a random answer from the xp support applications")] +async fn xp(ctx: &Context, msg: &Message, _: Args) -> CommandResult { + lazy_static! { + static ref XP_RESPONSES: &'static Vec = CONFIG.get("xp").expect(CONFIG_ERR).as_array().expect(CONFIG_ERR); + } + let index = rand::thread_rng().gen_range(0..XP_RESPONSES.len()); + let random_value = XP_RESPONSES[index].as_str().expect(CONFIG_ERR); + msg.channel_id.say(&ctx.http, random_value).await?; + Ok(()) +} + +#[help] +#[individual_command_tip = +"w i d e t o m\n\n\ +tom moment."] +#[command_not_found_text = "Could not find: `{}`."] +#[max_levenshtein_distance(3)] +#[strikethrough_commands_tip_in_dm = ""] +#[strikethrough_commands_tip_in_guild = ""] +pub async fn my_help( + context: &Context, + msg: &Message, + args: Args, + help_options: &'static HelpOptions, + groups: &[&'static CommandGroup], + owners: HashSet, +) -> CommandResult { + let _ = help_commands::with_embeds(context, msg, args, help_options, groups, owners).await; + Ok(()) +} diff --git a/src/general.rs b/src/general.rs index 731d373..6e47e85 100644 --- a/src/general.rs +++ b/src/general.rs @@ -1,15 +1,15 @@ use std::collections::HashMap; use std::fs; +use fancy_regex::Regex; use lazy_static::lazy_static; use serenity::client::Context; use serenity::framework::standard::{macros::hook}; use serenity::model::channel::{Message, ReactionType}; use serenity::model::id::EmojiId; use toml::Value; -use fancy_regex::Regex; -use crate::LastMessageInChannel; +use crate::LastMessageInChannel; pub static CONFIG_ERR: &'static str = "Invalid config file"; diff --git a/src/main.rs b/src/main.rs index bb807fa..0f22337 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,26 +1,17 @@ -mod general; -mod commands; - -use std::collections::{HashSet, HashMap}; +use std::collections::{HashMap, HashSet}; use std::fs; -use serenity::{ - async_trait, - model::{channel::Message, gateway::Ready}, - prelude::*, -}; +use serenity::{async_trait, model::gateway::Ready, prelude::*}; use serenity::client::Context; -use serenity::framework::standard::{Args, CommandGroup, CommandResult, help_commands, HelpOptions, macros::{command, group, help}}; use serenity::framework::StandardFramework; use serenity::http::Http; -use serenity::model::id::{UserId, ChannelId}; -use serenity::utils::{content_safe, ContentSafeOptions}; -use uwuifier::uwuify_str_sse; -use lazy_static::lazy_static; +use serenity::model::id::{ChannelId, UserId}; -use rand::Rng; -use toml::Value; -use crate::general::{normal_message, CONFIG_ERR, reply, CONFIG}; +use crate::commands::{ADMIN_GROUP, GENERAL_GROUP, MEME_GROUP, MY_HELP}; +use crate::general::normal_message; + +mod general; +mod commands; pub struct LastMessageInChannel; @@ -28,22 +19,6 @@ impl TypeMapKey for LastMessageInChannel { type Value = HashMap; } -#[group] -#[commands(say)] -#[description = "General widetom commands"] -struct General; - -#[group] -#[commands(uwuify, xp)] -#[description = "meme commands"] -struct Meme; - -#[group] -#[commands(shutdown)] -#[owners_only] -#[description = "bot admin commands"] -struct Admin; - struct Handler; #[async_trait] @@ -103,81 +78,3 @@ async fn main() { println!("Client error: {:?}", why); } } - -#[command] -async fn say(ctx: &Context, msg: &Message, args: Args) -> CommandResult { - let settings = if let Some(guild_id) = msg.guild_id { - ContentSafeOptions::default() - .clean_channel(false) - .display_as_member_from(guild_id) - } else { - ContentSafeOptions::default() - .clean_channel(false) - .clean_role(false) - }; - - let content = content_safe(&ctx.cache, &args.rest(), &settings).await; - msg.channel_id.say(&ctx.http, &content).await?; - Ok(()) -} - -#[command] -#[description("uwuifies the arguments, or the last message in the channel if no args are supplied")] -async fn uwuify(ctx: &Context, msg: &Message, args: Args) -> CommandResult { - if args.is_empty() { - let mut data = ctx.data.write().await; - let map = data.get_mut::().expect("No LastMessageInChannel in TypeMap"); - let old_message = map.get(&msg.channel_id); - match old_message { - Some(s) => { - let uwu = uwuify_str_sse(s); - msg.channel_id.say(&ctx.http, uwu).await?; - } - None => { - msg.channel_id.say(&ctx.http, "Could not find last message.").await?; - } - } - } else { - let uwu = uwuify_str_sse(args.rest()); - msg.channel_id.say(&ctx.http, uwu).await?; - } - Ok(()) -} - -#[command] -async fn shutdown(ctx: &Context, msg: &Message, _: Args) -> CommandResult { - reply("<:tom:811324632082415626> bye <:tom:811324632082415626>", &msg, &ctx).await; - std::process::exit(0); -} - - -#[command] -async fn xp(ctx: &Context, msg: &Message, _: Args) -> CommandResult { - lazy_static! { - static ref XP_RESPONSES: &'static Vec = CONFIG.get("xp").expect(CONFIG_ERR).as_array().expect(CONFIG_ERR); - } - let index = rand::thread_rng().gen_range(0..XP_RESPONSES.len()); - let random_value = XP_RESPONSES[index].as_str().expect(CONFIG_ERR); - msg.channel_id.say(&ctx.http, random_value).await?; - Ok(()) -} - -#[help] -#[individual_command_tip = -"w i d e t o m\n\n\ -tom moment."] -#[command_not_found_text = "Could not find: `{}`."] -#[max_levenshtein_distance(3)] -#[strikethrough_commands_tip_in_dm = ""] -#[strikethrough_commands_tip_in_guild = ""] -pub async fn my_help( - context: &Context, - msg: &Message, - args: Args, - help_options: &'static HelpOptions, - groups: &[&'static CommandGroup], - owners: HashSet, -) -> CommandResult { - let _ = help_commands::with_embeds(context, msg, args, help_options, groups, owners).await; - Ok(()) -}