/// <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(); }
/// <summary> /// Method where the bot is instatiated and initialized. /// </summary> public async Task InitializeAsync() { var botBase = new TarsBase(this); botBase.DiscordSetup((await this.GetOrCreateJsonConfig()).Token); botBase.LavalinkSetup(); botBase.CommandsSetup(new string[] { "tars" }, services: new ServiceCollection().AddSingleton(this).AddSingleton(new MusicService(botBase.GetLavalink(), botBase.Discord))); await botBase.StartAsync(); }
/// <summary> /// Method for configuring MongoDB. /// </summary> /// <param name="botBase"></param> /// <param name="mongoClient">Mongo class instance.</param> /// <exception cref="ArgumentNullException"></exception> public static void MongoClientSetup(this TarsBase botBase, MongoClient mongoClient) { if (botBase is null) { throw new ArgumentNullException("The BotBase can be null!"); } _mongoClient = mongoClient ?? throw new ArgumentNullException("The MongoClient can be null!"); (typeof(TarsBase).GetField("_services", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(botBase) as IServiceCollection).AddSingleton(_mongoClient); }
/// <summary> /// Instantiates the scheduled events class. /// </summary> /// <param name="botBase"></param> /// <exception cref="NullReferenceException"></exception> /// <exception cref="InvalidOperationException"></exception> public static void ScheduledEventsSetup(this TarsBase botBase) { if (botBase is null) { throw new NullReferenceException("The Tars can be null! Instantiate the framework!"); } if (!(_scheduledEvents is null)) { throw new InvalidOperationException("The scheduled events has already been instantiated!"); } _scheduledEvents = new ConcurrentDictionary <Event, byte>(); AddScheduledEventAsService(botBase); }
public async Task TestInitialize() { this._bot = new TarsBase(this); // This was done to escape Discord's warning that the bot's token is "unprotected". this._bot.DiscordSetup(Encoding.UTF8.GetString(new byte[] { 78, 122, 81, 53, 78, 122, 69, 51, 78, 106, 107, 48, 77, 122, 81, 53, 77, 84, 103, 119, 79, 84, 85, 52, 46, 88, 48, 119, 68, 65, 119, 46, 80, 77, 110, 55, 51, 99, 71, 122, 118, 101, 83, 54, 56, 122, 113, 66, 74, 79, 117, 90, 98, 49, 105, 66, 48, 80, 107 })); this._bot.CommandsSetup(new string[] { "tars" }); _ = Task.Run(async() => await this._bot.StartAsync(new DiscordActivity { Name = "Running all tests..." }, UserStatus.DoNotDisturb)); await Task.Delay(TimeSpan.FromSeconds(15)); }
/// <summary> /// Method for configuring scheduled events. /// </summary> /// <param name="botBase"></param> /// <param name="events">Instantiate the scheduled events here.</param> /// <exception cref="InvalidOperationException"></exception> public static void ScheduledEventsSetup(this TarsBase botBase, params Event[] events) { if (!(_scheduledEvents is null)) { throw new InvalidOperationException("The event list has already been instantiated!"); } _scheduledEvents = new ConcurrentDictionary <Event, byte>(); var iEvent = 0; foreach (Event scheduledEvent in events) { if (!_scheduledEvents.Keys.Any(e => e.Name == scheduledEvent.Name)) { _scheduledEvents.TryAdd(scheduledEvent, 0); } ++iEvent; } AddScheduledEventAsService(botBase); botBase.Discord.LogMessage($"{(iEvent > 1 ? $"A total of {iEvent} scheduled events were recorded." : $"A total of {iEvent} scheduled event were recorded.")}", logLevel: LogLevel.Debug);
private static void AddScheduledEventAsService(TarsBase botBase) => (typeof(TarsBase).GetField("_services", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(botBase) as IServiceCollection) .AddSingleton(GetScheduledEvents(botBase));
/// <summary> /// Method to get the current Mongo instance. /// </summary> /// <param name="_"></param> /// <returns>The current instance of <see cref="MongoClient"/>.</returns> /// <exception cref="NullReferenceException"></exception> public static MongoClient GetMongoClient(this TarsBase _) => _mongoClient ?? throw new NullReferenceException("The MongoClient is null! Call the MongoClientSetup!");
private static void RegisterLavalinkAsService(TarsBase botBase) => (typeof(TarsBase).GetField("_services", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(botBase) as IServiceCollection).AddSingleton(_lavalink);
/// <summary> /// Get the <see cref="LavalinkExtension"/>. /// </summary> /// <param name="_"></param> /// <returns>The <see cref="LavalinkExtension"/>.</returns> /// <exception cref="NullReferenceException"></exception> public static LavalinkExtension GetLavalink(this TarsBase _) => _lavalink ?? throw new NullReferenceException("Connect the bot to Lavalink!");