credentials.json changed, some moved to data/config.json, blacklisting

This commit is contained in:
Master Kwoth 2016-03-03 21:24:07 +01:00
parent a6ebff0a17
commit d62275d1c8
16 changed files with 168 additions and 104 deletions

1
.gitignore vendored
View File

@ -37,3 +37,4 @@ Tests/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config
NadekoBot/bin/Debug/data/nadekobot.sqlite
NadekoBot/bin/Debug/data/config.json

View File

@ -1,7 +0,0 @@
using System;
namespace NadekoBot.Classes.JSONModels {
internal class LocalizedStrings {
public string[] _8BallAnswers { get; set; }
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
namespace NadekoBot.Classes.JSONModels {
public class Configuration {
public bool DontJoinServers = false;
public bool ForwardMessages = true;
public HashSet<ulong> ServerBlacklist = new HashSet<ulong>();
public HashSet<ulong> ChannelBlacklist = new HashSet<ulong>();
public HashSet<ulong> UserBlacklist = new HashSet<ulong>();
}
}

View File

@ -3,16 +3,14 @@ namespace NadekoBot.Classes.JSONModels
{
public class Credentials
{
public string Username;
public string Password;
public string BotId;
public string GoogleAPIKey;
public ulong[] OwnerIds;
public string TrelloAppKey;
public bool? ForwardMessages;
public string SoundCloudClientID;
public string MashapeKey;
public string LOLAPIKey;
public bool DontJoinServers = false;
public string Username = "myemail@email.com";
public string Password = "xxxxxxx";
public ulong BotId = 1231231231231;
public string GoogleAPIKey = "";
public ulong[] OwnerIds = {123123123123, 5675675679845};
public string TrelloAppKey = "";
public string SoundCloudClientID = "";
public string MashapeKey = "";
public string LOLAPIKey = "";
}
}

View File

@ -102,6 +102,7 @@ namespace NadekoBot.Classes.Music {
try {
if (!SongCancelSource.IsCancellationRequested)
SongCancelSource.Cancel();
audioClient.Disconnect();
}
catch {
Console.WriteLine("STOP");

View File

@ -7,17 +7,17 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
namespace NadekoBot.Classes.Permissions {
internal class PermissionChecker : IPermissionChecker {
public static PermissionChecker Instance { get; } = new PermissionChecker();
private ConcurrentDictionary<User, DateTime> timeBlackList { get; } = new ConcurrentDictionary<User, DateTime>();
private HashSet<ulong> serverBlacklist { get; } = new HashSet<ulong>();
static PermissionChecker() { }
public PermissionChecker() {
Task.Run(async () => {
while (true) {
//blacklist is cleared every 1.75 seconds. That is the most time anyone will be blocked for ever
//blacklist is cleared every 1.75 seconds. That is the most time anyone will be blocked
await Task.Delay(1750);
timeBlackList.Clear();
}
@ -27,6 +27,12 @@ namespace NadekoBot.Classes.Permissions {
public bool CanRun(Command command, User user, Channel channel, out string error) {
error = String.Empty;
if (NadekoBot.IsUserBlacklisted(user.Id) ||
(!channel.IsPrivate &&
(NadekoBot.IsServerBlacklisted(channel.Server.Id) || NadekoBot.IsChannelBlacklisted(channel.Id)))) {
return false;
}
if (timeBlackList.ContainsKey(user))
return false;

View File

@ -1,10 +1,10 @@
using Discord.Commands;
using Discord.Modules;
using System;
using System;
using System.Linq;
using Discord;
using Discord.Commands;
using Discord.Modules;
namespace NadekoBot.Classes {
namespace NadekoBot.Classes.Permissions {
internal static class PermissionHelper {
public static bool ValidateBool(string passedArg) {
if (string.IsNullOrWhiteSpace(passedArg)) {

View File

@ -248,7 +248,7 @@ namespace NadekoBot.Modules {
e.Channel.SendFile("ripzor_m8.png",
RipName(text, string.IsNullOrWhiteSpace(e.GetArg("year")) ? null : e.GetArg("year")));
});
if (!NadekoBot.Creds.DontJoinServers) {
if (!NadekoBot.Config.DontJoinServers) {
cgb.CreateCommand("j")
.Description("Joins a server using a code.")
.Parameter("id", ParameterType.Required)

View File

@ -1,9 +1,10 @@
using System;
using Discord.Modules;
using Discord.Commands;
using NadekoBot.Classes;
using PermsHandler = NadekoBot.Classes.Permissions.PermissionsHandler;
using System.Linq;
using System.Threading.Tasks;
using NadekoBot.Classes.Permissions;
using NadekoBot.Extensions;
namespace NadekoBot.Modules {
@ -17,7 +18,7 @@ namespace NadekoBot.Modules {
public override void Install(ModuleManager manager) {
manager.CreateCommands("", cgb => {
cgb.AddCheck(Classes.Permissions.PermissionChecker.Instance);
cgb.AddCheck(PermissionChecker.Instance);
commands.ForEach(cmd => cmd.Init(cgb));
@ -35,8 +36,7 @@ namespace NadekoBot.Modules {
Discord.Role role = null;
try {
role = PermissionHelper.ValidateRole(e.Server, arg);
}
catch (Exception ex) {
} catch (Exception ex) {
Console.WriteLine(ex.Message);
await e.Channel.SendMessage($"Role `{arg}` probably doesn't exist. Create the role with that name first.");
return;
@ -76,8 +76,7 @@ namespace NadekoBot.Modules {
if (!string.IsNullOrWhiteSpace(arg))
try {
role = PermissionHelper.ValidateRole(e.Server, arg);
}
catch (Exception ex) {
} catch (Exception ex) {
await e.Channel.SendMessage("💢 Error: " + ex.Message);
return;
}
@ -99,8 +98,7 @@ namespace NadekoBot.Modules {
if (!string.IsNullOrWhiteSpace(arg))
try {
channel = PermissionHelper.ValidateChannel(e.Server, arg);
}
catch (Exception ex) {
} catch (Exception ex) {
await e.Channel.SendMessage("💢 Error: " + ex.Message);
return;
}
@ -120,8 +118,7 @@ namespace NadekoBot.Modules {
if (!string.IsNullOrWhiteSpace(e.GetArg("user")))
try {
user = PermissionHelper.ValidateUser(e.Server, e.GetArg("user"));
}
catch (Exception ex) {
} catch (Exception ex) {
await e.Channel.SendMessage("💢 Error: " + ex.Message);
return;
}
@ -143,11 +140,9 @@ namespace NadekoBot.Modules {
PermsHandler.SetServerModulePermission(e.Server, module, state);
await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** on this server.");
}
catch (ArgumentException exArg) {
} catch (ArgumentException exArg) {
await e.Channel.SendMessage(exArg.Message);
}
catch (Exception ex) {
} catch (Exception ex) {
await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message);
}
});
@ -163,11 +158,9 @@ namespace NadekoBot.Modules {
PermsHandler.SetServerCommandPermission(e.Server, command, state);
await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** on this server.");
}
catch (ArgumentException exArg) {
} catch (ArgumentException exArg) {
await e.Channel.SendMessage(exArg.Message);
}
catch (Exception ex) {
} catch (Exception ex) {
await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message);
}
});
@ -187,18 +180,15 @@ namespace NadekoBot.Modules {
PermsHandler.SetRoleModulePermission(role, module, state);
}
await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** for **ALL** roles.");
}
else {
} else {
var role = PermissionHelper.ValidateRole(e.Server, e.GetArg("role"));
PermsHandler.SetRoleModulePermission(role, module, state);
await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** for **{role.Name}** role.");
}
}
catch (ArgumentException exArg) {
} catch (ArgumentException exArg) {
await e.Channel.SendMessage(exArg.Message);
}
catch (Exception ex) {
} catch (Exception ex) {
await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message);
}
});
@ -218,18 +208,15 @@ namespace NadekoBot.Modules {
PermsHandler.SetRoleCommandPermission(role, command, state);
}
await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** for **ALL** roles.");
}
else {
} else {
var role = PermissionHelper.ValidateRole(e.Server, e.GetArg("role"));
PermsHandler.SetRoleCommandPermission(role, command, state);
await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** for **{role.Name}** role.");
}
}
catch (ArgumentException exArg) {
} catch (ArgumentException exArg) {
await e.Channel.SendMessage(exArg.Message);
}
catch (Exception ex) {
} catch (Exception ex) {
await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message);
}
});
@ -249,18 +236,15 @@ namespace NadekoBot.Modules {
PermsHandler.SetChannelModulePermission(channel, module, state);
}
await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** on **ALL** channels.");
}
else {
} else {
var channel = PermissionHelper.ValidateChannel(e.Server, e.GetArg("channel"));
PermsHandler.SetChannelModulePermission(channel, module, state);
await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** for **{channel.Name}** channel.");
}
}
catch (ArgumentException exArg) {
} catch (ArgumentException exArg) {
await e.Channel.SendMessage(exArg.Message);
}
catch (Exception ex) {
} catch (Exception ex) {
await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message);
}
});
@ -280,18 +264,15 @@ namespace NadekoBot.Modules {
PermsHandler.SetChannelCommandPermission(channel, command, state);
}
await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** on **ALL** channels.");
}
else {
} else {
var channel = PermissionHelper.ValidateChannel(e.Server, e.GetArg("channel"));
PermsHandler.SetChannelCommandPermission(channel, command, state);
await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** for **{channel.Name}** channel.");
}
}
catch (ArgumentException exArg) {
} catch (ArgumentException exArg) {
await e.Channel.SendMessage(exArg.Message);
}
catch (Exception ex) {
} catch (Exception ex) {
await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message);
}
});
@ -309,11 +290,9 @@ namespace NadekoBot.Modules {
PermsHandler.SetUserModulePermission(user, module, state);
await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** for user **{user.Name}**.");
}
catch (ArgumentException exArg) {
} catch (ArgumentException exArg) {
await e.Channel.SendMessage(exArg.Message);
}
catch (Exception ex) {
} catch (Exception ex) {
await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message);
}
});
@ -331,11 +310,9 @@ namespace NadekoBot.Modules {
PermsHandler.SetUserCommandPermission(user, command, state);
await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** for user **{user.Name}**.");
}
catch (ArgumentException exArg) {
} catch (ArgumentException exArg) {
await e.Channel.SendMessage(exArg.Message);
}
catch (Exception ex) {
} catch (Exception ex) {
await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message);
}
});
@ -351,11 +328,9 @@ namespace NadekoBot.Modules {
PermsHandler.SetServerModulePermission(e.Server, module.Name, state);
}
await e.Channel.SendMessage($"All modules have been **{(state ? "enabled" : "disabled")}** on this server.");
}
catch (ArgumentException exArg) {
} catch (ArgumentException exArg) {
await e.Channel.SendMessage(exArg.Message);
}
catch (Exception ex) {
} catch (Exception ex) {
await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message);
}
});
@ -373,11 +348,9 @@ namespace NadekoBot.Modules {
PermsHandler.SetServerCommandPermission(e.Server, command.Text, state);
}
await e.Channel.SendMessage($"All commands from the **{module}** module have been **{(state ? "enabled" : "disabled")}** on this server.");
}
catch (ArgumentException exArg) {
} catch (ArgumentException exArg) {
await e.Channel.SendMessage(exArg.Message);
}
catch (Exception ex) {
} catch (Exception ex) {
await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message);
}
});
@ -395,11 +368,9 @@ namespace NadekoBot.Modules {
}
await e.Channel.SendMessage($"All modules have been **{(state ? "enabled" : "disabled")}** for **{channel.Name}** channel.");
}
catch (ArgumentException exArg) {
} catch (ArgumentException exArg) {
await e.Channel.SendMessage(exArg.Message);
}
catch (Exception ex) {
} catch (Exception ex) {
await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message);
}
});
@ -418,11 +389,9 @@ namespace NadekoBot.Modules {
PermsHandler.SetChannelCommandPermission(channel, command.Text, state);
}
await e.Channel.SendMessage($"All commands from the **{module}** module have been **{(state ? "enabled" : "disabled")}** for **{channel.Name}** channel.");
}
catch (ArgumentException exArg) {
} catch (ArgumentException exArg) {
await e.Channel.SendMessage(exArg.Message);
}
catch (Exception ex) {
} catch (Exception ex) {
await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message);
}
});
@ -440,11 +409,9 @@ namespace NadekoBot.Modules {
}
await e.Channel.SendMessage($"All modules have been **{(state ? "enabled" : "disabled")}** for **{role.Name}** role.");
}
catch (ArgumentException exArg) {
} catch (ArgumentException exArg) {
await e.Channel.SendMessage(exArg.Message);
}
catch (Exception ex) {
} catch (Exception ex) {
await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message);
}
});
@ -463,14 +430,57 @@ namespace NadekoBot.Modules {
PermsHandler.SetRoleCommandPermission(role, command.Text, state);
}
await e.Channel.SendMessage($"All commands from the **{module}** module have been **{(state ? "enabled" : "disabled")}** for **{role.Name}** role.");
}
catch (ArgumentException exArg) {
} catch (ArgumentException exArg) {
await e.Channel.SendMessage(exArg.Message);
}
catch (Exception ex) {
} catch (Exception ex) {
await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message);
}
});
cgb.CreateCommand(prefix + "ubl")
.Description("Blacklists a mentioned user.\n**Usage**: ;ubl [user_mention]")
.Parameter("user", ParameterType.Unparsed)
.Do(async e => {
await Task.Run(async () => {
if (!e.Message.MentionedUsers.Any()) return;
var usr = e.Message.MentionedUsers.First();
NadekoBot.Config.UserBlacklist.Add(usr.Id);
NadekoBot.SaveConfig();
await e.Channel.SendMessage($"`Sucessfully blacklisted user {usr.Name}`");
});
});
cgb.CreateCommand(prefix + "ucl")
.Description("Blacklists a mentioned channel (#general for example).\n**Usage**: ;ubl [channel_mention]")
.Parameter("user", ParameterType.Unparsed)
.Do(async e => {
await Task.Run(async () => {
if (!e.Message.MentionedChannels.Any()) return;
var ch = e.Message.MentionedChannels.First();
NadekoBot.Config.UserBlacklist.Add(ch.Id);
NadekoBot.SaveConfig();
await e.Channel.SendMessage($"`Sucessfully blacklisted channel {ch.Name}`");
});
});
cgb.CreateCommand(prefix + "usl")
.Description("Blacklists a server by a name or id (#general for example).\n**Usage**: ;usl [servername/serverid]")
.Parameter("user", ParameterType.Unparsed)
.Do(async e => {
await Task.Run(async () => {
var arg = e.GetArg("user");
if (string.IsNullOrWhiteSpace(arg))
return;
var server = NadekoBot.Client.FindServers(arg.Trim()).FirstOrDefault();
if (server == null) {
await e.Channel.SendMessage("Cannot find that server");
return;
}
NadekoBot.Config.ServerBlacklist.Add(server.Id);
NadekoBot.SaveConfig();
await e.Channel.SendMessage($"`Sucessfully blacklisted server {server.Name}`");
});
});
});
}
}

