From 5d910a62a1d008b7de9a1e306152a35762f724c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Henrique=20Ivanchechen?= Date: Thu, 2 Feb 2023 22:14:17 -0300 Subject: [PATCH] a couple of stuff + skip --- .github/workflows/master.yml | 8 ++-- Modules/PublicModule.cs | 9 ++++ Services/CommandHandlingService.cs | 13 ++++-- Services/PlayerService.cs | 71 ++++++++++++++++++++++++++---- 4 files changed, 86 insertions(+), 15 deletions(-) diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index 9db4aeb..894fc26 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -85,10 +85,10 @@ jobs: - name: Recreate container uses: appleboy/ssh-action@v0.1.7 with: - host: ${{ secrets.DEPLOY_HOST }} - username: ${{ secrets.DEPLOY_USER }} - key: ${{ secrets.DEPLOY_KEY }} - port: ${{ secrets.DEPLOY_PORT }} + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + key: ${{ secrets.KEY }} + port: ${{ secrets.PORT }} script: | cd docker/kasbot docker-compose down diff --git a/Modules/PublicModule.cs b/Modules/PublicModule.cs index 5e02901..e1cdce2 100644 --- a/Modules/PublicModule.cs +++ b/Modules/PublicModule.cs @@ -53,6 +53,15 @@ namespace TextCommandFramework.Modules await PlayerService.Stop(Context.Guild.Id); } + [Command("skip", RunMode = RunMode.Async)] + public async Task SkipAsync() + { + var user = Context.User; + if (user.IsBot) return; + + await PlayerService.Skip(Context.Guild.Id); + } + [Command("leave", RunMode = RunMode.Async)] public async Task LeaveAsync() { diff --git a/Services/CommandHandlingService.cs b/Services/CommandHandlingService.cs index 65fe94c..025cbb9 100644 --- a/Services/CommandHandlingService.cs +++ b/Services/CommandHandlingService.cs @@ -14,6 +14,8 @@ namespace TextCommandFramework.Services private readonly DiscordSocketClient _discord; private readonly IServiceProvider _services; + private readonly string CommandPrefix = "!"; + public CommandHandlingService(IServiceProvider services) { _commands = services.GetRequiredService(); @@ -22,6 +24,8 @@ namespace TextCommandFramework.Services _commands.CommandExecuted += CommandExecutedAsync; _discord.MessageReceived += MessageReceivedAsync; + + CommandPrefix = Environment.GetEnvironmentVariable("COMMAND_PREFIX") ?? "!"; } public async Task InitializeAsync() @@ -37,10 +41,9 @@ namespace TextCommandFramework.Services return; var argPos = 0; - var prefix = "-"; //Check if the message sent has the specified prefix - if (!message.HasStringPrefix(prefix, ref argPos)) return; + if (!message.HasStringPrefix(CommandPrefix, ref argPos)) return; var context = new SocketCommandContext(_discord, message); await _commands.ExecuteAsync(context, argPos, _services); @@ -54,7 +57,11 @@ namespace TextCommandFramework.Services if (result.IsSuccess) return; - await context.Channel.SendMessageAsync($"error: {result}"); + var message = await context.Channel.SendMessageAsync($"error: {result}"); + + await Task.Delay(5_000); + + await message.DeleteAsync(); } } } diff --git a/Services/PlayerService.cs b/Services/PlayerService.cs index 43e1b75..eaaeae4 100644 --- a/Services/PlayerService.cs +++ b/Services/PlayerService.cs @@ -5,6 +5,8 @@ using Discord.Rest; using Discord.WebSocket; using System.Diagnostics; using YoutubeExplode; +using YoutubeExplode.Search; +using YoutubeExplode.Videos; using YoutubeExplode.Videos.Streams; namespace Kasbot.Services @@ -18,12 +20,23 @@ namespace Kasbot.Services Clients = new Dictionary(); } - private async Task DownloadAudioFromYoutube(Media media) + private async Task DownloadAudioFromYoutube(Media media) { var memoryStream = new MemoryStream(); var youtube = new YoutubeClient(); - var videoId = await youtube.Search.GetVideosAsync(media.Search).FirstOrDefaultAsync(); + IVideo? videoId = null; + + if (media.Search.StartsWith("http://") || media.Search.StartsWith("https://")) + videoId = await youtube.Videos.GetAsync(media.Search); + else + videoId = await youtube.Search.GetVideosAsync(media.Search).FirstOrDefaultAsync(); + + if (videoId == null) + { + return null; + } + var streamInfoSet = await youtube.Videos.Streams.GetManifestAsync(videoId.Id); var streamInfo = streamInfoSet.GetAudioOnlyStreams().GetWithHighestBitrate(); var streamVideo = await youtube.Videos.Streams.GetAsync(streamInfo); @@ -65,7 +78,6 @@ namespace Kasbot.Services if (Clients.TryGetValue(Context.Guild.Id, out var conn)) { conn.Queue.Enqueue(media); - Console.WriteLine("conn.Queue.Count " + conn.Queue.Count); if (conn.Queue.Count == 1) { await PlayNext(Context.Guild.Id); @@ -83,7 +95,6 @@ namespace Kasbot.Services conn = CreateConnection(audioClient, Context.Guild.Id); conn.Queue.Enqueue(media); - Console.WriteLine("conn.Queue.Count " + conn.Queue.Count); if (conn.Queue.Count == 1) { await PlayNext(Context.Guild.Id); @@ -101,15 +112,24 @@ namespace Kasbot.Services if (nextMedia == null) { + Clients[guildId].Queue.Dequeue(); await Stop(guildId); return; } var mp3Stream = await DownloadAudioFromYoutube(nextMedia); + + if (mp3Stream == null) + { + await Stop(guildId); + return; + } + var audioClient = Clients[guildId].AudioClient; var ffmpeg = CreateStream(); - nextMedia.PlayMessage = await nextMedia.Message.Channel.SendMessageAsync($"⏯ Playing: {nextMedia.Name} **({nextMedia.Length.Minutes.ToString("00")}:{nextMedia.Length.Seconds:00})**"); + var message = $"⏯ Playing: {nextMedia.Name} **({nextMedia.Length.TotalMinutes:00}:{nextMedia.Length.Seconds:00})**"; + nextMedia.PlayMessage = await nextMedia.Message.Channel.SendMessageAsync(message); Task stdin = new Task(() => { @@ -120,6 +140,7 @@ namespace Kasbot.Services input.CopyTo(ffmpeg.StandardInput.BaseStream); ffmpeg.StandardInput.Close(); } + catch { } finally { input.Flush(); @@ -135,8 +156,10 @@ namespace Kasbot.Services { try { + Clients[guildId].CurrentAudioStream = output; output.CopyTo(discord); } + catch { } finally { discord.Flush(); @@ -184,6 +207,21 @@ namespace Kasbot.Services return process; } + public Task Skip(ulong guildId) + { + if (!Clients.ContainsKey(guildId)) + return Task.CompletedTask; + + var media = Clients[guildId]; + + if (media.CurrentAudioStream == null) + return Task.CompletedTask; + + media.CurrentAudioStream.Close(); + + return Task.CompletedTask; + } + public async Task Stop(ulong guildId) { if (!Clients.ContainsKey(guildId)) @@ -193,18 +231,35 @@ namespace Kasbot.Services foreach (var v in media.Queue) { - await v.Message.DeleteAsync(); - await v.PlayMessage.DeleteAsync(); + await RemoveMediaMessages(v); } - await media.AudioClient.StopAsync(); + + if (media.AudioClient != null) + await media.AudioClient.StopAsync(); Clients.Remove(guildId); } + + private async Task RemoveMediaMessages(Media media) + { + try + { + if (media.Message != null) + await media.Message.DeleteAsync(); + } + catch { } + try + { + if (media.PlayMessage != null) + await media.PlayMessage.DeleteAsync(); + } catch { } + } } public class Connection { public IAudioClient AudioClient { get; set; } + public Stream? CurrentAudioStream { get; set; } public Queue Queue { get; set; } = new Queue(); }