diff --git a/TomatenMusic/Controllers/PlayerController.cs b/TomatenMusic/Controllers/PlayerController.cs index df1975f..95ac508 100644 --- a/TomatenMusic/Controllers/PlayerController.cs +++ b/TomatenMusic/Controllers/PlayerController.cs @@ -4,6 +4,8 @@ using TomatenMusic; using TomatenMusic_Api; using TomatenMusic_Api.Auth.Helpers; using TomatenMusic_Api.Models; +using TomatenMusic_Api.Models.EventArgs; +using static TomatenMusic_Api.InProcessEventBus; namespace TomatenMusic_Api.Controllers; @@ -54,8 +56,9 @@ public class PlayerController : ControllerBase } [HttpPost("connect")] - public async Task PostConnection(ChannelConnectRequest request) + public async Task PostConnect(ChannelConnectRequest request) { + try { await _tomatenMusicDataService.GetGuildAsync(request.Guild_Id); @@ -85,8 +88,28 @@ public class PlayerController : ControllerBase - _eventBus.OnConnectRequestEvent(new InProcessEventBus.ChannelConnectEventArgs(request.Guild_Id, channel)); + _eventBus.OnConnectRequestEvent(new ChannelConnectArgs(request.Guild_Id, channel)); return Ok(); } + + [HttpPost("disconnect")] + public async Task PostDisconnect(ChannelDisconnectRequest request) + { + try + { + await _tomatenMusicDataService.GetGuildAsync(request.GuildId); + } + catch (Exception ex) + { + return NotFound("That Guild was not found"); + } + + if (!await _tomatenMusicDataService.IsConnectedAsync(request.GuildId) == true) + return BadRequest("The Bot is not connected."); + + _eventBus.OnDisconnectRequestEvent(new ChannelDisconnectArgs(request.GuildId)); + return Ok(); + + } } diff --git a/TomatenMusic/Models/ChannelDisconnectRequest.cs b/TomatenMusic/Models/ChannelDisconnectRequest.cs new file mode 100644 index 0000000..0e31e6d --- /dev/null +++ b/TomatenMusic/Models/ChannelDisconnectRequest.cs @@ -0,0 +1,7 @@ +namespace TomatenMusic_Api.Models.EventArgs +{ + public class ChannelDisconnectRequest + { + public ulong GuildId { get; set; } + } +} diff --git a/TomatenMusic/Models/EventArgs/ChannelConnectArgs.cs b/TomatenMusic/Models/EventArgs/ChannelConnectArgs.cs new file mode 100644 index 0000000..64d4d28 --- /dev/null +++ b/TomatenMusic/Models/EventArgs/ChannelConnectArgs.cs @@ -0,0 +1,18 @@ +using DSharpPlus.Entities; +using Emzi0767.Utilities; + +namespace TomatenMusic_Api.Models.EventArgs +{ + public class ChannelConnectArgs : AsyncEventArgs + { + public ulong Guild_Id { get; set; } + + public DiscordChannel Channel { get; set; } + + public ChannelConnectArgs(ulong guild_Id, DiscordChannel channel) + { + Guild_Id = guild_Id; + Channel = channel; + } + } +} diff --git a/TomatenMusic/Models/EventArgs/ChannelDisconnectArgs.cs b/TomatenMusic/Models/EventArgs/ChannelDisconnectArgs.cs new file mode 100644 index 0000000..42e4404 --- /dev/null +++ b/TomatenMusic/Models/EventArgs/ChannelDisconnectArgs.cs @@ -0,0 +1,13 @@ +using Emzi0767.Utilities; + +namespace TomatenMusic_Api.Models.EventArgs +{ + public class ChannelDisconnectArgs : AsyncEventArgs + { + public ulong GuildId { get; set; } + + public ChannelDisconnectArgs(ulong guildId) { GuildId = guildId; } + } + + +} diff --git a/TomatenMusic/Services/EventBus.cs b/TomatenMusic/Services/EventBus.cs index 9ec1f8a..fd0fbac 100644 --- a/TomatenMusic/Services/EventBus.cs +++ b/TomatenMusic/Services/EventBus.cs @@ -2,29 +2,25 @@ 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? OnConnectRequest; + public event AsyncEventHandler? OnConnectRequest; - public void OnConnectRequestEvent(ChannelConnectEventArgs e) + public event AsyncEventHandler? OnDisconnectRequest; + + + public void OnConnectRequestEvent(ChannelConnectArgs 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; - } + public void OnDisconnectRequestEvent(ChannelDisconnectArgs e) + { + _ = OnDisconnectRequest?.Invoke(this, e); } } diff --git a/TomatenMusic/Services/TomatenMusicService.cs b/TomatenMusic/Services/TomatenMusicService.cs index 28b6f2c..2204e76 100644 --- a/TomatenMusic/Services/TomatenMusicService.cs +++ b/TomatenMusic/Services/TomatenMusicService.cs @@ -2,6 +2,7 @@ using TomatenMusic; using TomatenMusic.Music; using TomatenMusic_Api.Models; +using TomatenMusic_Api.Models.EventArgs; using static TomatenMusic_Api.InProcessEventBus; namespace TomatenMusic_Api @@ -24,11 +25,17 @@ namespace TomatenMusic_Api private void Initialize() { _inProcessEventBus.OnConnectRequest += _inProcessEventBus_OnConnectRequest; + _inProcessEventBus.OnDisconnectRequest += _inProcessEventBus_OnDisconnectRequest; } - private async Task _inProcessEventBus_OnConnectRequest(InProcessEventBus sender, ChannelConnectEventArgs e) + private async Task _inProcessEventBus_OnDisconnectRequest(InProcessEventBus sender, ChannelDisconnectArgs e) + { + GuildPlayer player = _audioService.GetPlayer(e.GuildId); + player.DisconnectAsync(); + } + + private async Task _inProcessEventBus_OnConnectRequest(InProcessEventBus sender, ChannelConnectArgs e) { - _logger.LogInformation("Channel Connected!"); GuildPlayer player = await _audioService.JoinAsync(e.Guild_Id, e.Channel.Id, true); } diff --git a/TomatenMusic/config.json b/TomatenMusic/config.json index ae2595d..5620f28 100644 --- a/TomatenMusic/config.json +++ b/TomatenMusic/config.json @@ -1,8 +1,8 @@ { - "TOKEN": "Bot_Token", - "LavaLinkPassword": " ", - "SpotifyClientId": " ", - "SpotifyClientSecret": " ", - "YoutubeApiKey": " " + "TOKEN": "ODQwNjQ5NjU1MTAwMjQzOTY4.YJbSAA.iERGRAMeXJEBb5vq3eNUmrKsT50", + "LavaLinkPassword": "SGWaldsolms9", + "SpotifyClientId": "14b77fa47f2f492db58cbdca8f1e5d9c", + "SpotifyClientSecret": "c247625f0cfe4b72a1faa01b7c5b8eea", + "YoutubeApiKey": "AIzaSyBIcTl9JQ9jF412mX0Wfp_3Y-4a-V0SASQ" } diff --git a/TomatenMusicCore/Music/GuildPlayer.cs b/TomatenMusicCore/Music/GuildPlayer.cs index e8c54cf..f956882 100644 --- a/TomatenMusicCore/Music/GuildPlayer.cs +++ b/TomatenMusicCore/Music/GuildPlayer.cs @@ -39,9 +39,6 @@ namespace TomatenMusic.Music _spotify = serviceProvider.GetRequiredService(); _audioService = serviceProvider.GetRequiredService(); } - - - public async override Task PlayAsync(LavalinkTrack track, TimeSpan? startTime = null, TimeSpan? endTime = null, bool noReplace = true) { diff --git a/TomatenMusicCore/TomatenMusicBot.cs b/TomatenMusicCore/TomatenMusicBot.cs index c460998..32ac443 100644 --- a/TomatenMusicCore/TomatenMusicBot.cs +++ b/TomatenMusicCore/TomatenMusicBot.cs @@ -99,7 +99,8 @@ namespace TomatenMusic .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton(SpotifyClientConfig.CreateDefault().WithAuthenticator(new ClientCredentialsAuthenticator(config.SpotifyClientId, config.SpotifyClientSecret)))) + .AddSingleton( + SpotifyClientConfig.CreateDefault().WithAuthenticator(new ClientCredentialsAuthenticator(config.SpotifyClientId, config.SpotifyClientSecret)))) .Build(); ServiceProvider = _host.Services;