Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get songbird instance from external api call #197

Open
beig opened this issue Jul 27, 2023 · 1 comment
Open

Get songbird instance from external api call #197

beig opened this issue Jul 27, 2023 · 1 comment

Comments

@beig
Copy link

beig commented Jul 27, 2023

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?

@Pranav-Badrinathan
Copy link

Pranav-Badrinathan commented Mar 3, 2024

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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants