Loading almost done
This commit is contained in:
parent
4e2820080b
commit
4f898dc653
@ -44,6 +44,14 @@ namespace NadekoBot.Classes
|
||||
}
|
||||
}
|
||||
|
||||
internal IList<T> FindAll<T>(Expression<Func<T, bool>> p) where T : IDataModel, new()
|
||||
{
|
||||
using (var conn = new SQLiteConnection(FilePath))
|
||||
{
|
||||
return conn.Table<T>().Where(p).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
internal void DeleteAll<T>() where T : IDataModel
|
||||
{
|
||||
using (var conn = new SQLiteConnection(FilePath))
|
||||
|
@ -16,6 +16,10 @@ namespace NadekoBot.Classes.Music
|
||||
{
|
||||
public string Provider { get; internal set; }
|
||||
public MusicType ProviderType { get; internal set; }
|
||||
/// <summary>
|
||||
/// Will be set only if the providertype is normal
|
||||
/// </summary>
|
||||
public string Query { get; internal set; }
|
||||
public string Title { get; internal set; }
|
||||
public string Uri { get; internal set; }
|
||||
}
|
||||
@ -233,6 +237,7 @@ namespace NadekoBot.Classes.Music
|
||||
Provider = "SoundCloud",
|
||||
Uri = svideo.StreamLink,
|
||||
ProviderType = musicType,
|
||||
Query = query,
|
||||
});
|
||||
}
|
||||
var links = await SearchHelper.FindYoutubeUrlByKeywords(query);
|
||||
@ -252,6 +257,7 @@ namespace NadekoBot.Classes.Music
|
||||
Title = video.Title.Substring(0, video.Title.Length - 10), // removing trailing "- You Tube"
|
||||
Provider = "YouTube",
|
||||
Uri = video.Uri,
|
||||
Query = query,
|
||||
ProviderType = musicType,
|
||||
});
|
||||
}
|
||||
|
@ -1,13 +1,11 @@
|
||||
using SQLite;
|
||||
|
||||
namespace NadekoBot.Classes._DataModels
|
||||
namespace NadekoBot.Classes._DataModels
|
||||
{
|
||||
internal class SongInfo : IDataModel
|
||||
{
|
||||
public string Provider { get; internal set; }
|
||||
public int ProviderType { get; internal set; }
|
||||
public string Title { get; internal set; }
|
||||
[Unique]
|
||||
public string Uri { get; internal set; }
|
||||
public string Query { get; internal set; }
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
using Discord.Commands;
|
||||
using Discord.Modules;
|
||||
using NadekoBot.Classes;
|
||||
using NadekoBot.Classes._DataModels;
|
||||
using NadekoBot.Classes.Music;
|
||||
using NadekoBot.Classes.Permissions;
|
||||
using NadekoBot.Extensions;
|
||||
@ -10,7 +11,6 @@ using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Timer = System.Timers.Timer;
|
||||
|
||||
namespace NadekoBot.Modules
|
||||
{
|
||||
@ -20,24 +20,8 @@ namespace NadekoBot.Modules
|
||||
public static ConcurrentDictionary<Server, MusicPlayer> MusicPlayers = new ConcurrentDictionary<Server, MusicPlayer>();
|
||||
public static ConcurrentDictionary<ulong, float> DefaultMusicVolumes = new ConcurrentDictionary<ulong, float>();
|
||||
|
||||
private readonly Timer setgameTimer = new Timer();
|
||||
|
||||
private bool setgameEnabled = false;
|
||||
|
||||
public Music()
|
||||
{
|
||||
|
||||
setgameTimer.Interval = 20000;
|
||||
setgameTimer.Elapsed += (s, e) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var num = MusicPlayers.Count(kvp => kvp.Value.CurrentSong != null);
|
||||
NadekoBot.Client.SetGame($"{num} songs".SnPl(num) + $", {MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count())} queued");
|
||||
}
|
||||
catch { }
|
||||
};
|
||||
|
||||
// ready for 1.0
|
||||
//NadekoBot.Client.UserUpdated += (s, e) =>
|
||||
//{
|
||||
@ -449,10 +433,11 @@ namespace NadekoBot.Modules
|
||||
Provider = s.SongInfo.Provider,
|
||||
ProviderType = (int)s.SongInfo.ProviderType,
|
||||
Title = s.SongInfo.Title,
|
||||
Uri = s.SongInfo.Uri
|
||||
Uri = s.SongInfo.Uri,
|
||||
Query = s.SongInfo.Query
|
||||
});
|
||||
|
||||
var playlist = new Classes._DataModels.MusicPlaylist
|
||||
var playlist = new MusicPlaylist
|
||||
{
|
||||
CreatorId = (long)e.User.Id,
|
||||
CreatorName = e.User.Name,
|
||||
@ -461,7 +446,7 @@ namespace NadekoBot.Modules
|
||||
|
||||
DbHandler.Instance.SaveAll(songInfos);
|
||||
DbHandler.Instance.Save(playlist);
|
||||
DbHandler.Instance.InsertMany(songInfos.Select(s => new Classes._DataModels.PlaylistSongInfo
|
||||
DbHandler.Instance.InsertMany(songInfos.Select(s => new PlaylistSongInfo
|
||||
{
|
||||
PlaylistId = playlist.Id,
|
||||
SongInfoId = s.Id
|
||||
@ -472,39 +457,59 @@ namespace NadekoBot.Modules
|
||||
});
|
||||
|
||||
cgb.CreateCommand("load")
|
||||
.Description("Loads a playlist under a certain name. Name must be no longer than 20 characters and mustn't contain dashes.\n**Usage**: `!m load classical1`")
|
||||
.Description("Loads a playlist under a certain name. \n**Usage**: `!m load classical-1`")
|
||||
.Parameter("name", ParameterType.Unparsed)
|
||||
.Do(async e =>
|
||||
{
|
||||
var voiceCh = e.User.VoiceChannel;
|
||||
var textCh = e.Channel;
|
||||
if (voiceCh == null || voiceCh.Server != textCh.Server)
|
||||
{
|
||||
await textCh.SendMessage("💢 You need to be in a voice channel on this server.\n If you are already in a voice channel, try rejoining.");
|
||||
return;
|
||||
}
|
||||
var name = e.GetArg("name")?.Trim();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(name) ||
|
||||
name.Length > 20 ||
|
||||
name.Contains("-"))
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
return;
|
||||
|
||||
MusicPlayer musicPlayer;
|
||||
if (!MusicPlayers.TryGetValue(e.Server, out musicPlayer))
|
||||
var parts = name.Split('-');
|
||||
if (parts.Length != 2)
|
||||
return;
|
||||
var playlistName = parts[0];
|
||||
|
||||
int playlistNumber;
|
||||
if (!int.TryParse(parts[1], out playlistNumber))
|
||||
return;
|
||||
|
||||
//to avoid concurrency issues
|
||||
var currentPlaylist = new List<Song>(musicPlayer.Playlist);
|
||||
var playlist = DbHandler.Instance.FindOne<MusicPlaylist>(
|
||||
p => p.Name.ToLower() == name);
|
||||
|
||||
if (!currentPlaylist.Any())
|
||||
if (playlist == null)
|
||||
{
|
||||
await e.Channel.SendMessage("Can't find playlist under that name.");
|
||||
return;
|
||||
}
|
||||
|
||||
var psis = DbHandler.Instance.FindAll<PlaylistSongInfo>(psi =>
|
||||
psi.PlaylistId == playlist.Id);
|
||||
|
||||
var songInfos = psis.Select(psi => DbHandler.Instance
|
||||
.FindOne<Classes._DataModels.SongInfo>(si => si.Id == psi.SongInfoId));
|
||||
|
||||
|
||||
await e.Channel.SendMessage($"`Attempting to load {songInfos.Count()} songs`");
|
||||
foreach (var si in songInfos)
|
||||
{
|
||||
try
|
||||
{
|
||||
await QueueSong(textCh, voiceCh, si.Query, true, (MusicType)si.ProviderType);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Failed QueueSong in load playlist. {ex}");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//cgb.CreateCommand("debug")
|
||||
// .Description("Does something magical. **BOT OWNER ONLY**")
|
||||
// .AddCheck(Classes.Permissions.SimpleCheckers.OwnerOnly())
|
||||
// .Do(e => {
|
||||
// var inactivePlayers =
|
||||
// Console.WriteLine("");
|
||||
// });
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user