View File

@ -18,12 +18,30 @@ namespace NadekoBot {
public static DiscordClient Client;
public static bool ForwardMessages = false;
public static Credentials Creds { get; set; }
public static Configuration Config { get; set; }
public static string BotMention { get; set; } = "";
private static Channel OwnerPrivateChannel { get; set; }
private static void Main() {
Console.OutputEncoding = Encoding.Unicode;
// generate credentials example so people can know about the changes i make
try {
File.WriteAllText("credentials_example.json", JsonConvert.SerializeObject(new Credentials(), Formatting.Indented));
File.WriteAllText("data/config_example.json", JsonConvert.SerializeObject(new Configuration(), Formatting.Indented));
}
catch {
Console.WriteLine("Failed writing credentials_example.json or data/config_example.json");
}
try {
Config = JsonConvert.DeserializeObject<Configuration>(File.ReadAllText("data/config.json"));
}
catch {
Console.WriteLine("Failed loading configuration.");
}
try {
//load credentials from credentials.json
Creds = JsonConvert.DeserializeObject<Credentials>(File.ReadAllText("credentials.json"));
@ -39,7 +57,7 @@ namespace NadekoBot {
Console.WriteLine(string.IsNullOrWhiteSpace(Creds.TrelloAppKey)
? "No trello appkey found. You will not be able to use trello commands."
: "Trello app key provided.");
Console.WriteLine(Creds.ForwardMessages != true
Console.WriteLine(Config.ForwardMessages != true
? "Not forwarding messages."
: "Forwarding private messages to owner.");
Console.WriteLine(string.IsNullOrWhiteSpace(Creds.SoundCloudClientID)
@ -149,7 +167,6 @@ namespace NadekoBot {
}
private static bool repliedRecently = false;
private static async void Client_MessageReceived(object sender, MessageEventArgs e) {
try {
if (e.Server != null || e.User.Id == Client.CurrentUser.Id) return;
@ -157,12 +174,14 @@ namespace NadekoBot {
// just ban this trash AutoModerator
// and cancer christmass spirit
// and crappy shotaslave
if (IsBlackListed(e))
return;
if (e.User.Id == 105309315895693312 ||
e.User.Id == 119174277298782216 ||
e.User.Id == 143515953525817344)
return; // FU
if (!NadekoBot.Creds.DontJoinServers) {
if (!NadekoBot.Config.DontJoinServers) {
try {
await (await Client.GetInvite(e.Message.Text)).Accept();
await e.Channel.SendMessage("I got in!");
@ -188,6 +207,23 @@ namespace NadekoBot {
});
} catch { }
}
private static readonly object configLock = new object();
public static void SaveConfig() {
lock (configLock) {
File.WriteAllText("data/config.json", JsonConvert.SerializeObject(NadekoBot.Config, Formatting.Indented));
}
}
public static bool IsBlackListed(MessageEventArgs evArgs) => IsUserBlacklisted(evArgs.User.Id) ||
(!evArgs.Channel.IsPrivate &&
(IsChannelBlacklisted(evArgs.Channel.Id) || IsServerBlacklisted(evArgs.Server.Id)));
public static bool IsServerBlacklisted(ulong id) => NadekoBot.Config.ServerBlacklist.Contains(id);
public static bool IsChannelBlacklisted(ulong id) => NadekoBot.Config.ChannelBlacklist.Contains(id);
public static bool IsUserBlacklisted(ulong id) => NadekoBot.Config.UserBlacklist.Contains(id);
}
}

View File

@ -118,7 +118,7 @@
<Compile Include="Classes\DBHandler.cs" />
<Compile Include="Classes\FlowersHandler.cs" />
<Compile Include="Classes\JSONModels\AnimeResult.cs" />
<Compile Include="Classes\JSONModels\Config.cs" />
<Compile Include="Classes\JSONModels\Configuration.cs" />
<Compile Include="Classes\JSONModels\MangaResult.cs" />
<Compile Include="Classes\JSONModels\_JSONModels.cs" />
<Compile Include="Classes\Music\MusicControls.cs" />

Binary file not shown.

View File

@ -0,0 +1,7 @@
{
"DontJoinServers": false,
"ForwardMessages": true,
"ServerBlacklist": [],
"ChannelBlacklist": [],
"UserBlacklist": []
}