private async Task CmdInfoAsync(CommandContext context, string queueName, CancellationToken cancellationToken = default) { IdQueue queue = await GetQueueAsync(context, queueName, cancellationToken).ConfigureAwait(false); if (queue == null) { if (queueName.Equals("my", StringComparison.OrdinalIgnoreCase)) { await context.ReplyTextAsync("(n) Your queue not found.", cancellationToken).ConfigureAwait(false); } else { await context.ReplyTextAsync($"(n) Queue {queueName} not found.", cancellationToken).ConfigureAwait(false); } return; } WolfUser user = null; if (queue.OwnerID != null) { user = await context.Client.GetUserAsync(queue.OwnerID.Value, cancellationToken).ConfigureAwait(false); } await context.ReplyTextAsync( $"Name: {queue.Name}\r\n" + $"Owner ID: {(queue.OwnerID?.ToString() ?? "-")}\r\n" + $"Owner: {(user?.Nickname ?? "-")}\r\n" + $"ID Count: {queue.QueuedIDs?.Count ?? 0}\r\n" + $"\r\nID: {queue.ID}", cancellationToken).ConfigureAwait(false); }
public async Task <IdQueue> GetIdQueueByOwnerAsync(uint ownerID, CancellationToken cancellationToken = default) { await _lock.WaitAsync(cancellationToken).ConfigureAwait(false); try { // check cache first IdQueue result = _cache.Find(cachedQueue => cachedQueue.Entity.OwnerID == ownerID).FirstOrDefault(); if (result != null) { _log.LogTrace("IDs queue owned by user {UserID} found in cache", ownerID); return(result); } // get from DB _log.LogTrace("Retrieving IDs queue owned by user {UserID} from database", ownerID); result = await _idQueuesCollection.Find(dbQueue => dbQueue.OwnerID == ownerID).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false); if (result != null) { _cache.AddOrReplace(result); } else { _log.LogTrace("IDs queue owned by user {UserID} not found", ownerID); } return(result); } finally { _lock.Release(); } }
private async Task <IdQueue> GetOrCreateQueueAsync(CommandContext context, string name, CancellationToken cancellationToken = default) { // first try to get existing queue IdQueue result = await GetQueueAsync(context, name, cancellationToken).ConfigureAwait(false); if (result != null) { return(result); } // if not exist, resolve name for new queue bool claiming = false; string queueName = name; if (name.Equals("my", StringComparison.OrdinalIgnoreCase)) { WolfUser user = await context.Client.GetUserAsync(context.Message.SenderID.Value, cancellationToken).ConfigureAwait(false); queueName = user.Nickname; claiming = true; } // check forbidden name if (IsQueueNameForbidden(queueName)) { await context.ReplyTextAsync($"(n) Queue name \"{queueName}\" is invalid or forbidden.", cancellationToken).ConfigureAwait(false); return(null); } // if used "my", check if one with new name already exists if (claiming) { IdQueue existingQueue = await _idQueueStore.GetIdQueueByNameAsync(queueName, cancellationToken).ConfigureAwait(false); if (existingQueue != null) { if (existingQueue.OwnerID == null) { await context.ReplyTextAsync($"(n) Queue \"{existingQueue.Name}\" already exists, but is not claimed by you.\r\n" + $"Use '{context.Options.Prefix}{queueName}queue claim' to set as yours!", cancellationToken).ConfigureAwait(false); } else { WolfUser queueOwner = await context.Client.GetUserAsync(existingQueue.OwnerID.Value, cancellationToken).ConfigureAwait(false); await context.ReplyTextAsync($"(n) Queue \"{existingQueue.Name}\" already exists, but is claimed by {queueOwner.Nickname}. :(", cancellationToken).ConfigureAwait(false); } return(null); } } // if all checks succeeded, we can create a new one result = new IdQueue(queueName); if (claiming) { result.OwnerID = context.Message.SenderID.Value; } return(result); }
public async void Shuffle() { try { Queue <string> allQueue = IdQueue; foreach (var item in videoQueue) { allQueue.Enqueue(item.Id); } videoQueue = new Queue <YoutubeExplode.Videos.Video>(); var Rand = new Random(); var NewQueue = new Queue <string>(); foreach (var Song in allQueue.ToArray().OrderBy(x => Rand.Next())) { NewQueue.Enqueue(Song); } IdQueue = NewQueue; for (int i = 0; i < 5; i++) { videoQueue.Enqueue(await youtube.Videos.GetAsync(IdQueue.Dequeue())); } } catch (Exception) { } }
public async Task <IdQueue> GetIdQueueByNameAsync(string name, CancellationToken cancellationToken = default) { await _lock.WaitAsync(cancellationToken).ConfigureAwait(false); try { // check cache first IdQueue result = _cache.Find(cachedQueue => cachedQueue.Entity.Name.Equals(name, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); if (result != null) { _log.LogTrace("IDs queue {QueueName} found in cache", name); return(result); } // get from DB _log.LogTrace("Retrieving IDs queue {QueueName} from database", name); result = await _idQueuesCollection.Find(dbQueue => dbQueue.Name.ToLowerInvariant() == name.ToLowerInvariant()).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false); if (result != null) { _cache.AddOrReplace(result); } else { _log.LogTrace("IDs queue {QueueName} not found", name); } return(result); } finally { _lock.Release(); } }
public uint NextId() { if (this.Ids == null) { this.Ids = new IdQueue(); } return(this.Ids.NextId()); }
public uint NextId() { if (Ids == null) { Ids = new IdQueue(); } return(Ids.NextId()); }
private async Task CmdAddAsync(CommandContext context, string queueName, [MissingError("(n) Please provide IDs to add.")] string args, CancellationToken cancellationToken = default) { IdQueue queue = await GetOrCreateQueueAsync(context, queueName, cancellationToken).ConfigureAwait(false); if (queue == null) { return; // if null, it means it's a forbidden name } // perform adding (HashSet <uint> ids, HashSet <string> needsRevisit) = GetIDsFromArgs(args); int addedCount = 0; int skippedCount = 0; foreach (uint i in ids) { if (queue.QueuedIDs.Contains(i)) { skippedCount++; } else { queue.QueuedIDs.Enqueue(i); addedCount++; } } // build response StringBuilder builder = new StringBuilder(); if (addedCount > 0) { builder.AppendFormat("(y) Added {0} IDs to {1} queue.", addedCount.ToString(), queue.Name); } else { builder.AppendFormat("(n) No ID added to {0} queue.", queue.Name); } if (skippedCount > 0) { builder.AppendFormat("\r\n{0} IDs already exist on the queue so were skipped.", skippedCount.ToString()); } if (needsRevisit.Count > 0) { builder.AppendFormat("\r\n(n) {0} values weren't valid IDs, but contain digits, so may need revisiting: {1}", needsRevisit.Count.ToString(), string.Join(", ", needsRevisit)); } // send response await context.ReplyTextAsync(builder.ToString(), cancellationToken).ConfigureAwait(false); await SaveQueueAsync(context, queue, cancellationToken).ConfigureAwait(false); }
private async Task CmdRenameAsync(CommandContext context, string queueName, string args = null, CancellationToken cancellationToken = default) { string newName = args?.Trim(); // check forbidden name if (IsQueueNameForbidden(newName)) { await context.ReplyTextAsync($"(n) Queue name \"{newName}\" is invalid or forbidden.", cancellationToken).ConfigureAwait(false); return; } IdQueue queue = await GetOrCreateQueueAsync(context, queueName, cancellationToken).ConfigureAwait(false); if (queue == null) { return; // if null, it means it's a forbidden name } // check if this is owner's queue, or user is bot admin if (!await IsQueueOwnerOrBotAdmin(queue, context.Message.SenderID.Value, cancellationToken).ConfigureAwait(false)) { await context.ReplyTextAsync("(n) To rename a queue, you need to be its owner or a bot admin.", cancellationToken).ConfigureAwait(false); return; } // check same name if (newName.Equals(queue.Name, StringComparison.OrdinalIgnoreCase)) { await context.ReplyTextAsync($"(n) Queue is already named \"{newName}\".", cancellationToken).ConfigureAwait(false); return; } // check new queue name doesn't yet exist IdQueue existingQueue = await _idQueueStore.GetIdQueueByNameAsync(newName, cancellationToken).ConfigureAwait(false); if (existingQueue != null) { await context.ReplyTextAsync($"(n) Queue \"{existingQueue.Name}\" already exists.", cancellationToken).ConfigureAwait(false); return; } queue.Name = newName; await SaveQueueAsync(context, queue, cancellationToken).ConfigureAwait(false); _idQueueStore.FlushBatch(); await context.ReplyTextAsync($"(y) Queue renamed to \"{newName}\".", cancellationToken).ConfigureAwait(false); }
public async Task SetIdQueueAsync(IdQueue queue, CancellationToken cancellationToken = default) { await _lock.WaitAsync(cancellationToken).ConfigureAwait(false); try { _log.LogTrace("Inserting IDs queue {QueueName} into database", queue.Name); _cache.AddOrReplace(queue); await _batchInserter.BatchAsync(queue.ID, new MongoDelayedInsert <IdQueue>(dbQueue => dbQueue.ID == queue.ID, queue, _replaceOptions)).ConfigureAwait(false); } finally { _lock.Release(); } }
public async Task Start() { try { Cancel = new CancellationTokenSource(); while (!Cancel.IsCancellationRequested) { try { Skip = new CancellationTokenSource(); while (videoQueue.Count == 0 && !Cancel.IsCancellationRequested) { await Task.Delay(10); } playing = videoQueue.Dequeue(); while (videoQueue.Count < 5) { if (IdQueue.Count > 0) { videoQueue.Enqueue(await youtube.Videos.GetAsync(IdQueue.Dequeue())); } else { break; } } if (playing != null) { await channel.SendMessageAsync(embed : Embed.New(Program.Client.CurrentUser, Field.CreateFieldBuilder("now playing", $"[{playing.Title}]({playing.Url})\n{playing.Duration}"), Colors.information)); await PlaySong(playing); } playing = null; } catch (Exception) { } } } catch (Exception) { } }
private async Task CmdRemoveAsync(CommandContext context, string queueName, [MissingError("(n) Please provide IDs to remove.")] string args, CancellationToken cancellationToken = default) { IdQueue queue = await GetOrCreateQueueAsync(context, queueName, cancellationToken).ConfigureAwait(false); if (queue == null) { return; // if null, it means it's a forbidden name } // check if this is owner's queue, or user is bot admin if (!await IsQueueOwnerOrBotAdmin(queue, context.Message.SenderID.Value, cancellationToken).ConfigureAwait(false)) { await context.ReplyTextAsync("(n) To remove from a queue, you need to be its owner or a bot admin.", cancellationToken).ConfigureAwait(false); return; } // perform removing (HashSet <uint> ids, HashSet <string> needsRevisit) = GetIDsFromArgs(args); int previousCount = queue.QueuedIDs.Count; queue.QueuedIDs = new Queue <uint>(queue.QueuedIDs.Where(i => !ids.Contains(i))); // build response int removedCount = previousCount - queue.QueuedIDs.Count; int skippedCount = ids.Count - previousCount; StringBuilder builder = new StringBuilder(); if (removedCount > 0) { builder.AppendFormat("(y) Removed {0} IDs from {1} queue.", removedCount.ToString(), queue.Name); } else { builder.AppendFormat("(n) No ID added to {0} queue.", queue.Name); } if (skippedCount > 0) { builder.AppendFormat("\r\n{0} IDs did not exist on the queue so were skipped.", skippedCount.ToString()); } // send response await context.ReplyTextAsync(builder.ToString(), cancellationToken).ConfigureAwait(false); await SaveQueueAsync(context, queue, cancellationToken).ConfigureAwait(false); }
private async Task <bool> SaveQueueAsync(CommandContext context, IdQueue queue, CancellationToken cancellationToken = default) { try { await _idQueueStore.SetIdQueueAsync(queue, cancellationToken).ConfigureAwait(false); return(true); } catch (Exception ex) when(ex.LogAsError(_log, "Failed saving queue {QueueName} in the database", queue.Name)) { if (context?.Message != null) { await context.ReplyTextAsync($"/alert Failed saving queue '{queue.Name}' in the database.", cancellationToken).ConfigureAwait(false); } return(false); } }
private async Task <bool> IsQueueOwnerOrBotAdmin(IdQueue queue, uint userID, CancellationToken cancellationToken = default) { // check if this is owner's queue if (queue.OwnerID != null && queue.OwnerID.Value == userID) { return(true); } // if not, check if bot admin UserData user = await _userDataStore.GetUserDataAsync(userID, cancellationToken).ConfigureAwait(false); if (user.IsBotAdmin) { return(true); } // if checks failed, is not owner or admin return(false); }
private async Task CmdShowAsync(CommandContext context, string queueName, CancellationToken cancellationToken = default) { IdQueue queue = await GetOrCreateQueueAsync(context, queueName, cancellationToken).ConfigureAwait(false); if (queue == null) { return; // if null, it means it's a forbidden name } if (queue.QueuedIDs?.Any() != true) { await context.ReplyTextAsync($"`{queue.Name}` queue is empty.", cancellationToken).ConfigureAwait(false); return; } bool plural = queue.QueuedIDs.Count > 1; await context.ReplyTextAsync($"Currently there {(plural ? "are" : "is")} {queue.QueuedIDs.Count} ID{(plural ? "s" : "")} on {queue.Name} queue:\r\n{string.Join(", ", queue.QueuedIDs)}"); }
private async Task CmdTransferAsync(CommandContext context, string queueName, [MissingError("(n) Please provide ID of the user to transfer the queue to.")][ConvertingError("(n) `{{Arg}}` is not a valid user ID.")] uint newOwnerID, CancellationToken cancellationToken = default) { IdQueue queue = await GetOrCreateQueueAsync(context, queueName, cancellationToken).ConfigureAwait(false); if (queue == null) { return; // if null, it means it's a forbidden name } // check if this is owner's queue, or user is bot admin if (queue.OwnerID != null && !await IsQueueOwnerOrBotAdmin(queue, context.Message.SenderID.Value, cancellationToken).ConfigureAwait(false)) { await context.ReplyTextAsync("(n) To transfer a queue, you need to be it's owner or a bot admin.", cancellationToken).ConfigureAwait(false); return; } WolfUser user = await context.Client.GetUserAsync(newOwnerID, cancellationToken).ConfigureAwait(false); if (user == null) { await context.ReplyTextAsync($"(n) User {newOwnerID} not found.", cancellationToken).ConfigureAwait(false); return; } IdQueue userCurrentQueue = await _idQueueStore.GetIdQueueByOwnerAsync(user.ID, cancellationToken).ConfigureAwait(false); if (userCurrentQueue != null) { await context.ReplyTextAsync($"(n) User {user.Nickname} already owns a queue. One user can only own one queue.", cancellationToken).ConfigureAwait(false); return; } queue.OwnerID = user.ID; await SaveQueueAsync(context, queue, cancellationToken).ConfigureAwait(false); _idQueueStore.FlushBatch(); await context.ReplyTextAsync($"(y) Queue `{queue.Name}` transferred to {user.Nickname}.", cancellationToken).ConfigureAwait(false); }
private async Task CmdNextAsync(CommandContext context, string queueName, CancellationToken cancellationToken = default) { // get queue and ensure not empty IdQueue queue = await GetOrCreateQueueAsync(context, queueName, cancellationToken).ConfigureAwait(false); if (queue == null) { return; // if null, it means it's a forbidden name } if (!queue.QueuedIDs.Any()) { await context.ReplyTextAsync($"Queue {queue.Name} is empty.", cancellationToken).ConfigureAwait(false); return; } uint gameID = queue.QueuedIDs.Dequeue(); await context.ReplyTextAsync(BotInteractionUtilities.GetSubmissionBotShowCommand(_botOptions, gameID), cancellationToken).ConfigureAwait(false); await SaveQueueAsync(context, queue, cancellationToken).ConfigureAwait(false); }
private async Task CmdClearAsync(CommandContext context, string queueName, CancellationToken cancellationToken = default) { IdQueue queue = await GetOrCreateQueueAsync(context, queueName, cancellationToken).ConfigureAwait(false); if (queue == null) { return; // if null, it means it's a forbidden name } // check if this is owner's queue, or user is bot admin if (!await IsQueueOwnerOrBotAdmin(queue, context.Message.SenderID.Value, cancellationToken).ConfigureAwait(false)) { await context.ReplyTextAsync("(n) To clear a queue, you need to be its owner or a bot admin.", cancellationToken).ConfigureAwait(false); return; } int idCount = queue.QueuedIDs.Count; queue.QueuedIDs.Clear(); await context.ReplyTextAsync($"(y) {idCount} ID{(idCount > 1 ? "s" : "")} removed from queue \"{queue.Name}\" .", cancellationToken).ConfigureAwait(false); await SaveQueueAsync(context, queue, cancellationToken).ConfigureAwait(false); }
public uint NextId() { if (Ids == null) { Ids = new IdQueue(); } return Ids.NextId(); }
public async void Add(string[] input) { try { if (input.Length > 0) { if (input.FirstOrDefault().StartsWith("https://www.youtube.com/watch?v=")) { var video = await youtube.Videos.GetAsync(input.FirstOrDefault()); if (videoQueue.Count < 5) { videoQueue.Enqueue(video); } else { IdQueue.Enqueue(video.Id); } await channel.SendMessageAsync(embed : Embed.New(Program.Client.CurrentUser, Field.CreateFieldBuilder("queue added", $"[{video.Title}]({video.Url})\n{video.Duration}"), Colors.information)); } else if (input.FirstOrDefault().StartsWith("https://www.youtube.com/playlist?list=")) { var playlist = await youtube.Playlists.GetAsync(input.FirstOrDefault()); var videos = await youtube.Playlists.GetVideosAsync(playlist.Id).CollectAsync(); foreach (var video in videos) { if (videoQueue.Count < 5) { videoQueue.Enqueue(await youtube.Videos.GetAsync(video.Id)); } else { IdQueue.Enqueue(video.Id); } } await channel.SendMessageAsync(embed : Embed.New(Program.Client.CurrentUser, Field.CreateFieldBuilder("queue", $"added {videos.Count} songs"), Colors.information)); } else { string searchString = ""; foreach (var item in input) { searchString = searchString + item + " "; } var search = await youtube.Search.GetVideosAsync(searchString).CollectAsync(1); var video = await youtube.Videos.GetAsync(search.FirstOrDefault().Id); if (videoQueue.Count < 5) { videoQueue.Enqueue(video); } else { IdQueue.Enqueue(video.Id); } await channel.SendMessageAsync(embed : Embed.New(Program.Client.CurrentUser, Field.CreateFieldBuilder("queue added", $"[{video.Title}]({video.Url})\n{video.Duration}"), Colors.information)); } } } catch (Exception) { } }