Moved config statics to ConfigHandler class

This commit is contained in:
Master Kwoth 2016-03-10 15:05:38 +01:00
parent d9d368a7d1
commit ff67e35d37
5 changed files with 36 additions and 31 deletions

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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})");
});

View File

@ -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);

View File

@ -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);
}
}