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));
        }
Example #2
0
        public async Task <IActionResult> RemembotSettings(ulong guildId,
                                                           [FromServices] IDiscordGuildService guildService)
        {
            var settings = await _reminderSettingsService.GetSettingsByGuild(guildId);

            var channels = await guildService.GetChannels(guildId);

            var textChannels = channels.Where(x => x.Type == 0).ToList();

            if (settings is null)
            {
                settings = new RemembotSettings
                {
                    Enabled = false,
                    GuildId = guildId
                };

                await _reminderSettingsService.SaveSettings(settings);
            }

            var reminders = await _recurringReminderService.Get(x => x.GuildId == guildId);

            settings.RecurringReminders = reminders;
            var viewModel = ReminderSettingsViewModel.FromData(settings, channels);

            return(View(viewModel));
        }
        public async Task <JsonResult> GetGuildChannels([FromServices] IDiscordGuildService guildService, ulong guildId)
        {
            var channels = await guildService.GetChannels(guildId);

            // Format the ulong to string.
            return(Json(channels.FilterChannelType(0).Select(c => new { id = c.Id.ToString(), name = c.Name })));
        }
        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> 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));
        }
Example #6
0
        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));
        }
Example #7
0
        public async Task <IActionResult> NewPoll(ulong guildId, [FromServices] IDiscordGuildService guildService)
        {
            // All channels for guild.
            var channels = await guildService.GetChannels(guildId);

            // Text channels for guild.
            var textChannels = channels.Where(x => x.Type == (int)ChannelType.Text).ToList();

            var viewModel = new NewPollViewModel
            {
                Channels     = new SelectList(textChannels, "Id", "Name"),
                TotalOptions = ModuleConstants.TotalOptions,
                GuildId      = guildId.ToString()
            };

            return(View(viewModel));
        }
        public async Task <IActionResult> EditRecurringReminder(ulong guildId, [FromQuery] long rid)
        {
            var channels = await _guildService.GetChannels(guildId);

            var textChannels = channels.Where(x => x.Type == 0);

            if (rid == default(long))
            {
                var newVm = new EditRecurringReminderMessageViewModel
                {
                    Channels       = new SelectList(textChannels, "Id", "Name"),
                    Enabled        = true,
                    GuildId        = guildId,
                    CronExpression = Cron.Minutely()
                };

                return(View(newVm));
            }

            var reminder = await _reminderService.Find(rid);

            if (reminder is null)
            {
                return(RedirectToAction("RemembotSettings", "Settings"));
            }

            var selectedChannel = channels.Find(x => x.Id == reminder.ChannelId);
            var editVm          = new EditRecurringReminderMessageViewModel
            {
                GuildId        = guildId,
                Channels       = new SelectList(textChannels, "Id", "Name", selectedChannel),
                CronExpression = reminder.CronExpression,
                Enabled        = reminder.Enabled,
                Message        = reminder.Message,
                FaultMessage   = reminder.GetFaultLongMessage(),
                ChannelId      = reminder.ChannelId,
                ChannelName    = selectedChannel.Name,
                FaultType      = (int)reminder.Fault,
                Id             = reminder.Id
            };

            return(View(editVm));
        }
Example #9
0
        public async Task <IActionResult> StreamAnnouncerSettings(ulong guildId,
                                                                  [FromServices] IDiscordGuildService guildService)
        {
            var channels = await guildService.GetChannels(guildId);

            var viewModel = new StreamAnnouncerSettingsViewModel
            {
                Channels = new SelectList(channels.FilterChannelType(0), "Id", "Name")
            };

            var settings = await _streamAnnouncerSettingsService.GetSettingsByGuild(guildId);

            if (settings == null)
            {
                return(View(viewModel));
            }

            viewModel.ChannelId      = settings.AnnouncementChannelId;
            viewModel.Enabled        = settings.Enabled;
            viewModel.RemoveMessages = settings.RemoveMessages;

            return(View(viewModel));
        }
Example #10
0
        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"));
        }
Example #11
0
        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"));
        }
Example #12
0
        public async Task <IActionResult> NewPoll([FromServices] IDiscordGuildService guildService, [FromServices] IMessageService messageService,
                                                  ulong guildId, NewPollViewModel viewModel, List <string> options)
        {
            // All channels for guild.
            var channels = await guildService.GetChannels(guildId);

            // Text channels for guild.
            var textChannels = channels.Where(x => x.Type == (int)ChannelType.Text).ToList();

            // Authorization - make sure channel belongs to the guild (prevents exploitation by providing a channel that isn't theirs, if they're authorized).
            // Note - this should be extracted into a seperate web filter.
            if (textChannels.FirstOrDefault(x => x.Id == viewModel.ChannelId) == null)
            {
                ModelState.AddModelError("All", "You are not authorized to perform this action.");

                viewModel.Channels = new SelectList(textChannels, "Id", "Name");

                return(View(viewModel));
            }

            var validOptions = options.Where(x => !string.IsNullOrWhiteSpace(x)).ToList();

            // Make sure user supplied valid no of options
            if (validOptions.Count < 2 || validOptions.Count > ModuleConstants.TotalOptions)
            {
                ModelState.AddModelError("All", $"Please provide between 2 and {ModuleConstants.TotalOptions} valid options.");

                viewModel.Channels = new SelectList(textChannels, "Id", "Name");

                return(View(viewModel));
            }

            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            var poll = new StringBuilder();

            // Add poll title.
            poll.Append(":small_blue_diamond: " + viewModel.Title + Environment.NewLine);

            var discordNumbers = MessageService.DiscordNumberEmotes;

            // Add options to poll.
            for (var i = 0; i < validOptions.Count; i++)
            {
                poll.Append($"{discordNumbers[i + 1]} {validOptions[i]}{Environment.NewLine}");
            }

            // Post message and get message details.
            var message = await messageService.Post(viewModel.ChannelId, poll.ToString());

            // Create poll settings entry for guild in db, if doesn't exist.
            if (await _entityServicePollSettings.Find(guildId) == null)
            {
                await _entityServicePollSettings.Create(new PollSettings()
                {
                    GuildId = guildId
                });
            }

            // Save poll to database
            await _entityServicePoll.Create(new Poll()
            {
                ChannelId = viewModel.ChannelId,
                MessageId = message.Id,
                PollTitle = viewModel.Title,
                GuildId   = guildId
            });

            // Add reactions to message to act as voting buttons.
            for (var i = 1; i < validOptions.Count + 1; i++)
            {
                await messageService.AddReaction(message, new Emoji(discordNumbers[i]));
            }

            return(RedirectToAction("Index"));
        }