From 9c0ff08c677dccdfa58250834a41c42fcdfa075f Mon Sep 17 00:00:00 2001 From: Kwoth Date: Mon, 3 Oct 2016 23:07:22 +0200 Subject: [PATCH] Filtering words works --- .../Permissions/Commands/FilterCommands.cs | 59 +++++++++++++------ .../Resources/CommandStrings.Designer.cs | 54 ++++++++--------- src/NadekoBot/Resources/CommandStrings.resx | 12 ++-- src/NadekoBot/Services/CommandHandler.cs | 7 ++- 4 files changed, 78 insertions(+), 54 deletions(-) diff --git a/src/NadekoBot/Modules/Permissions/Commands/FilterCommands.cs b/src/NadekoBot/Modules/Permissions/Commands/FilterCommands.cs index 127cb8d4..f8cf9ae6 100644 --- a/src/NadekoBot/Modules/Permissions/Commands/FilterCommands.cs +++ b/src/NadekoBot/Modules/Permissions/Commands/FilterCommands.cs @@ -28,17 +28,17 @@ namespace NadekoBot.Modules.Permissions public static HashSet FilteredWordsForChannel(ulong channelId, ulong guildId) { - var words = FilteredWordsForServer(guildId); - - if (!words.Any() || WordFilteringChannels.Contains(channelId)) - return words; - return new HashSet(); + HashSet words = new HashSet(); + if(WordFilteringChannels.Contains(channelId)) + ServerFilteredWords.TryGetValue(guildId, out words); + return words; } public static HashSet FilteredWordsForServer(ulong guildId) { var words = new HashSet(); - 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()); + var filteredWords = ServerFilteredWords.GetOrAdd(channel.Guild.Id, new HashSet()); + 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 filteredWords; + ServerFilteredWords.TryGetValue(channel.Guild.Id, out filteredWords); + + await channel.SendMessageAsync($"`List of banned words:`\n" + string.Join(",\n", filteredWords)) + .ConfigureAwait(false); + } } } } diff --git a/src/NadekoBot/Resources/CommandStrings.Designer.cs b/src/NadekoBot/Resources/CommandStrings.Designer.cs index c841bb54..a5bad982 100644 --- a/src/NadekoBot/Resources/CommandStrings.Designer.cs +++ b/src/NadekoBot/Resources/CommandStrings.Designer.cs @@ -113,33 +113,6 @@ namespace NadekoBot.Resources { } } - /// - /// Looks up a localized string similar to Adds a new word to the list of filtered words. - /// - public static string addfilterword_desc { - get { - return ResourceManager.GetString("addfilterword_desc", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to `;afw poop`. - /// - public static string addfilterword_summary { - get { - return ResourceManager.GetString("addfilterword_summary", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to addfilterword afw. - /// - public static string addfilterword_text { - get { - return ResourceManager.GetString("addfilterword_text", resourceCulture); - } - } - /// /// 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!**. /// @@ -2543,6 +2516,33 @@ namespace NadekoBot.Resources { } } + /// + /// Looks up a localized string similar to Adds or removes (if it exists) a word from the list of filtered words. + /// + public static string filterword_desc { + get { + return ResourceManager.GetString("filterword_desc", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to `;fw poop`. + /// + public static string filterword_summary { + get { + return ResourceManager.GetString("filterword_summary", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to fw. + /// + public static string filterword_text { + get { + return ResourceManager.GetString("filterword_text", resourceCulture); + } + } + /// /// Looks up a localized string similar to Shows a unicode fire message. Optional parameter [x] tells her how many times to repeat the fire.. /// diff --git a/src/NadekoBot/Resources/CommandStrings.resx b/src/NadekoBot/Resources/CommandStrings.resx index 58f84736..8a376095 100644 --- a/src/NadekoBot/Resources/CommandStrings.resx +++ b/src/NadekoBot/Resources/CommandStrings.resx @@ -981,14 +981,14 @@ `;cfw enable #general-chat` - - addfilterword afw + + fw - - Adds a new word to the list of filtered words + + Adds or removes (if it exists) a word from the list of filtered words - - `;afw poop` + + `;fw poop` rmvfilterword rw diff --git a/src/NadekoBot/Services/CommandHandler.cs b/src/NadekoBot/Services/CommandHandler.cs index 579790c2..979da273 100644 --- a/src/NadekoBot/Services/CommandHandler.cs +++ b/src/NadekoBot/Services/CommandHandler.cs @@ -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 {