Exemple #1
0
        public async Task <Embed> JoinOrPlayAsync(SocketGuildUser user, IMessageChannel textChannel, ulong guildId, string query = null)
        {
            if (user.VoiceChannel == null)
            {
                return(await EmbedHandler.CreateErrorEmbed("Music Join/Play", "You must first join a voice channel!"));
            }

            if (Options.TryGetValue(user.Guild.Id, out var options) && options.Summoner.Id != user.Id)
            {
                return(await EmbedHandler.CreateErrorEmbed("Music, Join/Play", $"I can't join another voice channel until {options.Summoner} disconnects me."));
            }
            try
            {
                _lavaPlayer = _lavaShardClient.GetPlayer(guildId);
                if (_lavaPlayer == null)
                {
                    await _lavaShardClient.ConnectAsync(user.VoiceChannel);

                    Options.TryAdd(user.Guild.Id, new AudioOptions
                    {
                        Summoner = user
                    });
                    _lavaPlayer = _lavaShardClient.GetPlayer(guildId);
                }

                LavaTrack track;
                var       search = await _lavaRestClient.SearchYouTubeAsync(query);

                if (search.LoadType == LoadType.NoMatches && query != null)
                {
                    return(await EmbedHandler.CreateErrorEmbed("Music", $"I wasn't able to find anything for {query}."));
                }
                if (search.LoadType == LoadType.LoadFailed && query != null)
                {
                    return(await EmbedHandler.CreateErrorEmbed("Music", $"I failed to load {query}."));
                }

                track = search.Tracks.FirstOrDefault();

                if (_lavaPlayer.CurrentTrack != null && _lavaPlayer.IsPlaying || _lavaPlayer.IsPaused)
                {
                    _lavaPlayer.Queue.Enqueue(track);
                    return(await EmbedHandler.CreateBasicEmbed("Music", $"{track.Title} has been added to queue."));
                }
                await _lavaPlayer.PlayAsync(track);

                return(await EmbedHandler.CreateMusicEmbed("Music", $"Now Playing: {track.Title}\nUrl: {track.Uri}"));
            }
            catch (Exception e)
            {
                return(await EmbedHandler.CreateErrorEmbed("Music, Join/Play", e.Message));
            }
        }
Exemple #2
0
        public async Task <Embed> SkipTrackAsync(ulong guildId)
        {
            var config    = GlobalGuildAccounts.GetGuildAccount(guildId);
            var cmdPrefix = config.CommandPrefix;

            try
            {
                var player = _lavaShardClient.GetPlayer(guildId);
                if (player == null)
                {
                    return(await EmbedHandler.CreateErrorEmbed("Music, List", $"Could not aquire player.\nAre you using the bot right now? check{cmdPrefix}Help for info on how to use the bot."));
                }
                if (player.Queue.Count == 1)
                {
                    return(await EmbedHandler.CreateMusicEmbed("Music Skipping", "This is the last song in the queue, so I have stopped playing."));
                }
                await player.StopAsync();

                if (player.Queue.Count == 0)
                {
                    return(await EmbedHandler.CreateErrorEmbed("Music Skipping", "There are no songs to skip!"));
                }
                else
                {
                    try
                    {
                        var currentTrack = player.CurrentTrack;
                        await player.SkipAsync();

                        return(await EmbedHandler.CreateBasicEmbed("Music Skip", $"Successfully skipped {currentTrack.Title}"));
                    }
                    catch (Exception ex)
                    {
                        return(await EmbedHandler.CreateErrorEmbed("Music Skipping Exception:", ex.ToString()));
                    }
                }
            }
            catch (Exception ex)
            {
                return(await EmbedHandler.CreateErrorEmbed("Music Skip", ex.ToString()));
            }
        }
Exemple #3
0
        private async Task TrackFinished(LavaPlayer player, LavaTrack track, TrackEndReason reason)
        {
            if (!reason.ShouldPlayNext())
            {
                return;
            }

            if (!player.Queue.TryDequeue(out var item) || !(item is LavaTrack nextTrack))
            {
                await player.TextChannel.SendMessageAsync(Config.pre.error + " There are no more tracks in the queue.");

                return;
            }

            await player.PlayAsync(nextTrack);

            Embed embed = await EmbedHandler.CreateMusicEmbed(":musical_note: Now playing " + nextTrack.Title + " :musical_note:", nextTrack.Uri.ToString());

            await player.TextChannel.SendMessageAsync(null, false, embed);
        }
Exemple #4
0
        public async Task <Embed> Pause(ulong guildId)
        {
            try
            {
                var player = _lavaShardClient.GetPlayer(guildId);
                if (player.IsPaused)
                {
                    await player.ResumeAsync();

                    return(await EmbedHandler.CreateMusicEmbed("▶️ Music", $"**Resumed:** Now Playing {player.CurrentTrack.Title}"));
                }
                else
                {
                    await player.PauseAsync();

                    return(await EmbedHandler.CreateMusicEmbed("⏸️ Music", $"**Paused:** {player.CurrentTrack.Title}"));
                }
            }
            catch (InvalidOperationException e)
            {
                return(await EmbedHandler.CreateErrorEmbed("Music Play/Pause", e.Message));
            }
        }
Exemple #5
0
        public async Task <Embed> PlayAsync(string query, ulong guildId)
        {
            _player = _lavaSocketClient.GetPlayer(guildId);
            var results = await _lavaRestClient.SearchYouTubeAsync(query);

            if (results.LoadType == LoadType.NoMatches || results.LoadType == LoadType.LoadFailed)
            {
                return(await EmbedHandler.CreateBasicEmbed("No Results", "Your search returned no results.", Color.Blue));
            }

            var track = results.Tracks.FirstOrDefault();

            if (_player.IsPlaying)
            {
                _player.Queue.Enqueue(track);
                return(await EmbedHandler.CreateMusicQueueEmbed(":arrow_right: " + track.Title + " added to queue", track.Uri.ToString()));
            }
            else
            {
                await _player.PlayAsync(track);

                return(await EmbedHandler.CreateMusicEmbed(":musical_note: Now playing " + track.Title + " :musical_note:", track.Uri.ToString()));
            }
        }