reafactoring

This commit is contained in:
José Henrique Ivanchechen 2023-08-04 23:19:31 -03:00
parent 1ea940e03e
commit 73b0bac359
6 changed files with 82 additions and 66 deletions

View File

@ -39,7 +39,7 @@ jobs:
- name: Image digest - name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }} run: echo ${{ steps.docker_build.outputs.digest }}
build_arm64: build_arm64:
name: Build and Push Docker Image (arm64) name: Build and Push Docker Image (arm64)
runs-on: ubuntu-latest runs-on: ubuntu-latest
@ -75,7 +75,7 @@ jobs:
- name: Image digest - name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }} run: echo ${{ steps.docker_build.outputs.digest }}
deploy: deploy:
name: Update running container name: Update running container
runs-on: ubuntu-latest runs-on: ubuntu-latest
@ -95,4 +95,3 @@ jobs:
docker-compose rm docker-compose rm
docker-compose pull docker-compose pull
docker-compose up -d docker-compose up -d

View File

@ -11,6 +11,7 @@ namespace TextCommandFramework
class Program class Program
{ {
private static string TOKEN = Environment.GetEnvironmentVariable("TOKEN"); private static string TOKEN = Environment.GetEnvironmentVariable("TOKEN");
private static int SHARDS = int.Parse(Environment.GetEnvironmentVariable("SHARDS"));
static void Main(string[] args) static void Main(string[] args)
{ {
@ -18,8 +19,16 @@ namespace TextCommandFramework
{ {
throw new Exception("Discord Bot Token was not found."); throw new Exception("Discord Bot Token was not found.");
} }
if (SHARDS == 0)
{
Console.WriteLine("Shards amount not found, defaulting to 1.");
SHARDS = 1;
}
new Program().MainAsync().GetAwaiter().GetResult(); new Program()
.MainAsync()
.GetAwaiter()
.GetResult();
} }
public async Task MainAsync() public async Task MainAsync()
@ -84,7 +93,7 @@ namespace TextCommandFramework
.AddSingleton(new DiscordSocketConfig .AddSingleton(new DiscordSocketConfig
{ {
GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent, GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent,
TotalShards = 3 TotalShards = SHARDS
}) })
.AddSingleton<DiscordShardedClient>() .AddSingleton<DiscordShardedClient>()
.AddSingleton<CommandService>() .AddSingleton<CommandService>()

View File

@ -1,4 +1,5 @@
using System.Diagnostics; using Discord.Audio;
using System.Diagnostics;
namespace Kasbot.Services.Internal namespace Kasbot.Services.Internal
{ {
@ -6,7 +7,57 @@ namespace Kasbot.Services.Internal
{ {
public AudioService() { } public AudioService() { }
public Process CreateStream() public void StartAudioTask(Stream inputStream, IAudioClient outputAudioClient, Action<Stream> onStartAudio, Action<Task> onFinish)
{
var ffmpeg = CreateFFmpeg();
Task stdin = new Task(() =>
{
using (var output = inputStream)
{
try
{
output.CopyTo(ffmpeg.StandardInput.BaseStream);
ffmpeg.StandardInput.Close();
}
catch { }
finally
{
output.Flush();
}
}
});
Task stdout = new Task(() =>
{
using (var output = ffmpeg.StandardOutput.BaseStream)
using (var discord = outputAudioClient.CreatePCMStream(AudioApplication.Music))
{
try
{
onStartAudio.Invoke(ffmpeg.StandardOutput.BaseStream);
output.CopyTo(discord);
}
catch { }
finally
{
discord.Flush();
}
}
});
stdin.Start();
stdout.Start();
stdin.ContinueWith(onFinish);
stdout.ContinueWith(onFinish);
Task.WaitAll(stdin, stdout);
ffmpeg.Close();
}
private Process CreateFFmpeg()
{ {
var process = Process.Start(new ProcessStartInfo var process = Process.Start(new ProcessStartInfo
{ {

View File

@ -22,12 +22,14 @@ namespace Kasbot.Services.Internal
var playlistInfo = await youtube.Playlists.GetAsync(search); var playlistInfo = await youtube.Playlists.GetAsync(search);
await youtube.Playlists.GetVideosAsync(search).ForEachAsync(videoId => await youtube.Playlists.GetVideosAsync(search).ForEachAsync(videoId =>
{ {
var media = new Media(); var media = new Media
{
media.Name = videoId.Title; Name = videoId.Title,
media.Length = videoId.Duration ?? new TimeSpan(0); Length = videoId.Duration ?? new TimeSpan(0),
media.VideoId = videoId.Id; VideoId = videoId.Id,
media.Message = message; Message = message,
Flags = new Flags()
};
collection.Medias.Add(media); collection.Medias.Add(media);
}); });

View File

@ -143,7 +143,6 @@ namespace Kasbot.Services
} }
var audioClient = Clients[guildId].AudioClient; var audioClient = Clients[guildId].AudioClient;
var ffmpeg = AudioService.CreateStream();
if (!nextMedia.Flags.Silent) if (!nextMedia.Flags.Silent)
{ {
@ -156,62 +155,17 @@ namespace Kasbot.Services
await nextMedia.QueueMessage.TryDeleteAsync(); await nextMedia.QueueMessage.TryDeleteAsync();
} }
Task stdin = new Task(() => AudioService.StartAudioTask(mp3Stream, audioClient,
{ (outAudioStream) =>
using (var input = mp3Stream)
{ {
try Clients[guildId].CurrentAudioStream = outAudioStream;
{ }, async (ac) =>
input.CopyTo(ffmpeg.StandardInput.BaseStream);
ffmpeg.StandardInput.Close();
}
catch { }
finally
{
input.Flush();
}
}
});
Task stdout = new Task(() =>
{
using (var output = ffmpeg.StandardOutput.BaseStream)
using (var discord = audioClient.CreatePCMStream(AudioApplication.Music))
{ {
try if (ac.Exception != null)
{ {
Clients[guildId].CurrentAudioStream = output; await nextMedia.Channel.SendTemporaryMessageAsync("Error in stream: " + ac.Exception.ToString());
output.CopyTo(discord);
} }
catch { } });
finally
{
discord.Flush();
}
}
});
stdin.Start();
stdout.Start();
await stdin.ContinueWith(async ac =>
{
if (ac.Exception != null)
{
await nextMedia.Channel.SendTemporaryMessageAsync("Error in input stream: " + ac.Exception.ToString());
}
});
await stdout.ContinueWith(async ac =>
{
if (ac.Exception != null)
{
await nextMedia.Channel.SendTemporaryMessageAsync("Error while playing: " + ac.Exception.ToString());
}
});
Task.WaitAll(stdin, stdout);
ffmpeg.Close();
await nextMedia.PlayMessage.TryDeleteAsync(); await nextMedia.PlayMessage.TryDeleteAsync();

View File

@ -6,4 +6,5 @@ services:
environment: environment:
- TOKEN=<Your bot token here> - TOKEN=<Your bot token here>
- COMMAND_PREFIX=! - COMMAND_PREFIX=!
- SHARDS=3
restart: unless-stopped restart: unless-stopped