This commit is contained in:
José Henrique Ivanchechen 2023-03-04 15:50:08 -03:00
parent 205b71dea7
commit 2862ab0f1b
3 changed files with 60 additions and 21 deletions

View File

@ -21,47 +21,55 @@ namespace TextCommandFramework.Modules
[Command("play", RunMode = RunMode.Async)] [Command("play", RunMode = RunMode.Async)]
public async Task PlayAsync([Remainder] string text) public async Task PlayAsync([Remainder] string text)
{ {
var user = Context.User;
if (user.IsBot) return;
Console.WriteLine("Joining on " + Context.Guild.Name); Console.WriteLine("Joining on " + Context.Guild.Name);
string youtubeUrl = text; string youtubeUrl = text;
IVoiceChannel channel = (Context.User as IVoiceState).VoiceChannel; IVoiceChannel channel = (Context.User as IVoiceState).VoiceChannel;
if (channel is null) if (channel is null)
{ {
await Context.Channel.SendMessageAsync("You need to be in a voice channel to use this command."); throw new Exception("You must be connect in a voice channel!");
return;
} }
await PlayerService.Play(Context, text); await PlayerService.Play(Context, text);
} }
[Command("join", RunMode = RunMode.Async)]
public async Task JoinAsync()
{
Console.WriteLine("Joining on " + Context.Guild.Name);
IVoiceChannel channel = (Context.User as IVoiceState).VoiceChannel;
if (channel is null)
{
throw new Exception("You must be connect in a voice channel!");
}
await PlayerService.Join(Context);
}
[Command("stop", RunMode = RunMode.Async)] [Command("stop", RunMode = RunMode.Async)]
public async Task StopAsync() public async Task StopAsync()
{ {
var user = Context.User;
if (user.IsBot) return;
await PlayerService.Stop(Context.Guild.Id); await PlayerService.Stop(Context.Guild.Id);
} }
[Command("skip", RunMode = RunMode.Async)] [Command("skip", RunMode = RunMode.Async)]
public async Task SkipAsync() public async Task SkipAsync()
{ {
var user = Context.User;
if (user.IsBot) return;
await PlayerService.Skip(Context.Guild.Id); await PlayerService.Skip(Context.Guild.Id);
} }
[Command("leave", RunMode = RunMode.Async)] [Command("leave", RunMode = RunMode.Async)]
public async Task LeaveAsync() public async Task LeaveAsync()
{ {
var user = Context.User;
if (user.IsBot) return;
await PlayerService.Leave(Context.Guild.Id); await PlayerService.Leave(Context.Guild.Id);
} }
[Alias("r")]
[Command("repeat", RunMode = RunMode.Async)]
public async Task RepeatAsync()
{
await PlayerService.Repeat(Context.Guild.Id);
}
} }
} }

View File

@ -37,7 +37,7 @@ namespace Kasbot.Services
return conn; return conn;
} }
public async Task Play(SocketCommandContext Context, string arguments) public async Task Play(ShardedCommandContext Context, string arguments)
{ {
var media = new Media() var media = new Media()
{ {
@ -60,6 +60,14 @@ namespace Kasbot.Services
{ {
var startPlay = conn.Queue.Count == 0; var startPlay = conn.Queue.Count == 0;
if (media.Search.Contains("-r"))
{
media.Repeat = true;
media.Search.Replace("-r", "");
}
media.Search.Trim();
switch (YoutubeService.GetSearchType(media.Search)) switch (YoutubeService.GetSearchType(media.Search))
{ {
case SearchType.StringSearch: case SearchType.StringSearch:
@ -115,7 +123,7 @@ namespace Kasbot.Services
return; return;
} }
// since we can't verify if the bot was disconnected by a websocket error, we do this check // since we can't verify if the bot was disconnected by a websocket error, we do this check which should do enough
if (Clients[guildId].AudioClient.ConnectionState == ConnectionState.Disconnected) if (Clients[guildId].AudioClient.ConnectionState == ConnectionState.Disconnected)
{ {
var voiceChannel = Clients[guildId].AudioChannel; var voiceChannel = Clients[guildId].AudioChannel;
@ -201,7 +209,7 @@ namespace Kasbot.Services
await nextMedia.PlayMessage.TryDeleteAsync(); await nextMedia.PlayMessage.TryDeleteAsync();
if (Clients[guildId].Queue.Count > 0) if (Clients[guildId].Queue.Count > 0 && !Clients[guildId].Queue.First().Repeat)
Clients[guildId].Queue.Dequeue(); Clients[guildId].Queue.Dequeue();
await PlayNext(guildId); await PlayNext(guildId);
@ -229,12 +237,12 @@ namespace Kasbot.Services
public Task Skip(ulong guildId) public Task Skip(ulong guildId)
{ {
if (!Clients.ContainsKey(guildId)) if (!Clients.ContainsKey(guildId))
return Task.CompletedTask; throw new Exception("Bot is not connected!");
var media = Clients[guildId]; var media = Clients[guildId];
if (media.CurrentAudioStream == null) if (media.CurrentAudioStream == null)
return Task.CompletedTask; throw new Exception("There is no audio playing!");
media.CurrentAudioStream.Close(); media.CurrentAudioStream.Close();
@ -244,7 +252,7 @@ namespace Kasbot.Services
public async Task Stop(ulong guildId) public async Task Stop(ulong guildId)
{ {
if (!Clients.ContainsKey(guildId)) if (!Clients.ContainsKey(guildId))
return; throw new Exception("Bot is not connected!");
var media = Clients[guildId]; var media = Clients[guildId];
@ -262,7 +270,7 @@ namespace Kasbot.Services
public async Task Leave(ulong guildId) public async Task Leave(ulong guildId)
{ {
if (!Clients.ContainsKey(guildId)) if (!Clients.ContainsKey(guildId))
return; throw new Exception("Bot is not connected!");
await Stop(guildId); await Stop(guildId);
var media = Clients[guildId]; var media = Clients[guildId];
@ -272,6 +280,28 @@ namespace Kasbot.Services
Clients.Remove(guildId); Clients.Remove(guildId);
} }
public async Task Repeat(ulong guildId)
{
if (!Clients.ContainsKey(guildId))
throw new Exception("Bot is not connected!");
if (Clients[guildId].Queue.Count == 0)
throw new Exception("The queue is empty!");
var media = Clients[guildId].Queue.First();
Clients[guildId].Queue.First().Repeat = !media.Repeat;
await media.Channel.SendTemporaryMessageAsync(media.Repeat ? "Repeat turned on!" : "Repeat turned off!");
}
public async Task Join(ShardedCommandContext Context)
{
var guildId = Context.Guild.Id;
if (Clients.ContainsKey(guildId))
return;
await CreateConnection(guildId, (Context.User as IVoiceState).VoiceChannel);
}
} }
public class Connection public class Connection

View File

@ -118,6 +118,7 @@ namespace Kasbot.Services
public VideoId? VideoId { get; set; } public VideoId? VideoId { get; set; }
public RestUserMessage PlayMessage { get; set; } public RestUserMessage PlayMessage { get; set; }
public RestUserMessage? QueueMessage { get; set; } public RestUserMessage? QueueMessage { get; set; }
public bool Repeat { get; set; }
private SocketUserMessage message; private SocketUserMessage message;
public SocketUserMessage Message public SocketUserMessage Message