observing streams are now in serverspecificconfig

This commit is contained in:
Master Kwoth 2016-03-22 21:02:43 +01:00
parent b073a08030
commit ac8ade46a2
5 changed files with 57 additions and 36 deletions

3
.gitignore vendored
View File

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

View File

@ -13,9 +13,6 @@ namespace NadekoBot.Classes.JSONModels {
[JsonIgnore] [JsonIgnore]
public List<Quote> Quotes { get; set; } = new List<Quote>(); public List<Quote> Quotes { get; set; } = new List<Quote>();
public HashSet<StreamNotificationConfig> ObservingStreams { get; set; } =
new HashSet<StreamNotificationConfig>();
public List<string> RotatingStatuses { get; set; } = new List<string>(); public List<string> RotatingStatuses { get; set; } = new List<string>();
public CommandPrefixesModel CommandPrefixes { get; set; } = new CommandPrefixesModel(); public CommandPrefixesModel CommandPrefixes { get; set; } = new CommandPrefixesModel();
public HashSet<ulong> ServerBlacklist { get; set; } = new HashSet<ulong>(); public HashSet<ulong> ServerBlacklist { get; set; } = new HashSet<ulong>();
@ -113,29 +110,6 @@ namespace NadekoBot.Classes.JSONModels {
public static bool IsUserBlacklisted(ulong id) => NadekoBot.Config.UserBlacklist.Contains(id); public static bool IsUserBlacklisted(ulong id) => NadekoBot.Config.UserBlacklist.Contains(id);
} }
public class StreamNotificationConfig : IEquatable<StreamNotificationConfig> {
public string Username { get; set; }
public StreamType Type { get; set; }
public ulong ServerId { get; set; }
public ulong ChannelId { get; set; }
public bool LastStatus { get; set; }
public enum StreamType {
Twitch,
Hitbox,
YoutubeGaming
}
public bool Equals(StreamNotificationConfig other) =>
this.Username.ToLower().Trim() == other.Username.ToLower().Trim() &&
this.Type == other.Type &&
this.ServerId == other.ServerId;
public override int GetHashCode() {
return (int)((int)ServerId + Username.Length + (int)Type);
}
}
public class Quote { public class Quote {
public string Author { get; set; } public string Author { get; set; }
public string Text { get; set; } public string Text { get; set; }

View File

@ -5,6 +5,7 @@ using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.IO; using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using NadekoBot.Classes.JSONModels;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace NadekoBot.Classes { namespace NadekoBot.Classes {
@ -34,6 +35,8 @@ namespace NadekoBot.Classes {
private readonly ConcurrentDictionary<ulong, ServerSpecificConfig> configs; private readonly ConcurrentDictionary<ulong, ServerSpecificConfig> configs;
public IEnumerable<ServerSpecificConfig> AllConfigs => configs.Values;
public ServerSpecificConfig Of(ulong id) => public ServerSpecificConfig Of(ulong id) =>
configs.GetOrAdd(id, _ => new ServerSpecificConfig()); configs.GetOrAdd(id, _ => new ServerSpecificConfig());
@ -82,8 +85,23 @@ namespace NadekoBot.Classes {
} }
} }
[JsonIgnore]
private ObservableCollection<StreamNotificationConfig> observingStreams;
public ObservableCollection<StreamNotificationConfig> ObservingStreams {
get { return observingStreams; }
set {
observingStreams = value;
if (value != null)
observingStreams.CollectionChanged += (s, e) => {
if (!SpecificConfigurations.Instantiated) return;
OnPropertyChanged();
};
}
}
public ServerSpecificConfig() { public ServerSpecificConfig() {
ListOfSelfAssignableRoles = new ObservableCollection<ulong>(); ListOfSelfAssignableRoles = new ObservableCollection<ulong>();
ObservingStreams = new ObservableCollection<StreamNotificationConfig>();
} }
public event PropertyChangedEventHandler PropertyChanged = delegate { SpecificConfigurations.Default.Save(); }; public event PropertyChangedEventHandler PropertyChanged = delegate { SpecificConfigurations.Default.Save(); };
@ -93,4 +111,27 @@ namespace NadekoBot.Classes {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
} }
} }
public class StreamNotificationConfig : IEquatable<StreamNotificationConfig> {
public string Username { get; set; }
public StreamType Type { get; set; }
public ulong ServerId { get; set; }
public ulong ChannelId { get; set; }
public bool LastStatus { get; set; }
public enum StreamType {
Twitch,
Hitbox,
YoutubeGaming
}
public bool Equals(StreamNotificationConfig other) =>
this.Username.ToLower().Trim() == other.Username.ToLower().Trim() &&
this.Type == other.Type &&
this.ServerId == other.ServerId;
public override int GetHashCode() {
return (int)((int)ServerId + Username.Length + (int)Type);
}
}
} }

