restructuring

This commit is contained in:
nora 2021-05-04 18:25:12 +02:00
parent 65c6f3eedc
commit 19e0ecc500
3 changed files with 122 additions and 113 deletions

View file

@ -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::<LastMessageInChannel>().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<Value> = 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<UserId>,
) -> CommandResult {
let _ = help_commands::with_embeds(context, msg, args, help_options, groups, owners).await;
Ok(())
}

View file

@ -1,15 +1,15 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::fs; use std::fs;
use fancy_regex::Regex;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use serenity::client::Context; use serenity::client::Context;
use serenity::framework::standard::{macros::hook}; use serenity::framework::standard::{macros::hook};
use serenity::model::channel::{Message, ReactionType}; use serenity::model::channel::{Message, ReactionType};
use serenity::model::id::EmojiId; use serenity::model::id::EmojiId;
use toml::Value; use toml::Value;
use fancy_regex::Regex;
use crate::LastMessageInChannel;
use crate::LastMessageInChannel;
pub static CONFIG_ERR: &'static str = "Invalid config file"; pub static CONFIG_ERR: &'static str = "Invalid config file";

View file

@ -1,26 +1,17 @@
mod general; use std::collections::{HashMap, HashSet};
mod commands;
use std::collections::{HashSet, HashMap};
use std::fs; use std::fs;
use serenity::{ use serenity::{async_trait, model::gateway::Ready, prelude::*};
async_trait,
model::{channel::Message, gateway::Ready},
prelude::*,
};
use serenity::client::Context; use serenity::client::Context;
use serenity::framework::standard::{Args, CommandGroup, CommandResult, help_commands, HelpOptions, macros::{command, group, help}};
use serenity::framework::StandardFramework; use serenity::framework::StandardFramework;
use serenity::http::Http; use serenity::http::Http;
use serenity::model::id::{UserId, ChannelId}; use serenity::model::id::{ChannelId, UserId};
use serenity::utils::{content_safe, ContentSafeOptions};
use uwuifier::uwuify_str_sse;
use lazy_static::lazy_static;
use rand::Rng; use crate::commands::{ADMIN_GROUP, GENERAL_GROUP, MEME_GROUP, MY_HELP};
use toml::Value; use crate::general::normal_message;
use crate::general::{normal_message, CONFIG_ERR, reply, CONFIG};
mod general;
mod commands;
pub struct LastMessageInChannel; pub struct LastMessageInChannel;
@ -28,22 +19,6 @@ impl TypeMapKey for LastMessageInChannel {
type Value = HashMap<ChannelId, String>; type Value = HashMap<ChannelId, String>;
} }
#[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; struct Handler;
#[async_trait] #[async_trait]
@ -103,81 +78,3 @@ async fn main() {
println!("Client error: {:?}", why); 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::<LastMessageInChannel>().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<Value> = 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<UserId>,
) -> CommandResult {
let _ = help_commands::with_embeds(context, msg, args, help_options, groups, owners).await;
Ok(())
}