// Ban user Command public async Task <bool> BanUser(ulong id, string reason) { if (!_bannedUsers.TryAdd(id, true)) { return(false); } // Add to DB using (var soraContext = new SoraContext()) { if (string.IsNullOrWhiteSpace(reason)) { reason = ""; } soraContext.Bans.Add(new Ban() { UserId = id, BannedAt = DateTime.UtcNow, Reason = reason }); await soraContext.SaveChangesAsync(); } // notify other shards to ban user int port = int.Parse(ConfigService.GetConfigData("port")); for (int i = 0; i < Utility.TOTAL_SHARDS; i++) { if (i == Utility.SHARD_ID) { continue; } try { using (var httpClient = new HttpClient()) using (var request = new HttpRequestMessage(HttpMethod.Post, $"http://localhost:{(port+i)}/api/SoraApi/BanEvent/")) { string json = JsonConvert.SerializeObject(new { userId = id }); request.Content = new StringContent(json); request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); HttpResponseMessage response = await httpClient.SendAsync(request); response.Dispose(); } } catch (Exception e) { Console.WriteLine(e); await SentryService.SendMessage($"COULDN'T SEND BAN EVENT TO SHARD {i} FOR ID: {id}"); } } return(true); }
// UnBan user Command public async Task <bool> UnBanUser(ulong id) { if (!_bannedUsers.TryRemove(id, out _)) { return(false); } // remove from DB using (var soraContext = new SoraContext()) { soraContext.Bans.Remove(soraContext.Bans.FirstOrDefault(x => x.UserId == id)); await soraContext.SaveChangesAsync(); } // notify other shards to ban user int port = int.Parse(ConfigService.GetConfigData("port")); for (int i = 0; i < Utility.TOTAL_SHARDS; i++) { if (i == Utility.SHARD_ID) { continue; } try { using (var httpClient = new HttpClient()) using (var request = new HttpRequestMessage(HttpMethod.Post, $"http://localhost:{(port+i)}/api/SoraApi/UnBanEvent/")) { string json = JsonConvert.SerializeObject(new { userId = id }); request.Content = new StringContent(json); request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); HttpResponseMessage response = await httpClient.SendAsync(request); response.Dispose(); } } catch (Exception e) { Console.WriteLine(e); await SentryService.SendMessage($"COULDN'T SEND UNBAN EVENT TO SHARD {i} FOR ID: {id}"); } } return(true); }
public AnimeSearchService(InteractiveService interactiveService) { _interactive = interactiveService; _timeToRequestAgain = DateTime.UtcNow; _clientId = ConfigService.GetConfigData("client_id"); _clientSecret = ConfigService.GetConfigData("client_secret"); if (string.IsNullOrWhiteSpace(_clientId) || string.IsNullOrWhiteSpace(_clientSecret)) { Console.WriteLine("FAILED AINILIST DATA"); } var headers = new Dictionary <string, string> { { "grant_type", "client_credentials" }, { "client_id", _clientId }, { "client_secret", _clientSecret }, }; _formContent = new FormUrlEncodedContent(headers); #pragma warning disable 4014 RequestAuth(); #pragma warning restore 4014 }
public WeatherService() { _weatherId = ConfigService.GetConfigData("weather"); }
static TheMovieDbProvider() { apiKey = ConfigService.GetConfigData("movieDB"); }
public WeebService() { _token = ConfigService.GetConfigData("weebToken"); _weebClient = new WeebClient("Sora", Utility.SORA_VERSION); }
public GuildCountUpdaterService() { token = ConfigService.GetConfigData("discordbotsToken"); }