Filtering words works
This commit is contained in:
		@@ -28,17 +28,17 @@ namespace NadekoBot.Modules.Permissions
 | 
			
		||||
 | 
			
		||||
            public static HashSet<string> FilteredWordsForChannel(ulong channelId, ulong guildId)
 | 
			
		||||
            {
 | 
			
		||||
                var words = FilteredWordsForServer(guildId);
 | 
			
		||||
 | 
			
		||||
                if (!words.Any() || WordFilteringChannels.Contains(channelId))
 | 
			
		||||
                    return words;
 | 
			
		||||
                return new HashSet<string>();
 | 
			
		||||
                HashSet<string> words = new HashSet<string>();
 | 
			
		||||
                if(WordFilteringChannels.Contains(channelId))
 | 
			
		||||
                    ServerFilteredWords.TryGetValue(guildId, out words);
 | 
			
		||||
                return words;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            public static HashSet<string> FilteredWordsForServer(ulong guildId)
 | 
			
		||||
            {
 | 
			
		||||
                var words = new HashSet<string>();
 | 
			
		||||
                ServerFilteredWords.TryGetValue(guildId, out words);
 | 
			
		||||
                if(WordFilteringServers.Contains(guildId))
 | 
			
		||||
                    ServerFilteredWords.TryGetValue(guildId, out words);
 | 
			
		||||
                return words;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
@@ -80,12 +80,12 @@ namespace NadekoBot.Modules.Permissions
 | 
			
		||||
                if (enabled)
 | 
			
		||||
                {
 | 
			
		||||
                    InviteFilteringServers.Add(channel.Guild.Id);
 | 
			
		||||
                    await channel.SendMessageAsync("`Invite filtering enabled on the whole server.`").ConfigureAwait(false);
 | 
			
		||||
                    await channel.SendMessageAsync("`Invite filtering enabled on this server.`").ConfigureAwait(false);
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    InviteFilteringServers.Remove(channel.Guild.Id);
 | 
			
		||||
                    await channel.SendMessageAsync("`Invite filtering disabled on the whole server.`").ConfigureAwait(false);
 | 
			
		||||
                    await channel.SendMessageAsync("`Invite filtering disabled on this server.`").ConfigureAwait(false);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
@@ -139,12 +139,12 @@ namespace NadekoBot.Modules.Permissions
 | 
			
		||||
                if (enabled)
 | 
			
		||||
                {
 | 
			
		||||
                    WordFilteringServers.Add(channel.Guild.Id);
 | 
			
		||||
                    await channel.SendMessageAsync("`Word filtering enabled on the whole server.`").ConfigureAwait(false);
 | 
			
		||||
                    await channel.SendMessageAsync("`Word filtering enabled on this server.`").ConfigureAwait(false);
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    WordFilteringServers.Remove(channel.Guild.Id);
 | 
			
		||||
                    await channel.SendMessageAsync("`Word filtering disabled on the whole server.`").ConfigureAwait(false);
 | 
			
		||||
                    await channel.SendMessageAsync("`Word filtering disabled on this server.`").ConfigureAwait(false);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
@@ -183,7 +183,7 @@ namespace NadekoBot.Modules.Permissions
 | 
			
		||||
 | 
			
		||||
            [LocalizedCommand, LocalizedRemarks, LocalizedSummary, LocalizedAlias]
 | 
			
		||||
            [RequireContext(ContextType.Guild)]
 | 
			
		||||
            public async Task AddFilterWord(IUserMessage imsg, [Remainder] string word)
 | 
			
		||||
            public async Task FilterWord(IUserMessage imsg, [Remainder] string word)
 | 
			
		||||
            {
 | 
			
		||||
                var channel = (ITextChannel)imsg.Channel;
 | 
			
		||||
 | 
			
		||||
@@ -192,26 +192,47 @@ namespace NadekoBot.Modules.Permissions
 | 
			
		||||
                if (string.IsNullOrWhiteSpace(word))
 | 
			
		||||
                    return;
 | 
			
		||||
 | 
			
		||||
                bool contains;
 | 
			
		||||
                int removed;
 | 
			
		||||
                using (var uow = DbHandler.UnitOfWork())
 | 
			
		||||
                {
 | 
			
		||||
                    var config = uow.GuildConfigs.For(channel.Guild.Id);
 | 
			
		||||
 | 
			
		||||
                    contains = config.FilteredWords.Any(fw => fw.Word == word);
 | 
			
		||||
                    removed = config.FilteredWords.RemoveWhere(fw => fw.Word == word);
 | 
			
		||||
 | 
			
		||||
                    if (!contains)
 | 
			
		||||
                        config.FilteredWords.Add(new Services.Database.Models.FilteredWord() { Word = word});
 | 
			
		||||
                    if (removed == 0)
 | 
			
		||||
                        config.FilteredWords.Add(new Services.Database.Models.FilteredWord() { Word = word });
 | 
			
		||||
 | 
			
		||||
                    await uow.CompleteAsync().ConfigureAwait(false);
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (!contains)
 | 
			
		||||
                {
 | 
			
		||||
                    var filteredWords = ServerFilteredWords.GetOrAdd(channel.Guild.Id, new HashSet<string>());
 | 
			
		||||
                var filteredWords = ServerFilteredWords.GetOrAdd(channel.Guild.Id, new HashSet<string>());
 | 
			
		||||
 | 
			
		||||
                if (removed == 0)
 | 
			
		||||
                {
 | 
			
		||||
                    filteredWords.Add(word);
 | 
			
		||||
                    await channel.SendMessageAsync($"Word `{word}` successfully added to the list of filtered words.");
 | 
			
		||||
                    await channel.SendMessageAsync($"Word `{word}` successfully added to the list of filtered words.")
 | 
			
		||||
                            .ConfigureAwait(false);
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    filteredWords.Remove(word);
 | 
			
		||||
                    await channel.SendMessageAsync($"Word `{word}` removed from the list of filtered words.")
 | 
			
		||||
                            .ConfigureAwait(false);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            [LocalizedCommand, LocalizedRemarks, LocalizedSummary, LocalizedAlias]
 | 
			
		||||
            [RequireContext(ContextType.Guild)]
 | 
			
		||||
            public async Task LstFilterWords(IUserMessage imsg)
 | 
			
		||||
            {
 | 
			
		||||
                var channel = (ITextChannel)imsg.Channel;
 | 
			
		||||
 | 
			
		||||
                HashSet<string> filteredWords;
 | 
			
		||||
                ServerFilteredWords.TryGetValue(channel.Guild.Id, out filteredWords);
 | 
			
		||||
 | 
			
		||||
                await channel.SendMessageAsync($"`List of banned words:`\n" + string.Join(",\n", filteredWords))
 | 
			
		||||
                        .ConfigureAwait(false);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										54
									
								
								src/NadekoBot/Resources/CommandStrings.Designer.cs
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										54
									
								
								src/NadekoBot/Resources/CommandStrings.Designer.cs
									
									
									
										generated
									
									
									
								
							@@ -113,33 +113,6 @@ namespace NadekoBot.Resources {
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        ///    Looks up a localized string similar to Adds a new word to the list of filtered words.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public static string addfilterword_desc {
 | 
			
		||||
            get {
 | 
			
		||||
                return ResourceManager.GetString("addfilterword_desc", resourceCulture);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        ///    Looks up a localized string similar to `;afw poop`.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public static string addfilterword_summary {
 | 
			
		||||
            get {
 | 
			
		||||
                return ResourceManager.GetString("addfilterword_summary", resourceCulture);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        ///    Looks up a localized string similar to addfilterword afw.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public static string addfilterword_text {
 | 
			
		||||
            get {
 | 
			
		||||
                return ResourceManager.GetString("addfilterword_text", resourceCulture);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        ///    Looks up a localized string similar to Adds a specified string to the list of playing strings to rotate. Supported placeholders: %servers%, %users%, %playing%, %queued%, %trivia% **Bot Owner Only!**.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
@@ -2543,6 +2516,33 @@ namespace NadekoBot.Resources {
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        ///    Looks up a localized string similar to Adds or removes (if it exists) a word from the list of filtered words.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public static string filterword_desc {
 | 
			
		||||
            get {
 | 
			
		||||
                return ResourceManager.GetString("filterword_desc", resourceCulture);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        ///    Looks up a localized string similar to `;fw poop`.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public static string filterword_summary {
 | 
			
		||||
            get {
 | 
			
		||||
                return ResourceManager.GetString("filterword_summary", resourceCulture);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        ///    Looks up a localized string similar to fw.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public static string filterword_text {
 | 
			
		||||
            get {
 | 
			
		||||
                return ResourceManager.GetString("filterword_text", resourceCulture);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        ///    Looks up a localized string similar to Shows a unicode fire message. Optional parameter [x] tells her how many times to repeat the fire..
 | 
			
		||||
        /// </summary>
 | 
			
		||||
 
 | 
			
		||||
@@ -981,14 +981,14 @@
 | 
			
		||||
  <data name="chnlfilterwords_summary" xml:space="preserve">
 | 
			
		||||
    <value>`;cfw enable #general-chat`</value>
 | 
			
		||||
  </data>
 | 
			
		||||
  <data name="addfilterword_text" xml:space="preserve">
 | 
			
		||||
    <value>addfilterword afw</value>
 | 
			
		||||
  <data name="filterword_text" xml:space="preserve">
 | 
			
		||||
    <value>fw</value>
 | 
			
		||||
  </data>
 | 
			
		||||
  <data name="addfilterword_desc" xml:space="preserve">
 | 
			
		||||
    <value>Adds a new word to the list of filtered words</value>
 | 
			
		||||
  <data name="filterword_desc" xml:space="preserve">
 | 
			
		||||
    <value>Adds or removes (if it exists) a word from the list of filtered words</value>
 | 
			
		||||
  </data>
 | 
			
		||||
  <data name="addfilterword_summary" xml:space="preserve">
 | 
			
		||||
    <value>`;afw poop`</value>
 | 
			
		||||
  <data name="filterword_summary" xml:space="preserve">
 | 
			
		||||
    <value>`;fw poop`</value>
 | 
			
		||||
  </data>
 | 
			
		||||
  <data name="rmvfilterword_text" xml:space="preserve">
 | 
			
		||||
    <value>rmvfilterword rw</value>
 | 
			
		||||
 
 | 
			
		||||
@@ -55,7 +55,7 @@ namespace NadekoBot.Services
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (guild != null)
 | 
			
		||||
            if (guild != null && guild.OwnerId != usrMsg.Author.Id)
 | 
			
		||||
            {
 | 
			
		||||
                if (Permissions.FilterCommands.InviteFilteringChannels.Contains(usrMsg.Channel.Id) ||
 | 
			
		||||
                    Permissions.FilterCommands.InviteFilteringServers.Contains(guild.Id))
 | 
			
		||||
@@ -73,9 +73,12 @@ namespace NadekoBot.Services
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            if (guild != null && guild.OwnerId != usrMsg.Author.Id)
 | 
			
		||||
            {
 | 
			
		||||
                var filteredWords = Permissions.FilterCommands.FilteredWordsForChannel(usrMsg.Channel.Id, guild.Id).Concat(Permissions.FilterCommands.FilteredWordsForServer(guild.Id));
 | 
			
		||||
                var wordsInMessage = usrMsg.Content.ToLowerInvariant().Split(' ');
 | 
			
		||||
                if (filteredWords.Any())
 | 
			
		||||
                if (filteredWords.Any(w=>wordsInMessage.Contains(w)))
 | 
			
		||||
                {
 | 
			
		||||
                    try
 | 
			
		||||
                    {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user