private async Task StartAsync() { Cooldowns = new ConcurrentDictionary <ulong, DateTimeOffset>(); RateLimits = new ConcurrentDictionary <ulong, Tuple <List <SocketUserMessage>, TimeSpan> >(); RateLimitedUsers = new ConcurrentDictionary <ulong, DateTimeOffset>(); VotesForRemoval = new HashSet <ulong>(); _client = new DiscordSocketClient(new DiscordSocketConfig { AlwaysDownloadUsers = true, }); _commands = new CommandService(new CommandServiceConfig { DefaultRunMode = RunMode.Async, CaseSensitiveCommands = false }); _services = new ServiceCollection() .AddSingleton(_client) .AddSingleton(_commands) .BuildServiceProvider(); _client.Log += Log; _client.Ready += ClientReady; _client.GuildMemberUpdated += GuildMemberUpdated; _client.UserLeft += UserLeft; _client.UserJoined += UserJoined; _client.ReactionAdded += ReactionAdded; _client.ReactionRemoved += ReactionAdded; _client.LatencyUpdated += ClientLatencyUpdated; //_client.Connected += ClientConnected; await InstallCommandsAsync(); // TODO token.txt var tokenPath = "bot.token"; string token = ""; if (!File.Exists(tokenPath)) { await Log(new LogMessage(LogSeverity.Critical, "Startup", "No bot.token file found, token not present")); await Task.Delay(-1); } Console.Title = $"tModLoader Bot - {AppContext.BaseDirectory} - {await ModsManager.GetTMLVersion()}"; await Console.Out.WriteLineAsync($"https://discordapp.com/api/oauth2/authorize?client_id=&scope=bot"); await Console.Out.WriteLineAsync($"Start date: {DateTime.Now}"); token = File.ReadAllText(tokenPath).Trim(); await _client.LoginAsync(TokenType.Bot, token); await _client.StartAsync(); await Task.Delay(-1); }
/// <summary> /// Once the client is ready: /// * setup owner var /// * initialize the config manager /// * setup configs for every guild if needed /// * run timers to do stuff with data /// </summary> private async Task ClientReady() { await _client.SetGameAsync($"Starting...", type : ActivityType.Playing); BotOwner = (await _client.GetApplicationInfoAsync()).Owner; await Log(new LogMessage(LogSeverity.Info, "ClientReady", "Initializing ConfigManager")); await ConfigManager.Initialize(); await Log(new LogMessage(LogSeverity.Info, "ClientReady", "Initializing ModsManager")); await ModsManager.Initialize(); await Log(new LogMessage(LogSeverity.Info, "ClientReady", "Initializing Guilds")); foreach (var guild in _client.Guilds) { if (!ConfigManager.IsGuildManaged(guild.Id)) { await ConfigManager.SetupForGuild(guild.Id); } } await Log(new LogMessage(LogSeverity.Info, "ClientReady", "Setting update timer")); var timer = new Timer(async o => { await Log(new LogMessage(LogSeverity.Critical, "SystemMain", "Starting maintenance")); var now = DateTimeOffset.UtcNow; Cooldowns = Cooldowns .Where(x => now < x.Value) .ToDictionary(x => x.Key, x => x.Value); RateLimitedUsers = RateLimitedUsers .Where(x => now < x.Value) .ToDictionary(x => x.Key, x => x.Value); try { await ModsManager.Maintain(_client); } catch (Exception e) { await Log(new LogMessage(LogSeverity.Critical, "SystemMain", "", e)); } }, null, TimeSpan.FromMinutes(0), TimeSpan.FromMinutes(1)); var timerStatusCache = new Timer(async o => { await Log(new LogMessage(LogSeverity.Critical, "SystemMain", "Clearing status addresses cache")); foreach (var clientGuild in _client.Guilds) { if (ConfigManager.GetManagedConfig(clientGuild.Id) is GuildConfig config) { config.StatusAddressesCache.Clear(); } } }, null, TimeSpan.FromMinutes(0), TimeSpan.FromMinutes(5)); var timerTagListsCache = new Timer(async o => { await Log(new LogMessage(LogSeverity.Critical, "SystemMain", "Clearing tag lists cache")); foreach (var clientGuild in _client.Guilds) { if (ConfigManager.GetManagedConfig(clientGuild.Id) is GuildConfig config) { var now = DateTimeOffset.UtcNow; config.TagListCache.Where(x => x.expiryTime >= now); } } }, null, TimeSpan.FromMinutes(0), TimeSpan.FromMinutes(2)); await Log(new LogMessage(LogSeverity.Info, "ClientReady", "Setting game")); await _client.SetGameAsync($"tModLoader {ModsManager.tMLVersion}", type : ActivityType.Playing); await Log(new LogMessage(LogSeverity.Info, "ClientReady", "Maintaining ModsManager")); await ModsManager.Maintain(_client); await Log(new LogMessage(LogSeverity.Info, "ClientReady", "Done.")); }