This commit is contained in:
Tim Müller
2022-03-29 22:12:22 +02:00
commit 147eed234f
76 changed files with 6489 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using DSharpPlus.SlashCommands;
using DSharpPlus;
using TomatenMusic.Music;
namespace TomatenMusic.Commands.Checks
{
public class OnlyGuildCheck : SlashCheckBaseAttribute
{
public override async Task<bool> ExecuteChecksAsync(InteractionContext ctx)
{
if (ctx.Guild == null)
{
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DSharpPlus.Entities.DiscordInteractionResponseBuilder().WithContent("This Command is only available on Guilds.").AsEphemeral(true));
return false;
}
return true;
}
}
}

View File

@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using DSharpPlus.SlashCommands;
using DSharpPlus.EventArgs;
using DSharpPlus;
using TomatenMusic.Music;
using Emzi0767.Utilities;
using Lavalink4NET;
using Microsoft.Extensions.DependencyInjection;
namespace TomatenMusic.Commands.Checks
{
public class UserInMusicChannelCheck : SlashCheckBaseAttribute
{
public bool _passIfNull { get; set; }
public UserInMusicChannelCheck(bool passIfNull = false)
{
_passIfNull = passIfNull;
}
public override async Task<bool> ExecuteChecksAsync(InteractionContext ctx)
{
IAudioService audioService = TomatenMusicBot.ServiceProvider.GetRequiredService<IAudioService>();
GuildPlayer player = audioService.GetPlayer<GuildPlayer>(ctx.Guild.Id);
bool allowed;
//TODO
if (player != null)
{
allowed = ctx.Member.VoiceState.Channel != null && ctx.Member.VoiceState.Channel.Id == player.VoiceChannelId;
}
else
allowed = _passIfNull;
if (!allowed)
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DSharpPlus.Entities.DiscordInteractionResponseBuilder().WithContent("❌ Please connect to the Bots Channel to use this Command").AsEphemeral(true));
return allowed;
}
}
}

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using DSharpPlus.SlashCommands;
using DSharpPlus;
using TomatenMusic.Music;
namespace TomatenMusic.Commands.Checks
{
class UserInVoiceChannelCheck : SlashCheckBaseAttribute
{
public override async Task<bool> ExecuteChecksAsync(InteractionContext ctx)
{
if (ctx.Member.VoiceState == null || ctx.Member.VoiceState.Channel == null)
{
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DSharpPlus.Entities.DiscordInteractionResponseBuilder().WithContent("You are not in a Voice Channel.").AsEphemeral(true));
return false;
}
return true;
}
}
}

View File

