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

feat: add async context api for hooks #1893

Merged
merged 1 commit into from
Mar 29, 2024
Merged

feat: add async context api for hooks #1893

merged 1 commit into from
Mar 29, 2024

Conversation

twlite
Copy link
Collaborator

@twlite twlite commented Mar 28, 2024

Alternate api for hooks

Previous methods still work and this mode is opt-in. In order to enable this api, the command handler needs to provide context to HooksContextProvider and hooks must be called without any arguments. The benefit of this approach is you don't need to care about passing guild id every time you want to access something inside the command. Hooks will resolve correct data even if the same command is executed multiple times concurrently.

Note: This only works inside the call stack created by withContext function.

const { withContext } = require('discord-player');

client.on('interactionCreate', async (interaction) => {
    if (!interaction.isCommand()) return;

    // assuming the following code is your target command to be executed
    const command = getCommandSomehow(); // shape is roughly { execute: Function }

    const context = { guild: interaction.guild };

    // provide context and execute the command
    await withContext(context, () => command.execute(interaction));
});

Then discord-player hooks can be used without providing guild id.

const { useQueue } = require('discord-player');

// your command
export async function execute(interaction) {
    // here we are not passing any arguments, discord-player will automatically resolve correct queue based on the context
    const queue = useQueue();
}

@twlite twlite added the area:main Related to discord-player label Mar 28, 2024
Copy link

vercel bot commented Mar 28, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
discord-player-website ✅ Ready (Inspect) Visit Preview Mar 28, 2024 8:05pm

@twlite
Copy link
Collaborator Author

twlite commented Mar 28, 2024

This is now available in @dev for testing

@twlite twlite merged commit a3d3d5b into master Mar 29, 2024
4 checks passed
@twlite twlite deleted the async-hooks branch March 29, 2024 04:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:main Related to discord-player
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant