Exemplo n.º 1
0
        private async Task Vnc_VoiceDisconnected(DiscordGuild guild)
        {
            VoiceNextConnection vnc = null;

            if (this.ActiveConnections.ContainsKey(guild.Id))
            {
                this.ActiveConnections.TryRemove(guild.Id, out vnc);
            }

            var vsd = new VoiceDispatch
            {
                OpCode  = 4,
                Payload = new VoiceStateUpdatePayload
                {
                    GuildId   = guild.Id,
                    ChannelId = null
                }
            };
            var vsj = JsonConvert.SerializeObject(vsd, Formatting.None);

            await(guild.Discord as DiscordClient)._webSocketClient.SendMessageAsync(vsj).ConfigureAwait(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create a VoiceNext connection for the specified channel.
        /// </summary>
        /// <param name="channel">Channel to connect to.</param>
        /// <returns>VoiceNext connection for this channel.</returns>
        public async Task <VoiceNextConnection> ConnectAsync(DiscordChannel channel)
        {
            if (channel.Type != ChannelType.Voice)
            {
                throw new ArgumentException(nameof(channel), "Invalid channel specified; needs to be voice channel");
            }

            if (channel.Guild == null)
            {
                throw new ArgumentException(nameof(channel), "Invalid channel specified; needs to be guild channel");
            }

            if (!channel.PermissionsFor(channel.Guild.CurrentMember).HasPermission(Permissions.UseVoice))
            {
                throw new InvalidOperationException("You need UseVoice permission to connect to this voice channel");
            }

            var gld = channel.Guild;

            if (ActiveConnections.ContainsKey(gld.Id))
            {
                throw new InvalidOperationException("This guild already has a voice connection");
            }

            var vstut = new TaskCompletionSource <VoiceStateUpdateEventArgs>();
            var vsrut = new TaskCompletionSource <VoiceServerUpdateEventArgs>();

            this.VoiceStateUpdates[gld.Id]  = vstut;
            this.VoiceServerUpdates[gld.Id] = vsrut;

            var vsd = new VoiceDispatch
            {
                OpCode  = 4,
                Payload = new VoiceStateUpdatePayload
                {
                    GuildId   = gld.Id,
                    ChannelId = channel.Id,
                    Deafened  = false,
                    Muted     = false
                }
            };
            var vsj = JsonConvert.SerializeObject(vsd, Formatting.None);

            await(channel.Discord as DiscordClient)._webSocketClient.SendMessageAsync(vsj).ConfigureAwait(false);

            var vstu = await vstut.Task.ConfigureAwait(false);

            var vstup = new VoiceStateUpdatePayload
            {
                SessionId = vstu.SessionId,
                UserId    = vstu.User.Id
            };
            var vsru = await vsrut.Task.ConfigureAwait(false);

            var vsrup = new VoiceServerUpdatePayload
            {
                Endpoint = vsru.Endpoint,
                GuildId  = vsru.Guild.Id,
                Token    = vsru.VoiceToken
            };

            var vnc = new VoiceNextConnection(this.Client, gld, channel, this.Configuration, vsrup, vstup);

            vnc.VoiceDisconnected += this.Vnc_VoiceDisconnected;
            await vnc.ConnectAsync().ConfigureAwait(false);

            await vnc.WaitForReadyAsync().ConfigureAwait(false);

            this.ActiveConnections[gld.Id] = vnc;
            return(vnc);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create a VoiceNext connection for the specified channel.
        /// </summary>
        /// <param name="channel">Channel to connect to.</param>
        /// <returns>VoiceNext connection for this channel.</returns>
        public async Task <VoiceNextConnection> ConnectAsync(DiscordChannel channel)
        {
            if (channel.Type != ChannelType.Voice)
            {
                throw new ArgumentException(nameof(channel), "Invalid channel specified; needs to be voice channel");
            }

            if (channel.Parent == null)
            {
                throw new ArgumentException(nameof(channel), "Invalid channel specified; needs to be guild channel");
            }

            var gld = channel.Parent;

            if (ActiveConnections.ContainsKey(gld.ID))
            {
                throw new InvalidOperationException("This guild already has a voice connection");
            }

            var vstut = new TaskCompletionSource <VoiceStateUpdateEventArgs>();
            var vsrut = new TaskCompletionSource <VoiceServerUpdateEventArgs>();

            this.VoiceStateUpdates[gld.ID]  = vstut;
            this.VoiceServerUpdates[gld.ID] = vsrut;

            var vsd = new VoiceDispatch
            {
                OpCode  = 4,
                Payload = new VoiceStateUpdatePayload
                {
                    GuildId   = gld.ID,
                    ChannelId = channel.ID,
                    Deafened  = false,
                    Muted     = false
                }
            };
            var vsj = JsonConvert.SerializeObject(vsd, Formatting.None);

            DiscordClient._websocketClient.SendMessage(vsj);

            var vstu  = await vstut.Task;
            var vstup = new VoiceStateUpdatePayload
            {
                SessionId = vstu.SessionID,
                UserId    = vstu.UserID
            };
            var vsru  = await vsrut.Task;
            var vsrup = new VoiceServerUpdatePayload
            {
                Endpoint = vsru.Endpoint,
                GuildId  = vsru.GuildID,
                Token    = vsru.VoiceToken
            };

            var d1 = (TaskCompletionSource <VoiceStateUpdateEventArgs>)null;
            var d2 = (TaskCompletionSource <VoiceServerUpdateEventArgs>)null;

            this.VoiceStateUpdates.TryRemove(gld.ID, out d1);
            this.VoiceServerUpdates.TryRemove(gld.ID, out d2);

            var vnc = new VoiceNextConnection(this.Client, gld, channel, this.Configuration, vsrup, vstup);

            vnc.VoiceDisconnected += this.Vnc_VoiceDisconnected;
            await vnc.ConnectAsync();

            await vnc.WaitForReady();

            this.ActiveConnections[gld.ID] = vnc;
            return(vnc);
        }