Initial commit

This commit is contained in:
2023-01-30 22:35:08 -03:00
parent 2998d33d33
commit 8216e6c85a
8 changed files with 346 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Reflection;
using System.Threading.Tasks;
namespace TextCommandFramework.Services
{
public class CommandHandlingService
{
private readonly CommandService _commands;
private readonly DiscordSocketClient _discord;
private readonly IServiceProvider _services;
public CommandHandlingService(IServiceProvider services)
{
_commands = services.GetRequiredService<CommandService>();
_discord = services.GetRequiredService<DiscordSocketClient>();
_services = services;
_commands.CommandExecuted += CommandExecutedAsync;
_discord.MessageReceived += MessageReceivedAsync;
}
public async Task InitializeAsync()
{
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
}
public async Task MessageReceivedAsync(SocketMessage rawMessage)
{
if (!(rawMessage is SocketUserMessage message))
return;
if (message.Source != MessageSource.User)
return;
var argPos = 0;
var prefix = "!";
//Check if the message sent has the specified prefix
if (!message.HasStringPrefix(prefix, ref argPos)) return;
var context = new SocketCommandContext(_discord, message);
await _commands.ExecuteAsync(context, argPos, _services);
}
public async Task CommandExecutedAsync(Optional<CommandInfo> command, ICommandContext context, IResult result)
{
if (!command.IsSpecified)
return;
if (result.IsSuccess)
return;
await context.Channel.SendMessageAsync($"error: {result}");
}
}
}

View File

@@ -0,0 +1,20 @@
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace TextCommandFramework.Services
{
public class PictureService
{
private readonly HttpClient _http;
public PictureService(HttpClient http)
=> _http = http;
public async Task<Stream> GetCatPictureAsync()
{
var resp = await _http.GetAsync("https://cataas.com/cat");
return await resp.Content.ReadAsStreamAsync();
}
}
}

87
Services/PlayerService.cs Normal file
View File

@@ -0,0 +1,87 @@
using Discord;
using Discord.Audio;
using Discord.Commands;
using Discord.WebSocket;
using NAudio.Wave;
using YoutubeExplode;
namespace Kasbot.Services
{
public class PlayerService
{
public Dictionary<SocketGuild, Media> Clients { get; set; }
public PlayerService()
{
Clients = new Dictionary<SocketGuild, Media>();
}
private async Task<Stream> DownloadAudioFromYoutube(string youtubeUrl)
{
var youtube = new YoutubeClient();
var videoId = await youtube.Search.GetVideosAsync(youtubeUrl).FirstOrDefaultAsync();
var streamInfoSet = await youtube.Videos.Streams.GetManifestAsync(videoId.Id);
var highestAudioStreamInfo = streamInfoSet.GetAudioStreams().OrderByDescending(s => s.Bitrate).FirstOrDefault();
var streamVideo = await youtube.Videos.Streams.GetAsync(highestAudioStreamInfo);
var memoryStream = new MemoryStream();
await streamVideo.CopyToAsync(memoryStream);
memoryStream.Position = 0;
return memoryStream;
}
public async Task Play(SocketCommandContext Context, string arguments)
{
IVoiceChannel channel = (Context.User as IVoiceState).VoiceChannel;
var audioStream = await DownloadAudioFromYoutube(arguments);
if (audioStream is null)
{
await Context.Channel.SendMessageAsync("Failed to download audio from YouTube.");
return;
}
var audioClient = await channel.ConnectAsync();
using (var mp3Reader = new StreamMediaFoundationReader(audioStream))
{
var audioOut = audioClient.CreatePCMStream(AudioApplication.Music);
await audioClient.SetSpeakingAsync(true);
var media = new Media
{
AudioClient = audioClient,
Message = Context.Message,
Name = "",
AudioOutStream = audioOut,
};
Clients.Add(Context.Guild, media);
await mp3Reader.CopyToAsync(audioOut);
await audioClient.SetSpeakingAsync(false);
}
}
public async Task Stop(SocketCommandContext Context)
{
if (!Clients.ContainsKey(Context.Guild))
return;
var media = Clients[Context.Guild];
Clients.Remove(Context.Guild);
await Context.Message.DeleteAsync();
await media.Message.DeleteAsync();
await media.AudioOutStream.DisposeAsync();
await media.AudioOutStream.ClearAsync(new CancellationToken());
await media.AudioClient.StopAsync();
}
}
public class Media
{
public string Name { get; set; }
public IAudioClient AudioClient { get; set; }
public AudioOutStream AudioOutStream { get; set; }
public SocketUserMessage Message { get; set; }
}
}