From ff67e35d37d2fe70331b6340f81650db6a0a4526 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Thu, 10 Mar 2016 15:05:38 +0100 Subject: [PATCH] Moved config statics to ConfigHandler class --- NadekoBot/Classes/JSONModels/Configuration.cs | 22 ++++++++++++++++ .../Classes/Permissions/PermissionChecker.cs | 5 ++-- NadekoBot/Commands/PlayingRotate.cs | 7 ++--- NadekoBot/Modules/Permissions.cs | 7 ++--- NadekoBot/NadekoBot.cs | 26 +++---------------- 5 files changed, 36 insertions(+), 31 deletions(-) diff --git a/NadekoBot/Classes/JSONModels/Configuration.cs b/NadekoBot/Classes/JSONModels/Configuration.cs index 3cd319e1..3b7a612a 100644 --- a/NadekoBot/Classes/JSONModels/Configuration.cs +++ b/NadekoBot/Classes/JSONModels/Configuration.cs @@ -1,5 +1,8 @@ using System; using System.Collections.Generic; +using System.IO; +using Discord; +using Newtonsoft.Json; namespace NadekoBot.Classes.JSONModels { public class Configuration { @@ -42,4 +45,23 @@ namespace NadekoBot.Classes.JSONModels { "https://49.media.tumblr.com/8e8a099c4eba22abd3ec0f70fd087cce/tumblr_nxovj9oY861ur1mffo1_500.gif ", }; } + + public static class ConfigHandler { + 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); + } } diff --git a/NadekoBot/Classes/Permissions/PermissionChecker.cs b/NadekoBot/Classes/Permissions/PermissionChecker.cs index 837d74de..5694ad91 100644 --- a/NadekoBot/Classes/Permissions/PermissionChecker.cs +++ b/NadekoBot/Classes/Permissions/PermissionChecker.cs @@ -5,6 +5,7 @@ using Discord; using Discord.Commands; using System.Collections.Concurrent; using System.Collections.Generic; +using NadekoBot.Classes.JSONModels; namespace NadekoBot.Classes.Permissions { @@ -27,9 +28,9 @@ namespace NadekoBot.Classes.Permissions { public bool CanRun(Command command, User user, Channel channel, out string error) { error = String.Empty; - if (NadekoBot.IsUserBlacklisted(user.Id) || + if (ConfigHandler.IsUserBlacklisted(user.Id) || (!channel.IsPrivate && - (NadekoBot.IsServerBlacklisted(channel.Server.Id) || NadekoBot.IsChannelBlacklisted(channel.Id)))) { + (ConfigHandler.IsServerBlacklisted(channel.Server.Id) || ConfigHandler.IsChannelBlacklisted(channel.Id)))) { return false; } diff --git a/NadekoBot/Commands/PlayingRotate.cs b/NadekoBot/Commands/PlayingRotate.cs index 8cd5384e..dec92efd 100644 --- a/NadekoBot/Commands/PlayingRotate.cs +++ b/NadekoBot/Commands/PlayingRotate.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading.Tasks; using Discord.Commands; using System.Timers; +using NadekoBot.Classes.JSONModels; using NadekoBot.Modules; namespace NadekoBot.Commands { @@ -66,7 +67,7 @@ namespace NadekoBot.Commands { else timer.Start(); NadekoBot.Config.IsRotatingStatus = timer.Enabled; - NadekoBot.SaveConfig(); + ConfigHandler.SaveConfig(); } await e.Channel.SendMessage($"❗`Rotating playing status has been {(timer.Enabled ? "enabled" : "disabled")}.`"); }; @@ -90,7 +91,7 @@ namespace NadekoBot.Commands { return; lock (playingPlaceholderLock) { NadekoBot.Config.RotatingStatuses.Add(arg); - NadekoBot.SaveConfig(); + ConfigHandler.SaveConfig(); } await e.Channel.SendMessage("🆗 `Added a new playing string.`"); }); @@ -124,7 +125,7 @@ namespace NadekoBot.Commands { return; str = NadekoBot.Config.RotatingStatuses[num - 1]; NadekoBot.Config.RotatingStatuses.RemoveAt(num - 1); - NadekoBot.SaveConfig(); + ConfigHandler.SaveConfig(); } await e.Channel.SendMessage($"🆗 `Removed playing string #{num}`({str})"); }); diff --git a/NadekoBot/Modules/Permissions.cs b/NadekoBot/Modules/Permissions.cs index 33c0f08a..3a338485 100644 --- a/NadekoBot/Modules/Permissions.cs +++ b/NadekoBot/Modules/Permissions.cs @@ -4,6 +4,7 @@ using Discord.Commands; using PermsHandler = NadekoBot.Classes.Permissions.PermissionsHandler; using System.Linq; using System.Threading.Tasks; +using NadekoBot.Classes.JSONModels; using NadekoBot.Classes.Permissions; using NadekoBot.Commands; using NadekoBot.Extensions; @@ -446,7 +447,7 @@ namespace NadekoBot.Modules { if (!e.Message.MentionedUsers.Any()) return; var usr = e.Message.MentionedUsers.First(); NadekoBot.Config.UserBlacklist.Add(usr.Id); - NadekoBot.SaveConfig(); + ConfigHandler.SaveConfig(); await e.Channel.SendMessage($"`Sucessfully blacklisted user {usr.Name}`"); }); }); @@ -459,7 +460,7 @@ namespace NadekoBot.Modules { if (!e.Message.MentionedChannels.Any()) return; var ch = e.Message.MentionedChannels.First(); NadekoBot.Config.UserBlacklist.Add(ch.Id); - NadekoBot.SaveConfig(); + ConfigHandler.SaveConfig(); await e.Channel.SendMessage($"`Sucessfully blacklisted channel {ch.Name}`"); }); }); @@ -479,7 +480,7 @@ namespace NadekoBot.Modules { return; } NadekoBot.Config.ServerBlacklist.Add(server.Id); - NadekoBot.SaveConfig(); + ConfigHandler.SaveConfig(); //cleanup trivias and typeracing Classes.Trivia.TriviaGame trivia; Commands.Trivia.RunningTrivias.TryRemove(server.Id, out trivia); diff --git a/NadekoBot/NadekoBot.cs b/NadekoBot/NadekoBot.cs index 52959f2a..afe07ef0 100644 --- a/NadekoBot/NadekoBot.cs +++ b/NadekoBot/NadekoBot.cs @@ -9,14 +9,12 @@ using Discord.Audio; using System.Linq; using System.Text; using System.Threading.Tasks; -using Discord.Commands.Permissions.Userlist; using NadekoBot.Classes.JSONModels; using NadekoBot.Commands; namespace NadekoBot { internal class NadekoBot { public static DiscordClient Client; - public static bool ForwardMessages = false; public static Credentials Creds { get; set; } public static Configuration Config { get; set; } public static LocalizedStrings Locale { get; set; } = new LocalizedStrings(); @@ -26,7 +24,6 @@ namespace NadekoBot { 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)); @@ -167,7 +164,7 @@ namespace NadekoBot { public static bool IsOwner(User u) => IsOwner(u.Id); public async Task SendMessageToOwner(string message) { - if (ForwardMessages && OwnerPrivateChannel != null) + if (Config.ForwardMessages && OwnerPrivateChannel != null) await OwnerPrivateChannel.SendMessage(message); } @@ -176,7 +173,7 @@ namespace NadekoBot { try { if (e.Server != null || e.User.Id == Client.CurrentUser.Id) return; if (PollCommand.ActivePolls.SelectMany(kvp => kvp.Key.Users.Select(u => u.Id)).Contains(e.User.Id)) return; - if (IsBlackListed(e)) + if (ConfigHandler.IsBlackListed(e)) return; if (!NadekoBot.Config.DontJoinServers) { @@ -192,7 +189,7 @@ namespace NadekoBot { } } - if (ForwardMessages && OwnerPrivateChannel != null) + if (Config.ForwardMessages && OwnerPrivateChannel != null) await OwnerPrivateChannel.SendMessage(e.User + ": ```\n" + e.Message.Text + "\n```"); if (repliedRecently) return; @@ -205,23 +202,6 @@ 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); } }