Ejemplo n.º 1
0
        void IModule.Install(ModuleManager manager)
        {
            _manager = manager;
            _client  = manager.Client;

            manager.CreateCommands("music", cgb => {
                //queue all more complex commands
                // commands.ForEach(cmd => cmd.Init(cgb));

                cgb.CreateCommand("next")

                .Description("Goes to the next song in the queue.")
                .Do(async e => {
                    if (musicPlayers.ContainsKey(e.Server) == false)
                    {
                        return;
                    }
                    await musicPlayers[e.Server].LoadNextSong();
                });

                cgb.CreateCommand("stop")

                .Description("Completely stops the music and unbinds the bot from the channel and cleanes up files.")
                .Do(e => {
                    if (musicPlayers.ContainsKey(e.Server) == false)
                    {
                        return;
                    }
                    musicPlayers[e.Server].Stop();
                });

                cgb.CreateCommand("pause")

                .Description("Pauses or Unpauses the song")
                .Do(async e => {
                    if (musicPlayers.ContainsKey(e.Server) == false)
                    {
                        return;
                    }
                    if (musicPlayers[e.Server].TogglePause())
                    {
                        await _client.Reply(e, "🎵`Music player paused.`");
                    }
                    else
                    {
                        await _client.Reply(e, "🎵`Music player unpaused.`");
                    }
                });

                cgb.CreateCommand("q")
                .Alias("yq")
                .Description("Queue a song using keywords or link. Bot will join your voice channel. **You must be in a voice channel**.\n**Usage**: `!m q Dream Of Venice`")
                .Parameter("query", ParameterType.Unparsed)
                .Do(async e => await QueueSong(e, e.GetArg("query")));

                cgb.CreateCommand("listq")
                .Alias("ls").Alias("lp")
                .Description("Lists up to 10 currently queued songs.")
                .Do(async e => {
                    if (musicPlayers.ContainsKey(e.Server) == false)
                    {
                        await _client.Reply(e, "🎵 No active music player.");
                        return;
                    }
                    var player    = musicPlayers[e.Server];
                    string toSend = "🎵 **" + player.SongQueue.Count + "** `videos currently queued.` ";
                    if (player.SongQueue.Count >= 25)
                    {
                        toSend += "**Song queue is full!**\n";
                    }
                    await _client.Reply(e, toSend);
                    int number = 1;
                    await _client.Reply(e, string.Join("\n", player.SongQueue.Take(10).Select(v => $"`{number++}.` {v.FullPrettyName}")));
                });

                cgb.CreateCommand("nowplaying")
                .Alias("playing")
                .Description("Shows the song currently playing.")
                .Do(async e => {
                    if (musicPlayers.ContainsKey(e.Server) == false)
                    {
                        return;
                    }
                    var player = musicPlayers[e.Server];
                    await _client.Reply(e, $"🎵`Now Playing` {player.CurrentSong.FullPrettyName}");
                });

                cgb.CreateCommand("vol")
                .Description("Sets the music volume 0-150%")
                .Parameter("val", ParameterType.Required)
                .Do(async e => {
                    if (musicPlayers.ContainsKey(e.Server) == false)
                    {
                        return;
                    }
                    var player = musicPlayers[e.Server];
                    var arg    = e.GetArg("val");
                    int volume;
                    if (!int.TryParse(arg, out volume))
                    {
                        await _client.Reply(e, "Volume number invalid.");
                        return;
                    }
                    volume = player.SetVolume(volume);
                    await _client.Reply(e, $"🎵 `Volume set to {volume}%`");
                });

                cgb.CreateCommand("min").Alias("mute")
                .Description("Sets the music volume to 0%")
                .Do(e => {
                    if (musicPlayers.ContainsKey(e.Server) == false)
                    {
                        return;
                    }
                    var player = musicPlayers[e.Server];
                    player.SetVolume(0);
                });

                cgb.CreateCommand("max")
                .Description("Sets the music volume to 100% (real max is actually 150%).")
                .Do(e => {
                    if (musicPlayers.ContainsKey(e.Server) == false)
                    {
                        return;
                    }
                    var player = musicPlayers[e.Server];
                    player.SetVolume(100);
                });

                cgb.CreateCommand("half")
                .Description("Sets the music volume to 50%.")
                .Do(e => {
                    if (musicPlayers.ContainsKey(e.Server) == false)
                    {
                        return;
                    }
                    var player = musicPlayers[e.Server];
                    player.SetVolume(50);
                });

                cgb.CreateCommand("shuffle")
                .Description("Shuffles the current playlist.")
                .Do(async e => {
                    if (musicPlayers.ContainsKey(e.Server) == false)
                    {
                        return;
                    }
                    var player = musicPlayers[e.Server];
                    if (player.SongQueue.Count < 2)
                    {
                        await _client.Reply(e, "Not enough songs in order to perform the shuffle.");
                        return;
                    }

                    player.SongQueue.Shuffle();
                    await _client.Reply(e, "🎵 `Songs shuffled.`");
                });

                bool setgameEnabled   = true;
                Timer setgameTimer    = new Timer();
                setgameTimer.Interval = 20000;
                setgameTimer.Elapsed += (s, e) => {
                    int num = musicPlayers.Where(kvp => kvp.Value.CurrentSong != null).Count();
                    _client.SetGame($"{num} songs".SnPl(num) + $", {musicPlayers.Sum(kvp => kvp.Value.SongQueue.Count())} queued");
                };
                cgb.CreateCommand("setgame")
                .Description("Sets the game of the bot to the number of songs playing.**Owner only**")
                .Do(async e => {
                    setgameEnabled = !setgameEnabled;
                    if (setgameEnabled)
                    {
                        setgameTimer.Start();
                    }
                    else
                    {
                        setgameTimer.Stop();
                    }

                    await _client.Reply(e, "`Music status " + (setgameEnabled ? "enabled`" : "disabled`"));
                });

                cgb.CreateCommand("playlist")
                .Description("Queues up to 25 songs from a youtube playlist specified by a link, or keywords.")
                .Parameter("playlist", ParameterType.Unparsed)
                .Do(async e => {
                    if (e.User.VoiceChannel?.Server != e.Server)
                    {
                        await _client.Reply(e, "💢 You need to be in the voice channel on this server.");
                        return;
                    }
                    var ids = await Searches.GetVideoIDs(await Searches.GetPlaylistIdByKeyword(e.GetArg("playlist")));
                    //todo TEMPORARY SOLUTION, USE RESOLVE QUEUE IN THE FUTURE
                    await _client.Reply(e, $"🎵 Attempting to queue **{ids.Count}** songs".SnPl(ids.Count));
                    foreach (var id in ids)
                    {
                        Task.Run(async() => await QueueSong(e, id, true)).ConfigureAwait(false);
                        await Task.Delay(150);
                    }
                    await _client.Reply(e, $"🎵 Attempting to queue **{ids.Count}** songs".SnPl(ids.Count));
                });

                /*  cgb.CreateCommand("radio").Alias("ra")
                 *    .Description("Queues a direct radio stream from a link.")
                 *    .Parameter("radio_link", ParameterType.Required)
                 *    .Do(async e => {
                 *        if (e.User.VoiceChannel?.Server != e.Server) {
                 *            await _client.Reply(e, "💢 You need to be in the voice channel on this server.");
                 *            return;
                 *        }
                 *        await QueueSong(e, e.GetArg("radio_link"),_client, radio: true);
                 *    });*/

                cgb.CreateCommand("debug")

                .Description("Writes some music data to console.")
                .Do(e => {
                    var output = "SERVER_NAME---SERVER_ID-----USERCOUNT----QUEUED\n" +
                                 string.Join("\n", musicPlayers.Select(kvp => kvp.Key.Name + "--" + kvp.Key.Id + " --" + kvp.Key.Users.Count() + "--" + kvp.Value.SongQueue.Count));
                    Console.WriteLine(output);
                });
            });
        }