Filtering words works

This commit is contained in:
Kwoth 2016-10-03 23:07:22 +02:00
parent f12aa16948
commit 9c0ff08c67
4 changed files with 78 additions and 54 deletions

View File

@ -28,16 +28,16 @@ namespace NadekoBot.Modules.Permissions
public static HashSet<string> FilteredWordsForChannel(ulong channelId, ulong guildId) public static HashSet<string> FilteredWordsForChannel(ulong channelId, ulong guildId)
{ {
var words = FilteredWordsForServer(guildId); HashSet<string> words = new HashSet<string>();
if(WordFilteringChannels.Contains(channelId))
if (!words.Any() || WordFilteringChannels.Contains(channelId)) ServerFilteredWords.TryGetValue(guildId, out words);
return words; return words;
return new HashSet<string>();
} }
public static HashSet<string> FilteredWordsForServer(ulong guildId) public static HashSet<string> FilteredWordsForServer(ulong guildId)
{ {
var words = new HashSet<string>(); var words = new HashSet<string>();
if(WordFilteringServers.Contains(guildId))
ServerFilteredWords.TryGetValue(guildId, out words); ServerFilteredWords.TryGetValue(guildId, out words);
return words; return words;
} }
@ -80,12 +80,12 @@ namespace NadekoBot.Modules.Permissions
if (enabled) if (enabled)
{ {
InviteFilteringServers.Add(channel.Guild.Id); 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 else
{ {
InviteFilteringServers.Remove(channel.Guild.Id); 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) if (enabled)
{ {
WordFilteringServers.Add(channel.Guild.Id); 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 else
{ {
WordFilteringServers.Remove(channel.Guild.Id); 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] [LocalizedCommand, LocalizedRemarks, LocalizedSummary, LocalizedAlias]
[RequireContext(ContextType.Guild)] [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; var channel = (ITextChannel)imsg.Channel;
@ -192,26 +192,47 @@ namespace NadekoBot.Modules.Permissions
if (string.IsNullOrWhiteSpace(word)) if (string.IsNullOrWhiteSpace(word))
return; return;
bool contains; int removed;
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
var config = uow.GuildConfigs.For(channel.Guild.Id); 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) if (removed == 0)
config.FilteredWords.Add(new Services.Database.Models.FilteredWord() { Word = word}); 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); 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);
}
} }
} }
} }

View File

@ -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> /// <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!**. /// 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> /// </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> /// <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.. /// 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> /// </summary>

View File

@ -981,14 +981,14 @@
<data name="chnlfilterwords_summary" xml:space="preserve"> <data name="chnlfilterwords_summary" xml:space="preserve">
<value>`;cfw enable #general-chat`</value> <value>`;cfw enable #general-chat`</value>
</data> </data>
<data name="addfilterword_text" xml:space="preserve"> <data name="filterword_text" xml:space="preserve">
<value>addfilterword afw</value> <value>fw</value>
</data> </data>
<data name="addfilterword_desc" xml:space="preserve"> <data name="filterword_desc" xml:space="preserve">
<value>Adds a new word to the list of filtered words</value> <value>Adds or removes (if it exists) a word from the list of filtered words</value>
</data> </data>
<data name="addfilterword_summary" xml:space="preserve"> <data name="filterword_summary" xml:space="preserve">
<value>`;afw poop`</value> <value>`;fw poop`</value>
</data> </data>
<data name="rmvfilterword_text" xml:space="preserve"> <data name="rmvfilterword_text" xml:space="preserve">
<value>rmvfilterword rw</value> <value>rmvfilterword rw</value>

View File

@ -55,7 +55,7 @@ namespace NadekoBot.Services
return; return;
} }
if (guild != null) if (guild != null && guild.OwnerId != usrMsg.Author.Id)
{ {
if (Permissions.FilterCommands.InviteFilteringChannels.Contains(usrMsg.Channel.Id) || if (Permissions.FilterCommands.InviteFilteringChannels.Contains(usrMsg.Channel.Id) ||
Permissions.FilterCommands.InviteFilteringServers.Contains(guild.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 filteredWords = Permissions.FilterCommands.FilteredWordsForChannel(usrMsg.Channel.Id, guild.Id).Concat(Permissions.FilterCommands.FilteredWordsForServer(guild.Id));
var wordsInMessage = usrMsg.Content.ToLowerInvariant().Split(' '); var wordsInMessage = usrMsg.Content.ToLowerInvariant().Split(' ');
if (filteredWords.Any()) if (filteredWords.Any(w=>wordsInMessage.Contains(w)))
{ {
try try
{ {