public async Task <IActionResult> Punishments(ulong guildId, [FromServices] IDiscordGuildService guildService) { var punishments = await _entityServicePunishments.Get(x => x.GuildId == guildId); var punishmentModels = new List <PunishmentModel>(); var roles = await guildService.GetRoles(guildId); foreach (var p in punishments) { var role = roles.FirstOrDefault(r => r.Id == p.RoleId); punishmentModels.Add(new PunishmentModel { PunishDuration = p.PunishDuration, PunishType = p.PunishType, RoleId = p.RoleId, WarningThreshold = p.WarningThreshold, WarningType = p.WarningType, Role = new SelectList(roles, "Id", "Name", role), DeletePunishment = false, PunishmentId = p.Id }); } var vm = new PunishmentsViewModel { Punishments = punishmentModels }; return(View(vm)); }
public async Task <IActionResult> LinkFilter(ulong guildId, [FromServices] IDiscordGuildService guildService) { var settings = await _moderationModuleUtils.GetModerationSettings(guildId); var guildChannels = await guildService.GetChannels(guildId); var textChannels = guildChannels?.Where(c => c.Type == (int)ChannelType.Text); var roles = await guildService.GetRoles(guildId); var alreadyWhitelistedRoles = settings?.WhitelistedRoles.Where(r => r.WhitelistType == FilterType.Link) .Select(r => r.RoleId); var alreadyWhitelistedChannels = settings?.WhitelistedChannels .Where(r => r.WhitelistType == FilterType.Link).Select(c => c.ChannelId); var links = settings.LinkFilter?.WhitelistedLinks.Select(l => l.Link) ?? new List <string>(); var vm = new LinkFilterViewModel { Enabled = settings?.LinkFilter?.Enabled ?? false, WhitelistedChannels = new MultiSelectList(textChannels, "Id", "Name", alreadyWhitelistedChannels), WhitelistedRoles = new MultiSelectList(roles, "Id", "Name", alreadyWhitelistedRoles), WhitelistedLinks = new MultiSelectList(links, links), WarningExpirePeriod = settings?.LinkFilter?.WarningExpirePeriod ?? 0 }; return(View(vm)); }
public async Task <IActionResult> ProfanityFilter(ulong guildId, [FromServices] IDiscordGuildService guildService) { var settings = await _moderationModuleUtils.GetModerationSettings(guildId); var guildChannels = await guildService.GetChannels(guildId); var textChannels = guildChannels?.Where(c => c.Type == (int)ChannelType.Text); var roles = await guildService.GetRoles(guildId); var alreadyWhitelistedRoles = settings?.WhitelistedRoles.Where(r => r.WhitelistType == FilterType.Profanity) .Select(r => r.RoleId); var alreadyWhitelistedChannels = settings?.WhitelistedChannels .Where(r => r.WhitelistType == FilterType.Profanity).Select(c => c.ChannelId); var words = settings?.ProfanityFilter?.BannedWords.Select(w => w.Word) ?? new List <string>(); var vm = new ProfanityFilterViewModel { Enabled = settings?.ProfanityFilter?.Enabled ?? false, WhitelistedChannels = new MultiSelectList(textChannels, "Id", "Name", alreadyWhitelistedChannels), WhitelistedRoles = new MultiSelectList(roles, "Id", "Name", alreadyWhitelistedRoles), BannedWords = new MultiSelectList(words, words), UseDefaultBannedWords = settings?.ProfanityFilter?.UseDefaultList ?? true, WarningExpirePeriod = settings?.ProfanityFilter?.WarningExpirePeriod ?? 0 }; return(View(vm)); }
public async Task <IActionResult> General(ulong guildId, [FromServices] IDiscordGuildService guildService) { var settings = await _moderationModuleUtils.GetModerationSettings(guildId); var guildChannels = await guildService.GetChannels(guildId); var textChannels = guildChannels.Where(c => c.Type == (int)ChannelType.Text); var roles = await guildService.GetRoles(guildId); var alreadyWhitelistedRoles = settings?.WhitelistedRoles.Where(r => r.WhitelistType == FilterType.Global) .Select(r => r.RoleId); var alreadyWhitelistedChannels = settings?.WhitelistedChannels .Where(r => r.WhitelistType == FilterType.Global).Select(c => c.ChannelId); var vm = new GlobalSettingsViewModel { Enabled = settings?.Enabled ?? false, WhitelistedChannels = new MultiSelectList(textChannels, "Id", "Name", alreadyWhitelistedChannels), WhitelistedRoles = new MultiSelectList(roles, "Id", "Name", alreadyWhitelistedRoles) }; return(View("GlobalSettings", vm)); }
public async Task <IActionResult> StreamerRoleSettings(ulong guildId, StreamerRoleSettingsViewModel viewModel, [FromServices] IBot bot, [FromServices] IDiscordGuildService guildService) { var botRolePosition = bot.GetBotRoleHierarchy(guildId); var roles = await guildService.GetRoles(guildId); var selectedRolePosition = roles.FirstOrDefault(r => r.Id == viewModel.RoleId)?.Position; // Discord bots cannot assign to roles that are higher then them in the hierarchy. if (selectedRolePosition > botRolePosition) { ModelState.AddModelError("RolePosition", "The bots managed role must be positioned higher then the selected role"); viewModel.Roles = new SelectList(roles.RemoveManaged(), "Id", "Name"); return(View(viewModel)); } // Save the settings to the database await _streamerRoleSettingsService.SaveSettings(new StreamerRoleSettings { GuildId = guildId, Enabled = viewModel.Enabled, RoleId = viewModel.RoleId }); return(RedirectToAction("Index")); }
public async Task <IActionResult> StreamerSettings(ulong guildId, [FromServices] IDiscordGuildService guildService, [FromServices] IEntityService <StreamerChannelSettings> channelSettingsService) { // All channels in guild. var channels = await guildService.GetChannels(guildId); // Text channels in guild. var textChannels = channels.Where(x => x.Type == 0).ToList(); // Roles in guild. var roles = await guildService.GetRoles(guildId); var viewModel = new StreamerSettingsViewModel { Channels = new SelectList(textChannels, "Id", "Name"), ChannelSettings = new StreamerChannelSettingsViewModel(), GuildId = guildId.ToString(), Roles = new SelectList(roles.RemoveManaged(), "Id", "Name") }; // Get general module settings for guild, from database. var settings = await _streamAnnouncerSettingsService.GetSettingsByGuild(guildId, x => x.WhiteListedRoleIds); if (settings == null) { return(View(viewModel)); } viewModel.Enabled = settings.Enabled; viewModel.StreamerRoleEnabled = settings.StreamerRoleEnabled; viewModel.RoleId = settings.RoleId; viewModel.WhiteListedRoleIds = settings.WhiteListedRoleIds?.Select(r => r.RoleId).ToList(); if (settings.ChannelSettings == null) { settings.ChannelSettings = await channelSettingsService.Get(c => c.GuildId == guildId); } // Gets first text channel's settings to prepopulate view with. var defaultChannel = settings.ChannelSettings?.FirstOrDefault(x => x.ChannelId == textChannels[0].Id); // No channels setting saved, return view model as is. if (defaultChannel == null) { return(View(viewModel)); } viewModel.ChannelSettings.RemoveMessages = defaultChannel.RemoveMessage; // Channel settings only exist if the module is enabled. viewModel.ChannelSettings.Enabled = true; viewModel.RoleId = settings.RoleId; viewModel.Enabled = settings.Enabled; return(View(viewModel)); }
public async Task <IActionResult> NewPunishment(ulong guildId, [FromServices] IDiscordGuildService guildService) { var roles = await guildService.GetRoles(guildId); return(View(new PunishmentModel { Role = new SelectList(roles, "Id", "Name") })); }
public async Task <IActionResult> StreamerRoleSettings(ulong guildId, [FromServices] IDiscordGuildService guildService) { var roles = await guildService.GetRoles(guildId); var viewModel = new StreamerRoleSettingsViewModel { Roles = new SelectList(roles.RemoveManaged(), "Id", "Name") }; var settings = await _streamerRoleSettingsService.GetSettingsByGuild(guildId); if (settings == null) { return(View(viewModel)); } viewModel.RoleId = settings.RoleId; viewModel.Enabled = settings.Enabled; return(View(viewModel)); }
public async Task <IActionResult> StreamerSettings(ulong guildId, StreamerSettingsViewModel viewModel, [FromServices] IBot bot, [FromServices] IDiscordGuildService guildService) { var botRolePosition = bot.GetBotRoleHierarchy(guildId); var roles = await guildService.GetRoles(guildId); var selectedRolePosition = roles.FirstOrDefault(r => r.Id == viewModel.RoleId)?.Position; // Discord bots cannot assign to roles that are higher then them in the hierarchy. if (selectedRolePosition > botRolePosition) { ModelState.AddModelError("RolePosition", "The bots managed role must be positioned higher then the selected role"); // All channels in guild. var channels = await guildService.GetChannels(guildId); // Text channels in guild. var textChannels = channels.Where(x => x.Type == 0).ToList(); viewModel.Channels = new SelectList(textChannels, "Id", "Name"); viewModel.Roles = new SelectList(roles.RemoveManaged(), "Id", "Name"); return(View(viewModel)); } var settings = await _streamAnnouncerChannelSettingsService.Find(viewModel.ChannelId); // Remember if there were settings in db, as settings will be populated later if they aren't. var isSettingsInDb = settings != null; var saveSettingsTasks = new List <Task> { _streamAnnouncerSettingsService.SaveSettings(new StreamerSettings { GuildId = guildId, Enabled = viewModel.Enabled, StreamerRoleEnabled = viewModel.StreamerRoleEnabled, RoleId = viewModel.RoleId }) }; // Save general module settings to the database if (!isSettingsInDb) { settings = new StreamerChannelSettings { GuildId = guildId, ChannelId = viewModel.ChannelId } } ; settings.RemoveMessage = viewModel.ChannelSettings.RemoveMessages; // Save specific channel settings to the database. if (viewModel.ChannelSettings.Enabled) { saveSettingsTasks.Add(!isSettingsInDb ? _streamAnnouncerChannelSettingsService.Create(settings) : _streamAnnouncerChannelSettingsService.Update(settings)); } else { if (isSettingsInDb) { saveSettingsTasks.Add(_streamAnnouncerChannelSettingsService.Remove(settings)); } } await Task.WhenAll(saveSettingsTasks.ToArray()); return(RedirectToAction("Index")); }
public async Task <IActionResult> StreamerSettings(ulong guildId, StreamerSettingsViewModel viewModel, [FromServices] IBot bot, [FromServices] IDiscordGuildService guildService) { var botRolePosition = bot.GetBotRoleHierarchy(guildId); var roles = await guildService.GetRoles(guildId); var selectedRolePosition = roles.FirstOrDefault(r => r.Id == viewModel.RoleId)?.Position; // Discord bots cannot assign to roles that are higher then them in the hierarchy. if (selectedRolePosition > botRolePosition) { ModelState.AddModelError("RolePosition", "The bots managed role must be positioned higher than the selected role"); // All channels in guild. var channels = await guildService.GetChannels(guildId); // Text channels in guild. var textChannels = channels.Where(x => x.Type == 0).ToList(); viewModel.Channels = new SelectList(textChannels, "Id", "Name"); viewModel.Roles = new SelectList(roles.RemoveManaged(), "Id", "Name"); return(View(viewModel)); } var moduleSettings = await _streamAnnouncerSettingsService.GetSettingsByGuild(guildId); var saveSettingsTasks = new List <Task>(); // If the guild doesn't already have a settings DB entry, we want to add one before we do anything else. // Running that operation along side adding individual channel settings risks throwing an FK exception. if (moduleSettings == null) { var streamerSettings = new StreamerSettings { GuildId = guildId, Enabled = viewModel.Enabled, StreamerRoleEnabled = viewModel.StreamerRoleEnabled, RoleId = viewModel.RoleId, WhiteListedRoleIds = new List <WhiteListedRole>() }; if (viewModel.WhiteListedRoleIds != null) { streamerSettings.WhiteListedRoleIds = new List <WhiteListedRole>(viewModel.WhiteListedRoleIds .Select(r => new WhiteListedRole { RoleId = r })); } await _streamAnnouncerSettingsService.SaveSettings(streamerSettings); } else { // Clear all white listed roles from the database var whiteListedRoles = await _whiteListedRoleEntityService.Get(w => w.GuildId == guildId); if (whiteListedRoles != null) { await _whiteListedRoleEntityService.RemoveBulk(whiteListedRoles); } if (viewModel.WhiteListedRoleIds != null) { await _whiteListedRoleEntityService.CreateBulk(viewModel.WhiteListedRoleIds.Select(r => new WhiteListedRole { RoleId = r, GuildId = guildId })); } saveSettingsTasks.Add(_streamAnnouncerSettingsService.SaveSettings(new StreamerSettings { GuildId = guildId, Enabled = viewModel.Enabled, StreamerRoleEnabled = viewModel.StreamerRoleEnabled, RoleId = viewModel.RoleId })); } // Value defaults to 0, if the value is 0, EF will try to auto increment the ID, throwing an error. if (viewModel.ChannelId != 0) { // Settings for the specified channel of a guild. var settings = await _streamAnnouncerChannelSettingsService.Find(viewModel.ChannelId); // Remember if there were settings in db, as settings will be populated later if they aren't. var isSettingsInDb = settings != null; // Save general module settings to the database if (!isSettingsInDb) { settings = new StreamerChannelSettings { GuildId = guildId, ChannelId = viewModel.ChannelId } } ; settings.RemoveMessage = viewModel.ChannelSettings.RemoveMessages; // Save specific channel settings to the database. if (viewModel.ChannelSettings.Enabled) { saveSettingsTasks.Add(!isSettingsInDb ? _streamAnnouncerChannelSettingsService.Create(settings) : _streamAnnouncerChannelSettingsService.Update(settings)); } else { if (isSettingsInDb) { saveSettingsTasks.Add(_streamAnnouncerChannelSettingsService.Remove(settings)); } } } await Task.WhenAll(saveSettingsTasks.ToArray()); return(RedirectToAction("Index")); }