INIT
This commit is contained in:
31
TomatenMusic/Services/EventBus.cs
Normal file
31
TomatenMusic/Services/EventBus.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using DSharpPlus.Entities;
|
||||
using Emzi0767.Utilities;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using TomatenMusic_Api.Models;
|
||||
using TomatenMusic_Api.Models.EventArgs;
|
||||
|
||||
namespace TomatenMusic_Api;
|
||||
|
||||
public class InProcessEventBus
|
||||
{
|
||||
public event AsyncEventHandler<InProcessEventBus, ChannelConnectArgs>? OnConnectRequest;
|
||||
|
||||
public event AsyncEventHandler<InProcessEventBus, ChannelDisconnectArgs>? OnDisconnectRequest;
|
||||
|
||||
public event AsyncEventHandler<InProcessEventBus, TrackPlayArgs> OnPlayRequest;
|
||||
public void OnConnectRequestEvent(ChannelConnectArgs e)
|
||||
{
|
||||
_ = OnConnectRequest?.Invoke(this, e);
|
||||
}
|
||||
|
||||
public void OnDisconnectRequestEvent(ChannelDisconnectArgs e)
|
||||
{
|
||||
_ = OnDisconnectRequest?.Invoke(this, e);
|
||||
}
|
||||
|
||||
public void OnPlayRequestEvent(TrackPlayArgs e)
|
||||
{
|
||||
_ = OnPlayRequest?.Invoke(this, e);
|
||||
}
|
||||
}
|
||||
|
91
TomatenMusic/Services/TomatenMusicDataService.cs
Normal file
91
TomatenMusic/Services/TomatenMusicDataService.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
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;
|
||||
private IServiceProvider _serviceProvider { get; set; } = TomatenMusicBot.ServiceProvider;
|
||||
public IAudioService _audioService { get; set; }
|
||||
public TrackProvider TrackProvider { get; set; }
|
||||
public TomatenMusicDataService(ILogger<TomatenMusicDataService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_audioService = _serviceProvider.GetRequiredService<IAudioService>();
|
||||
TrackProvider = _serviceProvider.GetRequiredService<TrackProvider>();
|
||||
}
|
||||
|
||||
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 guild_id, ulong channel_id)
|
||||
{
|
||||
var client = _serviceProvider.GetRequiredService<DiscordShardedClient>();
|
||||
var guildClient = client.GetShard(guild_id);
|
||||
return guildClient.GetChannelAsync(channel_id);
|
||||
}
|
||||
|
||||
public Task<DiscordGuild> GetGuildAsync(ulong guild_id)
|
||||
{
|
||||
var client = _serviceProvider.GetRequiredService<DiscordShardedClient>();
|
||||
var guildClient = client.GetShard(guild_id);
|
||||
|
||||
return guildClient.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;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
92
TomatenMusic/Services/TomatenMusicService.cs
Normal file
92
TomatenMusic/Services/TomatenMusicService.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using Lavalink4NET;
|
||||
using TomatenMusic;
|
||||
using TomatenMusic.Music;
|
||||
using TomatenMusic_Api.Models;
|
||||
using TomatenMusic_Api.Models.EventArgs;
|
||||
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;
|
||||
_inProcessEventBus.OnDisconnectRequest += _inProcessEventBus_OnDisconnectRequest;
|
||||
_inProcessEventBus.OnPlayRequest += _inProcessEventBus_OnPlayRequest;
|
||||
}
|
||||
|
||||
private async Task _inProcessEventBus_OnPlayRequest(InProcessEventBus sender, TrackPlayArgs e)
|
||||
{
|
||||
GuildPlayer player = _audioService.GetPlayer<GuildPlayer>(e.GuildId);
|
||||
|
||||
if (e.Response.Tracks != null && e.Response.Tracks.Any())
|
||||
{
|
||||
if (e.Now)
|
||||
await player.PlayNowAsync(e.Response.Tracks);
|
||||
else
|
||||
await player.PlayItemAsync(e.Response.Tracks);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.Response.IsPlaylist)
|
||||
{
|
||||
if (e.Now)
|
||||
await player.PlayPlaylistNowAsync(e.Response.Playlist);
|
||||
else
|
||||
await player.PlayPlaylistAsync(e.Response.Playlist);
|
||||
}else
|
||||
{
|
||||
if (e.Now)
|
||||
await player.PlayNowAsync(e.Response.Track, e.StartTime);
|
||||
else
|
||||
await player.PlayAsync(e.Response.Track, e.StartTime);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private async Task _inProcessEventBus_OnDisconnectRequest(InProcessEventBus sender, ChannelDisconnectArgs e)
|
||||
{
|
||||
GuildPlayer player = _audioService.GetPlayer<GuildPlayer>(e.GuildId);
|
||||
player.DisconnectAsync();
|
||||
}
|
||||
|
||||
private async Task _inProcessEventBus_OnConnectRequest(InProcessEventBus sender, ChannelConnectArgs e)
|
||||
{
|
||||
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