botcat-capoo 1.0.3

A NapCat based QQ bot implemented in Rust
Documentation
use serde_json::{Value, json};
use tracing::{error, info};

use crate::{api::napcat::napcat_request, config::AppConfig};

async fn poke(config: &AppConfig, user_id: u64, target_id: Option<u64>, group_id: Option<u64>) {
    let mut body = json!({
        "user_id": user_id,
    });

    if let Some(gid) = group_id {
        body["group_id"] = Value::Number(gid.into());
    }

    if let Some(tid) = target_id {
        body["target_id"] = Value::Number(tid.into());
    }

    let res = napcat_request(config, "/send_poke", &body).await;
    match res {
        Ok(response) => {
            info!("Poke sent successfully: {response:?}");
        }
        Err(err) => {
            error!("Error sending poke: {err:?}");
        }
    }
}

pub async fn private_poke(config: &AppConfig, with: u64, to_self: bool) {
    let target_id = if to_self {
        Some(config.botcat_capoo.id)
    } else {
        None
    };
    poke(config, with, target_id, None).await;
}

pub async fn group_poke(config: &AppConfig, group_id: u64, to: u64) {
    poke(config, to, None, Some(group_id)).await;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    #[ignore = "Requires an API server in internal network"]
    async fn test_private_poke() {
        let config = AppConfig::init().expect("Failed to load config");
        println!("Loaded config");
        private_poke(&config, 46595749, false).await;
    }

    #[tokio::test]
    #[ignore = "Requires an API server in internal network"]
    async fn test_private_self_poke() {
        let config = AppConfig::init().expect("Failed to load config");
        println!("Loaded config");
        private_poke(&config, 46595749, true).await;
    }

    #[tokio::test]
    #[ignore = "Requires an API server in internal network"]
    async fn test_group_poke() {
        let config = AppConfig::init().expect("Failed to load config");
        println!("Loaded config");
        group_poke(&config, 738943282, 46595749).await;
    }

    #[tokio::test]
    #[ignore = "Requires an API server in internal network"]
    async fn test_group_self_poke() {
        let config = AppConfig::init().expect("Failed to load config");
        println!("Loaded config");
        group_poke(&config, 738943282, config.botcat_capoo.id).await;
    }
}