From 3a3865ba3dad413f0b08d9a6fb1d68d651ff8d30 Mon Sep 17 00:00:00 2001 From: Macintosh II <95250141+mctaylors@users.noreply.github.com> Date: Fri, 22 Sep 2023 20:23:08 +0300 Subject: [PATCH] Add /resetsettings (#111) Signed-off-by: Macintosh II Signed-off-by: Octol1ttle Co-authored-by: Octol1ttle --- locale/Messages.resx | 6 +++ locale/Messages.ru.resx | 6 +++ locale/Messages.tt-ru.resx | 6 +++ src/Commands/SettingsCommandGroup.cs | 75 ++++++++++++++++++++++++++++ src/Data/Options/IOption.cs | 1 + src/Data/Options/Option.cs | 6 +++ src/Messages.Designer.cs | 12 +++++ 7 files changed, 112 insertions(+) diff --git a/locale/Messages.resx b/locale/Messages.resx index 82392a8..84d7fa7 100644 --- a/locale/Messages.resx +++ b/locale/Messages.resx @@ -594,4 +594,10 @@ You don't have any reminders created! + + Setting {0} reset + + + All settings have been reset + diff --git a/locale/Messages.ru.resx b/locale/Messages.ru.resx index 7b5b3d4..abeaf88 100644 --- a/locale/Messages.ru.resx +++ b/locale/Messages.ru.resx @@ -594,4 +594,10 @@ У вас нет созданных напоминаний! + + Настройка {0} сброшена + + + Все настройки были сброшены + diff --git a/locale/Messages.tt-ru.resx b/locale/Messages.tt-ru.resx index bb8c204..fb01e72 100644 --- a/locale/Messages.tt-ru.resx +++ b/locale/Messages.tt-ru.resx @@ -594,4 +594,10 @@ ты еще не крафтил напоминалки + + {0} откачен к заводским + + + откатываемся к заводским... + diff --git a/src/Commands/SettingsCommandGroup.cs b/src/Commands/SettingsCommandGroup.cs index dcb0a5f..50ea955 100644 --- a/src/Commands/SettingsCommandGroup.cs +++ b/src/Commands/SettingsCommandGroup.cs @@ -229,4 +229,79 @@ public class SettingsCommandGroup : CommandGroup return await _feedback.SendContextualEmbedResultAsync(embed, ct); } + + /// + /// A slash command that resets per-guild GuildSettings. + /// + /// The setting to reset. + /// A feedback sending result which may have succeeded. + [Command("resetsettings")] + [DiscordDefaultMemberPermissions(DiscordPermission.ManageGuild)] + [DiscordDefaultDMPermission(false)] + [RequireContext(ChannelContext.Guild)] + [RequireDiscordPermission(DiscordPermission.ManageGuild)] + [Description("Reset settings for this server")] + [UsedImplicitly] + public async Task ExecuteResetSettingsAsync( + [Description("Setting to reset")] AllOptionsEnum? setting = null) + { + if (!_context.TryGetContextIDs(out var guildId, out _, out _)) + { + return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context"); + } + + var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken); + if (!currentUserResult.IsDefined(out var currentUser)) + { + return Result.FromError(currentUserResult); + } + + var cfg = await _guildData.GetSettings(guildId, CancellationToken); + Messages.Culture = GuildSettings.Language.Get(cfg); + + if (setting is not null) + { + return await ResetSingleSettingAsync(cfg, currentUser, AllOptions[(int)setting], CancellationToken); + } + + return await ResetAllSettingsAsync(cfg, currentUser, CancellationToken); + } + + private async Task ResetSingleSettingAsync(JsonNode cfg, IUser currentUser, + IOption option, CancellationToken ct = default) + { + var resetResult = option.Reset(cfg); + if (!resetResult.IsSuccess) + { + return Result.FromError(resetResult.Error); + } + + var embed = new EmbedBuilder().WithSmallTitle( + string.Format(Messages.SingleSettingReset, option.Name), currentUser) + .WithColour(ColorsList.Green) + .Build(); + + return await _feedback.SendContextualEmbedResultAsync(embed, ct); + } + + private async Task ResetAllSettingsAsync(JsonNode cfg, IUser currentUser, + CancellationToken ct = default) + { + var failedResults = new List(); + foreach (var resetResult in AllOptions.Select(option => option.Reset(cfg))) + { + failedResults.AddIfFailed(resetResult); + } + + if (failedResults.Count is not 0) + { + return failedResults.AggregateErrors(); + } + + var embed = new EmbedBuilder().WithSmallTitle(Messages.AllSettingsReset, currentUser) + .WithColour(ColorsList.Green) + .Build(); + + return await _feedback.SendContextualEmbedResultAsync(embed, ct); + } } diff --git a/src/Data/Options/IOption.cs b/src/Data/Options/IOption.cs index 6f435e5..42138dc 100644 --- a/src/Data/Options/IOption.cs +++ b/src/Data/Options/IOption.cs @@ -8,4 +8,5 @@ public interface IOption string Name { get; } string Display(JsonNode settings); Result Set(JsonNode settings, string from); + Result Reset(JsonNode settings); } diff --git a/src/Data/Options/Option.cs b/src/Data/Options/Option.cs index c96b6ac..a62eb04 100644 --- a/src/Data/Options/Option.cs +++ b/src/Data/Options/Option.cs @@ -48,4 +48,10 @@ public class Option : IOption var property = settings[Name]; return property != null ? property.GetValue() : DefaultValue; } + + public Result Reset(JsonNode settings) + { + settings[Name] = null; + return Result.FromSuccess(); + } } diff --git a/src/Messages.Designer.cs b/src/Messages.Designer.cs index 0fee435..b45ae70 100644 --- a/src/Messages.Designer.cs +++ b/src/Messages.Designer.cs @@ -1001,5 +1001,17 @@ namespace Boyfriend { return ResourceManager.GetString("NoRemindersFound", resourceCulture); } } + + internal static string SingleSettingReset { + get { + return ResourceManager.GetString("SingleSettingReset", resourceCulture); + } + } + + internal static string AllSettingsReset { + get { + return ResourceManager.GetString("AllSettingsReset", resourceCulture); + } + } } }