smol bot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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 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 async fn get(k: u64) -> (String, u64) {
    MAP.lock().await[&k].clone()
}
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
}