NadekoBot/src/NadekoBot/Modules/Music/Music.cs

882 lines
36 KiB
C#
Raw Normal View History

using Discord.Commands;
using NadekoBot.Modules.Music.Classes;
using System.Collections.Concurrent;
using Discord.WebSocket;
using NadekoBot.Services;
using System.IO;
using Discord;
using System.Threading.Tasks;
using NadekoBot.Attributes;
using System;
using System.Linq;
using NadekoBot.Extensions;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using NadekoBot.Services.Database.Models;
2016-12-14 19:29:01 +00:00
using System.Text.RegularExpressions;
namespace NadekoBot.Modules.Music
{
[NadekoModule("Music", "!!", AutoLoad = false)]
2016-08-21 22:47:06 +00:00
public partial class Music : DiscordModule
{
2016-12-08 17:46:08 +00:00
public static ConcurrentDictionary<ulong, MusicPlayer> MusicPlayers { get; } = new ConcurrentDictionary<ulong, MusicPlayer>();
public const string MusicDataPath = "data/musicdata";
2016-12-08 17:46:08 +00:00
public Music() : base()
{
//it can fail if its currenctly opened or doesn't exist. Either way i don't care
try { Directory.Delete(MusicDataPath, true); } catch { }
2016-12-16 19:45:46 +00:00
NadekoBot.Client.UserVoiceStateUpdated += Client_UserVoiceStateUpdated;
Directory.CreateDirectory(MusicDataPath);
}
2016-12-16 19:45:46 +00:00
private async Task Client_UserVoiceStateUpdated(SocketUser iusr, SocketVoiceState oldState, SocketVoiceState newState)
2016-12-14 19:48:44 +00:00
{
var usr = iusr as IGuildUser;
if (usr == null ||
oldState.VoiceChannel == newState.VoiceChannel)
2016-12-16 19:45:46 +00:00
return;
2016-12-14 19:48:44 +00:00
MusicPlayer player;
if (!MusicPlayers.TryGetValue(usr.Guild.Id, out player))
2016-12-16 19:45:46 +00:00
return;
2016-12-14 19:48:44 +00:00
2016-12-16 19:45:46 +00:00
var users = await player.PlaybackVoiceChannel.GetUsersAsync().Flatten().ConfigureAwait(false);
2016-12-14 19:48:44 +00:00
if ((player.PlaybackVoiceChannel == newState.VoiceChannel && //if joined first, and player paused, unpause
player.Paused &&
2016-12-16 19:45:46 +00:00
users.Count() == 2) || // keep in mind bot is in the channel (+1)
2016-12-14 19:48:44 +00:00
(player.PlaybackVoiceChannel == oldState.VoiceChannel && // if left last, and player unpaused, pause
!player.Paused &&
2016-12-16 19:45:46 +00:00
users.Count() == 1))
2016-12-14 19:48:44 +00:00
{
player.TogglePause();
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public Task Next(int skipCount = 1)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
if (skipCount < 1)
return Task.CompletedTask;
MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer)) return Task.CompletedTask;
2016-12-16 18:43:57 +00:00
if (musicPlayer.PlaybackVoiceChannel == ((IGuildUser)Context.User).VoiceChannel)
{
while (--skipCount > 0)
{
musicPlayer.RemoveSongAt(0);
}
musicPlayer.Next();
}
return Task.CompletedTask;
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public Task Stop()
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer)) return Task.CompletedTask;
2016-12-16 18:43:57 +00:00
if (((IGuildUser)Context.User).VoiceChannel == musicPlayer.PlaybackVoiceChannel)
{
musicPlayer.Autoplay = false;
musicPlayer.Stop();
}
return Task.CompletedTask;
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public Task Destroy()
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer;
2016-10-15 23:37:29 +00:00
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer)) return Task.CompletedTask;
2016-12-16 18:43:57 +00:00
if (((IGuildUser)Context.User).VoiceChannel == musicPlayer.PlaybackVoiceChannel)
2016-10-15 23:37:29 +00:00
if(MusicPlayers.TryRemove(channel.Guild.Id, out musicPlayer))
musicPlayer.Destroy();
return Task.CompletedTask;
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public Task Pause()
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer;
2016-12-15 15:51:09 +00:00
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer)) return Task.CompletedTask;
2016-12-16 18:43:57 +00:00
if (((IGuildUser)Context.User).VoiceChannel != musicPlayer.PlaybackVoiceChannel)
2016-12-15 15:51:09 +00:00
return Task.CompletedTask;
musicPlayer.TogglePause();
2016-12-15 15:51:09 +00:00
return Task.CompletedTask;
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public async Task Queue([Remainder] string query)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
2016-12-16 18:43:57 +00:00
await QueueSong(((IGuildUser)Context.User), channel, ((IGuildUser)Context.User).VoiceChannel, query).ConfigureAwait(false);
if (channel.Guild.GetCurrentUser().GetPermissions(channel).ManageMessages)
{
await Task.Delay(10000).ConfigureAwait(false);
2016-12-16 19:45:46 +00:00
await Context.Message.DeleteAsync().ConfigureAwait(false);
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public async Task SoundCloudQueue([Remainder] string query)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
2016-12-16 18:43:57 +00:00
await QueueSong(((IGuildUser)Context.User), channel, ((IGuildUser)Context.User).VoiceChannel, query, musicType: MusicType.Soundcloud).ConfigureAwait(false);
if (channel.Guild.GetCurrentUser().GetPermissions(channel).ManageMessages)
{
await Task.Delay(10000).ConfigureAwait(false);
2016-12-16 19:45:46 +00:00
await Context.Message.DeleteAsync().ConfigureAwait(false);
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public async Task ListQueue(int page = 1)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer))
{
2016-12-14 19:29:01 +00:00
await channel.SendErrorAsync("🎵 No active music player.").ConfigureAwait(false);
return;
}
if (page <= 0)
return;
var currentSong = musicPlayer.CurrentSong;
if (currentSong == null)
return;
2016-10-19 10:40:43 +00:00
if (currentSong.TotalLength == TimeSpan.Zero)
{
await musicPlayer.UpdateSongDurationsAsync().ConfigureAwait(false);
}
2016-12-14 19:29:01 +00:00
//var toSend = $"🎵 Currently Playing {currentSong.PrettyName} " + $"`{currentSong.PrettyCurrentTime()}`\n";
2016-12-16 19:45:46 +00:00
var toSend = $"🎵 Currently Playing {currentSong.PrettyName}\n";
if (musicPlayer.RepeatSong)
toSend += "🔂";
else if (musicPlayer.RepeatPlaylist)
toSend += "🔁";
2016-12-14 19:29:01 +00:00
toSend += $" `{musicPlayer.Playlist.Count} tracks currently queued. Showing page {page}:` ";
if (musicPlayer.MaxQueueSize != 0 && musicPlayer.Playlist.Count >= musicPlayer.MaxQueueSize)
toSend += "**Song queue is full!**\n";
else
toSend += "\n";
const int itemsPerPage = 15;
int startAt = itemsPerPage * (page - 1);
var number = 1 + startAt;
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync(toSend + string.Join("\n", musicPlayer.Playlist.Skip(startAt).Take(15).Select(v => $"`{number++}.` {v.PrettyName}"))).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public async Task NowPlaying()
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
2016-12-16 19:45:46 +00:00
MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer))
return;
var currentSong = musicPlayer.CurrentSong;
if (currentSong == null)
return;
2016-12-16 19:45:46 +00:00
var videoid = Regex.Match(currentSong.SongInfo.Query, "<=v=[a-zA-Z0-9-]+(?=&)|(?<=[0-9])[^&\n]+|(?<=v=)[^&\n]+");
2016-10-19 10:40:43 +00:00
if (currentSong.TotalLength == TimeSpan.Zero)
{
await musicPlayer.UpdateSongDurationsAsync().ConfigureAwait(false);
}
2016-12-16 19:45:46 +00:00
var embed = new EmbedBuilder()
.WithAuthor(eab => eab.WithName("Now Playing").WithIconUrl("https://cdn.discordapp.com/attachments/155726317222887425/258605269972549642/music1.png"))
.WithTitle($"{currentSong.SongInfo.Title}")
.WithUrl($"{currentSong.SongInfo.Query}")
.WithDescription($"{currentSong.PrettyCurrentTime()}")
.WithFooter(ef => ef.WithText($"{currentSong.PrettyProvider} | {currentSong.PrettyUser}"))
.WithColor(NadekoBot.OkColor);
if (currentSong.SongInfo.Provider.Equals("YouTube", StringComparison.OrdinalIgnoreCase))
{
embed.WithThumbnailUrl($"https://img.youtube.com/vi/{videoid}/0.jpg");
}
else if (currentSong.SongInfo.Provider.Equals("SoundCloud", StringComparison.OrdinalIgnoreCase))
{
embed.WithThumbnailUrl($"{currentSong.SongInfo.AlbumArt}");
}
await channel.EmbedAsync(embed).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public async Task Volume(int val)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer))
return;
2016-12-16 18:43:57 +00:00
if (((IGuildUser)Context.User).VoiceChannel != musicPlayer.PlaybackVoiceChannel)
return;
if (val < 0)
return;
var volume = musicPlayer.SetVolume(val);
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync($"🎵 Volume set to {volume}%").ConfigureAwait(false);
}
2016-08-30 01:22:15 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-30 01:22:15 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public async Task Defvol([Remainder] int val)
2016-08-30 01:22:15 +00:00
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
2016-08-30 01:22:15 +00:00
if (val < 0 || val > 100)
{
2016-12-14 19:29:01 +00:00
await channel.SendErrorAsync("Volume number invalid. Must be between 0 and 100").ConfigureAwait(false);
2016-08-30 01:22:15 +00:00
return;
}
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(channel.Guild.Id, set => set).DefaultMusicVolume = val / 100.0f;
2016-08-30 01:22:15 +00:00
uow.Complete();
}
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync($"🎵 Default volume set to {val}%").ConfigureAwait(false);
2016-08-30 01:22:15 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public async Task ShufflePlaylist()
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer))
return;
2016-12-16 18:43:57 +00:00
if (((IGuildUser)Context.User).VoiceChannel != musicPlayer.PlaybackVoiceChannel)
return;
if (musicPlayer.Playlist.Count < 2)
{
2016-12-14 19:29:01 +00:00
await channel.SendErrorAsync("💢 Not enough songs in order to perform the shuffle.").ConfigureAwait(false);
return;
}
musicPlayer.Shuffle();
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync("🎵 Songs shuffled.").ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public async Task Playlist([Remainder] string playlist)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
var arg = playlist;
if (string.IsNullOrWhiteSpace(arg))
return;
2016-12-16 18:43:57 +00:00
if (((IGuildUser)Context.User).VoiceChannel?.Guild != channel.Guild)
{
2016-12-14 19:29:01 +00:00
await channel.SendErrorAsync("💢 You need to be in a **voice channel** on this server.\n If you are already in a voice channel, try rejoining it.").ConfigureAwait(false);
return;
}
2016-12-08 17:46:08 +00:00
var plId = (await NadekoBot.Google.GetPlaylistIdsByKeywordsAsync(arg).ConfigureAwait(false)).FirstOrDefault();
if (plId == null)
{
2016-12-14 19:29:01 +00:00
await channel.SendErrorAsync("No search results for that query.");
return;
}
2016-12-08 17:46:08 +00:00
var ids = await NadekoBot.Google.GetPlaylistTracksAsync(plId, 500).ConfigureAwait(false);
if (!ids.Any())
{
2016-12-14 19:29:01 +00:00
await channel.SendErrorAsync($"🎵 Failed to find any songs.").ConfigureAwait(false);
return;
}
var idArray = ids as string[] ?? ids.ToArray();
var count = idArray.Length;
var msg =
2016-12-14 19:29:01 +00:00
await channel.SendMessageAsync($"🎵 Attempting to queue **{count}** songs".SnPl(count) + "...").ConfigureAwait(false);
foreach (var id in idArray)
{
try
{
2016-12-16 18:43:57 +00:00
await QueueSong(((IGuildUser)Context.User), channel, ((IGuildUser)Context.User).VoiceChannel, id, true).ConfigureAwait(false);
}
catch (SongNotFoundException) { }
catch { break; }
}
2016-12-14 19:29:01 +00:00
await msg.ModifyAsync(m => m.Content = "✅ Playlist queue complete.").ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public async Task SoundCloudPl([Remainder] string pl)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
pl = pl?.Trim();
if (string.IsNullOrWhiteSpace(pl))
return;
using (var http = new HttpClient())
{
var scvids = JObject.Parse(await http.GetStringAsync($"http://api.soundcloud.com/resolve?url={pl}&client_id={NadekoBot.Credentials.SoundCloudClientId}").ConfigureAwait(false))["tracks"].ToObject<SoundCloudVideo[]>();
2016-12-16 18:43:57 +00:00
await QueueSong(((IGuildUser)Context.User), channel, ((IGuildUser)Context.User).VoiceChannel, scvids[0].TrackLink).ConfigureAwait(false);
MusicPlayer mp;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out mp))
return;
foreach (var svideo in scvids.Skip(1))
{
try
{
mp.AddSong(new Song(new Classes.SongInfo
{
Title = svideo.FullName,
Provider = "SoundCloud",
Uri = svideo.StreamLink,
ProviderType = MusicType.Normal,
Query = svideo.TrackLink,
2016-12-16 18:43:57 +00:00
}), ((IGuildUser)Context.User).Username);
}
catch (PlaylistFullException) { break; }
}
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[OwnerOnly]
2016-12-16 19:45:46 +00:00
public async Task LocalPl([Remainder] string directory)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
var arg = directory;
if (string.IsNullOrWhiteSpace(arg))
return;
try
{
var dir = new DirectoryInfo(arg);
var fileEnum = dir.GetFiles("*", SearchOption.AllDirectories)
.Where(x => !x.Attributes.HasFlag(FileAttributes.Hidden | FileAttributes.System));
foreach (var file in fileEnum)
{
try
{
2016-12-16 18:43:57 +00:00
await QueueSong(((IGuildUser)Context.User), channel, ((IGuildUser)Context.User).VoiceChannel, file.FullName, true, MusicType.Local).ConfigureAwait(false);
}
catch (PlaylistFullException)
{
break;
}
catch { }
}
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync("🎵 Directory queue complete.").ConfigureAwait(false);
}
catch { }
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public async Task Radio(string radio_link)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
if (((IGuildUser)Context.User).VoiceChannel?.Guild != channel.Guild)
{
2016-12-14 19:29:01 +00:00
await channel.SendErrorAsync("💢 You need to be in a voice channel on this server.\n If you are already in a voice channel, try rejoining it.").ConfigureAwait(false);
return;
}
2016-12-16 18:43:57 +00:00
await QueueSong(((IGuildUser)Context.User), channel, ((IGuildUser)Context.User).VoiceChannel, radio_link, musicType: MusicType.Radio).ConfigureAwait(false);
if (channel.Guild.GetCurrentUser().GetPermissions(channel).ManageMessages)
{
await Task.Delay(10000).ConfigureAwait(false);
2016-12-16 19:45:46 +00:00
await Context.Message.DeleteAsync().ConfigureAwait(false);
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[OwnerOnly]
2016-12-16 19:45:46 +00:00
public async Task Local([Remainder] string path)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
var arg = path;
if (string.IsNullOrWhiteSpace(arg))
return;
2016-12-16 18:43:57 +00:00
await QueueSong(((IGuildUser)Context.User), channel, ((IGuildUser)Context.User).VoiceChannel, path, musicType: MusicType.Local).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public async Task Move()
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer;
2016-12-16 18:43:57 +00:00
var voiceChannel = ((IGuildUser)Context.User).VoiceChannel;
if (voiceChannel == null || voiceChannel.Guild != channel.Guild || !MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer))
return;
await musicPlayer.MoveToVoiceChannel(voiceChannel);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-10-19 08:10:57 +00:00
[Priority(0)]
2016-12-16 19:45:46 +00:00
public async Task Remove(int num)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer))
{
return;
}
2016-12-16 18:43:57 +00:00
if (((IGuildUser)Context.User).VoiceChannel != musicPlayer.PlaybackVoiceChannel)
return;
if (num <= 0 || num > musicPlayer.Playlist.Count)
return;
var song = (musicPlayer.Playlist as List<Song>)?[num - 1];
musicPlayer.RemoveSongAt(num - 1);
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync($"🎵 Track {song.PrettyName} at position `#{num}` has been **removed**.").ConfigureAwait(false);
}
2016-09-03 13:04:07 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-10-19 08:10:57 +00:00
[Priority(1)]
2016-12-16 19:45:46 +00:00
public async Task Remove(string all)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
if (all.Trim().ToUpperInvariant() != "ALL")
return;
MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer)) return;
musicPlayer.ClearQueue();
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync($"🎵 Queue cleared!").ConfigureAwait(false);
return;
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public async Task MoveSong([Remainder] string fromto)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer))
{
return;
}
fromto = fromto?.Trim();
var fromtoArr = fromto.Split('>');
int n1;
int n2;
var playlist = musicPlayer.Playlist as List<Song> ?? musicPlayer.Playlist.ToList();
if (fromtoArr.Length != 2 || !int.TryParse(fromtoArr[0], out n1) ||
!int.TryParse(fromtoArr[1], out n2) || n1 < 1 || n2 < 1 || n1 == n2 ||
n1 > playlist.Count || n2 > playlist.Count)
{
2016-12-14 19:29:01 +00:00
await channel.SendErrorAsync("Invalid input.").ConfigureAwait(false);
return;
}
var s = playlist[n1 - 1];
playlist.Insert(n2 - 1, s);
var nn1 = n2 < n1 ? n1 : n1 - 1;
playlist.RemoveAt(nn1);
2016-12-16 19:45:46 +00:00
var embed = new EmbedBuilder()
.WithTitle($"{s.SongInfo.Title.TrimTo(70)}")
.WithUrl($"{s.SongInfo.Query}")
.WithAuthor(eab => eab.WithName("Song Moved").WithIconUrl("https://cdn.discordapp.com/attachments/155726317222887425/258605269972549642/music1.png"))
.AddField(fb => fb.WithName("**From Position**").WithValue($"#{n1}").WithIsInline(true))
.AddField(fb => fb.WithName("**To Position**").WithValue($"#{n2}").WithIsInline(true))
.WithColor(NadekoBot.OkColor);
2016-12-16 18:43:57 +00:00
await channel.EmbedAsync(embed).ConfigureAwait(false);
2016-12-14 19:29:01 +00:00
//await channel.SendConfirmAsync($"🎵Moved {s.PrettyName} `from #{n1} to #{n2}`").ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public async Task SetMaxQueue(uint size)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer))
{
return;
}
musicPlayer.MaxQueueSize = size;
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync($"🎵 Max queue set to {(size == 0 ? ("unlimited") : size + " tracks")}.");
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public async Task ReptCurSong()
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer))
return;
var currentSong = musicPlayer.CurrentSong;
if (currentSong == null)
return;
var currentValue = musicPlayer.ToggleRepeatSong();
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync(currentValue ?
$"🔂 Repeating track: {currentSong.PrettyName}" :
$"🔂 Current track repeat stopped.")
.ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public async Task RepeatPl()
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer))
return;
var currentValue = musicPlayer.ToggleRepeatPlaylist();
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync($"🔁 Repeat playlist {(currentValue ? "**enabled**." : "**disabled**.")}").ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public async Task Save([Remainder] string name)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer))
return;
var curSong = musicPlayer.CurrentSong;
var songs = musicPlayer.Playlist.Append(curSong)
.Select(s=> new PlaylistSong() {
Provider = s.SongInfo.Provider,
ProviderType = s.SongInfo.ProviderType,
Title = s.SongInfo.Title,
Uri = s.SongInfo.Uri,
Query = s.SongInfo.Query,
}).ToList();
MusicPlaylist playlist;
using (var uow = DbHandler.UnitOfWork())
{
playlist = new MusicPlaylist
{
Name = name,
2016-12-16 18:43:57 +00:00
Author = Context.User.Username,
AuthorId = Context.User.Id,
Songs = songs,
};
uow.MusicPlaylists.Add(playlist);
await uow.CompleteAsync().ConfigureAwait(false);
}
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync(($"🎵 Saved playlist as **{name}**, ID: {playlist.Id}.")).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public async Task Load([Remainder] int id)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
MusicPlaylist mpl;
using (var uow = DbHandler.UnitOfWork())
{
mpl = uow.MusicPlaylists.GetWithSongs(id);
}
if (mpl == null)
{
2016-12-14 19:29:01 +00:00
await channel.SendErrorAsync("Can't find playlist with that ID.").ConfigureAwait(false);
return;
}
2016-10-05 05:01:19 +00:00
IUserMessage msg = null;
2016-12-14 19:29:01 +00:00
try { msg = await channel.SendMessageAsync($"🎶 Attempting to load **{mpl.Songs.Count}** songs...").ConfigureAwait(false); } catch (Exception ex) { _log.Warn(ex); }
foreach (var item in mpl.Songs)
{
2016-12-16 18:43:57 +00:00
var usr = (IGuildUser)Context.User;
try
{
await QueueSong(usr, channel, usr.VoiceChannel, item.Query, true, item.ProviderType).ConfigureAwait(false);
}
catch (SongNotFoundException) { }
catch { break; }
}
2016-10-05 05:01:19 +00:00
if (msg != null)
2016-12-14 19:29:01 +00:00
await msg.ModifyAsync(m => m.Content = $"✅ Done loading playlist **{mpl.Name}**.").ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public async Task Playlists([Remainder] int num = 1)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
if (num <= 0)
return;
List<MusicPlaylist> playlists;
using (var uow = DbHandler.UnitOfWork())
{
playlists = uow.MusicPlaylists.GetPlaylistsOnPage(num);
}
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync($@"🎶 **Page {num} of saved playlists:**
2016-12-14 19:29:01 +00:00
" + string.Join("\n", playlists.Select(r => $"`#{r.Id}` - **{r.Name}** by __{r.Author}__ ({r.Songs.Count} songs)"))).ConfigureAwait(false);
}
//todo only author or owner
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public async Task DeletePlaylist([Remainder] int id)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
bool success = false;
MusicPlaylist pl = null;
2016-10-02 01:00:03 +00:00
try
{
2016-10-02 01:00:03 +00:00
using (var uow = DbHandler.UnitOfWork())
{
2016-10-02 01:00:03 +00:00
pl = uow.MusicPlaylists.Get(id);
if (pl != null)
{
2016-12-16 18:43:57 +00:00
if (NadekoBot.Credentials.IsOwner(Context.User) || pl.AuthorId == Context.User.Id)
2016-10-02 01:00:03 +00:00
{
uow.MusicPlaylists.Remove(pl);
await uow.CompleteAsync().ConfigureAwait(false);
success = true;
}
else
success = false;
}
}
2016-10-02 01:00:03 +00:00
if (!success)
2016-12-14 19:29:01 +00:00
await channel.SendErrorAsync("Failed to delete that playlist. It either doesn't exist, or you are not its author.").ConfigureAwait(false);
2016-10-02 01:00:03 +00:00
else
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync("🗑 Playlist successfully **deleted**.").ConfigureAwait(false);
2016-10-02 01:00:03 +00:00
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public async Task Goto(int time)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer))
return;
2016-12-16 18:43:57 +00:00
if (((IGuildUser)Context.User).VoiceChannel != musicPlayer.PlaybackVoiceChannel)
return;
if (time < 0)
return;
var currentSong = musicPlayer.CurrentSong;
if (currentSong == null)
return;
//currentSong.PrintStatusMessage = false;
var gotoSong = currentSong.Clone();
gotoSong.SkipTo = time;
musicPlayer.AddSong(gotoSong, 0);
musicPlayer.Next();
var minutes = (time / 60).ToString();
var seconds = (time % 60).ToString();
if (minutes.Length == 1)
minutes = "0" + minutes;
if (seconds.Length == 1)
seconds = "0" + seconds;
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync($"Skipped to `{minutes}:{seconds}`").ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public async Task GetLink(int index = 0)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer))
return;
if (index < 0)
return;
if (index > 0)
{
var selSong = musicPlayer.Playlist.DefaultIfEmpty(null).ElementAtOrDefault(index - 1);
if (selSong == null)
{
2016-12-14 19:29:01 +00:00
await channel.SendErrorAsync("Could not select song, likely wrong index");
}
else
{
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync($"🎶 Selected song **{selSong.SongInfo.Title}**: <{selSong.SongInfo.Query}>").ConfigureAwait(false);
}
}
else
{
var curSong = musicPlayer.CurrentSong;
if (curSong == null)
return;
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync($"🎶 Current song **{curSong.SongInfo.Title}**: <{curSong.SongInfo.Query}>").ConfigureAwait(false);
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public async Task Autoplay()
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer))
return;
if (!musicPlayer.ToggleAutoplay())
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync("❌ Autoplay disabled.").ConfigureAwait(false);
else
2016-12-14 19:29:01 +00:00
await channel.SendConfirmAsync("✅ Autoplay enabled.").ConfigureAwait(false);
}
public static async Task QueueSong(IGuildUser queuer, ITextChannel textCh, IVoiceChannel voiceCh, string query, bool silent = false, MusicType musicType = MusicType.Normal)
{
if (voiceCh == null || voiceCh.Guild != textCh.Guild)
{
if (!silent)
2016-12-14 19:29:01 +00:00
await textCh.SendErrorAsync("💢 You need to be in a voice channel on this server.\n If you are already in a voice channel, try rejoining.").ConfigureAwait(false);
throw new ArgumentNullException(nameof(voiceCh));
}
if (string.IsNullOrWhiteSpace(query) || query.Length < 3)
throw new ArgumentException("💢 Invalid query for queue song.", nameof(query));
var musicPlayer = MusicPlayers.GetOrAdd(textCh.Guild.Id, server =>
{
2016-08-26 01:25:55 +00:00
float vol = 1;// SpecificConfigurations.Default.Of(server.Id).DefaultMusicVolume;
2016-08-30 01:11:25 +00:00
using (var uow = DbHandler.UnitOfWork())
{
vol = uow.GuildConfigs.For(textCh.Guild.Id, set => set).DefaultMusicVolume;
2016-08-30 01:11:25 +00:00
}
var mp = new MusicPlayer(voiceCh, vol);
IUserMessage playingMessage = null;
2016-12-16 19:45:46 +00:00
IUserMessage lastFinishedMessage = null;
mp.OnCompleted += async (s, song) =>
{
if (song.PrintStatusMessage)
{
try
{
if (lastFinishedMessage != null)
await lastFinishedMessage.DeleteAsync().ConfigureAwait(false);
if (playingMessage != null)
await playingMessage.DeleteAsync().ConfigureAwait(false);
2016-12-14 19:29:01 +00:00
try { lastFinishedMessage = await textCh.SendConfirmAsync($"🎵 Finished {song.PrettyName}").ConfigureAwait(false); } catch { }
if (mp.Autoplay && mp.Playlist.Count == 0 && song.SongInfo.Provider == "YouTube")
{
await QueueSong(queuer.Guild.GetCurrentUser(), textCh, voiceCh, (await NadekoBot.Google.GetRelatedVideosAsync(song.SongInfo.Query, 4)).ToList().Shuffle().FirstOrDefault(), silent, musicType).ConfigureAwait(false);
}
}
2016-10-05 05:01:19 +00:00
catch { }
}
};
mp.OnStarted += async (s, song) =>
{
if (song.PrintStatusMessage)
{
var sender = s as MusicPlayer;
if (sender == null)
return;
2016-12-14 19:48:44 +00:00
var msgTxt = $"🎵 Playing {song.PrettyName}\t `Vol: {(int)(sender.Volume * 100)}%`";
2016-12-14 19:29:01 +00:00
try { playingMessage = await textCh.SendConfirmAsync(msgTxt).ConfigureAwait(false); } catch { }
}
};
2016-12-16 19:45:46 +00:00
mp.OnPauseChanged += async (paused) =>
2016-12-14 19:48:44 +00:00
{
try
{
if (paused)
await textCh.SendConfirmAsync("🎵 Music playback **paused**.").ConfigureAwait(false);
else
await textCh.SendConfirmAsync("🎵 Music playback **resumed**.").ConfigureAwait(false);
}
catch { }
};
return mp;
});
Song resolvedSong;
try
{
musicPlayer.ThrowIfQueueFull();
resolvedSong = await Song.ResolveSong(query, musicType).ConfigureAwait(false);
if (resolvedSong == null)
throw new SongNotFoundException();
musicPlayer.AddSong(resolvedSong, queuer.Username);
}
catch (PlaylistFullException)
{
2016-12-14 19:29:01 +00:00
try { await textCh.SendConfirmAsync($"🎵 Queue is full at **{musicPlayer.MaxQueueSize}/{musicPlayer.MaxQueueSize}**. "); } catch { }
throw;
}
if (!silent)
{
2016-10-05 05:01:19 +00:00
try
{
2016-12-14 19:29:01 +00:00
var queuedMessage = await textCh.SendConfirmAsync($"🎵 Queued **{resolvedSong.SongInfo.Title.TrimTo(55)}** at `#{musicPlayer.Playlist.Count + 1}`").ConfigureAwait(false);
2016-10-05 05:01:19 +00:00
var t = Task.Run(async () =>
{
2016-10-05 05:01:19 +00:00
try
{
await Task.Delay(10000).ConfigureAwait(false);
2016-10-05 05:01:19 +00:00
await queuedMessage.DeleteAsync().ConfigureAwait(false);
}
catch { }
}).ConfigureAwait(false);
}
catch { } // if queued message sending fails, don't attempt to delete it
}
}
}
2016-12-14 19:29:01 +00:00
}