-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
StreamerBot.cs
207 lines (187 loc) · 6.6 KB
/
StreamerBot.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
Copyright (C) 2023-2024 Sehelitar
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Timers;
namespace Kick.Bot
{
internal class StreamerBotAppSettings
{
private static StreamerBotCommands _commands;
private static StreamerBotSettings _settings;
private static FileSystemWatcher _configWatcher;
public static List<StreamerBotCommand> Commands
{
get
{
if (_commands == null)
{
Load();
}
return _commands.Commands;
}
}
public static StreamerBotSettings Settings
{
get
{
if (_settings == null)
{
Load();
}
return _settings;
}
}
public static void StartWatcher()
{
if (_configWatcher != null)
return;
_configWatcher = new FileSystemWatcher("./data/", "*.json")
{
NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.Security
| NotifyFilters.Size,
IncludeSubdirectories = true,
EnableRaisingEvents = true
};
_configWatcher.Changed += delegate (object sender, FileSystemEventArgs e)
{
if (e.Name == "commands.json")
LoadCommandsSettings();
if (e.Name == "settings.json")
LoadSettings();
};
}
public static void StopWatcher()
{
if (_configWatcher != null)
{
_configWatcher.Dispose();
_configWatcher = null;
}
}
private static void LoadCommandsSettings()
{
BotClient.CPH?.LogVerbose("[Kick] Loading chat commands");
var fs = new FileStream("./data/commands.json", FileMode.Open);
var config = new StreamReader(fs).ReadToEnd();
fs.Close();
_commands = JsonConvert.DeserializeObject<StreamerBotCommands>(config);
Timer timer = new Timer(1000);
timer.Elapsed += delegate
{
timer.Stop();
BotChatCommander.ReloadCommands();
timer.Close();
};
timer.Start();
}
private static void LoadSettings()
{
BotClient.CPH?.LogVerbose("[Kick] Loading main cofiguration");
var fs = new FileStream("./data/settings.json", FileMode.Open);
var config = new StreamReader(fs).ReadToEnd();
fs.Close();
_settings = JsonConvert.DeserializeObject<StreamerBotSettings>(config);
Timer timer = new Timer(1000);
timer.Elapsed += delegate
{
timer.Stop();
BotTimedActionManager.ReloadTimedActions();
timer.Close();
};
timer.Start();
}
public static void Load()
{
LoadCommandsSettings();
LoadSettings();
StartWatcher();
}
}
internal class StreamerBotCommands
{
[JsonProperty("commands")]
public List<StreamerBotCommand> Commands { get; set; }
}
internal class StreamerBotCommand
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("sources")]
public List<long> Sources { get; set; }
[JsonProperty("permittedUsers")]
public List<string> PermittedUsers { get; set; }
[JsonProperty("permittedGroups")]
public List<string> PermittedGroups { get; set; }
[JsonProperty("enabled")]
public bool Enabled { get; set; } = true;
[JsonProperty("include")]
public bool Include { get; set; } = true;
[JsonProperty("mode")]
public int Mode { get; set; } = 0;
[JsonProperty("command")]
public string Command { get; set; }
[JsonProperty("location")]
public long Location { get; set; } = 0;
[JsonProperty("persistCounter")]
public bool PersistCounter { get; set; } = false;
[JsonProperty("persistUserCounter")]
public bool PersistUserCounter { get; set; } = false;
[JsonProperty("caseSensitive")]
public bool CaseSensitive { get; set; } = false;
[JsonProperty("globalCooldown")]
public long GlobalCooldown { get; set; } = 0;
[JsonProperty("userCooldown")]
public long UserCooldown { get; set; } = 0;
[JsonProperty("group")]
public string Group { get; set; } = String.Empty;
[JsonProperty("grantType")]
public int GrantType { get; set; } = 0;
}
internal class StreamerBotSettings
{
[JsonProperty("timedActions")]
public TimedActionsSettings TimedActions { get; set; }
}
internal class TimedActionsSettings
{
[JsonProperty("timers")]
public List<TimedAction> Timers { get; set; }
}
internal class TimedAction
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("enabled")]
public bool Enabled { get; set; } = true;
public bool Repeat { get; set; } = false;
public int Interval { get; set; } = 0;
public bool RandomInterval { get; set; } = false;
public int UpperInterval { get; set; } = 0;
public int Lines { get; set; } = 0;
public int Counter { get; set; } = 0;
}
}