karma: karmaweb command + web-board link on top lists, via configurable karma_url
All checks were successful
ci / check (push) Successful in 47s

This commit is contained in:
Jean Chevronnet 2026-07-30 00:39:06 +00:00
parent 739fc8ad94
commit 7f6327270b
No known key found for this signature in database
GPG key ID: 439666D63A9477E4
6 changed files with 92 additions and 7 deletions

View file

@ -28,7 +28,11 @@ impl Bot {
pub fn new(config: Config, conn: Connection) -> Bot {
let current_nick = config.nick.clone();
let mods = modules::all(&config.name, crate::i18n::Locale::from_code(&config.locale));
let mods = modules::all(
&config.name,
crate::i18n::Locale::from_code(&config.locale),
(!config.karma_url.is_empty()).then(|| config.karma_url.clone()),
);
let mut routes: HashMap<&'static str, usize> = HashMap::new();
for (i, m) in mods.iter().enumerate() {
for spec in m.commands() {

View file

@ -20,6 +20,11 @@ const COMMANDS: &[CommandSpec] = &[
usage: "karmabottom",
about: "the lowest-karma things",
},
CommandSpec {
name: "karmaweb",
usage: "karmaweb",
about: "link to the web karma board",
},
];
const MAX_THING_LEN: usize = 32;
@ -31,10 +36,11 @@ const ZWSP: char = '\u{200B}';
pub struct Karma {
store: Store,
locale: Locale,
url: Option<String>,
}
impl Karma {
pub fn new(network: &str, locale: Locale) -> Karma {
pub fn new(network: &str, locale: Locale, url: Option<String>) -> Karma {
let dir = std::env::var("RUSTBOT_DATA_DIR").unwrap_or_else(|_| ".".to_string());
let safe: String = network
.chars()
@ -44,6 +50,7 @@ impl Karma {
PathBuf::from(dir).join(format!("karma.{safe}.json")),
locale,
)
.with_url(url)
}
pub fn with_path(path: PathBuf) -> Karma {
@ -54,9 +61,15 @@ impl Karma {
Karma {
store: Store::load(path),
locale,
url: None,
}
}
pub fn with_url(mut self, url: Option<String>) -> Karma {
self.url = url;
self
}
pub fn get(&self, thing: &str) -> i64 {
self.store.entry(thing).map(|e| e.count).unwrap_or(0)
}
@ -112,7 +125,31 @@ impl Karma {
(Locale::Fr, false) => "top karma",
(Locale::Fr, true) => "pire karma",
};
format!("{label}: {}", items.join(", "))
let mut out = format!("{label}: {}", items.join(", "));
if let Some(u) = &self.url {
let more = match self.locale {
Locale::En => "full board",
Locale::Es => "tablero completo",
Locale::Fr => "tableau complet",
};
out.push_str(&format!(" · {more}: {u}"));
}
out
}
fn web_link(&self) -> String {
match &self.url {
Some(u) => match self.locale {
Locale::En => format!("karma board: {u}"),
Locale::Es => format!("tablero de karma: {u}"),
Locale::Fr => format!("tableau de karma : {u}"),
},
None => match self.locale {
Locale::En => "no web board configured".to_string(),
Locale::Es => "no hay tablero web configurado".to_string(),
Locale::Fr => "pas de tableau web configuré".to_string(),
},
}
}
}
@ -132,6 +169,7 @@ impl Module for Karma {
None => self.leaderboard(false),
},
"karmabottom" => self.leaderboard(true),
"karmaweb" => self.web_link(),
_ => return vec![],
};
vec![Action::reply(reply)]

View file

@ -13,12 +13,12 @@ pub mod wiki;
use super::module::Module;
use crate::i18n::Locale;
pub fn all(network: &str, locale: Locale) -> Vec<Box<dyn Module>> {
pub fn all(network: &str, locale: Locale, karma_url: Option<String>) -> Vec<Box<dyn Module>> {
vec![
Box::new(builtins::Builtins),
Box::new(dice::Dice::new()),
Box::new(hash::Hash),
Box::new(karma::Karma::new(network, locale)),
Box::new(karma::Karma::new(network, locale, karma_url)),
Box::new(crypto::Crypto),
Box::new(define::Define),
Box::new(down::Down),

View file

@ -19,6 +19,7 @@ pub struct Config {
pub sasl_user: Option<String>,
pub sasl_pass: Option<String>,
pub locale: String,
pub karma_url: String,
}
impl Default for Config {
@ -38,6 +39,7 @@ impl Default for Config {
sasl_user: None,
sasl_pass: None,
locale: "en".to_string(),
karma_url: String::new(),
}
}
}
@ -163,6 +165,7 @@ fn apply_kv(c: &mut Config, key: &str, value: &str, where_: &str) {
"sasl_user" => c.sasl_user = (!value.is_empty()).then(|| value.to_string()),
"sasl_pass" => c.sasl_pass = (!value.is_empty()).then(|| value.to_string()),
"locale" | "lang" => c.locale = value.to_string(),
"karma_url" | "web_url" => c.karma_url = value.to_string(),
other => log(&format!("{where_}: unknown key '{other}'")),
}
}
@ -223,6 +226,9 @@ fn apply_env(c: &mut Config) {
if let Ok(v) = std::env::var("IRC_LOCALE") {
c.locale = v;
}
if let Ok(v) = std::env::var("IRC_KARMA_URL") {
c.karma_url = v;
}
}
fn parse_bool(value: &str) -> bool {