コード例 #1
0
        public async Task <IActionResult> GetItem([FromRoute] ulong guildId, [FromRoute] AutoModerationType type)
        {
            await RequirePermission(guildId, DiscordPermission.Admin);

            return(Ok(new AutoModerationConfigView(await AutoModerationConfigRepository.CreateDefault(_serviceProvider, (await GetIdentity()).GetCurrentUser())
                                                   .GetConfigsByGuildAndType(guildId, type))));
        }
コード例 #2
0
        public async Task <IActionResult> DeleteItem([FromRoute] string guildid, [FromRoute] string type)
        {
            logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | Incoming request.");
            Identity currentIdentity = await identityManager.GetIdentity(HttpContext);

            User currentUser = await currentIdentity.GetCurrentDiscordUser();

            if (currentUser == null)
            {
                logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 401 Unauthorized.");
                return(Unauthorized());
            }
            if (!await currentIdentity.HasAdminRoleOnGuild(guildid, this.database) && !config.Value.SiteAdminDiscordUserIds.Contains(currentUser.Id))
            {
                logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 401 Unauthorized.");
                return(Unauthorized());
            }
            // ========================================================

            GuildConfig guildConfig = await database.SelectSpecificGuildConfig(guildid);

            if (guildConfig == null)
            {
                logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 400 Guild not registered.");
                return(BadRequest("Guild not registered."));
            }

            int x = 0;

            try {
                x = Int32.Parse(type);
            } catch (Exception e) {
                logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 400 Invalid type.", e);
                return(BadRequest("Invalid type"));
            }

            if (!Enum.IsDefined(typeof(AutoModerationType), x))
            {
                logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 400 Invalid type.");
                return(BadRequest("Invalid type"));
            }

            AutoModerationType   eType         = (AutoModerationType)x;
            AutoModerationConfig currentConfig = await database.SelectModerationConfigForGuildAndType(guildid, eType);

            if (currentConfig != null)
            {
                database.DeleteSpecificModerationConfig(currentConfig);
                await database.SaveChangesAsync();

                return(Ok(new { id = currentConfig.Id }));
            }
            else
            {
                return(NotFound());
            }
        }
コード例 #3
0
        private async Task <bool> CheckAutoMod(AutoModerationType autoModerationType, IMessage message, Func <IMessage, AutoModerationConfig, Task <bool> > predicate)
        {
            AutoModerationConfig autoModerationConfig = _autoModerationConfigs.FirstOrDefault(x => x.AutoModerationType == autoModerationType);

            if (autoModerationConfig != null)
            {
                if (await predicate(message, autoModerationConfig))
                {
                    if (!await IsProtectedByFilter(message, autoModerationConfig))
                    {
                        _logger.LogInformation($"U: {message.Author.Id} | C: {(message.Channel as ITextChannel).Id} | G: {(message.Channel as ITextChannel).Guild.Id} triggered {autoModerationConfig.AutoModerationType}.");
                        await ExecutePunishment(message, autoModerationConfig);

                        if (autoModerationConfig.AutoModerationType != AutoModerationType.TooManyAutoModerations)
                        {
                            await CheckAutoMod(AutoModerationType.TooManyAutoModerations, message, CheckMultipleEvents);
                        }
                        return(true);
                    }
                }
            }
            return(false);
        }
コード例 #4
0
ファイル: Database.cs プロジェクト: matrix2113/discord-masz
 public async Task <AutoModerationConfig> SelectModerationConfigForGuildAndType(string guildId, AutoModerationType type)
 {
     return(await context.AutoModerationConfigs.AsQueryable().FirstOrDefaultAsync(x => x.GuildId == guildId && x.AutoModerationType == type));
 }