moderatior
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// TODO pruning?
use kv::*;
use poise::serenity_prelude::*;
use std::sync::LazyLock;

fn cfg() -> kv::Config {
    kv::Config {
        path: "./db1".into(),
        temporary: false,
        use_compression: true,
        flush_every_ms: None,
        cache_capacity: None,
        segment_size: None,
    }
}
static DB: LazyLock<Store> = LazyLock::new(|| Store::new(cfg()).unwrap());
static BU: LazyLock<Bucket<Integer, Bincode<(String, Vec<String>, u64)>>> =
    LazyLock::new(|| DB.bucket(None).unwrap());

pub fn set(k: u64, v: (String, Vec<String>, u64)) {
    BU.set(&k.into(), &Bincode(v)).unwrap();
}
pub fn get(k: u64) -> Option<(String, Vec<String>, u64)> {
    BU.get(&k.into()).unwrap().map(|x| x.0)
}
pub fn keys() -> impl Iterator<Item = MessageId> {
    BU.iter()
        .filter_map(Result::ok)
        .filter_map(|x| x.key::<u64>().ok())
        .map(|x| MessageId::new(x))
}
pub fn values() -> impl Iterator<Item = (String, Vec<String>, UserId)> {
    BU.iter()
        .filter_map(Result::ok)
        .filter_map(|x| x.value::<Bincode<(String, Vec<String>, u64)>>().ok())
        .map(|Bincode((y, z, α))| (y, z, UserId::new(α)))
}
pub fn iter() -> impl Iterator<Item = (MessageId, (String, Vec<String>, UserId))> {
    BU.iter()
        .filter_map(Result::ok)
        .filter_map(|x| {
            x.key::<u64>()
                .and_then(|y| {
                    x.value::<Bincode<(String, Vec<String>, u64)>>()
                        .map(|x| (y, x))
                })
                .ok()
        })
        .map(|(x, Bincode((y, z, α)))| (MessageId::new(x), (y, z, UserId::new(α))))
}
pub fn sz() -> f32 {
    DB.size_on_disk().unwrap() as f32 / (1 << 20) as f32
}
pub fn set_m(m: poise::serenity_prelude::Message) {
    set(
        m.id.get(),
        (
            m.content,
            m.attachments
                .into_iter()
                .map(|x| x.url)
                .collect::<Vec<String>>(),
            m.author.id.into(),
        ),
    )
}