add /api/player/disconnect endpoint

This commit is contained in:
Tim Müller 2022-03-22 20:00:33 +01:00
parent 64bbf0598e
commit 482a64ac54
9 changed files with 88 additions and 26 deletions

@ -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<IActionResult> PostConnection(ChannelConnectRequest request)
public async Task<IActionResult> 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<IActionResult> 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();
}
}

@ -0,0 +1,7 @@
namespace TomatenMusic_Api.Models.EventArgs
{
public class ChannelDisconnectRequest
{
public ulong GuildId { get; set; }
}
}

@ -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;
}
}
}

@ -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; }
}
}

@ -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<InProcessEventBus, ChannelConnectEventArgs>? OnConnectRequest;
public event AsyncEventHandler<InProcessEventBus, ChannelConnectArgs>? OnConnectRequest;
public void OnConnectRequestEvent(ChannelConnectEventArgs e)
public event AsyncEventHandler<InProcessEventBus, ChannelDisconnectArgs>? 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);
}
}

@ -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<GuildPlayer>(e.GuildId);
player.DisconnectAsync();
}
private async Task _inProcessEventBus_OnConnectRequest(InProcessEventBus sender, ChannelConnectArgs e)
{
_logger.LogInformation("Channel Connected!");
GuildPlayer player = await _audioService.JoinAsync<GuildPlayer>(e.Guild_Id, e.Channel.Id, true);
}

@ -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"
}

@ -39,9 +39,6 @@ namespace TomatenMusic.Music
_spotify = serviceProvider.GetRequiredService<ISpotifyService>();
_audioService = serviceProvider.GetRequiredService<IAudioService>();
}
public async override Task PlayAsync(LavalinkTrack track, TimeSpan? startTime = null, TimeSpan? endTime = null, bool noReplace = true)
{

@ -99,7 +99,8 @@ namespace TomatenMusic
.AddSingleton<YoutubeService>()
.AddSingleton<LyricsOptions>()
.AddSingleton<LyricsService>()
.AddSingleton(SpotifyClientConfig.CreateDefault().WithAuthenticator(new ClientCredentialsAuthenticator(config.SpotifyClientId, config.SpotifyClientSecret))))
.AddSingleton(
SpotifyClientConfig.CreateDefault().WithAuthenticator(new ClientCredentialsAuthenticator(config.SpotifyClientId, config.SpotifyClientSecret))))
.Build();
ServiceProvider = _host.Services;