コード例 #1
0
ファイル: Extensions.cs プロジェクト: xHeaven/Anarchy
        /// <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
ファイル: Extensions.cs プロジェクト: Mariobob/Anarchy
        /// <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);
            }
        }