public async Task Play([Remainder, Summary("The query or the URL of the youtube video.")] string urlOrQuery) { SocketGuild guild = Context.Guild; QueueService queue = Queues.GetOrCreateService(guild.Id); string youtubeLink; string title = null; // Query, select first video found, works for links too. // If param looks like a valid Uri, don't search for title similarities. var result = Search(urlOrQuery, 1, Uri.IsWellFormedUriString(urlOrQuery, UriKind.Absolute) ? (Func <VideoInformation, int>)(x => (x.Url == urlOrQuery) ? 0 : 1) : null)[0]; youtubeLink = result.Url; title = result.Title; Enqueue(youtubeLink, title, guild.Id); await ReplyAsync(Enqueued(title)); if (IsInVoiceChannel(guild, Context.Message.Author) && !IAmInVoiceChannel(FindVoiceChannel(guild, Context.Message.Author))) { await JoinAndPlay(queue, FindVoiceChannel(guild, Context.Message.Author)); } else if (!queue.IsPlaying) { PlayQueue(queue, AudioClients.GetClient(guild.Id)); } }
public async Task Search([Remainder, Summary("The search query")] string query) { SocketGuild guild = Context.Guild; QueueService queue = Queues.GetOrCreateService(guild.Id); List <VideoInformation> results = Search(query, Config.MaxSearchResults); var lines = new List <string>(); for (int i = 0; i < results.Count; i++) { lines.Add($"{i + 1}. {results[i].Title}"); } string response = string.Join("\n", lines); var sentMessage = await ReplyAsync(response); using (var waiter = new AutoResetEvent(false)) { // define title reference for later use string title = ""; Task handler(Cacheable <IUserMessage, ulong> message, IMessageChannel channel, SocketReaction reaction) { if (message.Id != sentMessage.Id || reaction.UserId != Context.Message.Author.Id || (!Constants.Keycaps.Contains(reaction.Emote.Name) && Constants.EmojiX != reaction.Emote.Name)) { return(Task.CompletedTask); } if (Constants.EmojiX == reaction.Emote.Name) { title = "Nothing"; waiter.Set(); return(Task.CompletedTask); } for (int i = 0; i < Constants.Keycaps.Length; i++) { if (Constants.Keycaps[i + 1] == reaction.Emote.Name) { Console.WriteLine("i: {0}, Title: {1}", i, results[i].Title); Enqueue(results[i].Url, results[i].Title, Context.Guild.Id); title = results[i].Title; waiter.Set(); return(Task.CompletedTask); } } return(Task.CompletedTask); } for (int i = 1; i <= Math.Min(results.Count, 10); i++) { await sentMessage.AddReactionAsync(new Emoji(Constants.Keycaps[i])); } await sentMessage.AddReactionAsync(new Emoji(Constants.EmojiX)); Context.Client.ReactionAdded += handler; waiter.WaitOne(); Context.Client.ReactionAdded -= handler; await sentMessage.RemoveAllReactionsAsync(); await sentMessage.ModifyAsync(msgProp => msgProp.Content = Enqueued(title)); if (IsInVoiceChannel(guild, Context.Message.Author) && !IAmInVoiceChannel(FindVoiceChannel(guild, Context.Message.Author))) { await JoinAndPlay(queue, FindVoiceChannel(guild, Context.Message.Author)); } else if (!queue.IsPlaying) { PlayQueue(queue, AudioClients.GetClient(guild.Id)); } } }