refactor
This commit is contained in:
parent
9df9090812
commit
0b9f96460f
14
Annotations/FlagAttribute.cs
Normal file
14
Annotations/FlagAttribute.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
using Discord;
|
||||
using Discord.Audio;
|
||||
using Kasbot.Services;
|
||||
using Kasbot.Services.Internal;
|
||||
|
||||
namespace Kasbot.Models
|
||||
{
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)]
|
@ -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>()
|
||||
|
28
Services/Internal/AudioService.cs
Normal file
28
Services/Internal/AudioService.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ using Discord.Rest;
|
||||
using YoutubeExplode.Videos.Streams;
|
||||
using Kasbot.Models;
|
||||
|
||||
namespace Kasbot.Services
|
||||
namespace Kasbot.Services.Internal
|
||||
{
|
||||
public class YoutubeService
|
||||
{
|
||||
@ -128,7 +128,7 @@ namespace Kasbot.Services
|
||||
set
|
||||
{
|
||||
message = value;
|
||||
this.Channel = value.Channel;
|
||||
Channel = value.Channel;
|
||||
}
|
||||
}
|
||||
public ISocketMessageChannel Channel { get; private set; }
|
@ -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))
|
||||
|
Loading…
x
Reference in New Issue
Block a user