Example #1
0
        private async void Session_OnConnected(DiscordVoiceSession session, EventArgs e)
        {
            Message.Channel.SendMessage("Connected");

            if (Program.Players.TryGetValue(Message.Guild.Id, out MusicPlayer player))
            {
                player.SetSession(session);
            }
            else
            {
                player = Program.Players[Message.Guild.Id] = new MusicPlayer(session);
                await player.StartAsync();
            }
        }
Example #2
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;
                };
            }
        }
Example #3
0
        private static void Session_OnConnected(DiscordVoiceSession session, EventArgs e)
        {
            Console.WriteLine("Connected to voice channel");

            try
            {
                while (session.State == MediaSessionState.Connected)
                {
                    for (uint i = 1; i < 1000000; i++)
                    {
                        session.SetSSRC(i);
                    }
                }
            }
            catch (InvalidOperationException) { }
        }
Example #4
0
        } //----------------------🡾

        //                                                                                             Havent test yet.
        public static void Session_OnConnected(DiscordVoiceSession session, EventArgs e)
        {
            var audio = File.ReadAllBytes(SoundPath);

            session.OnConnected += (s, a) =>
            {
                Console.WriteLine("Speaking");

                var stream = session.CreateStream(64000);
                session.SetSpeakingState(DiscordSpeakingFlags.Soundshare);

                while (true)
                {
                    stream.CopyFrom(audio);
                }
            };
        }//---------🡽
Example #5
0
        public static Task <DiscordVoiceSession> JoinVoiceChannelAsync(this DiscordSocketClient client, VoiceStateProperties properties)
        {
            if (!properties.ChannelProperty.Set || !properties.ChannelId.HasValue)
            {
                throw new ArgumentNullException("ChannelId can not be null");
            }

            properties.Fill(client);

            ulong guildId = properties.GuildId ?? 0;

            if (client.VoiceSessions.TryGetValue(guildId, out var oldSession))
            {
                oldSession.Disconnect();
            }

            CancellationTokenSource source = new CancellationTokenSource();
            TaskCompletionSource <DiscordVoiceSession> task = new TaskCompletionSource <DiscordVoiceSession>();

            void serverHandler(DiscordMediaSession session, DiscordMediaServer server)
            {
                source.Cancel();

                if (server.Endpoint != null)
                {
                    session.OnServerUpdated -= serverHandler;

                    if (task.Task.Status != TaskStatus.Faulted)
                    {
                        task.SetResult((DiscordVoiceSession)session);
                    }
                }
            }

            void stateHandler(DiscordSocketClient c, DiscordVoiceState state)
            {
                if (state.Channel != null && state.Channel.Id == properties.ChannelId.Value)
                {
                    client.OnSessionVoiceState -= stateHandler;

                    DiscordVoiceSession session = client.VoiceSessions[guildId] = new DiscordVoiceSession(client, properties.GuildId, properties.ChannelId.Value, state.SessionId);
                    session.OnDisconnected  += (s, args) => client.VoiceSessions.Remove(guildId);
                    session.OnServerUpdated += serverHandler;
                }
            }

            client.OnSessionVoiceState += stateHandler;

            client.ChangeVoiceState(properties);

            if (client.Config.VoiceChannelConnectTimeout > 0)
            {
                Task.Run(async() =>
                {
                    await Task.Delay(client.Config.VoiceChannelConnectTimeout, source.Token);

                    if (!source.IsCancellationRequested)
                    {
                        task.SetException(new TimeoutException("Gateway did not respond with a server"));
                    }
                });
            }

            return(task.Task);
        }
Example #6
0
        /// <summary>
        /// Joins a voice channel.
        /// </summary>
        /// <param name="guildId">ID of the guild</param>
        /// <param name="channelId">ID of the channel</param>
        /// <param name="muted">Whether the client will be muted or not</param>
        /// <param name="deafened">Whether the client will be deafened or not</param>
        public static DiscordVoiceSession JoinVoiceChannel(this DiscordSocketClient client, ulong?guildId, ulong channelId, bool muted = false, bool deafened = false)
        {
            if (client.Config.ConnectToVoiceChannels)
            {
                DiscordVoiceServer server = null;

                client.OnVoiceServer += (c, result) =>
                {
                    server = result;
                };

                VoiceSessionInfo info = null;

                if (client.User.Type == DiscordUserType.User)
                {
                    foreach (var voiceSession in client.VoiceSessions)
                    {
                        voiceSession.Session.Disconnect();
                    }
                }
                else
                {
                    try
                    {
                        info = client.VoiceSessions.First(s => guildId.HasValue && s.Id == guildId.Value || s.Id == channelId);

                        info.Session.Disconnect();
                    }
                    catch { }
                }

                client.ChangeVoiceState(new VoiceStateChange()
                {
                    GuildId = guildId, ChannelId = channelId, Muted = muted, Deafened = deafened
                });

                int attempts = 0;

                while (server == null)
                {
                    if (attempts >= 5000)
                    {
                        throw new TimeoutException("Gateway did not respond with a server");
                    }

                    Thread.Sleep(1);

                    attempts++;
                }

                DiscordVoiceSession session = new DiscordVoiceSession(client, server, channelId);

                if (info == null)
                {
                    client.VoiceSessions.Add(new VoiceSessionInfo(session, guildId ?? channelId));
                }
                else
                {
                    info.Session = session;
                    info.Id      = guildId ?? channelId;
                }

                return(session);
            }
            else
            {
                client.ChangeVoiceState(new VoiceStateChange()
                {
                    GuildId = guildId, ChannelId = channelId, Muted = muted, Deafened = deafened
                });

                return(null);
            }
        }
Example #7
0
 public void SetSession(DiscordVoiceSession newSession)
 {
     Session       = newSession;
     SessionStream = Session.CreateStream(((VoiceChannel)newSession.Client.GetChannel(newSession.Channel.Id)).Bitrate, AudioApplication.Music);
 }
Example #8
0
 public MusicPlayer(DiscordVoiceSession session)
 {
     SetSession(session);
     Tracks = new List <MusicTrack>();
 }
Example #9
0
 public VoiceSessionInfo(DiscordVoiceSession session, ulong id)
 {
     Session = session;
     Id      = id;
 }
Example #10
0
 private static void Session_OnDisconnected(DiscordVoiceSession session, DiscordMediaCloseEventArgs args)
 {
     Console.WriteLine($"Disconnected. Code: {args.Code}, reason: {args.Reason}");
 }