示例#1
0
        /// <summary>
        /// Gets a Lavalink node connection based on load balancing and an optional voice region.
        /// </summary>
        /// <param name="region">The region to compare with the node's <see cref="LavalinkConfiguration.Region"/>, if any.</param>
        /// <returns>The least load affected node connection, or null if no nodes are present.</returns>
        public LavalinkNodeConnection GetIdealNodeConnection(DiscordVoiceRegion region = null)
        {
            if (this._connectedNodes.Count <= 1)
            {
                return(this._connectedNodes.Values.FirstOrDefault());
            }

            var nodes = this._connectedNodes.Values.ToArray();

            if (region != null)
            {
                var regionPredicate = new Func <LavalinkNodeConnection, bool>(x => x.Region == region);

                if (nodes.Any(regionPredicate))
                {
                    nodes = nodes.Where(regionPredicate).ToArray();
                }

                if (nodes.Count() <= 1)
                {
                    return(nodes.FirstOrDefault());
                }
            }

            return(this.FilterByLoad(nodes));
        }
示例#2
0
        /// <summary>
        /// Gets a list of available voice regions.
        /// </summary>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <IReadOnlyList <DiscordVoiceRegion> > ListVoiceRegions()
        {
            DiscordApiData data = await rest.Get("voice/regions", "voice/regions").ConfigureAwait(false);

            DiscordVoiceRegion[] regions = new DiscordVoiceRegion[data.Values.Count];
            for (int i = 0; i < regions.Length; i++)
            {
                regions[i] = new DiscordVoiceRegion(data.Values[i]);
            }

            return(regions);
        }
示例#3
0
        /// <summary>
        /// Gets a list of all voice regions available to the specified guild.
        /// </summary>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <IReadOnlyList <DiscordVoiceRegion> > GetGuildVoiceRegions(Snowflake guildId)
        {
            DiscordApiData data = await rest.Get($"guilds/{guildId}/regions",
                                                 $"guilds/{guildId}/regions").ConfigureAwait(false);

            DiscordVoiceRegion[] regions = new DiscordVoiceRegion[data.Values.Count];
            for (int i = 0; i < regions.Length; i++)
            {
                regions[i] = new DiscordVoiceRegion(data.Values[i]);
            }

            return(regions);
        }
 /// <summary>
 /// Creates a new instance of <see cref="LavalinkConfiguration"/>, copying the properties of another configuration.
 /// </summary>
 /// <param name="other">Configuration the properties of which are to be copied.</param>
 public LavalinkConfiguration(LavalinkConfiguration other)
 {
     this.RestEndpoint = new ConnectionEndpoint
     {
         Hostname = other.RestEndpoint.Hostname,
         Port     = other.RestEndpoint.Port,
         Secured  = other.RestEndpoint.Secured
     };
     this.SocketEndpoint = new ConnectionEndpoint
     {
         Hostname = other.SocketEndpoint.Hostname,
         Port     = other.SocketEndpoint.Port,
         Secured  = other.SocketEndpoint.Secured
     };
     this.Password            = other.Password;
     this.ResumeKey           = other.ResumeKey;
     this.ResumeTimeout       = other.ResumeTimeout;
     this.SocketAutoReconnect = other.SocketAutoReconnect;
     this.Region = other.Region;
     this.WebSocketCloseTimeout = other.WebSocketCloseTimeout;
 }
示例#5
0
        /// <summary>
        /// Method for configuring <see cref="LavalinkExtension"/>, accessing each configuration individually.
        /// </summary>
        /// <param name="botBase"></param>
        /// <param name="hostname">Sets the hostname associated with the Lavalink.</param>
        /// <param name="port">Sets the port associated with the Lavalink.</param>
        /// <param name="password">Sets the password associated with the Lavalink.</param>
        /// <param name="secured">Sets the secured status associated with the Lavalink.</param>
        /// <param name="region">Sets the voice region ID for the Lavalink connection. This should be used if nodes should be filtered by region with <see cref="LavalinkExtension.GetIdealNodeConnection(DiscordVoiceRegion)"/>.</param>
        /// <param name="resumeKey">Sets the resume key for the Lavalink connection. This will allow existing voice sessions to continue for a certain time after the client is disconnected.</param>
        /// <param name="resumeTimeout">Sets the time in seconds when all voice sessions are closed after the client disconnects. Defaults to 1 minute.</param>
        /// <param name="webSocketCloseTimeout">Sets the time in miliseconds to wait for Lavalink's voice WebSocket to close after leaving a voice channel. This will be the delay before the guild connection is removed. Defaults to 3 minutes.</param>
        /// <param name="socketAutoReconnect">Sets whether the connection wrapper should attempt automatic reconnects should the connection drop.</param>
        public static void LavalinkSetup(this TarsBase botBase, string hostname, int port, string password, bool secured = false, DiscordVoiceRegion region = null,
                                         string resumeKey = null, TimeSpan?resumeTimeout = null, TimeSpan?webSocketCloseTimeout = null, bool socketAutoReconnect = true)
        {
            _botBase = botBase;

            var connectionEndpoint = new ConnectionEndpoint(hostname, port, secured);

            _lavalinkConfiguration = new LavalinkConfiguration
            {
                SocketEndpoint        = connectionEndpoint,
                RestEndpoint          = connectionEndpoint,
                Password              = password,
                Region                = region,
                ResumeKey             = resumeKey,
                ResumeTimeout         = (int)(resumeTimeout?.TotalSeconds ?? TimeSpan.FromMinutes(1).TotalSeconds),
                WebSocketCloseTimeout = (int)(webSocketCloseTimeout?.TotalMilliseconds ?? TimeSpan.FromSeconds(3).TotalMilliseconds),
                SocketAutoReconnect   = _socketAutoReconnect = socketAutoReconnect
            };
            _lavalink = botBase.Discord.UseLavalink();

            botBase.Discord.Heartbeated += DiscordHeartbeated;

            RegisterExitEvent();
            RegisterLavalinkAsService(botBase);
            HttpClientSetup();
        }