예제 #1
0
        public void Execute(DiscordSocketClient client, DiscordMessage message)
        {
            VoiceChannel channel;

            try
            {
                channel = client.GetChannel(client.GetGuildVoiceStates(message.Guild).First(s => s.UserId == message.Author.User.Id).Channel.Id).ToVoiceChannel();
            }
            catch
            {
                message.Channel.SendMessage("You must be connected to a voice channel to play music");

                return;
            }

            if (!Program.Sessions.ContainsKey(message.Guild.Id))
            {
                DiscordVoiceSession voiceSession = client.JoinVoiceChannel(message.Guild, channel, false, true);

                voiceSession.OnConnected += (c, e) =>
                {
                    var session = new MusicSession(message.Guild)
                    {
                        Session = voiceSession,
                        Channel = channel,
                        Queue   = new Queue <Track>(),
                    };

                    Program.Sessions.Add(message.Guild.Id, session);

                    message.Channel.SendMessage("Connected to voice channel.");

                    Task.Run(() => session.StartQueue());
                };
            }
            else if (Program.Sessions[message.Guild.Id].Channel.Id != channel.Id)
            {
                var session = Program.Sessions[message.Guild.Id];

                session.SwitchingChannels = true;
                session.Channel           = channel;

                session.Session              = client.JoinVoiceChannel(message.Guild, channel, false, true);
                session.Session.OnConnected += (sender, e) =>
                {
                    Program.Sessions[message.Guild.Id].SwitchingChannels = false;
                };
            }
        }
예제 #2
0
        public override void Execute(DiscordSocketClient client, string[] args, Message message)
        {
            VoiceChannel channel;

            try
            {
                channel = client.GetChannel(Program.VoiceStates[message.Guild.Id].First(s => s.UserId == message.Author.User.Id).Channel.Id).ToVoiceChannel();
            }
            catch
            {
                message.Channel.SendMessage("You must be connected to a voice channel to play music");

                return;
            }

            if (!Program.Sessions.ContainsKey(message.Guild.Id) || Program.Sessions[message.Guild.Id].Channel.Id != channel.Id)
            {
                DiscordVoiceClient voiceClient = client.JoinVoiceChannel(message.Guild, channel, false, true);

                voiceClient.OnConnected += (c, e) =>
                {
                    var session = new MusicSession(message.Guild)
                    {
                        Client  = voiceClient,
                        Channel = channel,
                        Queue   = new Queue <Track>(),
                    };

                    Program.Sessions.Add(message.Guild.Id, session);

                    message.Channel.SendMessage("Connected to voice channel.");

                    Task.Run(() => session.StartQueue());
                };

                voiceClient.Connect();
            }
        }