예제 #1
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 DiscordVoiceClient JoinVoiceChannel(this DiscordSocketClient client, ulong guildId, ulong channelId, bool muted = false, bool deafened = false)
        {
            if (client.ConnectToVoiceChannels)
            {
                DiscordVoiceServer server = null;

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

                if (client.VoiceClients.ContainsKey(guildId))
                {
                    client.VoiceClients[guildId].Disconnect();
                }

                client.ChangeVoiceState(guildId, channelId, muted, deafened);

                int attempts = 0;

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

                    Thread.Sleep(1);

                    attempts++;
                }

                if (client.VoiceClients.ContainsKey(guildId))
                {
                    client.VoiceClients[guildId].ChannelId = channelId;
                    client.VoiceClients[guildId].Server    = server;
                    client.VoiceClients[guildId].RemoveHandlers();

                    return(client.VoiceClients[guildId]);
                }
                else
                {
                    var vClient = new DiscordVoiceClient(client, server, channelId);
                    client.VoiceClients.Add(guildId, vClient);
                    return(vClient);
                }
            }
            else
            {
                client.ChangeVoiceState(guildId, channelId, muted, deafened);

                return(null);
            }
        }
예제 #2
0
 /// <summary>
 /// Leaves a voice channel
 /// </summary>
 /// <param name="guildId">ID of the guild</param>
 public static void LeaveVoiceChannel(this DiscordSocketClient client, ulong guildId)
 {
     client.ChangeVoiceState(guildId, null);
 }
예제 #3
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 void JoinVoiceChannel(this DiscordSocketClient client, ulong guildId, ulong channelId, bool muted = false, bool deafened = false)
 {
     client.ChangeVoiceState(guildId, channelId, muted, deafened);
 }
예제 #4
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);
        }
예제 #5
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);
            }
        }