serenity-rs/songbird

Get songbird instance from external api call

beig opened this issue · 1 comments

beig commented

I want to play sounds from an external API call (Soundboard)
But I can't get the songbird instance
I tried storing it in a lazy_static but since it doesn't implement the Send trait I cant?

The bot is only on one server, so any hack/workaround is working for me

Any luck or is it not possible?

It should be possible to store the Arc<Songbird> instance within lazy_static. I use this to have it accessible in a different thread than where I initialize it:

lazy_static!{
    static ref SONG: OnceCell<Arc<Songbird>> = OnceCell::new();
}

I use a OnceCell here as it allows us to set the value once after it has been initialized. We do not require setting the variable more than once after its initialization, so this is perfect.

If you are using serenity, and want to know how to set this after initializing the client, its as follows:

let sb = client.data.read().await
        .get::<SongbirdKey>()
        .expect("Failed to get or initialize Songbird")
        .clone();

SONG.set(sb).expect("Error setting Songbird (SONG)");

Then, to use it anywhere in your code, all you need to do is:

let sb = SONG.get().expect("Songbird not found!").clone();