mirror of
https://github.com/Myxelium/Lunaris2.0.git
synced 2026-04-09 06:09:39 +00:00
Fix deadlock and auto leave voice channel after 3 min (#4)
* Fix for thread being busy handling discord commands blocking audioplayer to timeout * Auto leave if alone in voice channel after 3 min Co-authored-by: Myx <info@azaaxin.com>
This commit is contained in:
@@ -5,6 +5,7 @@ using Lavalink4NET;
|
||||
using Lavalink4NET.Events.Players;
|
||||
using Lavalink4NET.Players.Queued;
|
||||
using Lavalink4NET.Rest.Entities.Tracks;
|
||||
using System.Threading;
|
||||
|
||||
namespace Lunaris2.Handler.MusicPlayer.PlayCommand;
|
||||
|
||||
@@ -37,7 +38,12 @@ public class PlayHandler : IRequestHandler<PlayCommand>
|
||||
await _musicEmbed.NowPlayingEmbed(track, _context, _client);
|
||||
}
|
||||
|
||||
public async Task Handle(PlayCommand command, CancellationToken cancellationToken)
|
||||
public Task Handle(PlayCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
new Thread(PlayMusic).Start();
|
||||
return Task.CompletedTask;
|
||||
|
||||
async void PlayMusic()
|
||||
{
|
||||
await _audioService.StartAsync(cancellationToken);
|
||||
var context = command.Message;
|
||||
@@ -45,34 +51,25 @@ public class PlayHandler : IRequestHandler<PlayCommand>
|
||||
|
||||
var searchQuery = context.GetOptionValueByName(Option.Input);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(searchQuery)) {
|
||||
if (string.IsNullOrWhiteSpace(searchQuery))
|
||||
{
|
||||
await context.SendMessageAsync("Please provide search terms.", _client);
|
||||
return;
|
||||
}
|
||||
|
||||
var player = await _audioService.GetPlayerAsync(_client, context, connectToVoiceChannel: true);
|
||||
|
||||
if (player is null)
|
||||
return;
|
||||
if (player is null) return;
|
||||
|
||||
var trackLoadOptions = new TrackLoadOptions
|
||||
{
|
||||
SearchMode = TrackSearchMode.YouTube,
|
||||
};
|
||||
var trackLoadOptions = new TrackLoadOptions { SearchMode = TrackSearchMode.YouTube, };
|
||||
|
||||
var track = await _audioService.Tracks
|
||||
.LoadTrackAsync(
|
||||
searchQuery,
|
||||
trackLoadOptions,
|
||||
cancellationToken: cancellationToken);
|
||||
var track = await _audioService.Tracks.LoadTrackAsync(searchQuery, trackLoadOptions, cancellationToken: cancellationToken);
|
||||
|
||||
if (track is null)
|
||||
await context.SendMessageAsync("😖 No results.", _client);
|
||||
if (track is null) await context.SendMessageAsync("😖 No results.", _client);
|
||||
|
||||
if (player.CurrentTrack is null)
|
||||
{
|
||||
await player
|
||||
.PlayAsync(track, cancellationToken: cancellationToken)
|
||||
await player.PlayAsync(track, cancellationToken: cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
await _musicEmbed.NowPlayingEmbed(track, context, _client);
|
||||
@@ -91,4 +88,5 @@ public class PlayHandler : IRequestHandler<PlayCommand>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ using Lunaris2.Handler.ChatCommand;
|
||||
using Lavalink4NET.Extensions;
|
||||
using Lunaris2.Handler.MusicPlayer;
|
||||
using Lunaris2.Notification;
|
||||
using Lunaris2.Service;
|
||||
using Lunaris2.SlashCommand;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -41,10 +42,7 @@ public class Program
|
||||
.Build();
|
||||
|
||||
services
|
||||
.AddSingleton(client)
|
||||
.AddMediatR(mediatRServiceConfiguration => mediatRServiceConfiguration.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()))
|
||||
.AddSingleton<DiscordEventListener>()
|
||||
.AddSingleton(service => new InteractionService(service.GetRequiredService<DiscordSocketClient>()))
|
||||
.AddLavalink()
|
||||
.ConfigureLavalink(options =>
|
||||
{
|
||||
@@ -58,6 +56,10 @@ public class Program
|
||||
.AddSingleton<LavaNode>()
|
||||
.AddSingleton<MusicEmbed>()
|
||||
.AddSingleton<ChatSettings>()
|
||||
.AddSingleton(client)
|
||||
.AddSingleton<DiscordEventListener>()
|
||||
.AddSingleton<VoiceChannelMonitorService>()
|
||||
.AddSingleton(service => new InteractionService(service.GetRequiredService<DiscordSocketClient>()))
|
||||
.Configure<ChatSettings>(configuration.GetSection("LLM"));
|
||||
|
||||
client.Ready += () => Client_Ready(client);
|
||||
@@ -86,6 +88,8 @@ public class Program
|
||||
private static Task Client_Ready(DiscordSocketClient client)
|
||||
{
|
||||
client.RegisterCommands();
|
||||
|
||||
new VoiceChannelMonitorService(client).StartMonitoring();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
59
Bot/Service/VoiceChannelMonitorService.cs
Normal file
59
Bot/Service/VoiceChannelMonitorService.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace Lunaris2.Service;
|
||||
|
||||
public class VoiceChannelMonitorService
|
||||
{
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly Dictionary<ulong, Timer> _timers = new();
|
||||
|
||||
public VoiceChannelMonitorService(DiscordSocketClient client)
|
||||
{
|
||||
_client = client;
|
||||
}
|
||||
|
||||
public void StartMonitoring()
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
await CheckVoiceChannels();
|
||||
await Task.Delay(TimeSpan.FromMinutes(1));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async Task CheckVoiceChannels()
|
||||
{
|
||||
foreach (var guild in _client.Guilds)
|
||||
{
|
||||
var voiceChannel = guild.VoiceChannels.FirstOrDefault(vc => vc.ConnectedUsers.Count == 1);
|
||||
if (voiceChannel != null)
|
||||
{
|
||||
if (!_timers.ContainsKey(voiceChannel.Id))
|
||||
{
|
||||
_timers[voiceChannel.Id] = new Timer(async _ => await LeaveChannel(voiceChannel), null, TimeSpan.FromMinutes(3), Timeout.InfiniteTimeSpan);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (voiceChannel == null || !_timers.ContainsKey(voiceChannel.Id))
|
||||
continue;
|
||||
|
||||
await _timers[voiceChannel.Id].DisposeAsync();
|
||||
_timers.Remove(voiceChannel.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task LeaveChannel(SocketVoiceChannel voiceChannel)
|
||||
{
|
||||
if (voiceChannel.ConnectedUsers.Count == 1 && voiceChannel.Users.Any(u => u.Id == _client.CurrentUser.Id))
|
||||
{
|
||||
await voiceChannel.DisconnectAsync();
|
||||
await _timers[voiceChannel.Id].DisposeAsync();
|
||||
_timers.Remove(voiceChannel.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,18 +9,6 @@ public static class Option
|
||||
|
||||
public static class Command
|
||||
{
|
||||
// public static class Hello
|
||||
// {
|
||||
// public const string Name = "hello";
|
||||
// public const string Description = "Say hello to the bot!";
|
||||
// }
|
||||
//
|
||||
// public static class Goodbye
|
||||
// {
|
||||
// public const string Name = "goodbye";
|
||||
// public const string Description = "Say goodbye to the bot!";
|
||||
// }
|
||||
|
||||
public static class Disconnect
|
||||
{
|
||||
public const string Name = "disconnect";
|
||||
|
||||
Reference in New Issue
Block a user