forked from KarlOfDuty/SupportBoi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.cs
More file actions
122 lines (102 loc) · 5.95 KB
/
Config.cs
File metadata and controls
122 lines (102 loc) · 5.95 KB
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
using System;
using System.IO;
using DSharpPlus;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using YamlDotNet.Serialization;
namespace SupportBoi;
internal static class Config
{
// TODO: Rewrite with read-only properties
internal static string token = "";
internal static ulong logChannel;
internal static string welcomeMessage = "";
internal static TimestampFormat timestampFormat = TimestampFormat.RelativeTime;
internal static bool randomAssignment = false;
internal static bool randomAssignRoleOverride = false;
internal static string presenceType = "Playing";
internal static string presenceText = "";
internal static bool newCommandUsesSelector = false;
internal static int userTicketLimit = 0;
internal static int globalTicketLimit = 0;
internal static bool pinFirstMessage = false;
internal static string transcriptDir = "";
internal static string logFile = "";
internal static bool addCategoryTicketCount = false;
internal static bool ticketUpdatedNotifications = false;
internal static double ticketUpdatedNotificationDelay = 0.0;
internal static bool assignmentNotifications = false;
internal static bool closingNotifications = false;
internal static bool interviewsEnabled = false;
internal static bool deleteMessagesAfterInterviewEnd = false;
internal static string hostName = "127.0.0.1";
internal static int port = 3306;
internal static string database = "supportboi";
internal static string username = "supportboi";
internal static string password = "";
public static string ConfigPath { get; private set; } = "./config.yml";
public static bool Initialized { get; private set; } = false;
public static void LoadConfig()
{
if (!string.IsNullOrEmpty(SupportBoi.commandLineArgs.configPath))
{
ConfigPath = SupportBoi.commandLineArgs.configPath;
}
Logger.Log("Loading config \"" + Path.GetFullPath(ConfigPath) + "\"");
// Writes default config to file if it does not already exist
if (!File.Exists(ConfigPath))
{
File.WriteAllText(ConfigPath, Utilities.ReadManifestData("default_config.yml"));
}
// Reads config contents into FileStream
FileStream stream = File.OpenRead(ConfigPath);
// Converts the FileStream into a YAML object
IDeserializer deserializer = new DeserializerBuilder().Build();
object yamlObject = deserializer.Deserialize(new StreamReader(stream)) ?? "";
// Converts the YAML object into a JSON object as the YAML ones do not support traversal or selection of nodes by name
ISerializer serializer = new SerializerBuilder().JsonCompatible().Build();
JObject json = JObject.Parse(serializer.Serialize(yamlObject));
// Sets up the bot
token = json.SelectToken("bot.token")?.Value<string>() ?? "";
logChannel = json.SelectToken("bot.log-channel")?.Value<ulong>() ?? 0;
welcomeMessage = json.SelectToken("bot.welcome-message")?.Value<string>() ?? "";
string stringLogLevel = json.SelectToken("bot.console-log-level")?.Value<string>() ?? "";
if (!Enum.TryParse(stringLogLevel, true, out LogLevel logLevel))
{
logLevel = LogLevel.Information;
Logger.Warn("Log level '" + stringLogLevel + "' is invalid, using 'Information' instead.");
}
Logger.SetLogLevel(logLevel);
string stringTimestampFormat = json.SelectToken("bot.timestamp-format")?.Value<string>() ?? "RelativeTime";
if (!Enum.TryParse(stringTimestampFormat, true, out timestampFormat))
{
timestampFormat = TimestampFormat.RelativeTime;
Logger.Warn("Timestamp '" + stringTimestampFormat + "' is invalid, using 'RelativeTime' instead.");
}
randomAssignment = json.SelectToken("bot.random-assignment")?.Value<bool>() ?? false;
randomAssignRoleOverride = json.SelectToken("bot.random-assign-role-override")?.Value<bool>() ?? false;
presenceType = json.SelectToken("bot.presence-type")?.Value<string>() ?? "Playing";
presenceText = json.SelectToken("bot.presence-text")?.Value<string>() ?? "";
newCommandUsesSelector = json.SelectToken("bot.new-command-uses-selector")?.Value<bool>() ?? false;
userTicketLimit = json.SelectToken("bot.ticket-limits.user")?.Value<int>() ?? 0;
globalTicketLimit = json.SelectToken("bot.ticket-limits.total")?.Value<int>() ?? 0;
pinFirstMessage = json.SelectToken("bot.pin-first-message")?.Value<bool>() ?? false;
transcriptDir = json.SelectToken("bot.transcript-dir")?.Value<string>() ?? "";
logFile = json.SelectToken("bot.log-file")?.Value<string>() ?? "";
addCategoryTicketCount = json.SelectToken("bot.add-category-ticket-count")?.Value<bool>() ?? false;
ticketUpdatedNotifications = json.SelectToken("notifications.ticket-updated")?.Value<bool>() ?? false;
ticketUpdatedNotificationDelay = json.SelectToken("notifications.ticket-updated-delay")?.Value<double>() ?? 0.0;
assignmentNotifications = json.SelectToken("notifications.assignment")?.Value<bool>() ?? false;
closingNotifications = json.SelectToken("notifications.closing")?.Value<bool>() ?? false;
interviewsEnabled = json.SelectToken("interviews.enabled")?.Value<bool>() ?? false;
deleteMessagesAfterInterviewEnd = json.SelectToken("interviews.delete-messages-after-interview-end")?.Value<bool>() ?? false;
// Reads database info
hostName = json.SelectToken("database.address")?.Value<string>() ?? "";
port = json.SelectToken("database.port")?.Value<int>() ?? 3306;
database = json.SelectToken("database.name")?.Value<string>() ?? "supportboi";
username = json.SelectToken("database.user")?.Value<string>() ?? "supportboi";
password = json.SelectToken("database.password")?.Value<string>() ?? "";
Logger.SetupLogfile();
Initialized = true;
}
}