angelobreuer / Lavalink4NET

Lavalink4NET is a Lavalink wrapper with node clustering, caching and custom players for .NET with support for Discord.Net, DSharpPlus, Remora, and NetCord.
https://lavalink4net.angelobreuer.de/
MIT License
150 stars 27 forks source link

Inactivity tracking notifications not called #136

Closed BlyZeYT closed 8 months ago

BlyZeYT commented 8 months ago

Describe the bug I have this Player class that implements the Player listener but it doesnt fire the Notification events.

public sealed partial class MyPlayer : QueuedLavalinkPlayer, IInactivityPlayerListener
{
    public MyPlayerOptions Options { get; }

    public MyPlayer(IPlayerProperties<MyPlayer, MyPlayerOptions> properties) : base(properties)
    {
        Options = properties.Options.Value;
    }

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

        if (queueItem.Track is null) return;

        await Options.TextChannel.SendEmbedAsync(MobyEmbeds.GetNowPlaying(queueItem.Track));
    }

    public ValueTask NotifyPlayerActiveAsync(PlayerTrackingState trackingState, CancellationToken cancellationToken = default)
        => ValueTask.CompletedTask;

    public async ValueTask NotifyPlayerInactiveAsync(PlayerTrackingState trackingState, CancellationToken cancellationToken = default)
    {
        EnsureNotDestroyed();

        await DisconnectAsync(cancellationToken);
    }

    public ValueTask NotifyPlayerTrackedAsync(PlayerTrackingState trackingState, CancellationToken cancellationToken = default)
        => ValueTask.CompletedTask;
}

This is in my startup:

...
.AddLavalink()
.ConfigureLavalink(options =>
{
    options.ReadyTimeout = TimeSpan.FromSeconds(10);
    options.ResumptionOptions = new(TimeSpan.FromSeconds(30));
    options.Passphrase = config.GetLavalinkPassword();
})
.AddInactivityTracker<UsersInactivityTracker>()
.AddInactivityTracker<IdleInactivityTracker>()
.Configure<UsersInactivityTrackerOptions>(options => options.Timeout = TimeSpan.FromSeconds(5))
.Configure<IdleInactivityTrackerOptions>(options => options.Timeout = TimeSpan.FromSeconds(5))
...

Expected behavior The notifications getting called

(please complete the following information):

angelobreuer commented 8 months ago

Hello, @BlyZeYT ,

thank you for your report! By just flying over it quickly, this is my first thing you could try out:

It looks like you are not using Microsoft.Extensions.Hosting: The inactivity service internally queues up events and needs to be started (as similar how the audio service needs to be started). Could you try running .StartAsync on IInactivityTrackingService and check if the events/notifications are dispatched to the player?

Additionally, I do not see a call to .AddInactivityTracking() in your service pipeline. It might be that you are not even registering the inactivity service at all.

BlyZeYT commented 8 months ago

With .StartAsync() it works. I didn't knew I had to start the inactivity tracker as well :). Thanks for helping so fast 😄