public async Task UndoPunishment(ModCase modCase) { using var scope = _serviceProvider.CreateScope(); Database database = scope.ServiceProvider.GetRequiredService <Database>(); List <ModCase> parallelCases = await database.SelectAllModCasesThatHaveParallelPunishment(modCase); if (parallelCases.Count != 0) { _logger.LogInformation("Cannot undo punishment. There exists a parallel punishment for this case"); return; } GuildConfig guildConfig; try { guildConfig = await GuildConfigRepository.CreateDefault(scope.ServiceProvider).GetGuildConfig(modCase.GuildId); } catch (ResourceNotFoundException) { _logger.LogError($"Cannot execute punishment in guild {modCase.GuildId} - guildconfig not found."); return; } string reason = null; try { Translator translator = scope.ServiceProvider.GetRequiredService <Translator>(); reason = translator.T(guildConfig).NotificationDiscordAuditLogPunishmentsUndone(modCase.CaseId, modCase.Title.Truncate(400)); } catch (Exception ex) { _logger.LogError(ex, $"Failed to resolve audit log reason string for case {modCase.GuildId}/{modCase.CaseId}"); } switch (modCase.PunishmentType) { case PunishmentType.Mute: if (guildConfig.MutedRoles.Length != 0) { _logger.LogInformation($"Unmute User {modCase.UserId} in guild {modCase.GuildId} with roles " + string.Join(',', guildConfig.MutedRoles.Select(x => x.ToString()))); foreach (ulong role in guildConfig.MutedRoles) { await _discord.RemoveGuildUserRole(modCase.GuildId, modCase.UserId, role, reason); } } else { _logger.LogInformation($"Cannot Unmute User {modCase.UserId} in guild {modCase.GuildId} - mute role undefined."); } break; case PunishmentType.Ban: _logger.LogInformation($"Unban User {modCase.UserId} in guild {modCase.GuildId}."); await _discord.UnBanUser(modCase.GuildId, modCase.UserId, reason); _discord.RemoveFromCache(CacheKey.GuildBan(modCase.GuildId, modCase.UserId)); // refresh ban cache break; } }
public async Task <IActionResult> GetUser() { Identity identity = await GetIdentity(); IUser currentUser = identity.GetCurrentUser(); List <UserGuild> currentUserGuilds = identity.GetCurrentUserGuilds(); List <DiscordGuildView> memberGuilds = new(); List <DiscordGuildView> modGuilds = new(); List <DiscordGuildView> adminGuilds = new(); List <DiscordGuildView> bannedGuilds = new(); bool siteAdmin = _config.GetSiteAdmins().Contains(currentUser.Id) || identity is TokenIdentity; if (identity is DiscordOAuthIdentity) { List <GuildConfig> registeredGuilds = await GuildConfigRepository.CreateDefault(_serviceProvider).GetAllGuildConfigs(); foreach (GuildConfig guild in registeredGuilds) { UserGuild userGuild = currentUserGuilds.FirstOrDefault(x => x.Id == guild.GuildId); if (userGuild != null) { IGuild userGuildFetched = _discordAPI.FetchGuildInfo(userGuild.Id, CacheBehavior.Default); if (userGuildFetched != null) { if (await identity.HasModRoleOrHigherOnGuild(guild.GuildId)) { if (await identity.HasAdminRoleOnGuild(guild.GuildId)) { adminGuilds.Add(new DiscordGuildView(userGuildFetched)); } else { modGuilds.Add(new DiscordGuildView(userGuildFetched)); } } else { memberGuilds.Add(new DiscordGuildView(userGuildFetched)); } } } else { try { _discordAPI.GetFromCache <IBan>(CacheKey.GuildBan(guild.GuildId, currentUser.Id)); bannedGuilds.Add(new DiscordGuildView(_discordAPI.FetchGuildInfo(guild.GuildId, CacheBehavior.Default))); } catch (NotFoundInCacheException) { } } } } return(Ok(new APIUser(memberGuilds, bannedGuilds, modGuilds, adminGuilds, currentUser, siteAdmin))); }
private Task GuildBanRemoved(SocketUser user, SocketGuild guild) { using var scope = _serviceProvider.CreateScope(); // Refresh ban cache DiscordAPIInterface discordAPI = scope.ServiceProvider.GetRequiredService <DiscordAPIInterface>(); discordAPI.RemoveFromCache(CacheKey.GuildBan(guild.Id, user.Id)); return(Task.CompletedTask); }