@@ -0,0 +1,289 @@
using System;
using System.Collections.Generic;
using System.Text;
using DSharpPlus;
using DSharpPlus.SlashCommands;
using DSharpPlus.Entities;
using System.Threading.Tasks;
using TomatenMusic.Music;
using TomatenMusic.Music.Entitites;
using TomatenMusic.Commands.Checks;
using TomatenMusic.Util;
using Microsoft.Extensions.Logging;
using TomatenMusic.Prompt;
using TomatenMusic.Prompt.Model;
using TomatenMusic.Prompt.Implementation;
using TomatenMusic.Prompt.Option;
using System.Linq;
using Lavalink4NET;
using Lavalink4NET.Player;
using TomatenMusicCore.Prompt.Implementation;
namespace TomatenMusic.Commands
{
public class MusicCommands : ApplicationCommandModule
{
public IAudioService _audioService { get; set; }
public ILogger<MusicCommands> _logger { get; set; }
public TrackProvider _trackProvider { get; set; }
public MusicCommands(IAudioService audioService, ILogger<MusicCommands> logger, TrackProvider trackProvider)
{
_audioService = audioService;
_logger = logger;
_trackProvider = trackProvider;
}
[SlashCommand("stop", "Stops the current Playback and clears the Queue")]
[OnlyGuildCheck]
[UserInMusicChannelCheck]
public async Task StopCommand(InteractionContext ctx)
{
GuildPlayer player = (GuildPlayer)_audioService.GetPlayer(ctx.Guild.Id);
try
{
await player.DisconnectAsync();
}catch (Exception ex)
{
await ctx.CreateResponseAsync(new DiscordInteractionResponseBuilder
{
Content = $"❌ An Error occured : ``{ex.Message}``",
IsEphemeral = true
});
return;
}
await ctx.CreateResponseAsync(new DiscordInteractionResponseBuilder
{
Content = $"✔️ The Bot was stopped successfully",
IsEphemeral = true
});
}
[SlashCommand("skip", "Skips the current song and plays the next one in the queue")]
[OnlyGuildCheck]
[UserInMusicChannelCheck]
public async Task SkipCommand(InteractionContext ctx)
{
GuildPlayer player = (GuildPlayer)_audioService.GetPlayer(ctx.Guild.Id);
LavalinkTrack oldTrack = player.CurrentTrack;
try
{
await player.SkipAsync();
}
catch (Exception e)
{
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().WithContent($"⛔ Could not Skip Song, Queue Empty!").AsEphemeral(true));
return;
}
_ = ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().WithContent($"Skipped From Song ``{oldTrack.Title}`` To Song:")
.AddEmbed(Common.AsEmbed(player.CurrentTrack, loopType: player.PlayerQueue.LoopType)).AsEphemeral(true));
}
[SlashCommand("fav", "Shows the favorite Song Panel")]
[OnlyGuildCheck]
public async Task FavCommand(InteractionContext ctx)
{
}
[SlashCommand("search", "Searches for a specific query")]
[OnlyGuildCheck]
public async Task SearchCommand(InteractionContext ctx, [Option("query", "The Search Query")] string query)
{
await ctx.DeferAsync(true);
GuildPlayer player = (GuildPlayer)_audioService.GetPlayer(ctx.Guild.Id);
MusicActionResponse response;
try
{
response = await _trackProvider.SearchAsync(query, true);
}catch (Exception e)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent($"❌ Search failed: ``{e.Message}``, ```{e.StackTrace}```"));
return;
}
DiscordPromptBase prompt;
if (!response.IsPlaylist && response.Tracks.Count() == 1)
{
var sPrompt = new SongActionPrompt(response.Tracks.First(), ctx.Member);
prompt = sPrompt;
}
else if (response.IsPlaylist)
{
var sPrompt = new PlaylistSongSelectorPrompt(response.Playlist);
sPrompt.ConfirmCallback = async (tracks) =>
{
var selectPrompt = new SongListActionPrompt(tracks, ctx.Member, sPrompt);
await selectPrompt.UseAsync(sPrompt.Interaction, sPrompt.Message);
};
prompt = sPrompt;
}
else
{
var sPrompt = new SongSelectorPrompt($"Search results for {query}", response.Tracks);
sPrompt.ConfirmCallback = async (tracks) =>
{
var selectPrompt = new SongListActionPrompt(tracks, ctx.Member, sPrompt);
await selectPrompt.UseAsync(sPrompt.Interaction, sPrompt.Message);
};
prompt = sPrompt;
}
await prompt.UseAsync(ctx.Interaction, await ctx.GetOriginalResponseAsync());
}
[SlashCommand("time", "Sets the playing position of the current Song.")]
[OnlyGuildCheck]
[UserInMusicChannelCheck]
public async Task TimeCommand(InteractionContext ctx, [Option("time", "The time formatted like this: Hours: 1h, Minutes: 1m, Seconds 1s")] string time)
{
await ctx.DeferAsync(true);
GuildPlayer player = (GuildPlayer)_audioService.GetPlayer(ctx.Guild.Id);
TimeSpan timeSpan;
try
{
timeSpan = TimeSpan.Parse(time);
}
catch (Exception e)
{
try
{
timeSpan = Common.ToTimeSpan(time);
}
catch (Exception ex)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent("❌ An Error occured when parsing your input."));
return;
}
}
try
{
await player.SeekPositionAsync(timeSpan);
}catch (Exception ex)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent($"❌ An Error occured while Seeking the Track: ``{ex.Message}``"));
return;
}
await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent($"✔️ You successfully set the Song to ``{Common.GetTimestamp(timeSpan)}``."));
}
[SlashCommand("pause", "Pauses or Resumes the current Song.")]
[OnlyGuildCheck]
[UserInMusicChannelCheck]
public async Task PauseCommand(InteractionContext ctx)
{
await ctx.DeferAsync(true);
GuildPlayer player = (GuildPlayer)_audioService.GetPlayer(ctx.Guild.Id);
try
{
await player.TogglePauseAsync();
}catch (Exception ex)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent($"❌ An Error occured changing the pause state of the Song: ``{ex.Message}``"));
return;
}
await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent($"✔️ You {(player.State == PlayerState.Paused ? "successfully paused the Track" : "successfully resumed the Track")}"));
}
[SlashCommand("shuffle", "Shuffles the Queue.")]
[OnlyGuildCheck]
[UserInMusicChannelCheck]
public async Task ShuffleCommand(InteractionContext ctx)
{
await ctx.DeferAsync(true);
GuildPlayer player = _audioService.GetPlayer<GuildPlayer>(ctx.Guild.Id);
try
{
await player.ShuffleAsync();
}
catch (Exception ex)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent($"❌ An error occured while shuffling the Queue: ``{ex.Message}``"));
return;
}
await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent($"😀 You shuffled the Queue."));
}
[SlashCommand("loop", "Sets the loop type of the current player.")]
[OnlyGuildCheck]
[UserInMusicChannelCheck]
public async Task LoopCommand(InteractionContext ctx, [Option("Looptype", "The loop type which the player should be set to")] LoopType type)
{
await ctx.DeferAsync(true);
GuildPlayer player = _audioService.GetPlayer<GuildPlayer>(ctx.Guild.Id);
try
{
await player.SetLoopAsync(type);
}catch (Exception ex)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent($"❌ An error occured while change the Queue Loop: ``{ex.Message}``"));
}
await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent($"😀 You have set the Loop to ``{type.ToString()}``."));
}
[SlashCommand("autoplay", "Enables/Disables Autoplay")]
[OnlyGuildCheck]
[UserInMusicChannelCheck]
public async Task AutoplayCommand(InteractionContext ctx)
{
await ctx.DeferAsync(true);
GuildPlayer player = _audioService.GetPlayer<GuildPlayer>(ctx.Guild.Id);
player.Autoplay = !player.Autoplay;
await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent($"You have set Autoplay to ``{(player.Autoplay ? "Enabled" : "Disabled")}``"));
}
[SlashCommand("queue", "Shows the Queue")]
[OnlyGuildCheck]
public async Task QueueCommand(InteractionContext ctx)
{
await ctx.DeferAsync(true);
GuildPlayer player = _audioService.GetPlayer<GuildPlayer>(ctx.Guild.Id);
if (player == null)
{
_ = ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent("❌ ``Theres currently nothing playing``"));
return;
}
LavalinkTrack track = player.CurrentTrack;
if (track == null)
{
_ = ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent("❌ ``Theres currently nothing playing``"));
return;
}
QueuePrompt prompt = new QueuePrompt(player);
_ = prompt.UseAsync(ctx.Interaction, await ctx.GetOriginalResponseAsync());
}
}
}

