public async Task ExecutePunishment(ModCase modCase) { using var scope = _serviceProvider.CreateScope(); Database database = scope.ServiceProvider.GetRequiredService <Database>(); 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).NotificationDiscordAuditLogPunishmentsExecute(modCase.CaseId, modCase.LastEditedByModId, 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($"Mute 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.GrantGuildUserRole(modCase.GuildId, modCase.UserId, role, reason); } } else { _logger.LogInformation($"Cannot Mute User {modCase.UserId} in guild {modCase.GuildId} - mute role undefined."); } break; case PunishmentType.Ban: _logger.LogInformation($"Ban User {modCase.UserId} in guild {modCase.GuildId}."); await _discord.BanUser(modCase.GuildId, modCase.UserId, reason); await _discord.GetGuildUserBan(modCase.GuildId, modCase.UserId, CacheBehavior.IgnoreCache); // refresh ban cache break; case PunishmentType.Kick: _logger.LogInformation($"Kick User {modCase.UserId} in guild {modCase.GuildId}."); await _discord.KickGuildUser(modCase.GuildId, modCase.UserId, reason); break; } }
private async Task GuildBanAdded(SocketUser user, SocketGuild guild) { using var scope = _serviceProvider.CreateScope(); // Refresh ban cache DiscordAPIInterface discordAPI = scope.ServiceProvider.GetRequiredService <DiscordAPIInterface>(); await discordAPI.GetGuildUserBan(guild.Id, user.Id, CacheBehavior.IgnoreCache); discordAPI.RemoveFromCache(CacheKey.GuildMember(guild.Id, user.Id)); // Refresh identity memberships IdentityManager identityManager = scope.ServiceProvider.GetRequiredService <IdentityManager>(); foreach (Identity identity in identityManager.GetCurrentIdentities()) { if (identity.GetCurrentUser().Id == user.Id) { identity.RemoveGuildMembership(guild.Id); } } }