Пример #1
0
        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));
            }
        }
Пример #2
0
        private async Task <IAudioClient> JoinChannel(SocketVoiceChannel channel)
        {
            if (AudioClients.IsInChannelOf(channel.Guild.Id))
            {
                await AudioClients.LeaveChannelOn(channel.Guild.Id);
            }

            return(await AudioClients.Join(channel));
        }
Пример #3
0
        public async Task Leave()
        {
            if (!AudioClients.IsInChannelOf(Context.Guild.Id))
            {
                await ReplyAsync("I'm not even in a channel!");

                return;
            }

            await AudioClients.LeaveChannelOn(Context.Guild.Id);
        }
Пример #4
0
 private void PlayQueue(QueueService queue, IAudioClient audioClient)
 {
     // if the task ends with false, don't continue, the queue is empty.
     queue.Play(audioClient).ContinueWith(x =>
     {
         if (x.Result)
         {
             PlayQueue(queue, audioClient);
         }
         else if (!Config.RemainInChannel)
         {
             AudioClients.Stop(audioClient).Wait();
         }
     });
 }
Пример #5
0
        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));
                }
            }
        }