diff --git a/Framework/ArgsUniform/ArgsUniform.cs b/Framework/ArgsUniform/ArgsUniform.cs index 8804c25..68bf8f2 100644 --- a/Framework/ArgsUniform/ArgsUniform.cs +++ b/Framework/ArgsUniform/ArgsUniform.cs @@ -61,7 +61,7 @@ namespace ArgsUniform if (missingRequired.Any()) { - PrintResults(result, uniformProperties); + PrintResults(printResult,result, uniformProperties); Print(""); foreach (var missing in missingRequired) { @@ -75,34 +75,36 @@ namespace ArgsUniform Environment.Exit(1); } - if (printResult) - { - PrintResults(result, uniformProperties); - } + PrintResults(printResult, result, uniformProperties); return result; } - private void PrintResults(T result, PropertyInfo[] uniformProperties) - { - Print(""); - foreach (var p in uniformProperties) - { - Print($"\t{p.Name} = {p.GetValue(result)}"); - } - Print(""); - } - public void PrintHelp() { Print(""); - PrintAligned("CLI option:", "(short)", "Environment variable:", "Description"); - var attrs = typeof(T).GetProperties().Where(m => m.GetCustomAttributes(typeof(UniformAttribute), false).Length == 1).Select(p => p.GetCustomAttribute()).Where(a => a != null).ToArray(); - foreach (var attr in attrs) + PrintAligned("CLI option:", "(short)", "Environment variable:", "Description", "(default)"); + var props = typeof(T).GetProperties().Where(m => m.GetCustomAttributes(typeof(UniformAttribute), false).Length == 1).ToArray(); + foreach (var prop in props) { - var a = attr!; - var optional = !a.Required ? " *" : ""; - PrintAligned($"--{a.Arg}=...", $"({a.ArgShort})", a.EnvVar, a.Description + optional); + var a = prop.GetCustomAttribute(); + if (a != null) + { + var optional = !a.Required ? " (optional)" : ""; + var def = assigner.DescribeDefaultFor(prop); + PrintAligned($"--{a.Arg}=...", $"({a.ArgShort})", a.EnvVar, a.Description + optional, $"({def})"); + } + } + Print(""); + } + + private void PrintResults(bool printResult, T result, PropertyInfo[] uniformProperties) + { + if (!printResult) return; + Print(""); + foreach (var p in uniformProperties) + { + Print($"\t{p.Name} = {p.GetValue(result)}"); } Print(""); } @@ -112,7 +114,7 @@ namespace ArgsUniform Console.WriteLine(msg); } - private void PrintAligned(string cli, string s, string env, string desc) + private void PrintAligned(string cli, string s, string env, string desc, string def) { Console.CursorLeft = cliStart; Console.Write(cli); @@ -121,7 +123,8 @@ namespace ArgsUniform Console.CursorLeft = envStart; Console.Write(env); Console.CursorLeft = descStart; - Console.Write(desc + Environment.NewLine); + Console.Write(desc + " "); + Console.Write(def + Environment.NewLine); } } } diff --git a/Framework/ArgsUniform/Assigner.cs b/Framework/ArgsUniform/Assigner.cs index b25c68b..d1e2e81 100644 --- a/Framework/ArgsUniform/Assigner.cs +++ b/Framework/ArgsUniform/Assigner.cs @@ -23,20 +23,38 @@ namespace ArgsUniform return false; } + public string DescribeDefaultFor(PropertyInfo property) + { + var obj = Activator.CreateInstance(); + var defaultValue = GetDefaultValue(obj, property); + if (defaultValue == null) return ""; + if (defaultValue is string str) + { + return "\"" + str + "\""; + } + return defaultValue.ToString() ?? string.Empty; + } + + private object? GetDefaultValue(T result, PropertyInfo uniformProperty) + { + // Get value from object's static initializer if it's there. + var currentValue = uniformProperty.GetValue(result); + if (currentValue != null) return currentValue; + + // Get value from defaults-provider object if it's there. + if (defaultsProvider == null) return null; + var defaultProperty = defaultsProvider.GetType().GetProperties().SingleOrDefault(p => p.Name == uniformProperty.Name); + if (defaultProperty == null) return null; + return defaultProperty.GetValue(defaultsProvider); + } + private bool AssignFromDefaultsIfAble(T result, PropertyInfo uniformProperty) { - var currentValue = uniformProperty.GetValue(result); - var isEmptryString = (currentValue as string) == string.Empty; - if (currentValue != GetDefaultValueForType(uniformProperty.PropertyType) && !isEmptryString) return true; - if (defaultsProvider == null) return false; - - var defaultProperty = defaultsProvider.GetType().GetProperties().SingleOrDefault(p => p.Name == uniformProperty.Name); - if (defaultProperty == null) return false; - - var value = defaultProperty.GetValue(defaultsProvider); - if (value != null) + var defaultValue = GetDefaultValue(result, uniformProperty); + var isEmptryString = (defaultValue as string) == string.Empty; + if (defaultValue != null && defaultValue != GetDefaultValueForType(uniformProperty.PropertyType) && !isEmptryString) { - return Assign(result, uniformProperty, value); + return Assign(result, uniformProperty, defaultValue); } return false; } diff --git a/Tools/BiblioTech/AdminChecker.cs b/Tools/BiblioTech/AdminChecker.cs index e1c82cd..def2870 100644 --- a/Tools/BiblioTech/AdminChecker.cs +++ b/Tools/BiblioTech/AdminChecker.cs @@ -24,7 +24,7 @@ namespace BiblioTech public bool IsAdminChannel(IChannel channel) { - return channel.Name == Program.Config.AdminChannelName; + return channel.Id == Program.Config.AdminChannelId; } public ISocketMessageChannel GetAdminChannel() @@ -45,7 +45,7 @@ namespace BiblioTech private void UpdateAdminIds() { lastUpdate = DateTime.UtcNow; - var adminRole = guild.Roles.Single(r => r.Name == Program.Config.AdminRoleName); + var adminRole = guild.Roles.Single(r => r.Id == Program.Config.AdminRoleId); adminIds = adminRole.Members.Select(m => m.Id).ToArray(); } } diff --git a/Tools/BiblioTech/CommandHandler.cs b/Tools/BiblioTech/CommandHandler.cs index 7907177..c7263c5 100644 --- a/Tools/BiblioTech/CommandHandler.cs +++ b/Tools/BiblioTech/CommandHandler.cs @@ -22,7 +22,7 @@ namespace BiblioTech private async Task Client_Ready() { - var guild = client.Guilds.Single(g => g.Name == Program.Config.ServerName); + var guild = client.Guilds.Single(g => g.Id == Program.Config.ServerId); Program.AdminChecker.SetGuild(guild); Program.Log.Log($"Initializing for guild: '{guild.Name}'"); diff --git a/Tools/BiblioTech/Configuration.cs b/Tools/BiblioTech/Configuration.cs index 73e143e..9164cf7 100644 --- a/Tools/BiblioTech/Configuration.cs +++ b/Tools/BiblioTech/Configuration.cs @@ -10,7 +10,7 @@ namespace BiblioTech [Uniform("server-id", "sn", "SERVERID", true, "ID of the Discord server")] public ulong ServerId { get; set; } - [Uniform("datapath", "dp", "DATAPATH", false, "Root path where all data files will be saved.")] + [Uniform("datapath", "dp", "DATAPATH", true, "Root path where all data files will be saved.")] public string DataPath { get; set; } = "datapath"; [Uniform("admin-role-id", "a", "ADMINROLEID", true, "ID of the Discord server admin role")] @@ -23,15 +23,15 @@ namespace BiblioTech public ulong RewardsChannelId { get; set; } [Uniform("chain-events-channel-id", "cc", "CHAINEVENTSCHANNELID", false, "ID of the Discord server channel where chain events will be posted.")] - public ulong ChainEventsChannelID { get; set; } + public ulong ChainEventsChannelId { get; set; } - [Uniform("reward-api-port", "rp", "REWARDAPIPORT", false, "TCP listen port for the reward API.")] + [Uniform("reward-api-port", "rp", "REWARDAPIPORT", true, "TCP listen port for the reward API.")] public int RewardApiPort { get; set; } = 31080; - [Uniform("send-eth", "se", "SENDETH", false, "Amount of Eth send by the mint command. Default: 10.")] + [Uniform("send-eth", "se", "SENDETH", true, "Amount of Eth send by the mint command.")] public int SendEth { get; set; } = 10; - [Uniform("mint-tt", "mt", "MINTTT", false, "Amount of TestTokens minted by the mint command. Default: 1073741824")] + [Uniform("mint-tt", "mt", "MINTTT", true, "Amount of TestTokens minted by the mint command.")] public int MintTT { get; set; } = 1073741824; public string EndpointsPath diff --git a/Tools/BiblioTech/Rewards/RoleDriver.cs b/Tools/BiblioTech/Rewards/RoleDriver.cs index 6695af3..f0ea48f 100644 --- a/Tools/BiblioTech/Rewards/RoleDriver.cs +++ b/Tools/BiblioTech/Rewards/RoleDriver.cs @@ -16,8 +16,8 @@ namespace BiblioTech.Rewards { this.client = client; - rewardsChannel = GetChannel(Program.Config.RewardsChannelName); - eventsChannel = GetChannel(Program.Config.ChainEventsChannelName); + rewardsChannel = GetChannel(Program.Config.RewardsChannelId); + eventsChannel = GetChannel(Program.Config.ChainEventsChannelId); } public async Task GiveRewards(GiveRewardsCommand rewards) @@ -45,10 +45,10 @@ namespace BiblioTech.Rewards await context.ProcessGiveRewardsCommand(LookUpUsers(rewards)); } - private SocketTextChannel? GetChannel(string name) + private SocketTextChannel? GetChannel(ulong id) { - if (string.IsNullOrEmpty(name)) return null; - return GetGuild().TextChannels.SingleOrDefault(c => c.Name == name); + if (id == 0) return null; + return GetGuild().TextChannels.SingleOrDefault(c => c.Id == id); } private async Task ProcessChainEvents(string[] eventsOverview) @@ -147,11 +147,11 @@ namespace BiblioTech.Rewards private SocketGuild GetGuild() { - var guild = client.Guilds.SingleOrDefault(g => g.Name == Program.Config.ServerName); + var guild = client.Guilds.SingleOrDefault(g => g.Id == Program.Config.ServerId); if (guild == null) { - throw new Exception($"Unable to find guild by name: '{Program.Config.ServerName}'. " + - $"Known guilds: [{string.Join(",", client.Guilds.Select(g => g.Name))}]"); + throw new Exception($"Unable to find guild by id: '{Program.Config.ServerId}'. " + + $"Known guilds: [{string.Join(",", client.Guilds.Select(g => g.Name + " (" + g.Id + ")"))}]"); } return guild; }