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> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Remove="legacy\**" />
<EmbeddedResource Remove="legacy\**" />
<None Remove="legacy\**" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Discord.Net" Version="3.11.0" /> <PackageReference Include="Discord.Net" Version="3.11.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.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;
using Discord.Commands; using Discord.Commands;
using Kasbot.Models;
using Kasbot.Services; using Kasbot.Services;
using TextCommandFramework.Services; using TextCommandFramework.Services;
@ -30,7 +31,7 @@ namespace TextCommandFramework.Modules
throw new Exception("You must be connect in a voice channel!"); 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)] [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 | | `!leave` | Leaves the current voice channel and clears the queue |
| `!cat` | Sends a random cat pic into the channel :3 | | `!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. 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.Audio;
using Discord.Commands; using Discord.Commands;
using Kasbot.Extensions; using Kasbot.Extensions;
using Kasbot.Models;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
namespace Kasbot.Services namespace Kasbot.Services
{ {
@ -35,13 +35,14 @@ namespace Kasbot.Services
return conn; return conn;
} }
public async Task Play(ShardedCommandContext Context, string arguments) public async Task Play(ShardedCommandContext Context, string arguments, Flags flags)
{ {
var media = new Media() var media = new Media()
{ {
Message = Context.Message, Message = Context.Message,
Search = arguments, Search = arguments,
Name = "" Flags = flags,
Name = "",
}; };
var guildId = Context.Guild.Id; var guildId = Context.Guild.Id;
var userVoiceChannel = (Context.User as IVoiceState).VoiceChannel; var userVoiceChannel = (Context.User as IVoiceState).VoiceChannel;
@ -66,12 +67,6 @@ 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(); media.Search.Trim();
switch (YoutubeService.GetSearchType(media.Search)) switch (YoutubeService.GetSearchType(media.Search))
@ -148,8 +143,11 @@ namespace Kasbot.Services
var audioClient = Clients[guildId].AudioClient; var audioClient = Clients[guildId].AudioClient;
var ffmpeg = CreateStream(); var ffmpeg = CreateStream();
var message = $"⏯ Playing: **{nextMedia.Name}** *({nextMedia.Length.TotalMinutes:00}:{nextMedia.Length.Seconds:00})*"; if (!nextMedia.Flags.Silent)
nextMedia.PlayMessage = await nextMedia.Channel.SendMessageAsync(message); {
var message = $"⏯ Playing: **{nextMedia.Name}** *({nextMedia.Length.TotalMinutes:00}:{nextMedia.Length.Seconds:00})*";
nextMedia.PlayMessage = await nextMedia.Channel.SendMessageAsync(message);
}
if (nextMedia.QueueMessage != null) if (nextMedia.QueueMessage != null)
{ {
@ -215,7 +213,8 @@ namespace Kasbot.Services
await nextMedia.PlayMessage.TryDeleteAsync(); 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(); Clients[guildId].Queue.Dequeue();
await PlayNext(guildId); await PlayNext(guildId);
@ -296,8 +295,8 @@ namespace Kasbot.Services
throw new Exception("The queue is empty!"); throw new Exception("The queue is empty!");
var media = Clients[guildId].Queue.First(); var media = Clients[guildId].Queue.First();
Clients[guildId].Queue.First().Repeat = !media.Repeat; media.Flags.Repeat = !media.Flags.Repeat;
await media.Channel.SendTemporaryMessageAsync(media.Repeat ? "Repeat turned on!" : "Repeat turned off!"); await media.Channel.SendTemporaryMessageAsync(media.Flags.Repeat ? "Repeat turned on!" : "Repeat turned off!");
} }
public async Task Join(ShardedCommandContext Context) public async Task Join(ShardedCommandContext Context)
@ -309,12 +308,4 @@ namespace Kasbot.Services
await CreateConnection(guildId, (Context.User as IVoiceState).VoiceChannel); 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 YoutubeExplode;
using Discord.Rest; using Discord.Rest;
using YoutubeExplode.Videos.Streams; using YoutubeExplode.Videos.Streams;
using Kasbot.Models;
namespace Kasbot.Services namespace Kasbot.Services
{ {
@ -114,11 +115,11 @@ namespace Kasbot.Services
public string Name { get; set; } public string Name { get; set; }
public TimeSpan Length { get; set; } public TimeSpan Length { get; set; }
public Flags Flags { get; set; }
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