smol bot
Diffstat (limited to 'src/bot/ownership.rs')
| -rw-r--r-- | src/bot/ownership.rs | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/src/bot/ownership.rs b/src/bot/ownership.rs index a226bdc..d7aacc1 100644 --- a/src/bot/ownership.rs +++ b/src/bot/ownership.rs @@ -1,21 +1,33 @@ -use std::{ - collections::HashMap, - sync::{LazyLock, Mutex}, -}; +use serenity::all::MessageId; +use std::{collections::HashMap, sync::LazyLock}; +use tokio::sync::Mutex; static MAP: LazyLock<Mutex<HashMap<u64, (String, u64)>>> = LazyLock::new(|| { Mutex::new(serde_json::from_slice(&std::fs::read("repo/ownership.json").unwrap()).unwrap()) }); -pub fn insert(k: u64, v: (String, u64)) { - MAP.lock().unwrap().insert(k, v); - std::fs::write("repo/ownership.json", serde_json::to_string(&*MAP).unwrap()).unwrap(); +pub async fn insert(k: u64, v: (String, u64)) { + let mut lock = MAP.lock().await; + lock.insert(k, v); + std::fs::write( + "repo/ownership.json", + serde_json::to_string(&*lock).unwrap(), + ) + .unwrap(); } -pub fn get(k: u64) -> (String, u64) { - MAP.lock().unwrap()[&k].clone() +pub async fn get(k: u64) -> (String, u64) { + MAP.lock().await[&k].clone() } -pub fn erase(k: u64) -> Option<(String, u64)> { - let x = MAP.lock().unwrap().remove(&k); - std::fs::write("repo/ownership.json", serde_json::to_string(&*MAP).unwrap()).unwrap(); +pub async fn erase(k: u64) -> Option<String> { + let mut lock = MAP.lock().await; + let x = lock.remove(&k).map(|(x, _)| x); + std::fs::write( + "repo/ownership.json", + serde_json::to_string(&*lock).unwrap(), + ) + .unwrap(); x } +pub async fn whos(x: impl Into<MessageId>) -> String { + get(x.into().get()).await.0 +} |