View File

@ -22,8 +22,8 @@ namespace NadekoBot.Commands {
checkTimer.Elapsed += async (s, e) => { checkTimer.Elapsed += async (s, e) => {
try { try {
var streams = NadekoBot.Config.ObservingStreams; var streams = SpecificConfigurations.Default.AllConfigs.SelectMany(c => c.ObservingStreams);
if (streams == null || !streams.Any()) return; if (!streams.Any()) return;
foreach (var stream in streams) { foreach (var stream in streams) {
Tuple<bool, string> data; Tuple<bool, string> data;
@ -108,7 +108,9 @@ namespace NadekoBot.Commands {
if (string.IsNullOrWhiteSpace(username)) if (string.IsNullOrWhiteSpace(username))
return; return;
var toRemove = NadekoBot.Config.ObservingStreams var config = SpecificConfigurations.Default.Of(e.Server.Id);
var toRemove = config.ObservingStreams
.FirstOrDefault(snc => snc.ChannelId == e.Channel.Id && .FirstOrDefault(snc => snc.ChannelId == e.Channel.Id &&
snc.Username.ToLower().Trim() == username); snc.Username.ToLower().Trim() == username);
if (toRemove == null) { if (toRemove == null) {
@ -116,7 +118,7 @@ namespace NadekoBot.Commands {
return; return;
} }
NadekoBot.Config.ObservingStreams.Remove(toRemove); config.ObservingStreams.Remove(toRemove);
ConfigHandler.SaveConfig(); ConfigHandler.SaveConfig();
await e.Channel.SendMessage($":ok: Removed `{toRemove.Username}`'s stream from notifications."); await e.Channel.SendMessage($":ok: Removed `{toRemove.Username}`'s stream from notifications.");
}); });
@ -126,7 +128,10 @@ namespace NadekoBot.Commands {
.Description("Lists all streams you are following on this server." + .Description("Lists all streams you are following on this server." +
"\n**Usage**: ~ls") "\n**Usage**: ~ls")
.Do(async e => { .Do(async e => {
var streams = NadekoBot.Config.ObservingStreams.Where(snc =>
var config = SpecificConfigurations.Default.Of(e.Server.Id);
var streams = config.ObservingStreams.Where(snc =>
snc.ServerId == e.Server.Id); snc.ServerId == e.Server.Id);
var streamsArray = streams as StreamNotificationConfig[] ?? streams.ToArray(); var streamsArray = streams as StreamNotificationConfig[] ?? streams.ToArray();
@ -153,13 +158,15 @@ namespace NadekoBot.Commands {
if (string.IsNullOrWhiteSpace(username)) if (string.IsNullOrWhiteSpace(username))
return; return;
var config = SpecificConfigurations.Default.Of(e.Server.Id);
var stream = new StreamNotificationConfig { var stream = new StreamNotificationConfig {
ServerId = e.Server.Id, ServerId = e.Server.Id,
ChannelId = e.Channel.Id, ChannelId = e.Channel.Id,
Username = username, Username = username,
Type = type, Type = type,
}; };
var exists = NadekoBot.Config.ObservingStreams.Contains(stream); var exists = config.ObservingStreams.Contains(stream);
if (exists) { if (exists) {
await e.Channel.SendMessage(":anger: I am already notifying that stream on this channel."); await e.Channel.SendMessage(":anger: I am already notifying that stream on this channel.");
} }
@ -182,8 +189,7 @@ namespace NadekoBot.Commands {
if (!exists) if (!exists)
msg = $":ok: I will notify this channel when status changes.\n{msg}"; msg = $":ok: I will notify this channel when status changes.\n{msg}";
await e.Channel.SendMessage(msg); await e.Channel.SendMessage(msg);
NadekoBot.Config.ObservingStreams.Add(stream); config.ObservingStreams.Add(stream);
ConfigHandler.SaveConfig();
}; };
} }
} }

View File

@ -2,7 +2,6 @@
"DontJoinServers": false, "DontJoinServers": false,
"ForwardMessages": true, "ForwardMessages": true,
"IsRotatingStatus": false, "IsRotatingStatus": false,
"ObservingStreams": [],
"RotatingStatuses": [], "RotatingStatuses": [],
"CommandPrefixes": { "CommandPrefixes": {
"Administration": ".", "Administration": ".",