Add project files.
This commit is contained in:
30
TomatenMusic Api/Services/EventBus.cs
Normal file
30
TomatenMusic Api/Services/EventBus.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using DSharpPlus.Entities;
|
||||
using Emzi0767.Utilities;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using TomatenMusic_Api.Models;
|
||||
|
||||
namespace TomatenMusic_Api;
|
||||
|
||||
public class InProcessEventBus
|
||||
{
|
||||
public event AsyncEventHandler<InProcessEventBus, ChannelConnectEventArgs>? OnConnectRequest;
|
||||
|
||||
public void OnConnectRequestEvent(ChannelConnectEventArgs e)
|
||||
{
|
||||
_ = OnConnectRequest?.Invoke(this, e);
|
||||
}
|
||||
|
||||
public class ChannelConnectEventArgs : AsyncEventArgs
|
||||
{
|
||||
public ulong Guild_Id { get; set; }
|
||||
|
||||
public DiscordChannel Channel { get; set; }
|
||||
|
||||
public ChannelConnectEventArgs(ulong guild_Id, DiscordChannel channel)
|
||||
{
|
||||
Guild_Id = guild_Id;
|
||||
Channel = channel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
88
TomatenMusic Api/Services/TomatenMusicDataService.cs
Normal file
88
TomatenMusic Api/Services/TomatenMusicDataService.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
54
TomatenMusic Api/Services/TomatenMusicService.cs
Normal file
54
TomatenMusic Api/Services/TomatenMusicService.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using Lavalink4NET;
|
||||
using TomatenMusic;
|
||||
using TomatenMusic.Music;
|
||||
using TomatenMusic_Api.Models;
|
||||
using static TomatenMusic_Api.InProcessEventBus;
|
||||
|
||||
namespace TomatenMusic_Api
|
||||
{
|
||||
public class TomatenMusicService : IHostedService
|
||||
{
|
||||
private readonly InProcessEventBus _inProcessEventBus;
|
||||
private readonly ILogger<TomatenMusicService> _logger;
|
||||
public TomatenMusicBot _bot { get; set; }
|
||||
public IAudioService _audioService { get; set; }
|
||||
|
||||
public TomatenMusicService(InProcessEventBus inProcessEventBus, ILogger<TomatenMusicService> logger)
|
||||
{
|
||||
_inProcessEventBus = inProcessEventBus;
|
||||
_logger = logger;
|
||||
|
||||
Initialize();
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
_inProcessEventBus.OnConnectRequest += _inProcessEventBus_OnConnectRequest;
|
||||
}
|
||||
|
||||
private async Task _inProcessEventBus_OnConnectRequest(InProcessEventBus sender, ChannelConnectEventArgs e)
|
||||
{
|
||||
_logger.LogInformation("Channel Connected!");
|
||||
GuildPlayer player = await _audioService.JoinAsync<GuildPlayer>(e.Guild_Id, e.Channel.Id, true);
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Starting service...");
|
||||
_bot = new TomatenMusicBot();
|
||||
await _bot.InitBotAsync();
|
||||
_audioService = TomatenMusicBot.ServiceProvider.GetRequiredService<IAudioService>();
|
||||
_logger.LogInformation("Service started!");
|
||||
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Shutting down service...");
|
||||
await _bot.ShutdownBotAsync();
|
||||
_logger.LogInformation("Service shut down!");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user