89 lines
2.9 KiB
C#
89 lines
2.9 KiB
C#
using TomatenMusic.Music;
|
|
using DSharpPlus;
|
|
using DSharpPlus.Entities;
|
|
using TomatenMusic_Api.Models;
|
|
using Lavalink4NET.Player;
|
|
using TomatenMusic;
|
|
using Lavalink4NET;
|
|
|
|
namespace TomatenMusic_Api
|
|
{
|
|
public class TomatenMusicDataService : IHostedService
|
|
{
|
|
private ILogger<TomatenMusicDataService> _logger;
|
|
public IServiceProvider _serviceProvider { get; set; } = TomatenMusicBot.ServiceProvider;
|
|
public IAudioService _audioService { get; set; }
|
|
public TomatenMusicDataService(ILogger<TomatenMusicDataService> logger)
|
|
{
|
|
_logger = logger;
|
|
_audioService = _serviceProvider.GetRequiredService<IAudioService>();
|
|
}
|
|
|
|
public async Task<PlayerConnectionInfo> GetConnectionInfoAsync(ulong guild_id)
|
|
{
|
|
GuildPlayer player = (GuildPlayer)_audioService.GetPlayer(guild_id);
|
|
if (player == null)
|
|
return null;
|
|
return await PlayerConnectionInfo.Create(player);
|
|
}
|
|
|
|
public async Task<Boolean?> IsPlayingAsync(ulong guild_id)
|
|
{
|
|
GuildPlayer player = _audioService.GetPlayer<GuildPlayer>(guild_id);
|
|
|
|
if (player == null)
|
|
return false;
|
|
return player.State == PlayerState.Playing;
|
|
}
|
|
public async Task<Boolean?> IsConnectedAsync(ulong guild_id)
|
|
{
|
|
GuildPlayer player = _audioService.GetPlayer<GuildPlayer>(guild_id);
|
|
|
|
if (player == null)
|
|
return false;
|
|
|
|
return player.State != PlayerState.NotConnected;
|
|
}
|
|
|
|
public async Task<List<PlayerConnectionInfo>> GetAllGuildPlayersAsync()
|
|
{
|
|
List<PlayerConnectionInfo> list = new List<PlayerConnectionInfo>();
|
|
foreach (var guild in _audioService.GetPlayers<GuildPlayer>())
|
|
{
|
|
list.Add(await PlayerConnectionInfo.Create(guild));
|
|
}
|
|
if (list.Count == 0)
|
|
return null;
|
|
|
|
return list;
|
|
}
|
|
|
|
public Task<DiscordChannel> GetDiscordChannelAsync(ulong channel_id)
|
|
{
|
|
DiscordClient client = _serviceProvider.GetRequiredService<DiscordClient>();
|
|
|
|
return client.GetChannelAsync(channel_id);
|
|
}
|
|
|
|
public Task<DiscordGuild> GetGuildAsync(ulong guild_id)
|
|
{
|
|
DiscordClient client = _serviceProvider.GetRequiredService<DiscordClient>();
|
|
|
|
return client.GetGuildAsync(guild_id);
|
|
}
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
_logger.LogInformation("TomatenMusicDataService starting...");
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
{
|
|
_logger.LogInformation("TomatenMusicDataService stopping...");
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
}
|
|
}
|