diff --git a/youmubot-osu/src/discord/mod.rs b/youmubot-osu/src/discord/mod.rs index 8c7aeaf..b50d559 100644 --- a/youmubot-osu/src/discord/mod.rs +++ b/youmubot-osu/src/discord/mod.rs @@ -53,7 +53,7 @@ impl TypeMapKey for OsuClient { /// - Commands on the "osu" prefix /// - Hooks. Hooks are completely opt-in. /// -pub fn setup( +pub async fn setup( _path: &std::path::Path, data: &mut TypeMap, announcers: &mut AnnouncerHandler, @@ -71,13 +71,23 @@ pub fn setup( // API client let http_client = data.get::().unwrap().clone(); - let mk_osu_client = || { - Arc::new(OsuHttpClient::new( - std::env::var("OSU_API_KEY").expect("Please set OSU_API_KEY as osu! api key."), - http_client.clone(), - )) + let mk_osu_client = || async { + Arc::new( + OsuHttpClient::new( + std::env::var("OSU_API_KEY").expect("Please set OSU_API_KEY as osu! api key."), + http_client.clone(), + std::env::var("OSU_API_CLIENT_ID") + .expect("Please set OSU_API_CLIENT_ID as osu! api v2 client ID.") + .parse() + .expect("client_id should be u64"), + std::env::var("OSU_API_CLIENT_SECRET") + .expect("Please set OSU_API_CLIENT_SECRET as osu! api v2 client secret."), + ) + .await + .expect("osu! should be initialized"), + ) }; - let osu_client = mk_osu_client(); + let osu_client = mk_osu_client().await; data.insert::(osu_client.clone()); data.insert::(oppai_cache::BeatmapCache::new( http_client.clone(), @@ -89,7 +99,7 @@ pub fn setup( )); // Announcer - let osu_client = mk_osu_client(); + let osu_client = mk_osu_client().await; announcers.add( announcer::ANNOUNCER_KEY, announcer::Announcer::new(osu_client), diff --git a/youmubot-osu/src/lib.rs b/youmubot-osu/src/lib.rs index 5c4a903..eb451b0 100644 --- a/youmubot-osu/src/lib.rs +++ b/youmubot-osu/src/lib.rs @@ -19,6 +19,8 @@ const REQUESTS_PER_MINUTE: usize = 100; pub struct Client { client: Ratelimit, key: String, + + rosu: rosu_v2::Osu, } fn vec_try_into>(v: Vec) -> Result, T::Error> { @@ -33,13 +35,23 @@ fn vec_try_into>(v: Vec) -> Result, T:: impl Client { /// Create a new client from the given API key. - pub fn new(key: String, client: HTTPClient) -> Client { + pub async fn new( + key: String, + client: HTTPClient, + client_id: u64, + client_secret: impl Into, + ) -> Result { let client = Ratelimit::new( client, REQUESTS_PER_MINUTE, std::time::Duration::from_secs(60), ); - Client { client, key } + let rosu = rosu_v2::OsuBuilder::new() + .client_id(client_id) + .client_secret(client_secret) + .build() + .await?; + Ok(Client { client, key, rosu }) } pub(crate) async fn build_request(&self, url: &str) -> Result { diff --git a/youmubot/src/main.rs b/youmubot/src/main.rs index e6ff1e1..c488691 100644 --- a/youmubot/src/main.rs +++ b/youmubot/src/main.rs @@ -135,6 +135,7 @@ async fn main() { // osu! #[cfg(feature = "osu")] youmubot_osu::discord::setup(&db_path, &mut data, &mut announcers) + .await .expect("osu! is initialized"); // codeforces #[cfg(feature = "codeforces")]