This commit is contained in:
José Henrique Ivanchechen 2023-07-31 20:47:44 -03:00
parent 9df9090812
commit 0b9f96460f
8 changed files with 84 additions and 40 deletions

View File

@ -0,0 +1,14 @@
namespace Kasbot.Annotations
{
public class FlagAttribute : Attribute
{
public List<string> Names { get; set; }
public FlagAttribute() { }
public FlagAttribute(params string[] names)
{
Names = names.ToList();
}
}
}

View File

@ -1,6 +1,6 @@
using Discord;
using Discord.Audio;
using Kasbot.Services;
using Kasbot.Services.Internal;
namespace Kasbot.Models
{

View File

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

View File

@ -6,7 +6,7 @@ using TextCommandFramework.Services;
namespace TextCommandFramework.Modules
{
public class PublicModule : ModuleBase<ShardedCommandContext>
public class CommandModule : ModuleBase<ShardedCommandContext>
{
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)]

View File

@ -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<DiscordShardedClient>()
.AddSingleton<CommandService>()
.AddSingleton<YoutubeService>()
.AddSingleton<AudioService>()
.AddSingleton<PlayerService>()
.AddSingleton<CommandHandlingService>()
.AddSingleton<HttpClient>()

View File

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

View File

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

View File

@ -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<ulong, Connection> 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<ulong, Connection>();
}
@ -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))