adding shuffle

This commit is contained in:
José Henrique Ivanchechen 2023-12-24 14:33:52 -03:00
parent 9ef8af8e23
commit f0a81e5305
3 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,21 @@
using Discord.WebSocket;
namespace Kasbot.Extensions
{
public static class IListExtensions
{
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
{
var rng = new Random();
var buffer = source.ToList();
for (int i = 0; i < buffer.Count; i++)
{
int j = rng.Next(i, buffer.Count);
yield return buffer[j];
buffer[j] = buffer[i];
}
}
}
}

View File

@ -56,6 +56,7 @@ namespace Kasbot.Modules
await PlayerService.Stop(Context.Guild.Id);
}
[Alias("s", "next", "n")]
[Command("skip", RunMode = RunMode.Async)]
public async Task SkipAsync()
{
@ -74,5 +75,11 @@ namespace Kasbot.Modules
{
await PlayerService.Repeat(Context.Guild.Id);
}
[Command("shuffle", RunMode = RunMode.Async)]
public async Task Shuffle()
{
await PlayerService.Shuffle(Context.Guild.Id);
}
}
}

View File

@ -260,5 +260,25 @@ namespace Kasbot.Services
await CreateConnection(guildId, (Context.User as IVoiceState).VoiceChannel);
}
public async Task Shuffle(ulong guildId)
{
if (!Clients.ContainsKey(guildId))
throw new Exception("Bot is not connected!");
var media = Clients[guildId];
if (media.Queue.Count == 0)
throw new Exception("The queue is empty!");
var shuffled = media.Queue.Shuffle();
var newQueue = new Queue<Media>();
shuffled.ToList().ForEach(m => newQueue.Enqueue(m));
media.Queue = newQueue;
await media.Queue.First().Channel.SendTemporaryMessageAsync(":call_me: Queue shuffled!");
}
}
}