mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-01-31 00:19:00 +03:00
Add /resetsettings (#111)
Signed-off-by: Macintosh II <mctaylxrs@outlook.com> Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com> Co-authored-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
parent
d6e1468f3e
commit
3a3865ba3d
7 changed files with 112 additions and 0 deletions
|
@ -594,4 +594,10 @@
|
|||
<data name="NoRemindersFound" xml:space="preserve">
|
||||
<value>You don't have any reminders created!</value>
|
||||
</data>
|
||||
<data name="SingleSettingReset" xml:space="preserve">
|
||||
<value>Setting {0} reset</value>
|
||||
</data>
|
||||
<data name="AllSettingsReset" xml:space="preserve">
|
||||
<value>All settings have been reset</value>
|
||||
</data>
|
||||
</root>
|
||||
|
|
|
@ -594,4 +594,10 @@
|
|||
<data name="NoRemindersFound" xml:space="preserve">
|
||||
<value>У вас нет созданных напоминаний!</value>
|
||||
</data>
|
||||
<data name="SingleSettingReset" xml:space="preserve">
|
||||
<value>Настройка {0} сброшена</value>
|
||||
</data>
|
||||
<data name="AllSettingsReset" xml:space="preserve">
|
||||
<value>Все настройки были сброшены</value>
|
||||
</data>
|
||||
</root>
|
||||
|
|
|
@ -594,4 +594,10 @@
|
|||
<data name="NoRemindersFound" xml:space="preserve">
|
||||
<value>ты еще не крафтил напоминалки</value>
|
||||
</data>
|
||||
<data name="SingleSettingReset" xml:space="preserve">
|
||||
<value>{0} откачен к заводским</value>
|
||||
</data>
|
||||
<data name="AllSettingsReset" xml:space="preserve">
|
||||
<value>откатываемся к заводским...</value>
|
||||
</data>
|
||||
</root>
|
||||
|
|
|
@ -229,4 +229,79 @@ public class SettingsCommandGroup : CommandGroup
|
|||
|
||||
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A slash command that resets per-guild GuildSettings.
|
||||
/// </summary>
|
||||
/// <param name="setting">The setting to reset.</param>
|
||||
/// <returns>A feedback sending result which may have succeeded.</returns>
|
||||
[Command("resetsettings")]
|
||||
[DiscordDefaultMemberPermissions(DiscordPermission.ManageGuild)]
|
||||
[DiscordDefaultDMPermission(false)]
|
||||
[RequireContext(ChannelContext.Guild)]
|
||||
[RequireDiscordPermission(DiscordPermission.ManageGuild)]
|
||||
[Description("Reset settings for this server")]
|
||||
[UsedImplicitly]
|
||||
public async Task<Result> 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<Result> 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<Result> ResetAllSettingsAsync(JsonNode cfg, IUser currentUser,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var failedResults = new List<Result>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,4 +8,5 @@ public interface IOption
|
|||
string Name { get; }
|
||||
string Display(JsonNode settings);
|
||||
Result Set(JsonNode settings, string from);
|
||||
Result Reset(JsonNode settings);
|
||||
}
|
||||
|
|
|
@ -48,4 +48,10 @@ public class Option<T> : IOption
|
|||
var property = settings[Name];
|
||||
return property != null ? property.GetValue<T>() : DefaultValue;
|
||||
}
|
||||
|
||||
public Result Reset(JsonNode settings)
|
||||
{
|
||||
settings[Name] = null;
|
||||
return Result.FromSuccess();
|
||||
}
|
||||
}
|
||||
|
|
12
src/Messages.Designer.cs
generated
12
src/Messages.Designer.cs
generated
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue