From 5831f5205ca148ebc3cbfb01cd1371b800a6524f Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Tue, 22 Aug 2023 12:45:46 +0500 Subject: [PATCH] Add autocomplete for /editsettings setting keys (#98) This PR adds autocomplete for setting keys in `/editsetting` slash command. The usage of options provided by auto-complete is enforced client-side. Closes #97 Closes #95 Signed-off-by: Octol1ttle --- src/Commands/SettingsCommandGroup.cs | 17 ++++++++++------ src/Data/Options/AllOptionsEnum.cs | 29 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 src/Data/Options/AllOptionsEnum.cs diff --git a/src/Commands/SettingsCommandGroup.cs b/src/Commands/SettingsCommandGroup.cs index fab90c4..dcb0a5f 100644 --- a/src/Commands/SettingsCommandGroup.cs +++ b/src/Commands/SettingsCommandGroup.cs @@ -26,6 +26,13 @@ namespace Boyfriend.Commands; [UsedImplicitly] public class SettingsCommandGroup : CommandGroup { + /// + /// Represents all options as an array of objects implementing . + /// + /// + /// WARNING: If you update this array in any way, you must also update and make sure + /// that the orders match. + /// private static readonly IOption[] AllOptions = { GuildSettings.Language, @@ -158,7 +165,7 @@ public class SettingsCommandGroup : CommandGroup [UsedImplicitly] public async Task ExecuteEditSettingsAsync( [Description("The setting whose value you want to change")] - string setting, + AllOptionsEnum setting, [Description("Setting value")] string value) { if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId)) @@ -181,16 +188,14 @@ public class SettingsCommandGroup : CommandGroup var data = await _guildData.GetData(guildId, CancellationToken); Messages.Culture = GuildSettings.Language.Get(data.Settings); - return await EditSettingAsync(setting, value, data, channelId, user, currentUser, CancellationToken); + return await EditSettingAsync(AllOptions[(int)setting], value, data, channelId, user, currentUser, + CancellationToken); } private async Task EditSettingAsync( - string setting, string value, GuildData data, Snowflake channelId, IUser user, IUser currentUser, + IOption option, string value, GuildData data, Snowflake channelId, IUser user, IUser currentUser, CancellationToken ct = default) { - var option = AllOptions.Single( - o => string.Equals(setting, o.Name, StringComparison.InvariantCultureIgnoreCase)); - var setResult = option.Set(data.Settings, value); if (!setResult.IsSuccess) { diff --git a/src/Data/Options/AllOptionsEnum.cs b/src/Data/Options/AllOptionsEnum.cs new file mode 100644 index 0000000..d0b1ae7 --- /dev/null +++ b/src/Data/Options/AllOptionsEnum.cs @@ -0,0 +1,29 @@ +using Boyfriend.Commands; +using JetBrains.Annotations; + +namespace Boyfriend.Data.Options; + +/// +/// Represents all options as enums. +/// +/// +/// WARNING: This enum is order-dependent! It's values are used as indexes for +/// . +/// +public enum AllOptionsEnum +{ + [UsedImplicitly] Language, + [UsedImplicitly] WelcomeMessage, + [UsedImplicitly] ReceiveStartupMessages, + [UsedImplicitly] RemoveRolesOnMute, + [UsedImplicitly] ReturnRolesOnRejoin, + [UsedImplicitly] AutoStartEvents, + [UsedImplicitly] RenameHoistedUsers, + [UsedImplicitly] PublicFeedbackChannel, + [UsedImplicitly] PrivateFeedbackChannel, + [UsedImplicitly] EventNotificationChannel, + [UsedImplicitly] DefaultRole, + [UsedImplicitly] MuteRole, + [UsedImplicitly] EventNotificationRole, + [UsedImplicitly] EventEarlyNotificationOffset +}