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
34
35
36
37
38
39
40
41
42
43
44
use serenity::all::MessageId;
use std::collections::HashMap;
use tokio::sync::MutexGuard;

pub struct Ownership {
    pub map: HashMap<u64, (String, u64)>,
    path: &'static str,
}
impl Ownership {
    pub fn new(id: u64) -> Self {
        let path = format!("repos/{id:x}/ownership.json").leak();
        Self {
            map: serde_json::from_slice(&std::fs::read(&path).unwrap_or_default())
                .unwrap_or_default(),
            path,
        }
    }

    pub fn insert(&mut self, k: u64, v: (String, u64)) {
        self.map.insert(k, v);
        self.flush();
    }
    fn flush(&self) {
        std::fs::write(self.path, serde_json::to_string_pretty(&self.map).unwrap()).unwrap();
    }
    pub fn get(&self, k: u64) -> &(String, u64) {
        self.map.get(&k).unwrap()
    }
    pub fn erase(&mut self, k: impl Into<u64>) -> Option<String> {
        let x = self.map.remove(&k.into()).map(|(x, _)| x);
        self.flush();
        x
    }

    pub fn whos(&self, x: impl Into<MessageId>) -> &str {
        &self.get(x.into().get()).0
    }
}
pub async fn whos(g: u64, x: impl Into<u64>) -> String {
    super::repos::REPOS[&g].own().await.get(x.into()).0.clone()
}
pub async fn get(g: impl Into<u64>) -> MutexGuard<'static, Ownership> {
    super::repos::REPOS[&g.into()].own().await
}