|
| 1 | +namespace Grid.Bot.Commands.Private; |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Reflection; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Text.RegularExpressions; |
| 11 | + |
| 12 | +using Discord; |
| 13 | +using Discord.Commands; |
| 14 | + |
| 15 | +using Newtonsoft.Json; |
| 16 | + |
| 17 | +using Vault; |
| 18 | +using Configuration; |
| 19 | + |
| 20 | +using Utility; |
| 21 | +using Extensions; |
| 22 | + |
| 23 | +/// <summary> |
| 24 | +/// Represents the interaction for settings. |
| 25 | +/// </summary> |
| 26 | +[LockDownCommand(BotRole.Owner)] |
| 27 | +[RequireBotRole(BotRole.Owner)] |
| 28 | +[Group("settings"), Summary("Commands used for managing app settings.")] |
| 29 | +public partial class Settings(IServiceProvider services) : ModuleBase |
| 30 | +{ |
| 31 | + private class ProviderStringConverter : BaseProvider |
| 32 | + { |
| 33 | + public static ProviderStringConverter Singleton = new(); |
| 34 | + |
| 35 | + public object ConvertToPub(string value, Type type) => ConvertTo(value, type); |
| 36 | + public string ConvertFromPub(object value, Type type) => ConvertFrom(value, type); |
| 37 | + |
| 38 | + protected override bool GetRawValue(string key, out string value) |
| 39 | + { |
| 40 | + throw new NotImplementedException(); |
| 41 | + } |
| 42 | + protected override void SetRawValue<T>(string key, T value) |
| 43 | + { |
| 44 | + throw new NotImplementedException(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private readonly IServiceProvider _services = services ?? throw new ArgumentNullException(nameof(services)); |
| 49 | + |
| 50 | + private const string _namespace = "Grid.Bot"; |
| 51 | + private static readonly Assembly _settingsAssembly = Assembly.Load("Shared.Settings"); |
| 52 | + private static readonly Assembly _configAssembly = Assembly.Load("Configuration"); |
| 53 | + |
| 54 | + [GeneratedRegex(@"^([a-zA-Z]+)Settings$", RegexOptions.IgnoreCase | RegexOptions.Compiled)] |
| 55 | + private partial Regex GetProviderNameRegex(); |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Gets a list of settings providers that can be modified. |
| 59 | + /// </summary> |
| 60 | + [Command("info"), Summary("Gets information about settings such as environment and versions.")] |
| 61 | + public async Task GetInformationAsync() |
| 62 | + { |
| 63 | + var builder = new EmbedBuilder() |
| 64 | + .WithTitle("Configuration Information") |
| 65 | + .WithAuthor(Context.User) |
| 66 | + .WithCurrentTimestamp() |
| 67 | + .WithColor(Color.Green); |
| 68 | + |
| 69 | + var isUsingVault = VaultClientFactory.Singleton.GetClient() != null ? "yes" : "no"; |
| 70 | + var settingsAssemblyVersion = _settingsAssembly.GetName().Version; |
| 71 | + var configurationAssemblyVersion = _configAssembly.GetName().Version; |
| 72 | + |
| 73 | + var environment = Grid.Bot.EnvironmentProvider.EnvironmentName; |
| 74 | + |
| 75 | + builder.AddField("Settings Version", settingsAssemblyVersion, true) |
| 76 | + .AddField("Configuration Version", configurationAssemblyVersion, true) |
| 77 | + .AddField("Environment", environment, true) |
| 78 | + .AddField("Is using Vault", isUsingVault, true); |
| 79 | + |
| 80 | + await this.ReplyWithReferenceAsync(embed: builder.Build()); |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Gets a list of settings providers that can be modified. |
| 85 | + /// </summary> |
| 86 | + [Command("providers"), Summary("Lists the names of all available providers.")] |
| 87 | + public async Task ListProvidersAsync() |
| 88 | + { |
| 89 | + var providerTypes = _settingsAssembly.GetTypes().Where(type => type.BaseType == typeof(BaseSettingsProvider)); |
| 90 | + |
| 91 | + var builder = new EmbedBuilder() |
| 92 | + .WithTitle("Providers list") |
| 93 | + .WithAuthor(Context.User) |
| 94 | + .WithCurrentTimestamp() |
| 95 | + .WithColor(Color.Green); |
| 96 | + |
| 97 | + var desc = "```\n"; |
| 98 | + |
| 99 | + foreach (var provider in providerTypes) |
| 100 | + desc += $"{GetProviderNameRegex().Match(provider.Name).Groups[1]}\n"; |
| 101 | + |
| 102 | + desc += "```"; |
| 103 | + |
| 104 | + builder.WithDescription(desc); |
| 105 | + |
| 106 | + await this.ReplyWithReferenceAsync(embed: builder.Build()); |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Gets the settings for the specified provider. |
| 112 | + /// </summary> |
| 113 | + /// <param name="provider">The name of the provider.</param> |
| 114 | + /// <param name="refresh">Should the prvoider be refreshed beforehand?</param> |
| 115 | + [Command("all"), Summary("Gets all settings for the specified provider."), Alias("list")] |
| 116 | + public async Task GetAllAsync(string provider, bool refresh = true) |
| 117 | + { |
| 118 | + using var _ = Context.Channel.EnterTypingState(); |
| 119 | + |
| 120 | + var fullName = $"{provider}settings"; |
| 121 | + |
| 122 | + var type = _settingsAssembly.GetType($"{_namespace}.{fullName}", false, true); |
| 123 | + if (type == null) |
| 124 | + { |
| 125 | + await this.ReplyWithReferenceAsync($"The settings provider with the name {provider} was not found!"); |
| 126 | + |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + var instance = _services.GetService(type) as BaseSettingsProvider; |
| 131 | + if (type == null) |
| 132 | + { |
| 133 | + await this.ReplyWithReferenceAsync($"The settings provider with the name {provider} was not found!"); |
| 134 | + |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + if (refresh) instance.Refresh(); |
| 139 | + |
| 140 | + using var stream = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(instance.GetRawValues(), Formatting.Indented))); |
| 141 | + |
| 142 | + await this.ReplyWithFileAsync(stream, $"{provider}.json", "Here are the settings for the specified provider."); |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Get the value of the specified setting. |
| 147 | + /// </summary> |
| 148 | + /// <param name="provider">The name of the provider</param> |
| 149 | + /// <param name="settingName">The name of the setting</param> |
| 150 | + /// <param name="refresh">Should the prvoider be refreshed beforehand?</param> |
| 151 | + [Command("get"), Summary("Get the value of the specified setting.")] |
| 152 | + public async Task GetSettingAsync(string provider, string settingName, bool refresh = true) |
| 153 | + { |
| 154 | + using var _ = Context.Channel.EnterTypingState(); |
| 155 | + |
| 156 | + var fullName = $"{provider}settings"; |
| 157 | + |
| 158 | + var type = _settingsAssembly.GetType($"{_namespace}.{fullName}", false, true); |
| 159 | + if (type == null) |
| 160 | + { |
| 161 | + await this.ReplyWithReferenceAsync($"The settings provider with the name {provider} was not found!"); |
| 162 | + |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + var instance = _services.GetService(type) as BaseSettingsProvider; |
| 167 | + if (type == null) |
| 168 | + { |
| 169 | + await this.ReplyWithReferenceAsync($"The settings provider with the name {provider} was not found!"); |
| 170 | + |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + var property = type.GetProperty(settingName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); |
| 175 | + if (property == null) |
| 176 | + { |
| 177 | + await this.ReplyWithReferenceAsync($"The settings provider with the name {provider} does not define the setting {settingName}!"); |
| 178 | + |
| 179 | + return; |
| 180 | + } |
| 181 | + |
| 182 | + if (refresh) instance.Refresh(); |
| 183 | + |
| 184 | + var value = property.GetMethod.Invoke(instance, []); |
| 185 | + if (value is not string) value = JsonConvert.SerializeObject(value); |
| 186 | + if (string.IsNullOrEmpty(value as string)) value = "(empty)"; |
| 187 | + |
| 188 | + var embed = new EmbedBuilder() |
| 189 | + .WithTitle($"{type.Name}.{property.Name} ({property.PropertyType.Name})") |
| 190 | + .WithDescription($"```{value}```") |
| 191 | + .WithAuthor(Context.User) |
| 192 | + .WithCurrentTimestamp() |
| 193 | + .WithColor(Color.Green) |
| 194 | + .Build(); |
| 195 | + |
| 196 | + await this.ReplyWithReferenceAsync(embed: embed); |
| 197 | + } |
| 198 | + |
| 199 | + /// <summary> |
| 200 | + /// Sets the specified setting to the specified value. |
| 201 | + /// </summary> |
| 202 | + /// <param name="provider">The name of the provider</param> |
| 203 | + /// <param name="settingName">The name of the setting</param> |
| 204 | + /// <param name="newValue">The new value of the setting</param> |
| 205 | + /// <param name="refresh">Should the prvoider be refreshed beforehand?</param> |
| 206 | + [Command("set"), Summary("Sets the specified setting to the specified value.")] |
| 207 | + public async Task SetSettingAsync(string provider, string settingName, string newValue = "", bool refresh = true) |
| 208 | + { |
| 209 | + using var _ = Context.Channel.EnterTypingState(); |
| 210 | + |
| 211 | + var fullName = $"{provider}settings"; |
| 212 | + |
| 213 | + var type = _settingsAssembly.GetType($"{_namespace}.{fullName}", false, true); |
| 214 | + if (type == null) |
| 215 | + { |
| 216 | + await this.ReplyWithReferenceAsync($"The settings provider with the name {provider} was not found!"); |
| 217 | + |
| 218 | + return; |
| 219 | + } |
| 220 | + |
| 221 | + var instance = _services.GetService(type) as BaseSettingsProvider; |
| 222 | + if (type == null) |
| 223 | + { |
| 224 | + await this.ReplyWithReferenceAsync($"The settings provider with the name {provider} was not found!"); |
| 225 | + |
| 226 | + return; |
| 227 | + } |
| 228 | + |
| 229 | + var property = type.GetProperty(settingName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); |
| 230 | + if (property == null) |
| 231 | + { |
| 232 | + await this.ReplyWithReferenceAsync($"The settings provider with the name {provider} does not define the setting {settingName}!"); |
| 233 | + |
| 234 | + return; |
| 235 | + } |
| 236 | + |
| 237 | + if (refresh) instance.Refresh(); |
| 238 | + |
| 239 | + var value = property.GetMethod.Invoke(instance, []); |
| 240 | + var converted = ProviderStringConverter.Singleton.ConvertToPub(newValue, property.PropertyType); |
| 241 | + |
| 242 | + if (value.Equals(converted)) |
| 243 | + { |
| 244 | + await this.ReplyWithReferenceAsync("The value is identical to the current value, not changing!"); |
| 245 | + |
| 246 | + return; |
| 247 | + } |
| 248 | + |
| 249 | + var attribute = property.GetCustomAttribute<SettingNameAttribute>(); |
| 250 | + var name = attribute?.Name ?? property.Name; |
| 251 | + |
| 252 | + var genericSet = type.GetMethod("Set", BindingFlags.Instance | BindingFlags.Public).MakeGenericMethod([ property.PropertyType ]); |
| 253 | + |
| 254 | + genericSet.Invoke(instance, [name, converted]); |
| 255 | + |
| 256 | + if (value is not string) value = JsonConvert.SerializeObject(value); |
| 257 | + if (string.IsNullOrEmpty(value as string)) value = "(empty)"; |
| 258 | + |
| 259 | + var embed = new EmbedBuilder() |
| 260 | + .WithTitle($"{type.Name}.{property.Name} ({property.PropertyType.Name})") |
| 261 | + .AddField("Before", $"```{value}```") |
| 262 | + .AddField("After", $"```{newValue}```") |
| 263 | + .WithAuthor(Context.User) |
| 264 | + .WithCurrentTimestamp() |
| 265 | + .WithColor(Color.Green) |
| 266 | + .Build(); |
| 267 | + |
| 268 | + await this.ReplyWithReferenceAsync(embed: embed); |
| 269 | + } |
| 270 | + |
| 271 | + /// <summary> |
| 272 | + /// Refreshes the specified provider or all registered providers. |
| 273 | + /// </summary> |
| 274 | + /// <param name="provider">The name of the provider.</param> |
| 275 | + [Command("refresh"), Summary("Refreshes the specified provider or all registered providers.")] |
| 276 | + public async Task RefreshAsync(string provider = "") |
| 277 | + { |
| 278 | + using var _ = Context.Channel.EnterTypingState(); |
| 279 | + |
| 280 | + if (string.IsNullOrEmpty(provider)) |
| 281 | + { |
| 282 | + VaultProvider.RefreshAllProviders(); |
| 283 | + |
| 284 | + await this.ReplyWithReferenceAsync("Refreshed all registered providers!"); |
| 285 | + |
| 286 | + return; |
| 287 | + } |
| 288 | + |
| 289 | + var fullName = $"{provider}settings"; |
| 290 | + |
| 291 | + var type = _settingsAssembly.GetType($"{_namespace}.{fullName}", false, true); |
| 292 | + if (type == null) |
| 293 | + { |
| 294 | + await this.ReplyWithReferenceAsync($"The settings provider with the name {provider} was not found!"); |
| 295 | + |
| 296 | + return; |
| 297 | + } |
| 298 | + |
| 299 | + var instance = _services.GetService(type) as BaseSettingsProvider; |
| 300 | + if (type == null) |
| 301 | + { |
| 302 | + await this.ReplyWithReferenceAsync($"The settings provider with the name {provider} was not found!"); |
| 303 | + |
| 304 | + return; |
| 305 | + } |
| 306 | + |
| 307 | + instance.Refresh(); |
| 308 | + |
| 309 | + await this.ReplyWithReferenceAsync($"Successfully refreshed the {provider} settings provider!"); |
| 310 | + } |
| 311 | +} |
0 commit comments