This commit is contained in:
nora 2025-11-01 15:20:59 +01:00
parent 33d1738799
commit bb2c954418
7 changed files with 806 additions and 489 deletions

1140
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -11,9 +11,10 @@ tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
uwuify = "^0.2"
async-trait = "0.1.50"
lazy_static = "1.4.0"
toml = "0.8.8"
fancy-regex = "0.13.0"
rand = "0.8.3"
toml = "0.9.8"
fancy-regex = "0.16.2"
rand = "0.9.2"
serde = { version = "1.0.228", features = ["derive"] }
[dependencies.serenity]
version = "0.12.0"

View file

@ -1,30 +0,0 @@
FROM rust as build
RUN rustup toolchain install nightly
RUN rustup default nightly
RUN rustup target add x86_64-unknown-linux-musl
RUN apt-get update
RUN apt-get install musl-tools -y
WORKDIR /app
COPY Cargo.toml Cargo.lock ./
RUN mkdir src
RUN echo "fn main() {}" > src/main.rs
RUN cargo build --release --target x86_64-unknown-linux-musl
COPY src ./src
# now rebuild with the proper main
RUN touch src/main.rs
RUN cargo build --release --target x86_64-unknown-linux-musl
### RUN
FROM scratch
WORKDIR /app
COPY --from=build /app/target/x86_64-unknown-linux-musl/release/widertom widertom
CMD ["/app/widertom"]

4
run.sh
View file

@ -1,4 +0,0 @@
#!/usr/bin/env bash
docker run -d -v "$(pwd)/config.toml:/app/config.toml" -v "$(pwd)/bot_token:/app/bot_token" \
-e CONFIG_PATH=/app/config.toml -e BOT_TOKEN_PATH=/app/bot_token --name widertom widertom:1.0

View file

@ -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(())
}

View file

@ -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(

View file

@ -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 {