mirror of
https://github.com/Myxelium/Lunaris2.0.git
synced 2026-04-13 08:00:37 +00:00
@@ -1,59 +1,71 @@
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace Lunaris2.Service;
|
||||
|
||||
public class VoiceChannelMonitorService
|
||||
namespace Lunaris2.Service
|
||||
{
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly Dictionary<ulong, Timer> _timers = new();
|
||||
|
||||
public VoiceChannelMonitorService(DiscordSocketClient client)
|
||||
public class VoiceChannelMonitorService
|
||||
{
|
||||
_client = client;
|
||||
}
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly Dictionary<ulong, Timer> _timers = new();
|
||||
|
||||
public void StartMonitoring()
|
||||
{
|
||||
Task.Run(async () =>
|
||||
public VoiceChannelMonitorService(DiscordSocketClient client)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
await CheckVoiceChannels();
|
||||
await Task.Delay(TimeSpan.FromMinutes(1));
|
||||
}
|
||||
});
|
||||
}
|
||||
_client = client;
|
||||
}
|
||||
|
||||
private async Task CheckVoiceChannels()
|
||||
{
|
||||
foreach (var guild in _client.Guilds)
|
||||
public void StartMonitoring()
|
||||
{
|
||||
var voiceChannel = guild.VoiceChannels.FirstOrDefault(vc => vc.ConnectedUsers.Count == 1);
|
||||
if (voiceChannel != null)
|
||||
Task.Run(async () =>
|
||||
{
|
||||
if (!_timers.ContainsKey(voiceChannel.Id))
|
||||
while (true)
|
||||
{
|
||||
_timers[voiceChannel.Id] = new Timer(async _ => await LeaveChannel(voiceChannel), null, TimeSpan.FromMinutes(3), Timeout.InfiniteTimeSpan);
|
||||
await CheckVoiceChannels();
|
||||
await Task.Delay(TimeSpan.FromMinutes(1)); // Monitor every minute
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async Task CheckVoiceChannels()
|
||||
{
|
||||
foreach (var guild in _client.Guilds)
|
||||
{
|
||||
// Find voice channels where only the bot is left
|
||||
var voiceChannel = guild.VoiceChannels.FirstOrDefault(vc =>
|
||||
vc.ConnectedUsers.Count == 1 &&
|
||||
vc.Users.Any(u => u.Id == _client.CurrentUser.Id));
|
||||
|
||||
if (voiceChannel != null)
|
||||
{
|
||||
// If timer not set for this channel, start one
|
||||
if (!_timers.ContainsKey(voiceChannel.Id))
|
||||
{
|
||||
Console.WriteLine($"Bot is alone in channel {voiceChannel.Name}, starting timer...");
|
||||
_timers[voiceChannel.Id] = new Timer(async _ => await LeaveChannel(voiceChannel), null,
|
||||
TimeSpan.FromMinutes(3), Timeout.InfiniteTimeSpan); // Set delay before leaving
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Clean up timer if channel is no longer active
|
||||
var timersToDispose = _timers.Where(t => guild.VoiceChannels.All(vc => vc.Id != t.Key)).ToList();
|
||||
foreach (var timer in timersToDispose)
|
||||
{
|
||||
await timer.Value.DisposeAsync();
|
||||
_timers.Remove(timer.Key);
|
||||
Console.WriteLine($"Disposed timer for inactive voice channel ID: {timer.Key}");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
}
|
||||
|
||||
private async Task LeaveChannel(SocketVoiceChannel voiceChannel)
|
||||
{
|
||||
if (voiceChannel.ConnectedUsers.Count == 1 && voiceChannel.Users.Any(u => u.Id == _client.CurrentUser.Id))
|
||||
{
|
||||
if (voiceChannel == null || !_timers.ContainsKey(voiceChannel.Id))
|
||||
continue;
|
||||
|
||||
Console.WriteLine($"Leaving channel {voiceChannel.Name} due to inactivity...");
|
||||
await voiceChannel.DisconnectAsync();
|
||||
await _timers[voiceChannel.Id].DisposeAsync();
|
||||
_timers.Remove(voiceChannel.Id);
|
||||
_timers.Remove(voiceChannel.Id); // Clean up after leaving
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user