You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2b-rs/src/utils/context_data.rs

52 lines
1.2 KiB
Rust

use std::collections::HashMap;
use std::sync::Arc;
use serenity::model::id::GuildId;
use serenity::prelude::TypeMapKey;
use tokio::sync::Mutex;
use crate::providers::music::queue::MusicQueue;
use crate::providers::music::spotify::SpotifyApi;
use database::Database;
use serenity::client::Context;
pub struct Store;
pub struct StoreData {
pub minecraft_data_api: minecraft_data_rs::api::Api,
pub music_queues: HashMap<GuildId, Arc<Mutex<MusicQueue>>>,
pub spotify_api: SpotifyApi,
}
impl StoreData {
pub fn new() -> StoreData {
Self {
minecraft_data_api: minecraft_data_rs::api::Api::new(
minecraft_data_rs::api::versions::latest_stable().unwrap(),
),
music_queues: HashMap::new(),
spotify_api: SpotifyApi::new(),
}
}
}
impl TypeMapKey for Store {
type Value = StoreData;
}
pub struct DatabaseContainer;
impl TypeMapKey for DatabaseContainer {
type Value = Database;
}
/// Returns a copy of the database
pub async fn get_database_from_context(ctx: &Context) -> Database {
let data = ctx.data.read().await;
let database = data
.get::<DatabaseContainer>()
.expect("Invalid Context setup: Missing database");
database.clone()
}