adding flags

This commit is contained in:
José Henrique Ivanchechen 2023-07-30 23:37:37 -03:00
parent 59aa6a13a3
commit 9df9090812
7 changed files with 73 additions and 24 deletions

View File

@ -7,6 +7,12 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Remove="legacy\**" />
<EmbeddedResource Remove="legacy\**" />
<None Remove="legacy\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Discord.Net" Version="3.11.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />

14
Models/Connection.cs Normal file
View File

@ -0,0 +1,14 @@
using Discord;
using Discord.Audio;
using Kasbot.Services;
namespace Kasbot.Models
{
public class Connection
{
public IAudioClient AudioClient { get; set; }
public IVoiceChannel AudioChannel { get; set; }
public Stream? CurrentAudioStream { get; set; }
public Queue<Media> Queue { get; set; } = new Queue<Media>();
}
}

30
Models/Flags.cs Normal file
View File

@ -0,0 +1,30 @@
namespace Kasbot.Models
{
public class Flags
{
public bool Silent { get; set; }
public bool Repeat { get; set; }
public Flags() { }
public Flags(string command)
{
this.Parse(command);
}
public void Parse(string command)
{
if (command.Contains("-s") ||
command.Contains("-silent"))
{
Silent = true;
}
if (command.Contains("-r") ||
command.Contains("-repeat"))
{
Repeat = true;
}
}
}
}

View File

@ -1,5 +1,6 @@
using Discord;
using Discord.Commands;
using Kasbot.Models;
using Kasbot.Services;
using TextCommandFramework.Services;
@ -30,7 +31,7 @@ namespace TextCommandFramework.Modules
throw new Exception("You must be connect in a voice channel!");
}
await PlayerService.Play(Context, text);
await PlayerService.Play(Context, text, new Flags(text));
}
[Command("join", RunMode = RunMode.Async)]

View File

@ -19,4 +19,10 @@ dotnet bin/Kasbot.dll
| `!leave` | Leaves the current voice channel and clears the queue |
| `!cat` | Sends a random cat pic into the channel :3 |
## Play flags
| Flag | Description
| -- | -- |
| `-s`, `-silent` | Don't send play message into channel |
| `-r`, `-repeat` | Repeat music |
You can change the command prefix by putting another symbol in `COMMAND_PREFIX` environment variable.

View File

@ -2,8 +2,8 @@
using Discord.Audio;
using Discord.Commands;
using Kasbot.Extensions;
using Kasbot.Models;
using System.Diagnostics;
using System.Linq;
namespace Kasbot.Services
{
@ -35,13 +35,14 @@ namespace Kasbot.Services
return conn;
}
public async Task Play(ShardedCommandContext Context, string arguments)
public async Task Play(ShardedCommandContext Context, string arguments, Flags flags)
{
var media = new Media()
{
Message = Context.Message,
Search = arguments,
Name = ""
Flags = flags,
Name = "",
};
var guildId = Context.Guild.Id;
var userVoiceChannel = (Context.User as IVoiceState).VoiceChannel;
@ -66,12 +67,6 @@ namespace Kasbot.Services
{
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))
@ -148,8 +143,11 @@ namespace Kasbot.Services
var audioClient = Clients[guildId].AudioClient;
var ffmpeg = CreateStream();
if (!nextMedia.Flags.Silent)
{
var message = $"⏯ Playing: **{nextMedia.Name}** *({nextMedia.Length.TotalMinutes:00}:{nextMedia.Length.Seconds:00})*";
nextMedia.PlayMessage = await nextMedia.Channel.SendMessageAsync(message);
}
if (nextMedia.QueueMessage != null)
{
@ -215,7 +213,8 @@ namespace Kasbot.Services
await nextMedia.PlayMessage.TryDeleteAsync();
if (Clients[guildId].Queue.Count > 0 && !Clients[guildId].Queue.First().Repeat)
if (Clients[guildId].Queue.Count > 0 &&
!Clients[guildId].Queue.First().Flags.Repeat)
Clients[guildId].Queue.Dequeue();
await PlayNext(guildId);
@ -296,8 +295,8 @@ namespace Kasbot.Services
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!");
media.Flags.Repeat = !media.Flags.Repeat;
await media.Channel.SendTemporaryMessageAsync(media.Flags.Repeat ? "Repeat turned on!" : "Repeat turned off!");
}
public async Task Join(ShardedCommandContext Context)
@ -309,12 +308,4 @@ namespace Kasbot.Services
await CreateConnection(guildId, (Context.User as IVoiceState).VoiceChannel);
}
}
public class Connection
{
public IAudioClient AudioClient { get; set; }
public IVoiceChannel AudioChannel { get; set; }
public Stream? CurrentAudioStream { get; set; }
public Queue<Media> Queue { get; set; } = new Queue<Media>();
}
}

View File

@ -3,6 +3,7 @@ using YoutubeExplode.Videos;
using YoutubeExplode;
using Discord.Rest;
using YoutubeExplode.Videos.Streams;
using Kasbot.Models;
namespace Kasbot.Services
{
@ -114,11 +115,11 @@ namespace Kasbot.Services
public string Name { get; set; }
public TimeSpan Length { get; set; }
public Flags Flags { get; set; }
public VideoId? VideoId { get; set; }
public RestUserMessage PlayMessage { get; set; }
public RestUserMessage? QueueMessage { get; set; }
public bool Repeat { get; set; }
private SocketUserMessage message;
public SocketUserMessage Message