using System; using System.IO; using System.Text; using DSharpPlus; using Microsoft.Extensions.Logging; using Newtonsoft.Json.Linq; using Valkyrie.Properties; using YamlDotNet.Serialization; namespace Valkyrie; internal static class Config { internal static string token = ""; internal static ulong logChannel; internal static string welcomeMessage = ""; internal static LogLevel logLevel = LogLevel.Information; 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 ticketLimit = 5; internal static bool ticketUpdatedNotifications = false; internal static double ticketUpdatedNotificationDelay = 0.0; internal static bool assignmentNotifications = false; internal static bool closingNotifications = false; internal static string hostName = "127.0.0.1"; internal static int port = 3306; internal static string database = "akheemah"; internal static string username = "akheemah"; internal static string password = ""; public static void LoadConfig() { // Write default config to file if it does not already exist if (!File.Exists("./config.yml")) { File.WriteAllText("./config.yml", Encoding.UTF8.GetString(Resources.default_config)); } // Reads config contents into FileStream FileStream stream = File.OpenRead("./config.yml"); // 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 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() ?? ""; logChannel = json.SelectToken("bot.log-channel")?.Value() ?? 0; welcomeMessage = json.SelectToken("bot.welcome-message")?.Value() ?? ""; string stringLogLevel = json.SelectToken("bot.console-log-level")?.Value() ?? ""; if (!Enum.TryParse(stringLogLevel, true, out logLevel)) { logLevel = LogLevel.Information; Logger.Warn("Log level '" + stringLogLevel + "' invalid, using 'Information' instead."); } string stringTimestampFormat = json.SelectToken("bot.timestamp-format")?.Value() ?? "RelativeTime"; if (!Enum.TryParse(stringTimestampFormat, true, out timestampFormat)) { timestampFormat = TimestampFormat.RelativeTime; Logger.Warn("Timestamp '" + stringTimestampFormat + "' invalid, using 'RelativeTime' instead."); } randomAssignment = json.SelectToken("bot.random-assignment")?.Value() ?? false; randomAssignRoleOverride = json.SelectToken("bot.random-assign-role-override")?.Value() ?? false; presenceType = json.SelectToken("bot.presence-type")?.Value() ?? "Playing"; presenceText = json.SelectToken("bot.presence-text")?.Value() ?? ""; newCommandUsesSelector = json.SelectToken("bot.new-command-uses-selector")?.Value() ?? false; ticketLimit = json.SelectToken("bot.ticket-limit")?.Value() ?? 5; ticketUpdatedNotifications = json.SelectToken("notifications.ticket-updated")?.Value() ?? false; ticketUpdatedNotificationDelay = json.SelectToken("notifications.ticket-updated-delay")?.Value() ?? 0.0; assignmentNotifications = json.SelectToken("notifications.assignment")?.Value() ?? false; closingNotifications = json.SelectToken("notifications.closing")?.Value() ?? false; // Reads database info hostName = json.SelectToken("database.address")?.Value() ?? ""; port = json.SelectToken("database.port")?.Value() ?? 3306; database = json.SelectToken("database.name")?.Value() ?? "akheemah"; username = json.SelectToken("database.user")?.Value() ?? "akheemah"; password = json.SelectToken("database.password")?.Value() ?? ""; } }