Skip to main content

Handling events

Most players allow you to override methods to handle events inside of the player. For example, the QueuedLavalinkPlayer class allows you to override the OnTrackStartedAsync method to handle the TrackStarted event.

public sealed class CustomPlayer : QueuedLavalinkPlayer
{
private readonly ITextChannel _textChannel;

public CustomPlayer(IPlayerProperties<CustomPlayer, CustomPlayerOptions> properties)
: base(properties)
{
_textChannel = properties.Options.Value.TextChannel;
}

protected override async ValueTask NotifyTrackStartedAsync(ITrackQueueItem track, CancellationToken cancellationToken = default)
{
await base
.NotifyTrackStartedAsync(track, cancellationToken)
.ConfigureAwait(false);

// send a message to the text channel
await _textChannel
.SendMessageAsync($"Now playing: {track.Track.Title}")
.ConfigureAwait(false);
}
}

Player listeners

Lavalink4NET also provides various interfaces you can implement to handle additional events in the player. For example, the IInactivityPlayerListener interface allows you to handle events when the player is active, inactive, or tracked. See the inactivity tracking guide for more information.