From 0b9f96460f7e1118355b71aa9ebfe54cc4d46525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Henrique=20Ivanchechen?= Date: Mon, 31 Jul 2023 20:47:44 -0300 Subject: [PATCH] refactor --- Annotations/FlagAttribute.cs | 14 +++++++ Models/Connection.cs | 2 +- Models/Flags.cs | 37 +++++++++++++------ Modules/{PublicModule.cs => CommandModule.cs} | 6 ++- Program.cs | 2 + Services/Internal/AudioService.cs | 28 ++++++++++++++ Services/{ => Internal}/YoutubeService.cs | 6 +-- Services/PlayerService.cs | 29 +++------------ 8 files changed, 84 insertions(+), 40 deletions(-) create mode 100644 Annotations/FlagAttribute.cs rename Modules/{PublicModule.cs => CommandModule.cs} (90%) create mode 100644 Services/Internal/AudioService.cs rename Services/{ => Internal}/YoutubeService.cs (98%) diff --git a/Annotations/FlagAttribute.cs b/Annotations/FlagAttribute.cs new file mode 100644 index 0000000..242c989 --- /dev/null +++ b/Annotations/FlagAttribute.cs @@ -0,0 +1,14 @@ +namespace Kasbot.Annotations +{ + public class FlagAttribute : Attribute + { + public List Names { get; set; } + + public FlagAttribute() { } + + public FlagAttribute(params string[] names) + { + Names = names.ToList(); + } + } +} diff --git a/Models/Connection.cs b/Models/Connection.cs index 9531614..25858d1 100644 --- a/Models/Connection.cs +++ b/Models/Connection.cs @@ -1,6 +1,6 @@ using Discord; using Discord.Audio; -using Kasbot.Services; +using Kasbot.Services.Internal; namespace Kasbot.Models { diff --git a/Models/Flags.cs b/Models/Flags.cs index 3a2c552..5478b3b 100644 --- a/Models/Flags.cs +++ b/Models/Flags.cs @@ -1,8 +1,13 @@ -namespace Kasbot.Models +using Kasbot.Annotations; + +namespace Kasbot.Models { public class Flags { + [Flag("-s", "-silent")] public bool Silent { get; set; } + + [Flag("-r", "-repeat")] public bool Repeat { get; set; } public Flags() { } @@ -12,19 +17,29 @@ this.Parse(command); } - public void Parse(string command) + public string Parse(string command) { - if (command.Contains("-s") || - command.Contains("-silent")) - { - Silent = true; - } + string result = command; - if (command.Contains("-r") || - command.Contains("-repeat")) + this.GetType().GetProperties().ToList().ForEach(prop => { - Repeat = true; - } + Attribute.GetCustomAttributes(prop).ToList().ForEach(attr => + { + if (attr is FlagAttribute flag) + { + flag.Names.ForEach(name => + { + if (command.Contains(name)) + { + prop.SetValue(this, true); + result.Replace(name, string.Empty); + } + }); + } + }); + }); + + return result; } } } diff --git a/Modules/PublicModule.cs b/Modules/CommandModule.cs similarity index 90% rename from Modules/PublicModule.cs rename to Modules/CommandModule.cs index 0c95dbe..1ece9e5 100644 --- a/Modules/PublicModule.cs +++ b/Modules/CommandModule.cs @@ -6,7 +6,7 @@ using TextCommandFramework.Services; namespace TextCommandFramework.Modules { - public class PublicModule : ModuleBase + public class CommandModule : ModuleBase { public PictureService PictureService { get; set; } public PlayerService PlayerService { get; set; } @@ -31,7 +31,9 @@ namespace TextCommandFramework.Modules throw new Exception("You must be connect in a voice channel!"); } - await PlayerService.Play(Context, text, new Flags(text)); + var flags = new Flags(); + var withoutFlags = flags.Parse(text); + await PlayerService.Play(Context, withoutFlags, flags); } [Command("join", RunMode = RunMode.Async)] diff --git a/Program.cs b/Program.cs index 663fd38..43385c4 100644 --- a/Program.cs +++ b/Program.cs @@ -2,6 +2,7 @@ using Discord; using Discord.Commands; using Discord.WebSocket; using Kasbot.Services; +using Kasbot.Services.Internal; using Microsoft.Extensions.DependencyInjection; using TextCommandFramework.Services; @@ -88,6 +89,7 @@ namespace TextCommandFramework .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() diff --git a/Services/Internal/AudioService.cs b/Services/Internal/AudioService.cs new file mode 100644 index 0000000..d2f3b42 --- /dev/null +++ b/Services/Internal/AudioService.cs @@ -0,0 +1,28 @@ +using System.Diagnostics; + +namespace Kasbot.Services.Internal +{ + public class AudioService + { + public AudioService() { } + + public Process CreateStream() + { + var process = Process.Start(new ProcessStartInfo + { + FileName = "ffmpeg", + Arguments = $"-hide_banner -loglevel panic -i pipe:0 -ac 2 -f s16le -ar 48000 pipe:1", + UseShellExecute = false, + RedirectStandardInput = true, + RedirectStandardOutput = true + }); + + if (process == null || process.HasExited) + { + throw new Exception("Sorry, ffmpeg killed itself in a tragic accident!"); + } + + return process; + } + } +} diff --git a/Services/YoutubeService.cs b/Services/Internal/YoutubeService.cs similarity index 98% rename from Services/YoutubeService.cs rename to Services/Internal/YoutubeService.cs index a6c199a..c0eb3af 100644 --- a/Services/YoutubeService.cs +++ b/Services/Internal/YoutubeService.cs @@ -5,7 +5,7 @@ using Discord.Rest; using YoutubeExplode.Videos.Streams; using Kasbot.Models; -namespace Kasbot.Services +namespace Kasbot.Services.Internal { public class YoutubeService { @@ -120,7 +120,7 @@ namespace Kasbot.Services public VideoId? VideoId { get; set; } public RestUserMessage PlayMessage { get; set; } public RestUserMessage? QueueMessage { get; set; } - + private SocketUserMessage message; public SocketUserMessage Message { @@ -128,7 +128,7 @@ namespace Kasbot.Services set { message = value; - this.Channel = value.Channel; + Channel = value.Channel; } } public ISocketMessageChannel Channel { get; private set; } diff --git a/Services/PlayerService.cs b/Services/PlayerService.cs index 9b70883..0a57206 100644 --- a/Services/PlayerService.cs +++ b/Services/PlayerService.cs @@ -3,7 +3,7 @@ using Discord.Audio; using Discord.Commands; using Kasbot.Extensions; using Kasbot.Models; -using System.Diagnostics; +using Kasbot.Services.Internal; namespace Kasbot.Services { @@ -11,10 +11,12 @@ namespace Kasbot.Services { public Dictionary Clients { get; set; } public YoutubeService YoutubeService { get; set; } + public AudioService AudioService { get; set; } - public PlayerService(YoutubeService youtubeService) + public PlayerService(YoutubeService youtubeService, AudioService audioService) { - this.YoutubeService = youtubeService; + YoutubeService = youtubeService; + AudioService = audioService; Clients = new Dictionary(); } @@ -141,7 +143,7 @@ namespace Kasbot.Services } var audioClient = Clients[guildId].AudioClient; - var ffmpeg = CreateStream(); + var ffmpeg = AudioService.CreateStream(); if (!nextMedia.Flags.Silent) { @@ -220,25 +222,6 @@ namespace Kasbot.Services await PlayNext(guildId); } - private Process CreateStream() - { - var process = Process.Start(new ProcessStartInfo - { - FileName = "ffmpeg", - Arguments = $"-hide_banner -loglevel panic -i pipe:0 -ac 2 -f s16le -ar 48000 pipe:1", - UseShellExecute = false, - RedirectStandardInput = true, - RedirectStandardOutput = true - }); - - if (process == null || process.HasExited) - { - throw new Exception("Sorry, ffmpeg killed itself in a tragic accident!"); - } - - return process; - } - public Task Skip(ulong guildId) { if (!Clients.ContainsKey(guildId))