View File

@@ -0,0 +1,327 @@
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using Lavalink4NET;
using Lavalink4NET.Player;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using TomatenMusic.Commands.Checks;
using TomatenMusic.Music;
using TomatenMusic.Music.Entitites;
using TomatenMusic.Util;
using TomatenMusicCore.Music;
using TomatenMusicCore.Music.Entities;
namespace TomatenMusic.Commands
{
[SlashCommandGroup("playnow", "Plays the specified Song now and prepends the Current song to the Queue.")]
public class PlayNowGroup : ApplicationCommandModule
{
public IAudioService _audioService { get; set; }
public ILogger<PlayNowGroup> _logger { get; set; }
public TrackProvider _trackProvider { get; set; }
public PlayNowGroup(IAudioService audioService, ILogger<PlayNowGroup> logger, TrackProvider trackProvider)
{
_audioService = audioService;
_logger = logger;
_trackProvider = trackProvider;
}
[SlashCommand("query", "Play a song with its youtube/spotify link. (or youtube search)")]
[UserInVoiceChannelCheck]
[UserInMusicChannelCheck(true)]
[OnlyGuildCheck]
public async Task PlayQueryCommand(InteractionContext ctx, [Option("query", "The song search query.")] string query)
{
var sw = Stopwatch.StartNew();
await ctx.DeferAsync(true);
GuildPlayer player = (GuildPlayer)_audioService.GetPlayer(ctx.Guild.Id);
MusicActionResponse response;
try
{
response = await _trackProvider.SearchAsync(query);
}
catch (Exception ex)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder()
.WithContent($"❌ An error occured while resolving your query: ``{ex.Message}``, ```{ex.StackTrace}```")
);
sw.Stop();
_logger.LogDebug($"Command {ctx.CommandName} took {sw.ElapsedMilliseconds}ms to execute.");
return;
}
try
{
player = await _audioService.JoinAsync<GuildPlayer>(ctx.Guild.Id, ctx.Member.VoiceState.Channel.Id, true);
}
catch (Exception ex)
{
player = _audioService.GetPlayer<GuildPlayer>(ctx.Guild.Id);
if (player == null || player.VoiceChannelId == null)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder()
.WithContent($"❌ An error occured while connecting to your Channel: ``{ex.Message}``")
);
sw.Stop();
_logger.LogDebug($"Command {ctx.CommandName} took {sw.ElapsedMilliseconds}ms to execute.");
return;
}
}
try
{
if (response.IsPlaylist)
{
ILavalinkPlaylist playlist = response.Playlist;
await player.PlayPlaylistNowAsync(playlist);
_ = ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent("Now Playing:").AddEmbed(
Common.AsEmbed(playlist)
));
}
else
{
TomatenMusicTrack track = response.Track;
_ = ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent("Playing Now")
.AddEmbed(Common.AsEmbed(track, player.PlayerQueue.LoopType, 0)));
await player.PlayNowAsync(response.Track);
}
}
catch (Exception ex)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder()
.WithContent($"❌ An error occured while playing your Query: ``{ex.Message}``")
);
sw.Stop();
_logger.LogDebug($"Command {ctx.CommandName} took {sw.ElapsedMilliseconds}ms to execute.");
return;
}
sw.Stop();
_logger.LogDebug($"Command {ctx.CommandName} took {sw.ElapsedMilliseconds}ms to execute.");
}
[SlashCommand("file", "Play a song file. (mp3/mp4)")]
[UserInVoiceChannelCheck]
[UserInMusicChannelCheck(true)]
[OnlyGuildCheck]
public async Task PlayFileCommand(InteractionContext ctx, [Option("File", "The File that should be played.")] DiscordAttachment file)
{
var sw = Stopwatch.StartNew();
await ctx.DeferAsync(true);
GuildPlayer player = (GuildPlayer)_audioService.GetPlayer(ctx.Guild.Id);
MusicActionResponse response;
try
{
response = await _trackProvider.SearchAsync(new Uri(file.Url));
}
catch (Exception ex)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder()
.WithContent($"❌ An error occured while resolving your file: ``{ex.Message}``")
);
sw.Stop();
_logger.LogDebug($"Command {ctx.CommandName} took {sw.ElapsedMilliseconds}ms to execute.");
return;
}
try
{
player = await _audioService.JoinAsync<GuildPlayer>(ctx.Guild.Id, ctx.Member.VoiceState.Channel.Id, true);
}
catch (Exception ex)
{
player = _audioService.GetPlayer<GuildPlayer>(ctx.Guild.Id);
if (player == null || player.VoiceChannelId == null)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder()
.WithContent($"❌ An error occured while connecting to your Channel: ``{ex.Message}``")
);
sw.Stop();
_logger.LogDebug($"Command {ctx.CommandName} took {sw.ElapsedMilliseconds}ms to execute.");
return;
}
}
LavalinkTrack track = response.Track;
_ = ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent("Playing Now")
.AddEmbed(Common.AsEmbed(track, player.PlayerQueue.LoopType, 0)));
await player.PlayNowAsync(response.Track);
sw.Stop();
_logger.LogDebug($"Command {ctx.CommandName} took {sw.ElapsedMilliseconds}ms to execute.");
}
}
[SlashCommandGroup("play", "Queues or plays the Song")]
public class PlayQueueGroup : ApplicationCommandModule
{
public IAudioService _audioService { get; set; }
public ILogger<PlayQueueGroup> _logger { get; set; }
public TrackProvider _trackProvider { get; set; }
public PlayQueueGroup(IAudioService audioService, ILogger<PlayQueueGroup> logger, TrackProvider trackProvider)
{
_audioService = audioService;
_logger = logger;
_trackProvider = trackProvider;
}
[SlashCommand("query", "Play a song with its youtube/spotify link. (or youtube search)")]
[UserInVoiceChannelCheck]
[UserInMusicChannelCheck(true)]
[OnlyGuildCheck]
public async Task PlayQueryCommand(InteractionContext ctx, [Option("query", "The song search query.")] string query)
{
var sw = Stopwatch.StartNew();
await ctx.DeferAsync(true);
GuildPlayer player = (GuildPlayer)_audioService.GetPlayer(ctx.Guild.Id);
MusicActionResponse response;
try
{
response = await _trackProvider.SearchAsync(query);
}
catch (Exception ex)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder()
.WithContent($"❌ An error occured while resolving your query: ``{ex.Message}``, ```{ex.StackTrace}```")
);
sw.Stop();
_logger.LogDebug($"Command {ctx.CommandName} took {sw.ElapsedMilliseconds}ms to execute.");
return;
}
try
{
player = await _audioService.JoinAsync<GuildPlayer>(ctx.Guild.Id, ctx.Member.VoiceState.Channel.Id, true);
}
catch (Exception ex)
{
player = _audioService.GetPlayer<GuildPlayer>(ctx.Guild.Id);
if (player == null || player.VoiceChannelId == null)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder()
.WithContent($"❌ An error occured while connecting to your Channel: ``{ex.Message}``")
);
sw.Stop();
_logger.LogDebug($"Command {ctx.CommandName} took {sw.ElapsedMilliseconds}ms to execute.");
return;
}
}
try
{
if (response.IsPlaylist)
{
ILavalinkPlaylist playlist = response.Playlist;
await player.PlayPlaylistAsync(playlist);
await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent("Now Playing:").AddEmbed(
Common.AsEmbed(playlist)
));
}
else
{
LavalinkTrack track = response.Track;
_ = ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent(player.State == PlayerState.NotPlaying ? "Now Playing:" : "Added to Queue")
.AddEmbed(Common.AsEmbed(track, player.PlayerQueue.LoopType, player.State == PlayerState.NotPlaying ? 0 : player.PlayerQueue.Queue.Count + 1)));
await player.PlayItemAsync(response.Track);
}
}
catch (Exception ex)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder()
.WithContent($"❌ An error occured while playing your Track: ``{ex.Message}``, ```{ex.StackTrace}```")
);
sw.Stop();
_logger.LogDebug($"Command {ctx.CommandName} took {sw.ElapsedMilliseconds}ms to execute.");
return;
}
sw.Stop();
_logger.LogDebug($"Command {ctx.CommandName} took {sw.ElapsedMilliseconds}ms to execute.");
}
[SlashCommand("file", "Play a song file. (mp3/mp4)")]
[UserInVoiceChannelCheck]
[UserInMusicChannelCheck(true)]
[OnlyGuildCheck]
public async Task PlayFileCommand(InteractionContext ctx, [Option("File", "The File that should be played.")] DiscordAttachment file)
{
var sw = Stopwatch.StartNew();
await ctx.DeferAsync(true);
GuildPlayer player = (GuildPlayer)_audioService.GetPlayer(ctx.Guild.Id);
MusicActionResponse response;
try
{
response = await _trackProvider.SearchAsync(new Uri(file.Url));
}
catch (Exception ex)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder()
.WithContent($"❌ An error occured while resolving your file: ``{ex.Message}``")
);
sw.Stop();
_logger.LogDebug($"Command {ctx.CommandName} took {sw.ElapsedMilliseconds}ms to execute.");
return;
}
try
{
player = await _audioService.JoinAsync<GuildPlayer>(ctx.Guild.Id, ctx.Member.VoiceState.Channel.Id, true);
}
catch (Exception ex)
{
player = _audioService.GetPlayer<GuildPlayer>(ctx.Guild.Id);
if (player == null || player.VoiceChannelId == null)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder()
.WithContent($"❌ An error occured while connecting to your Channel: ``{ex.Message}``")
);
sw.Stop();
_logger.LogDebug($"Command {ctx.CommandName} took {sw.ElapsedMilliseconds}ms to execute.");
return;
}
}
LavalinkTrack track = response.Track;
_ = ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent(player.State == PlayerState.NotPlaying ? "Now Playing:" : "Added to Queue")
.AddEmbed(Common.AsEmbed(track, player.PlayerQueue.LoopType, player.State == PlayerState.NotPlaying ? 0 : player.PlayerQueue.Queue.Count + 1)));
await player.PlayItemAsync(response.Track);
sw.Stop();
_logger.LogDebug($"Command {ctx.CommandName} took {sw.ElapsedMilliseconds}ms to execute.");
}
}
}