Exemplo n.º 1
0
        private async Task FilterIgnoreCommandAsync(CommandContext ctx, ulong id, string?filterName = null)
        {
            var name = filterName?.ToLower() ?? GuildFilters.AllFilters; // A indicates ALL filters.

            var filter = await this._model.FindAsync <GuildFilters>(ctx.Guild.Id);

            if (filter is null)
            {
                filter = new GuildFilters(ctx.Guild.Id);
                await this._model.AddAsync(filter);

                await this._model.SaveChangesAsync();
            }

            if (name != GuildFilters.AllFilters && !filter.Filters.TryGetValue(name, out var fData))
            {
                await RespondBasicErrorAsync("Filter name not found.");

                return;
            }

            var exsists = filter.BypassFilters.TryGetValue(id, out HashSet <string>?currentNames);

            bool added = true;

            if (currentNames is null)
            {
                currentNames = new HashSet <string>();
                currentNames.Add(name);
            }
            else
            {
                if (currentNames.Contains(name))
                {
                    currentNames.Remove(name);
                    added = false;
                }
                else
                {
                    currentNames.Add(name);
                }
            }

            this._model.Update(filter);
            filter.BypassFilters[id] = currentNames;
            await this._model.SaveChangesAsync();

            await RespondBasicSuccessAsync($"Bypass filter {(added ? "added" : "removed")}.");
        }
Exemplo n.º 2
0
        public async Task CreateFilterCommandAsync(CommandContext ctx,
                                                   [Description("One word name of the new filter")]
                                                   string filterName,

                                                   [Description("Words to filter out with this filter, only letters are allowed.")]
                                                   params string[] words)
        {
            var name = filterName.ToLower();

            bool force = false;

            if (words.Contains("-force"))
            {
                force = true;
            }

            var filterWords = words.Where(x => GuildFilters.regex.IsMatch(x)).ToHashSet <string>();

            var filter = await this._model.FindAsync <GuildFilters>(ctx.Guild.Id);

            if (filter is null)
            {
                filter = new GuildFilters(ctx.Guild.Id);
                await this._model.AddAsync(filter);

                await this._model.SaveChangesAsync();
            }

            if (!force && filter.Filters.TryGetValue(name, out _))
            {
                await RespondBasicErrorAsync($"Filter name already exsits! Use `{ctx.Prefix}filterupdate` to update the filter.\n" +
                                             $"Or, use `-force` to replace the values. Ex: `{ctx.Prefix}createfilter {name} -force {string.Join(" ", filterWords)}`");

                return;
            }

            this._model.Update(filter);
            filter.Filters[name] = new Tuple <int, HashSet <string> >(1, filterWords);

            var data = JsonConvert.SerializeObject(filter.Filters);

            await this._model.SaveChangesAsync();

            await RespondBasicSuccessAsync($"Created new filter by the name of {name} with the words:\n" +
                                           $"`{string.Join("`, `", filterWords)}`");
        }
Exemplo n.º 3
0
        public static async Task Run(GuildFilters filter, MessageCreateEventArgs e, CancellationToken cancellationToken)
        {
            try
            {
                cancellationToken.ThrowIfCancellationRequested();

                var direct   = filter.DirectMatches;
                var anywhere = filter.FoundAnywhereMatches;

                if (filter.BypassFilters.ContainsKey(e.Channel.Id))
                {
                    var bypasses = filter.BypassFilters[e.Channel.Id];
                    if (bypasses.Contains(GuildFilters.AllFilters))
                    {
                        return;
                    }
                    else
                    {
                        foreach (var b in bypasses)
                        {
                            cancellationToken.ThrowIfCancellationRequested();

                            direct.ExceptWith(filter.Filters[b].Item2);
                            anywhere.ExceptWith(filter.Filters[b].Item2);
                        }
                    }
                }

                if (filter.BypassFilters.ContainsKey(e.Author.Id))
                {
                    var bypasses = filter.BypassFilters[e.Author.Id];
                    if (bypasses.Contains(GuildFilters.AllFilters))
                    {
                        return;
                    }
                    else
                    {
                        foreach (var b in bypasses)
                        {
                            cancellationToken.ThrowIfCancellationRequested();

                            direct.ExceptWith(filter.Filters[b].Item2);
                            anywhere.ExceptWith(filter.Filters[b].Item2);
                        }
                    }
                }

                cancellationToken.ThrowIfCancellationRequested();

                var member = await e.Guild.GetMemberAsync(e.Author.Id);

                // Guild managers auto bypass.
                if (member.PermissionsIn(e.Channel).HasPermission(Permissions.ManageGuild))
                {
                    return;
                }

                foreach (var role in member.Roles)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    if (filter.BypassFilters.ContainsKey(role.Id))
                    {
                        var bypasses = filter.BypassFilters[role.Id];
                        if (bypasses.Contains(GuildFilters.AllFilters))
                        {
                            return;
                        }
                        else
                        {
                            foreach (var b in bypasses)
                            {
                                cancellationToken.ThrowIfCancellationRequested();

                                direct.ExceptWith(filter.Filters[b].Item2);
                                anywhere.ExceptWith(filter.Filters[b].Item2);
                            }
                        }
                    }
                }

                cancellationToken.ThrowIfCancellationRequested();

                var content = e.Message.Content.Split(" ", StringSplitOptions.RemoveEmptyEntries);

                foreach (var word in content)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    if (direct.Contains(word))
                    {
                        await e.Message.DeleteAsync();

                        return;
                    }
                    else
                    {
                        foreach (var part in anywhere)
                        {
                            cancellationToken.ThrowIfCancellationRequested();

                            if (word.Contains(part))
                            {
                                await e.Message.DeleteAsync();

                                return;
                            }
                        }
                    }
                }
            }
            finally
            {
                FilterUtils.TryRemove(e.Message.Id, out _);
            }
        }