/// <summary> /// Initialises the bot and its required services and then subscribes to its events. /// </summary> /// <returns>No object or value is returned by this method when it completes.</returns> public async Task MainAsync() { Console.Title = "BotHATTwaffle"; // Concurrently writes the standard output stream to a log file. const string LOG_PATH = "c:/BotHATTwafflelogs/"; string logName = $"{DateTime.Now:hh_mmtt-MM_dd_yyyy}.log"; Directory.CreateDirectory(LOG_PATH); var _ = new ConsoleCopy(LOG_PATH + logName); // Dependency injection. All objects use constructor injection. _client = new DiscordSocketClient(); _commands = new CommandService(); _services = new ServiceCollection() .AddSingleton(_client) .AddSingleton(_commands) .AddSingleton <TimerService>() .AddSingleton <UtilityService>() .AddSingleton <ModerationServices>() .AddSingleton <LevelTesting>() .AddSingleton <ToolsService>() .AddSingleton <Eavesdropping>() .AddSingleton <DataServices>() .AddSingleton <Random>() .AddSingleton <DownloaderService>() .AddSingleton <GoogleCalendar>() .AddSingleton(s => new InteractiveService(_client, TimeSpan.FromSeconds(120))) .BuildServiceProvider(); // Retrieves services that this class uses. _dataServices = _services.GetRequiredService <DataServices>(); _timerService = _services.GetRequiredService <TimerService>(); _eavesdropping = _services.GetRequiredService <Eavesdropping>(); // Retrieves the bot's token from the config file; effectively exits the program if botToken can't be retrieved. // This is the only setting that has to be retreived this way so it can start up properly. // Once the guild becomes ready the rest of the settings are fully loaded. if (!_dataServices.Config.TryGetValue("botToken", out string botToken)) { return; } // Event subscriptions. _client.Log += LogEventHandler; _client.UserJoined += _eavesdropping.UserJoin; // When a user joins the server. _client.GuildAvailable += GuildAvailableEventHandler; // When a guild is available. await InstallCommandsAsync(); await _client.LoginAsync(TokenType.Bot, botToken); await _client.StartAsync(); // Subscribes to connect/disconnect after logging in because they would otherwise be raised before needed. _client.Disconnected += DisconnectedEventHandler; _client.Connected += ConnectedEventHandler; await Task.Delay(Timeout.Infinite); // Blocks this task until the program is closed. }
/// <summary> /// Initialises the bot and its required services and then subscribes to its events. /// </summary> /// <returns>No object or value is returned by this method when it completes.</returns> public async Task MainAsync() { Console.Title = "BotHATTwaffle"; // Concurrently writes the standard output stream to a log file. const string LOG_PATH = "c:/BotHATTwafflelogs/"; string logName = $"{DateTime.Now:hh_mmtt-MM_dd_yyyy}.log"; Directory.CreateDirectory(LOG_PATH); var _ = new ConsoleCopy(LOG_PATH + logName); // Dependency injection. All objects use constructor injection. _client = new DiscordSocketClient(); _commands = new CommandService(); _services = new ServiceCollection() .AddSingleton(_client) .AddSingleton(_commands) .AddSingleton <ITimerService, TimerService>() .AddSingleton <PlaytestingService>() .AddSingleton <MessageListener>() .AddSingleton <DataService>() .AddSingleton <Random>() .AddSingleton <DownloadService>() .AddSingleton <EventCalendarService>() .AddSingleton <IHelpService, HelpService>() .AddSingleton <IMuteService, MuteService>() .AddSingleton(s => new InteractiveService(_client, TimeSpan.FromSeconds(120))) .BuildServiceProvider(); // Retrieves services that this class uses. await SafeExecuteConfigCallback( () => { _data = _services.GetRequiredService <DataService>(); return(Task.CompletedTask); }); _timer = _services.GetRequiredService <ITimerService>(); _messageListener = _services.GetRequiredService <MessageListener>(); // Constructs services explicitly. Modules are transient so their dependencies would normally be constructed when // the module is initially used e.g. a command is invoked. _services.GetRequiredService <IHelpService>(); _services.GetRequiredService <PlaytestingService>(); // Event subscriptions. _client.GuildAvailable += GuildAvailableEventHandler; _client.Log += LogEventHandler; _client.Ready += ReadyEventHandler; _client.UserJoined += UserJoinedEventHandler; await InstallCommandsAsync(); await _client.LoginAsync(TokenType.Bot, _data.Config["botToken"]); await _client.StartAsync(); // Subscribes to connect/disconnect after logging in because they would otherwise be raised before needed. _client.Disconnected += DisconnectedEventHandler; _client.Connected += ConnectedEventHandler; await Task.Delay(Timeout.Infinite); // Blocks this task until the program is closed. }