added ;cfi and ;sfi, prepared for ;cw and ;sw

This commit is contained in:
Master Kwoth 2016-03-09 13:48:46 +01:00
parent 7bd1892e76
commit 08ae95d55d
5 changed files with 145 additions and 3 deletions

View File

@ -300,6 +300,46 @@ namespace NadekoBot.Classes.Permissions {
commands.Add(commandName, value);
Task.Run(() => WriteServerToJson(serverPerms));
}
public static void SetServerWordPermission(Server server, string word, bool value) {
var serverPerms = PermissionsDict.GetOrAdd(server.Id,
new ServerPermissions(server.Id, server.Name));
serverPerms.Permissions.Words.Add(word);
Task.Run(() => WriteServerToJson(serverPerms));
}
public static void SetChannelWordPermission(Channel channel, string word, bool value) {
var server = channel.Server;
var serverPerms = PermissionsDict.GetOrAdd(server.Id,
new ServerPermissions(server.Id, server.Name));
if (!serverPerms.ChannelPermissions.ContainsKey(channel.Id))
serverPerms.ChannelPermissions.Add(channel.Id, new Permissions(channel.Name));
serverPerms.ChannelPermissions[channel.Id].Words.Add(word);
Task.Run(() => WriteServerToJson(serverPerms));
}
public static void SetServerFilterInvitesPermission(Server server, bool value) {
var serverPerms = PermissionsDict.GetOrAdd(server.Id,
new ServerPermissions(server.Id, server.Name));
serverPerms.Permissions.FilterInvites = value;
Task.Run(() => WriteServerToJson(serverPerms));
}
public static void SetChannelFilterInvitesPermission(Channel channel, bool value) {
var server = channel.Server;
var serverPerms = PermissionsDict.GetOrAdd(server.Id,
new ServerPermissions(server.Id, server.Name));
if (!serverPerms.ChannelPermissions.ContainsKey(channel.Id))
serverPerms.ChannelPermissions.Add(channel.Id, new Permissions(channel.Name));
serverPerms.ChannelPermissions[channel.Id].FilterInvites = value;
Task.Run(() => WriteServerToJson(serverPerms));
}
}
/// <summary>
/// Holds a permission list
@ -317,11 +357,20 @@ namespace NadekoBot.Classes.Permissions {
/// Command name with allowed/disallowed
/// </summary>
public Dictionary<string, bool> Commands { get; set; }
/// <summary>
/// Banned words, usually profanities, like word "java"
/// </summary>
public HashSet<string> Words { get; set; }
/// <summary>
/// Should the bot filter invites to other discord servers (and ref links in the future)
/// </summary>
public bool FilterInvites { get; set; }
public Permissions(string name) {
Name = name;
Modules = new Dictionary<string, bool>();
Commands = new Dictionary<string, bool>();
Words = new HashSet<string>();
}
public override string ToString() {

View File

@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using NadekoBot.Classes.Permissions;
using ServerPermissions = NadekoBot.Classes.Permissions.ServerPermissions;
namespace NadekoBot.Commands {
internal class FilterInvitesCommand : IDiscordCommand {
private readonly Regex filterRegex = new Regex(@"(?:discord(?:\.gg|app\.com\/invite)\/(?<id>([\w]{16}|(?:[\w]+-?){3})))");
public FilterInvitesCommand() {
NadekoBot.Client.MessageReceived += async (sender, args) => {
try {
ServerPermissions serverPerms;
if (!IsChannelOrServerFiltering(args.Channel, out serverPerms)) return;
if (filterRegex.IsMatch(args.Message.RawText)) {
await args.Message.Delete();
if (serverPerms.Verbose)
await args.Channel.SendMessage($"{args.User.Mention} Invite links are not allowed on this channel.");
}
} catch { }
};
}
private static bool IsChannelOrServerFiltering(Channel channel, out ServerPermissions serverPerms) {
if (!PermissionsHandler.PermissionsDict.TryGetValue(channel.Server.Id, out serverPerms)) return false;
if (serverPerms.Permissions.FilterInvites)
return true;
Permissions perms;
return serverPerms.ChannelPermissions.TryGetValue(channel.Id, out perms) && perms.FilterInvites;
}
public void Init(CommandGroupBuilder cgb) {
cgb.CreateCommand(";cfi")
.Alias(";channelfilterinvites")
.Description("Enables or disables automatic deleting of invites on the channel." +
"If no channel supplied, it will default to current one. Use ALL to apply to all existing channels at once." +
"\n**Usage**: ;cfi enable #general-chat")
.Parameter("bool")
.Parameter("channel", ParameterType.Optional)
.Do(async e => {
try {
var state = PermissionHelper.ValidateBool(e.GetArg("bool"));
var chanStr = e.GetArg("channel");
if (chanStr?.ToLowerInvariant().Trim() != "all") {
var chan = string.IsNullOrWhiteSpace(chanStr)
? e.Channel
: PermissionHelper.ValidateChannel(e.Server, chanStr);
PermissionsHandler.SetChannelFilterInvitesPermission(chan, state);
await e.Channel.SendMessage($"Invite Filter has been **{(state ? "enabled" : "disabled")}** for **{chan.Name}** channel.");
return;
}
//all channels
foreach (var curChannel in e.Server.TextChannels) {
PermissionsHandler.SetChannelFilterInvitesPermission(curChannel, state);
}
await e.Channel.SendMessage($"Invite Filter has been **{(state ? "enabled" : "disabled")}** for **ALL** channels.");
} catch (Exception ex) {
await e.Channel.SendMessage($"💢 Error: {ex.Message}");
}
});
cgb.CreateCommand(";sfi")
.Alias(";serverfilterinvites")
.Description("Enables or disables automatic deleting of invites on the server.\n**Usage**: ;sfi disable")
.Parameter("bool")
.Do(async e => {
try {
var state = PermissionHelper.ValidateBool(e.GetArg("bool"));
PermissionsHandler.SetServerFilterInvitesPermission(e.Server, state);
await e.Channel.SendMessage($"Invite Filter has been **{(state ? "enabled" : "disabled")}** for this server.");
} catch (Exception ex) {
await e.Channel.SendMessage($"💢 Error: {ex.Message}");
}
});
}
}
}

View File

@ -5,6 +5,7 @@ using PermsHandler = NadekoBot.Classes.Permissions.PermissionsHandler;
using System.Linq;
using System.Threading.Tasks;
using NadekoBot.Classes.Permissions;
using NadekoBot.Commands;
using NadekoBot.Extensions;
namespace NadekoBot.Modules {
@ -12,9 +13,8 @@ namespace NadekoBot.Modules {
public override string Prefix { get; } = ";";
public PermissionModule() {
//Empty for now
commands.Add(new FilterInvitesCommand());
}
//todo word filtering/invite bans (?:discord(?:\.gg|app\.com\/invite)\/(?<id>([\w]{16}|(?:[\w]+-?){3})))
public override void Install(ModuleManager manager) {
manager.CreateCommands("", cgb => {

View File

@ -121,7 +121,7 @@ namespace NadekoBot {
modules.Add(new Conversations(), "Conversations", ModuleFilter.None);
modules.Add(new Gambling(), "Gambling", ModuleFilter.None);
modules.Add(new Games(), "Games", ModuleFilter.None);
//modules.Add(new Music(), "Music", ModuleFilter.None);
modules.Add(new Music(), "Music", ModuleFilter.None);
modules.Add(new Searches(), "Searches", ModuleFilter.None);
modules.Add(new NSFW(), "NSFW", ModuleFilter.None);
if (!string.IsNullOrWhiteSpace(Creds.TrelloAppKey))

View File

@ -142,6 +142,7 @@
<Compile Include="Classes\_DataModels\TypingArticleModel.cs" />
<Compile Include="Classes\_DataModels\UserQuoteModel.cs" />
<Compile Include="Commands\ClashOfClans.cs" />
<Compile Include="Commands\FilterInvitesCommand.cs" />
<Compile Include="Commands\LogCommand.cs" />
<Compile Include="Commands\LoLCommands.cs" />
<Compile Include="Commands\MessageRepeater.cs" />