mirror of
https://github.com/Noratrieb/widetom.git
synced 2026-03-15 05:46:04 +01:00
gamer
This commit is contained in:
parent
33d1738799
commit
bb2c954418
7 changed files with 806 additions and 489 deletions
|
|
@ -1,7 +1,6 @@
|
|||
use std::collections::HashSet;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use rand::Rng;
|
||||
use rand::seq::IndexedRandom;
|
||||
use serenity::all::{CreateEmbed, CreateMessage};
|
||||
use serenity::client::Context;
|
||||
use serenity::framework::standard::{
|
||||
|
|
@ -12,11 +11,10 @@ use serenity::framework::standard::{
|
|||
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::{reply, CONFIG, CONFIG_ERR, REACTION_EMOTES};
|
||||
use crate::LastMessageInChannel;
|
||||
use crate::general::reply;
|
||||
use crate::{Config, LastMessageInChannel};
|
||||
|
||||
#[group]
|
||||
#[commands(say, list)]
|
||||
|
|
@ -37,12 +35,15 @@ struct Admin;
|
|||
#[command]
|
||||
#[description("lists all the commands")]
|
||||
async fn list(ctx: &Context, msg: &Message, _: Args) -> CommandResult {
|
||||
let data = ctx.data.read().await;
|
||||
let config = data.get::<Config>().unwrap();
|
||||
msg.channel_id
|
||||
.send_message(
|
||||
&ctx.http,
|
||||
CreateMessage::new().embed(
|
||||
CreateEmbed::new().title("Widetom reaction emotes").fields(
|
||||
REACTION_EMOTES
|
||||
config
|
||||
.emotes
|
||||
.iter()
|
||||
.map(|em| (em.0, format!("<:{}:{}>", em.0, em.1.get()), false)),
|
||||
),
|
||||
|
|
@ -116,15 +117,12 @@ async fn shutdown(ctx: &Context, msg: &Message, _: Args) -> CommandResult {
|
|||
#[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);
|
||||
let data = ctx.data.read().await;
|
||||
let config = data.get::<Config>().unwrap();
|
||||
|
||||
let Some(random_value) = config.xp.choose(&mut rand::rng()) else {
|
||||
return Ok(());
|
||||
};
|
||||
msg.channel_id.say(&ctx.http, random_value).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,50 +1,11 @@
|
|||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::sync::LazyLock;
|
||||
|
||||
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 crate::LastMessageInChannel;
|
||||
|
||||
pub static CONFIG_ERR: &'static str = "Invalid config file";
|
||||
|
||||
lazy_static! {
|
||||
pub static ref CONFIG: Value = {
|
||||
let config_path =
|
||||
std::env::var("CONFIG_PATH").unwrap_or_else(|_| "config.toml".to_string());
|
||||
let config = fs::read_to_string(config_path)
|
||||
.expect("Config file not found. Add 'config.toml' to this directory");
|
||||
config.parse::<Value>().expect(CONFIG_ERR)
|
||||
};
|
||||
pub static ref REACTION_EMOTES: HashMap<String, EmojiId> = {
|
||||
let mut m = HashMap::new();
|
||||
let emotes = CONFIG.get("emotes").expect(CONFIG_ERR);
|
||||
|
||||
for v in emotes.as_array().expect(CONFIG_ERR) {
|
||||
let name = v[0].as_str().expect(CONFIG_ERR).to_string();
|
||||
let id = EmojiId::new(v[1].as_integer().expect(CONFIG_ERR).clone() as u64);
|
||||
m.insert(name, id);
|
||||
}
|
||||
m
|
||||
};
|
||||
static ref RESPONSES: HashMap<String, String> = {
|
||||
let mut m = HashMap::new();
|
||||
|
||||
let emotes = CONFIG.get("responses").expect(CONFIG_ERR);
|
||||
|
||||
for v in emotes.as_array().expect(CONFIG_ERR) {
|
||||
let trigger = v[0].as_str().expect(CONFIG_ERR).to_string();
|
||||
let response = v[1].as_str().expect(CONFIG_ERR).to_string();
|
||||
m.insert(trigger, response);
|
||||
}
|
||||
m
|
||||
};
|
||||
}
|
||||
use crate::{Config, LastMessageInChannel};
|
||||
|
||||
#[hook]
|
||||
pub async fn normal_message(ctx: &Context, msg: &Message) {
|
||||
|
|
@ -54,9 +15,10 @@ pub async fn normal_message(ctx: &Context, msg: &Message) {
|
|||
.expect("LastMessageInChannel not found");
|
||||
map.insert(msg.channel_id.clone(), msg.content.clone());
|
||||
|
||||
lazy_static! {
|
||||
static ref TOM_REGEX: Regex = Regex::new(r"(?<=^|\D)(\d{6})(?=\D|$)").unwrap();
|
||||
}
|
||||
let config = data.get::<Config>().unwrap();
|
||||
|
||||
static TOM_REGEX: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(r"(?<=^|\D)(\d{6})(?=\D|$)").unwrap());
|
||||
|
||||
let is_nsfw = msg
|
||||
.channel_id
|
||||
|
|
@ -75,13 +37,13 @@ pub async fn normal_message(ctx: &Context, msg: &Message) {
|
|||
}
|
||||
}
|
||||
|
||||
for (trigger, answer) in RESPONSES.iter() {
|
||||
for (trigger, answer) in config.responses.iter() {
|
||||
if msg.content.to_lowercase() == *trigger {
|
||||
reply(answer, &msg, &ctx).await;
|
||||
}
|
||||
}
|
||||
|
||||
for (name, id) in REACTION_EMOTES.iter() {
|
||||
for (name, id) in config.emotes.iter() {
|
||||
if msg.content.to_lowercase().contains(name) {
|
||||
if let Err(why) = msg
|
||||
.react(
|
||||
|
|
|
|||
32
src/main.rs
32
src/main.rs
|
|
@ -5,6 +5,7 @@ use std::collections::{HashMap, HashSet};
|
|||
use std::fs;
|
||||
|
||||
use serenity::all::standard::Configuration;
|
||||
use serenity::all::EmojiId;
|
||||
use serenity::client::Context;
|
||||
use serenity::framework::StandardFramework;
|
||||
use serenity::http::Http;
|
||||
|
|
@ -17,12 +18,29 @@ use crate::general::normal_message;
|
|||
mod commands;
|
||||
mod general;
|
||||
|
||||
pub struct LastMessageInChannel;
|
||||
#[derive(serde::Deserialize)]
|
||||
struct ConfigFile {
|
||||
emotes: Vec<(String, EmojiId)>,
|
||||
responses: Vec<(String, String)>,
|
||||
xp: Vec<String>,
|
||||
}
|
||||
|
||||
struct Config {
|
||||
emotes: HashMap<String, EmojiId>,
|
||||
responses: HashMap<String, String>,
|
||||
xp: Vec<String>,
|
||||
}
|
||||
|
||||
struct LastMessageInChannel;
|
||||
|
||||
impl TypeMapKey for LastMessageInChannel {
|
||||
type Value = HashMap<ChannelId, String>;
|
||||
}
|
||||
|
||||
impl TypeMapKey for Config {
|
||||
type Value = Self;
|
||||
}
|
||||
|
||||
struct Handler;
|
||||
|
||||
#[async_trait]
|
||||
|
|
@ -34,6 +52,17 @@ impl EventHandler for Handler {
|
|||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let config_path = std::env::var("CONFIG_PATH").unwrap_or_else(|_| "config.toml".to_string());
|
||||
let config = fs::read_to_string(config_path)
|
||||
.expect("Config file not found. Add 'config.toml' to this directory");
|
||||
let config = toml::from_str::<ConfigFile>(&config).expect("invalid config file");
|
||||
|
||||
let config = Config {
|
||||
emotes: config.emotes.into_iter().collect(),
|
||||
responses: config.responses.into_iter().collect(),
|
||||
xp: config.xp,
|
||||
};
|
||||
|
||||
let token_path = std::env::var("BOT_TOKEN_PATH").unwrap_or_else(|_| "bot_token".to_string());
|
||||
let token = fs::read_to_string(token_path).expect("Expected bot token in file 'bot_token'");
|
||||
let token = token.trim();
|
||||
|
|
@ -80,6 +109,7 @@ async fn main() {
|
|||
{
|
||||
let mut data = client.data.write().await;
|
||||
data.insert::<LastMessageInChannel>(HashMap::default());
|
||||
data.insert::<Config>(config);
|
||||
}
|
||||
|
||||
if let Err(why) = client.start().await {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue