Ejemplo n.º 1
0
        public async Task JoinAsync()
        {
            var user = Context.User as IVoiceState;

            if (_lavaNode.HasPlayer(Context.Guild))
            {
                await ReplyAsync("I'm already conntected your voice channel!");

                return;
            }

            if (user?.VoiceChannel is null)
            {
                await ReplyAsync("You need to connect to a voice channel first");

                return;
            }

            try {
                await _lavaNode.JoinAsync(user.VoiceChannel, Context.Channel as ITextChannel);
                await ReplyAsync($"Joined {user.VoiceChannel.Name}");
            } catch (Exception e) {
                await ReplyAsync(e.Message);
            }
        }
Ejemplo n.º 2
0
        public async Task JoinAsync()
        {
            Language lang = Commands.GetLanguage(Context.Guild.Id);

            if (_lavaNode.HasPlayer(Context.Guild))
            {
                await ReplyAsync(lang.Already_connected);

                return;
            }

            IVoiceState voiceState = Context.User as IVoiceState;

            if (voiceState?.VoiceChannel == null)
            {
                await ReplyAsync(lang.User_not_connected);

                return;
            }

            try
            {
                await _lavaNode.JoinAsync(voiceState.VoiceChannel, Context.Channel as ITextChannel);
                await ReplyAsync(lang.Joined_before + voiceState.VoiceChannel.Name + lang.Joined_after);
            }
            catch (Exception exception)
            {
                await ReplyAsync(exception.Message);

                throw;
            }
        }
Ejemplo n.º 3
0
        public async Task DisconnectAsync()
        {
            if (!_lavaNode.HasPlayer(Context.Guild))
            {
                await ReplyAsync("I'm not in a voice channel!");

                return;
            }

            var voiceState = Context.User as IVoiceState;

            if (voiceState?.VoiceChannel == null)
            {
                await ReplyAsync("You must be connected to a voice channel!");

                return;
            }

            try
            {
                await _lavaNode.LeaveAsync(voiceState.VoiceChannel);

                await Context.Channel.SendSuccessAsync("Success", $"Disconnected from {voiceState.VoiceChannel.Name}");
            }
            catch (Exception exception)
            {
                await Context.Channel.SendErrorAsync("Error", exception.Message);
            }
        }
Ejemplo n.º 4
0
        public async Task JoinAsync()
        {
            if (_lavaNode.HasPlayer(Context.Guild))
            {
                await ReplyAsync("I'm already connected to a voice Channel");

                return;
            }

            var voiceState = Context.User as IVoiceState;

            if (voiceState !.VoiceChannel == null)
            {
                await ReplyAsync("You must be connected to a voice channel!");

                return;
            }

            try
            {
                await _lavaNode.JoinAsync(voiceState.VoiceChannel, Context.Channel as ITextChannel);
            }
            catch (Exception ex)
            {
                await this.LogAsync(new LogMessage(LogSeverity.Error, "Music Module", ex.Message));
                await ReplyAsync("Something happened that i can't join to the VC!");

                return;
            }
        }
Ejemplo n.º 5
0
        public async Task JoinAsync(IVoiceChannel channel = null)
        {
            if (_lavaNode.HasPlayer(Context.Guild))
            {
                await ReplyAndDeleteAsync(":x: I am already connected to a voice channel.");

                return;
            }

            channel ??= (Context.User as IVoiceState).VoiceChannel;
            if (channel == null)
            {
                await ReplyAndDeleteAsync(":x: You have to be connected to a voice channel or specify a voice channel to connect to.");

                return;
            }

            try
            {
                await _lavaNode.JoinAsync(channel, Context.Channel as ITextChannel);
            }
            catch (Exception exception)
            {
                await ReplyAndDeleteAsync($":x: {exception.Message}");

                return;
            }

            await ReplyAndDeleteAsync($"Joined **{channel.Name}** and bound to **{Context.Channel.Name}**.");
        }
Ejemplo n.º 6
0
    public async Task JoinAsync()
    {
        if (_lavaNode.HasPlayer(Context.Guild))
        {
            await ReplyAsync("I'm already connected to a voice channel!");

            return;
        }

        var voiceState = Context.User as IVoiceState;

        if (voiceState?.VoiceChannel == null)
        {
            await ReplyAsync("You must be connected to a voice channel!");

            return;
        }

        if (!Context.Guild.CurrentUser.GetPermissions(voiceState.VoiceChannel).Has(ChannelPermission.Connect))
        {
            await ReplyAsync("The bot does not have permission to join that channel");

            return;
        }

        try
        {
            await _lavaNode.JoinAsync(voiceState.VoiceChannel, Context.Channel as ITextChannel);
            await ReplyAsync($"Joined {voiceState.VoiceChannel.Name}!");
        }
        catch (Exception exception)
        {
            await ReplyAsync(exception.Message);
        }
    }
Ejemplo n.º 7
0
        public async Task JoinAsync()
        {
            if (_lavaNode.HasPlayer(Context.Guild))
            {
                await ReplyAsync("I'm already connected to a voice channel!\nUse !Disconnect first to allow me to join another channel.");

                return;
            }

            var voiceState = Context.User as IVoiceState;

            if (voiceState?.VoiceChannel == null)
            {
                await ReplyAsync("You must be connected to a voice channel!");

                return;
            }

            try
            {
                await _lavaNode.JoinAsync(voiceState.VoiceChannel, Context.Channel as ITextChannel);
                await ReplyAsync($"Joined {voiceState.VoiceChannel.Name}!");
            }
            catch (Exception exception)
            {
                await ReplyAsync(exception.Message);
            }
        }
Ejemplo n.º 8
0
        public async Task JoinAsync()
        {
            if (_lavaNode.HasPlayer(Context.Guild))
            {
                await ReplyAsync("Im already being used.");

                return;
            }

            var voiceState = Context.User as IVoiceState;

            if (voiceState?.VoiceChannel == null)
            {
                await ReplyAsync("Where?");

                return;
            }

            try
            {
                await _lavaNode.JoinAsync(voiceState.VoiceChannel, Context.Channel as ITextChannel);
                await ReplyAsync($"I'm in #{voiceState.VoiceChannel.Name}.");
            }
            catch (Exception ex)
            {
                await ReplyAsync(ex.Message);
            }
        }
Ejemplo n.º 9
0
        public async Task JoinAsync()
        {
            if (_lavaNode.HasPlayer(Context.Guild))
            {
                await ReplyAsync("Уже");

                return;
            }

            var voiceState = Context.User as IVoiceState;

            if (voiceState?.VoiceChannel == null)
            {
                await ReplyAsync("Зайди сам сначала");

                return;
            }

            try
            {
                await _lavaNode.JoinAsync(voiceState.VoiceChannel, Context.Channel as ITextChannel);
                await ReplyAsync($"Ага {voiceState.VoiceChannel.Name}!");
            }
            catch (Exception exception)
            {
                await ReplyAsync(exception.Message);
            }
        }
Ejemplo n.º 10
0
        public async Task JoinAsync()
        {
            if (_lavaNode.HasPlayer(Context.Guild))
            {
                await ReplyAsync("I'm already connected to a voice channel!");

                return;
            }

            var voiceState = Context.User as IVoiceState;

            if (voiceState?.VoiceChannel == null)
            {
                await ReplyAsync("You must be connected to a voice channel!");

                return;
            }

            try
            {
                await _lavaNode.JoinAsync(voiceState.VoiceChannel, Context.Channel as ITextChannel);
                await ReplyAsync($"Joined {voiceState.VoiceChannel.Name}!");
            }
            catch (Exception exception) {
                await ReplyAsync(exception.Message);
            }

            var embed = new EmbedBuilder()
                        .WithColor(new Color(255, 255, 255))
                        .AddField("Connected to:", voiceState.VoiceChannel.Name, true)
                        .Build();

            await ReplyAsync(embed : embed);
        }
Ejemplo n.º 11
0
        public async Task JoinAsync()
        {
            if (_lavaNode.HasPlayer(Context.Guild))
            {
                await Context.Channel.SendErrorAsync("Music", "I'm already connected to a voice channel!");

                return;
            }

            var voiceState = Context.User as IVoiceState;

            if (voiceState?.VoiceChannel == null)
            {
                await Context.Channel.SendErrorAsync("Music", "You must be connected to a voice channel!");

                return;
            }

            try
            {
                await _lavaNode.JoinAsync(voiceState.VoiceChannel, Context.Channel as ITextChannel);

                await Context.Channel.SendSuccessAsync("Music", $"Joined {voiceState.VoiceChannel.Name}!");
            }
            catch (Exception exception)
            {
                await Context.Channel.SendErrorAsync("Music", exception.Message);
            }
        }
Ejemplo n.º 12
0
        public async Task <(bool Success, string Message)> JoinChannel(IVoiceState voiceState, SocketGuild guild, ITextChannel textChannel = null)
        {
            var result = (Success : true, Message : "");

            if (_lavaNode.HasPlayer(guild))
            {
                result.Success = false;
                result.Message = "I'm already connected to a voice channel!";
                return(result);
            }

            if (voiceState?.VoiceChannel == null)
            {
                result.Success = false;
                result.Message = "You must be connected to a voice channel!";
                return(result);
            }

            try
            {
                await _lavaNode.JoinAsync(voiceState.VoiceChannel, textChannel);

                result.Message = $"Joined {voiceState.VoiceChannel.Name}!";
            }
            catch (Exception exception)
            {
                result.Success = false;
                result.Message = exception.Message;
                return(result);
            }

            return(result);
        }
Ejemplo n.º 13
0
        public async Task JoinAsync()
        {
            if (_lavaNode.HasPlayer(Context.Guild))
            {
                await ReplyAndDeleteAsync($"{Global.ENo} | I'm already connected to a voice channel!");

                return;
            }

            var voiceState = Context.User as IVoiceState;

            if (voiceState?.VoiceChannel == null)
            {
                await ReplyAndDeleteAsync($"{Global.ENo} | You must be connected to a voice channel!");

                return;
            }

            try
            {
                await _lavaNode.JoinAsync(voiceState.VoiceChannel, Context.Channel as ITextChannel);
                await ReplyAsync($"✅ **|** Joined **{voiceState.VoiceChannel.Name}!**");
            }
            catch (Exception exception)
            {
                await ReplyAsync(exception.Message);
            }
        }
Ejemplo n.º 14
0
        public async Task JoinAsync()
        {
            //Check if bot is already connected any to voice chat
            if (_lavaNode.HasPlayer(Context.Guild))
            {
                await ReplyAsync("I'm already connected to a voice channel!");

                return;
            }

            var voiceState = Context.User as IVoiceState;

            //Check if user is connected to voice chat
            if (voiceState?.VoiceChannel == null)
            {
                await ReplyAsync("You must be connected to a voice channel!");

                return;
            }

            //try to join
            try
            {
                await _lavaNode.JoinAsync(voiceState.VoiceChannel, Context.Channel as ITextChannel);
                await ReplyAsync($"Joined {voiceState.VoiceChannel.Name}!");
            }
            catch (Exception exception)
            {
                await ReplyAsync(exception.Message);
            }
        }
Ejemplo n.º 15
0
        public async Task JoinAsync()
        {
            if (_lavaNode.HasPlayer(Context.Guild))
            {
                await FormatEmbedMessage("I'm already connected to a voice channel!");

                return;
            }

            var voiceState = Context.User as IVoiceState;

            if (voiceState?.VoiceChannel == null)
            {
                await FormatEmbedMessage("You must be connected to a voice channel!");

                return;
            }

            try
            {
                await _lavaNode.JoinAsync(voiceState.VoiceChannel, Context.Channel as ITextChannel);
            }
            catch (Exception exception)
            {
                await FormatEmbedMessage(exception.Message);
            }
        }
Ejemplo n.º 16
0
    public async Task JoinAsync()
    {
        // TODO: Music join if playing but kicked from voice channel.
        if (_lavaNode.HasPlayer(Context.Guild))
        {
            await RespondAsync("I'm already playing in a voice channel in this server.");

            return;
        }

        await TryJoinAsync(true);
    }
Ejemplo n.º 17
0
        public async Task ConnectAsync(SocketVoiceChannel voiceChannel, ITextChannel textChannel)
        {
            if (_lavaNode.HasPlayer(textChannel.Guild))
            {
                return;
            }

            LavaPlayer player = await _lavaNode.JoinAsync(voiceChannel, textChannel).ConfigureAwait(false);

            await player.UpdateVolumeAsync(50).ConfigureAwait(false); // since volume default value is not set, it shows 0, manually setting volume here to update the property
        }
Ejemplo n.º 18
0
        // ---------------------------- Audio Module Helpers ----------------------------

        private async Task PlaySongAsync(string searchQuery)
        {
            if (string.IsNullOrWhiteSpace(searchQuery))
            {
                await ReplyAsync("Please provide search terms.");

                return;
            }

            await JoinAsync();
            await QueryAndPlayHelperAsync(searchQuery);

            await Context.DeleteAuthorMessage();

            if (!_lavaNode.HasPlayer(Context.Guild))
            {
                await JoinAsync();
                await QueryAndPlayHelperAsync(searchQuery);
            }
        }
Ejemplo n.º 19
0
        // Called when the bot joins the channel
        public async Task <Embed> ConnectAsync(IGuild guild, IVoiceState voiceState, ITextChannel textChannel)
        {
            if (_lavaNode.HasPlayer(guild))
            {
                return(await EmbedHandler.CreateErrorEmbed("Music, Join", "I'm already connected to a voice channel!"));
            }

            if (voiceState.VoiceChannel is null)
            {
                return(await EmbedHandler.CreateErrorEmbed("Music, Join", "You must be connected to a voice channel!"));
            }

            try
            {
                await _lavaNode.JoinAsync(voiceState.VoiceChannel, textChannel);

                return(await EmbedHandler.CreateBasicEmbed("Music, Join", $"Joined {voiceState.VoiceChannel.Name}.", Color.Green));
            }
            catch (Exception ex)
            {
                return(await EmbedHandler.CreateErrorEmbed("Music, Join", ex.Message));
            }
        }
Ejemplo n.º 20
0
        public async Task <string> PlayTrack(string query, SocketUser user, SocketGuild guild, bool skipQueue)
        {
            if (!_lavaNode.HasPlayer(guild))
            {
                await ConnectToChannelAsync(user, guild);
            }
            var player = _lavaNode.GetPlayer(guild);

            var results = await _lavaNode.SearchYouTubeAsync(query);

            if (results.LoadStatus == LoadStatus.LoadFailed || results.LoadStatus == LoadStatus.NoMatches)
            {
                return("No matches found");
            }

            var track = results.Tracks.FirstOrDefault();

            string returnMessage = "";

            if (player.PlayerState == PlayerState.Playing && !skipQueue)
            {
                player.Queue.Enqueue(track);
                returnMessage = $"Track {track.Title} is queued";
            }
            else
            {
                await player.PlayAsync(track);

                returnMessage = $"Track {track.Title} is now playing";
            }

            using (var scope = _serviceScope.CreateScope()) {
                try {
                    var discordTrackService = scope.ServiceProvider.GetService <IDiscordTrackService>();

                    try {
                        await discordTrackService.AddTrackAsync(user, track.Url);
                    } catch (InvalidUrlException e) {
                        returnMessage = e.ToString();
                    } catch (DataContextException e) {
                        returnMessage = e.ToString();
                    }
                } catch (Exception e) {
                    Console.WriteLine(e);
                }
            }

            return(returnMessage);
        }
Ejemplo n.º 21
0
        private async Task LeaveAsync(LavaNode lavaNode)
        {
            if (!lavaNode.HasPlayer(Context.Guild))
            {
                await ReplyAsync("I'm not connected to a voice channel.");

                return;
            }
            else
            {
                if (Context.User is IVoiceState voiceState)
                {
                    await lavaNode.LeaveAsync(voiceState.VoiceChannel);
                }
            }
        }
Ejemplo n.º 22
0
        public async Task Command()
        {
            Server server = await DatabaseQueries.GetOrCreateServerAsync(Context.Guild.Id);

            LavaNode           node    = ConfigProperties.LavaNode;
            SocketGuildUser    curUser = Context.Guild.CurrentUser;
            SocketVoiceChannel curVc   = curUser.VoiceChannel;

            if (node.HasPlayer(Context.Guild))
            {
                if (curVc == null)
                {
                    await SendBasicErrorEmbedAsync($"{Context.User.Mention} Please ensure I " +
                                                   $"am actively in a voice channel before using " +
                                                   $"this command.");
                }
                else
                {
                    try
                    {
                        await node.LeaveAsync(curVc);
                    }
                    catch (Exception)
                    {
                        throw new KaguyaSupportException("Error when disconnecting from voice channel:\n\n" +
                                                         $"Channel name: `{curVc.Name}`\n" +
                                                         $"Users connected (including Kaguya): `{curVc.Users.Count:N0}`\n\n" +
                                                         $"Develper Note: `Please report this to the provided " +
                                                         $"Discord server and contact Stage. Thanks!`");
                    }

                    await SendBasicSuccessEmbedAsync(
                        $"{Context.User.Mention} Successfully disconnected from `{curVc.Name}`.");
                }
            }
            else
            {
                await SendBasicErrorEmbedAsync($"{Context.User.Mention} I must be in a voice channel via " +
                                               $"the `{server.CommandPrefix}join` command for this " +
                                               $"command to work. Please try **joining a new voice channel** via " +
                                               $"`{server.CommandPrefix}join` if I am refusing to connect/disconnect.");
            }
        }
Ejemplo n.º 23
0
        public async Task <string> JoinChannel(IGuild guild, IVoiceChannel voiceChannel, ITextChannel textChannel)
        {
            if (lavaNode.HasPlayer(guild))
            {
                return(await MoveAsync(guild, voiceChannel, textChannel));
            }
            LavaPlayer lp = await lavaNode.JoinAsync(voiceChannel, textChannel);

            if (lp == null)
            {
                return("Error joining voice channel.");
            }
            guildInfo.TryAdd(guild, new DJGuildInfo(voiceChannel));
            return($"Joined {voiceChannel.Name}.");
        }
Ejemplo n.º 24
0
        public async Task Command([Remainder] string vcMatch = null)
        {
            LavaNode           node     = ConfigProperties.LavaNode;
            SocketVoiceChannel botCurVc = Context.Guild.CurrentUser.VoiceChannel;
            SocketVoiceChannel vc       = vcMatch == null
                ? (Context.User as SocketGuildUser).VoiceChannel
                : Context.Guild.VoiceChannels.First(x => x.Name.ToLower().Contains(vcMatch.ToLower()));

            if (node.HasPlayer(Context.Guild) && vc != null)
            {
                try
                {
                    await node.MoveChannelAsync(vc);
                }
                catch (Exception)
                {
                    await SendBasicErrorEmbedAsync($"It looks like I'm already connected to this voice channel " +
                                                   $"via the WebSocket. If I am not present in the voice channel, this " +
                                                   $"is due to an error. To fix this issue, join a new voice channel and " +
                                                   $"try the command again. This issue usually arises when I am manually " +
                                                   $"force-disconnected from the channel.");

                    return;
                }

                await SendBasicSuccessEmbedAsync(
                    $"{Context.User.Mention} Successfully moved to `{vc.Name}`.");
            }
            else
            {
                if (vc == botCurVc)
                {
                    await SendBasicErrorEmbedAsync($"I am already connected to this voice channel.");

                    return;
                }

                await node.JoinAsync(vc);
                await SendBasicSuccessEmbedAsync($"{Context.User.Mention} Successfully joined `{vc.Name}`.");
            }
        }
Ejemplo n.º 25
0
        public static async Task <string> JoinAsync(IGuild guild, IVoiceState voiceState, ITextChannel channel)
        {
            if (_lavaNode.HasPlayer(guild))
            {
                return("ikiye mi bölüneyim ?");
            }
            if (voiceState.VoiceChannel is null)
            {
                return("bir yere gir babun");
            }

            try
            {
                await _lavaNode.JoinAsync(voiceState.VoiceChannel, channel);

                return($"{voiceState.VoiceChannel.Name} kanalına geldik sa");
            }
            catch (Exception ex)
            {
                return($"HATA\n{ex.Message}");
            }
        }
Ejemplo n.º 26
0
        /// <summary>
        /// On Reaction handler
        /// </summary>
        /// <param name="message">Cacheable struct from Discord</param>
        /// <param name="messageChannel">SocketChannel reaction occurred in</param>
        /// <param name="reaction">Reaction added</param>
        /// <returns></returns>
        private async Task OnReactionAdded(Cacheable <IUserMessage, ulong> message, Cacheable <IMessageChannel, ulong> messageChannel, SocketReaction reaction)
        {
            var channel = await messageChannel.GetOrDownloadAsync();

            if (channel is not IGuildChannel guildChannel)
            {
                return;
            }
            var msg = await message.GetOrDownloadAsync();

            if (channel.Id != Program.BotConfig.ChannelId && msg.Id != Program.BotConfig.MessageId && !Emojis.Contains(reaction.Emote))
            {
                await Task.CompletedTask;
                return;
            }

            _ = Task.Run(async() =>
            {
                EmojiStates currentState = (EmojiStates)Array.IndexOf(Emojis, reaction.Emote);

                if (reaction.UserId == discord.CurrentUser.Id)
                {
                    await Task.CompletedTask;
                    return;
                }

                await msg.RemoveReactionAsync(reaction.Emote, reaction.User.Value, options: new RequestOptions {
                    RetryMode = RetryMode.RetryRatelimit
                });

                try
                {
                    if (!node.HasPlayer(guildChannel.Guild))
                    {
                        return;
                    }
                }
                catch
                {
                    var error = await embedHelper.BuildErrorEmbed("Guild ID Error", $"Your Guild ID in **{ConfigHelper.ConfigName}** is most likely incorrect.");
                    await channel.SendAndRemove(embed: error, timeout: 15000);
                }

                var player = node.GetPlayer(guildChannel.Guild);

                if (!(player.PlayerState is PlayerState.Playing or PlayerState.Paused) && currentState != EmojiStates.Eject)
                {
                    return;
                }

                switch (currentState)
                {
                case EmojiStates.Previous:
                    {
                        await player.PreviousAsync();
                        break;
                    }

                case EmojiStates.PlayPause:
                    {
                        await player.PauseResumeAsync(embedHelper, player.PlayerState == PlayerState.Paused);
                        break;
                    }

                case EmojiStates.Next:
                    {
                        await player.NextTrackAsync(embedHelper);
                        break;
                    }

                case EmojiStates.Loop:
                    {
                        await player.LoopAsync(audioHelper, embedHelper, channel);
                        break;
                    }

                case EmojiStates.Shuffle:
                    {
                        await player.ShuffleAsync(audioHelper, embedHelper, channel);
                        break;
                    }

                case EmojiStates.Eject:
                    {
                        try
                        {
                            await node.EjectAsync(embedHelper, guildChannel.Guild);
                        }
                        catch
                        {
                            // ignored
                        }

                        break;
                    }

                default:
                    return;
                }
            });
        }
Ejemplo n.º 27
0
        public static async System.Threading.Tasks.Task <bool> MusicCommands(SocketMessage message, CommandContext context, DiscordSocketClient client, LavaNode lava)
        {
            if (Validation.CheckCommand(message, "play"))
            {
                if ((message.Author as IVoiceState).VoiceChannel == null)
                {
                    DiscordFunctions.EmbedThis("Music", "You must first join a voice channel!", "red", context);
                    return(true);
                }


                var temp = client.GetGuild(context.Guild.Id).CurrentUser.VoiceState;
                if (temp != null && client.GetGuild(context.Guild.Id).CurrentUser.VoiceChannel != (message.Author as IVoiceState).VoiceChannel)
                {
                    DiscordFunctions.EmbedThis("Music", "I can't join another voice channel until I'm disconnected from another channel.", "red", context);
                    return(true);
                }

                SearchResponse search    = new SearchResponse();
                var            videoId   = string.Empty;
                var            timestamp = string.Empty;
                string         query     = message.Content.ToLower().Replace("!play ", "");
                if (query.ToLower().Contains("www.youtube.com/watch?v="))
                {
                    var uri     = new Uri(@query);
                    var queryid = HttpUtility.ParseQueryString(uri.Query);

                    if (queryid.AllKeys.Contains("v"))
                    {
                        videoId = queryid["v"];
                    }
                    else
                    {
                        videoId = uri.Segments.Last();
                    }
                    if (queryid.AllKeys.Contains("t"))
                    {
                        timestamp = queryid["t"];
                    }
                    if (timestamp != string.Empty)
                    {
                        videoId = videoId.Replace("&t=" + timestamp, "");
                    }
                    search = await lava.SearchYouTubeAsync(query.Replace("&t=" + timestamp, ""));
                }
                else
                {
                    search = await lava.SearchYouTubeAsync(query);
                }
                LavaTrack track = new LavaTrack();
                if (query.ToLower().Contains("www.youtube.com/watch?v="))
                {
                    bool found = false;
                    foreach (var vid in search.Tracks)
                    {
                        if (vid.Id.ToLower() == videoId)
                        {
                            track = vid;
                            found = true;
                            break;
                        }
                    }
                    if (found == false)
                    {
                        track = search.Tracks.FirstOrDefault();
                    }
                }
                else
                {
                    track = search.Tracks.FirstOrDefault();
                }

                var player = lava.HasPlayer(context.Guild)
                    ? lava.GetPlayer(context.Guild)
                    : await lava.JoinAsync((context.User as IVoiceState).VoiceChannel, (ITextChannel)message.Channel);

                if (player.PlayerState == PlayerState.Playing)
                {
                    player.Queue.Enqueue(track);
                    DiscordFunctions.EmbedThis("Music", "Enqeued " + track.Title, "orange", context);
                }
                else
                {
                    await player.PlayAsync(track);

                    try
                    {
                        if (timestamp != string.Empty)
                        {
                            if (timestamp.ToLower().Contains("s"))
                            {
                                timestamp = timestamp.ToLower().Replace("s", "");
                            }
                            await player.SeekAsync(TimeSpan.FromSeconds(Convert.ToDouble(timestamp)));
                        }
                    }
                    catch { }
                    DiscordFunctions.EmbedThis("Music", "Playing " + track.Title, "green", context);
                }
                return(true);
            }
            else if (Validation.CheckCommand(message, "skip"))
            {
                var _player = lava.GetPlayer(context.Guild);
                if (_player is null || _player.Queue.Count is 0)
                {
                    DiscordFunctions.EmbedThis("Music", "Nothing in the queue", "orange", context);
                    return(true);
                }

                var oldTrack = _player.Track;
                await _player.SkipAsync();

                DiscordFunctions.EmbedThis("Music", "Skipped: " + oldTrack.Title + "\nNow Playing: " + _player.Track.Title, "orange", context);
                return(true);
            }
            else if (Validation.CheckCommand(message, "stop"))
            {
                var _player = lava.GetPlayer(context.Guild);
                if (_player == null)
                {
                    return(true);
                }

                await _player.StopAsync();

                DiscordFunctions.EmbedThis("Music", "Stopped player", "orange", context);
                return(true);
            }
            else if (Validation.CheckCommand(message, "volume"))
            {
                LavaPlayer _player;
                try
                {
                    _player = lava.GetPlayer(context.Guild);
                }
                catch
                {
                    DiscordFunctions.EmbedThis("Music", "Nothing is playing", "orange", context);
                    return(true);
                }
                if (string.IsNullOrWhiteSpace(message.Content.Replace("!volume", "")))
                {
                    DiscordFunctions.EmbedThis("Music", "Please use a number between 2- 150", "orange", context);
                    return(true);
                }
                var vol = Convert.ToUInt16(message.Content.Replace("!volume", "").Trim());
                if (vol > 150 || vol <= 2)
                {
                    DiscordFunctions.EmbedThis("Music", "Please use a number between 2- 150", "orange", context);
                    return(true);
                }

                await _player.UpdateVolumeAsync(vol);

                DiscordFunctions.EmbedThis("Music", "Volume set to: " + vol.ToString(), "green", context);
                return(true);
            }
            else if (Validation.CheckCommand(message, "pause"))
            {
                LavaPlayer _player;
                try
                {
                    _player = lava.GetPlayer(context.Guild);
                }
                catch
                {
                    DiscordFunctions.EmbedThis("Music", "Nothing is playing", "orange", context);
                    return(true);
                }
                if (_player.PlayerState != PlayerState.Paused)
                {
                    await _player.PauseAsync();

                    DiscordFunctions.EmbedThis("Music", "Player is Paused", "orange", context);
                    return(true);
                }
                else
                {
                    await _player.ResumeAsync();

                    DiscordFunctions.EmbedThis("Music", "Playback Resumed", "green", context);
                    return(true);
                }
            }
            else if (Validation.CheckCommand(message, "resume"))
            {
                LavaPlayer _player;
                try
                {
                    _player = lava.GetPlayer(context.Guild);
                }
                catch
                {
                    DiscordFunctions.EmbedThis("Music", "Nothing is playing", "orange", context);
                    return(true);
                }
                if (_player.PlayerState != PlayerState.Paused)
                {
                    await _player.ResumeAsync();

                    DiscordFunctions.EmbedThis("Music", "Playback Resumed", "green", context);
                    return(true);
                }
                else
                {
                    DiscordFunctions.EmbedThis("Music", "Playback is not paused", "orange", context);
                    return(true);
                }
            }
            else if (Validation.CheckCommand(message, "join"))
            {
                var user = context.User as SocketGuildUser;
                if (user.VoiceChannel is null)
                {
                    DiscordFunctions.EmbedThis("Music", "You need to connect to a voice channel", "red", context);
                    return(true);
                }
                else
                {
                    LavaPlayer _player;
                    try
                    {
                        _player = lava.GetPlayer(context.Guild);
                        DiscordFunctions.EmbedThis("Music", "Bot is already in a channel", "red", context);
                        return(true);
                    }
                    catch
                    {
                        await lava.JoinAsync((context.User as IVoiceState).VoiceChannel, (ITextChannel)message.Channel);

                        return(true);
                    }
                }
            }
            else if (Validation.CheckCommand(message, "leave"))
            {
                var user = context.User as SocketGuildUser;
                if (user.VoiceChannel is null)
                {
                    DiscordFunctions.EmbedThis("Music", "Please join the channel the bot is in to make it leave", "red", context);
                    return(true);
                }
                else
                {
                    LavaPlayer _player;
                    try
                    {
                        _player = lava.GetPlayer(context.Guild);
                    }
                    catch
                    {
                        DiscordFunctions.EmbedThis("Music", "Please join the channel the bot is in to make it leave", "red", context);
                        return(true);
                    }
                    if (_player.VoiceChannel == user.VoiceChannel)
                    {
                        await lava.LeaveAsync((context.User as IVoiceState).VoiceChannel);
                    }
                    else
                    {
                        DiscordFunctions.EmbedThis("Music", "Please join the channel the bot is in to make it leave", "red", context);
                        return(true);
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 28
0
        private async Task UserVoiceStateUpdated(SocketUser socketUser, SocketVoiceState oldSocketVoiceState, SocketVoiceState newSocketVoiceState)
        {
            if (socketUser.IsBot)
            {
                return;
            }

            var member = await _db.ServerMembers.FirstOrDefaultAsync(x => x.DiscordId == socketUser.Id.ToString());

            if (member?.IntroSong == null || socketUser.Id == _lastRequestBy)
            {
                return;
            }

            if (oldSocketVoiceState.VoiceChannel == null)
            {
                Console.WriteLine($"User {socketUser.Username} entered the server!");

                if (_lavaNode.HasPlayer(newSocketVoiceState.VoiceChannel.Guild))
                {
                    await _lavaNode.MoveChannelAsync(newSocketVoiceState.VoiceChannel);
                }
                else
                {
                    await _audioService.JoinChannel(socketUser as IVoiceState, newSocketVoiceState.VoiceChannel.Guild);
                }

                var player = _lavaNode.GetPlayer(newSocketVoiceState.VoiceChannel.Guild);
                await player.UpdateVolumeAsync(7);

                var searchResponse = await _lavaNode.SearchAsync(member.IntroSong.Url);

                var track    = searchResponse.Tracks[0];
                var timespan = member?.IntroSongSeek ?? TimeSpan.Zero;
                await player.PlayAsync(track);

                if (member?.IntroSongSeek.HasValue ?? false)
                {
                    await player.SeekAsync(member.IntroSongSeek);
                }

                _lastRequestBy = socketUser.Id;

                if (_cancelIntro != null && !_cancelIntro.IsCancellationRequested)
                {
                    _cancelIntro.Cancel();
                }

                _cancelIntro = new CancellationTokenSource();
                var cancellationToken = _cancelIntro.Token;

                Task.Run(async() =>
                {
                    await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }

                    if (player.PlayerState != PlayerState.Disconnected && socketUser.Id == _lastRequestBy)
                    {
                        await _lavaNode.LeaveAsync(newSocketVoiceState.VoiceChannel);
                    }
                }, cancellationToken);
            }

            return;
        }
Ejemplo n.º 29
0
        public async Task PlayAsync([Remainder] string query)
        {
            await Context.Channel.TriggerTypingAsync();

            _logger.LogInformation("{username}#{discriminator} executed play ({query}) on {server}/{channel}",
                                   Context.User.Username, Context.User.Discriminator, query, Context.Guild?.Name ?? "DM", Context.Channel.Name);

            if (!CheckIfLavaLinkIsEnabled())
            {
                return;
            }

            if (await ServerHelper.CheckIfContextIsDM(Context))
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(query))
            {
                await ReplyAsync("Please provide search terms.");

                return;
            }

            if (!_lavaNode.HasPlayer(Context.Guild))
            {
                await ReplyAsync("I'm not connected to a voice channel.");
                await ReplyAsync("Joining you ;-)");
                await JoinAsync();
            }

            //var searchResponse = await _lavaNode.SearchAsync(query);
            var searchResponse = await _lavaNode.SearchYouTubeAsync(query);

            if (searchResponse.LoadStatus == LoadStatus.LoadFailed ||
                searchResponse.LoadStatus == LoadStatus.NoMatches)
            {
                await ReplyAsync($"I wasn't able to find anything for `{query}`.");

                return;
            }

            var player = _lavaNode.GetPlayer(Context.Guild);

            if (player.PlayerState == PlayerState.Playing || player.PlayerState == PlayerState.Paused)
            {
                if (!string.IsNullOrWhiteSpace(searchResponse.Playlist.Name))
                {
                    foreach (var track in searchResponse.Tracks)
                    {
                        player.Queue.Enqueue(track);
                    }

                    await ReplyAsync($"Enqueued {searchResponse.Tracks.Count} tracks.");
                }
                else
                {
                    var track = searchResponse.Tracks[0];
                    player.Queue.Enqueue(track);
                    await ReplyAsync($"Enqueued: {track.Title}");
                }
            }
            else
            {
                var track = searchResponse.Tracks[0];                         // First result, maybe a random one?

                if (!string.IsNullOrWhiteSpace(searchResponse.Playlist.Name)) // Play a playlist, maybe make this an option?
                {
                    for (var i = 0; i < searchResponse.Tracks.Count; i++)
                    {
                        if (i == 0)
                        {
                            await player.PlayAsync(track);
                            await ReplyAsync($"Now Playing: {track.Title}");
                        }
                        else
                        {
                            player.Queue.Enqueue(searchResponse.Tracks[i]);
                        }
                    }

                    await ReplyAsync($"Enqueued {searchResponse.Tracks.Count} tracks.");
                }
                else
                {
                    await player.PlayAsync(track);
                    await ReplyAsync($"Now Playing: {track.Title}");
                }
            }
        }
Ejemplo n.º 30
0
        /// <summary>Joins the voice channel the user is in and reacts to user message with 'okEmoji'.
        /// </summary>
        public async Task JoinAsync(SocketCommandContext context)
        {
            var guild       = context.Guild;
            var voiceState  = (IVoiceState)context.User;
            var textChannel = (ITextChannel)context.Channel;

            var okEmoji = new Emoji("👍");

            //If the user isn't connected to a voice channel
            if (voiceState == null)
            {
                await context.Channel.SendMessageAsync(
                    embed : CustomFormats.CreateErrorEmbed("You must be connected to a voice channel!"));

                return;
            }

            //If bot is already connected to a voice channel, do nothing and return
            if (_lavaNode.HasPlayer(guild))
            {
                return;
            }

            //If user is connected to a voice channel, and the bot isn't connected to it
            //Then try to join
            try
            {
                await _lavaNode.JoinAsync(voiceState.VoiceChannel, textChannel);

                await context.Message.AddReactionAsync(okEmoji);
            }
            catch (Exception ex)
            {
                await context.Channel.SendMessageAsync(embed : CustomFormats.CreateErrorEmbed(ex.Message));
            }
        }