2023-07-09 16:32:14 +03:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Text;
|
2023-07-20 23:41:02 +03:00
|
|
|
|
using System.Text.Json.Nodes;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
using Boyfriend.Data;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
using Boyfriend.Data.Options;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
using Boyfriend.Services;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
using JetBrains.Annotations;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
using Remora.Commands.Attributes;
|
|
|
|
|
using Remora.Commands.Groups;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
using Remora.Discord.Commands.Attributes;
|
|
|
|
|
using Remora.Discord.Commands.Conditions;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
using Remora.Discord.Commands.Contexts;
|
|
|
|
|
using Remora.Discord.Commands.Feedback.Services;
|
|
|
|
|
using Remora.Discord.Extensions.Embeds;
|
|
|
|
|
using Remora.Discord.Extensions.Formatting;
|
|
|
|
|
using Remora.Results;
|
|
|
|
|
|
|
|
|
|
namespace Boyfriend.Commands;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the commands to list and modify per-guild settings: /settings and /settings list.
|
|
|
|
|
/// </summary>
|
2023-07-18 15:25:02 +03:00
|
|
|
|
[UsedImplicitly]
|
2023-07-09 16:32:14 +03:00
|
|
|
|
public class SettingsCommandGroup : CommandGroup {
|
2023-07-18 15:25:02 +03:00
|
|
|
|
private static readonly IOption[] AllOptions = {
|
|
|
|
|
GuildSettings.Language,
|
|
|
|
|
GuildSettings.WelcomeMessage,
|
|
|
|
|
GuildSettings.ReceiveStartupMessages,
|
|
|
|
|
GuildSettings.RemoveRolesOnMute,
|
|
|
|
|
GuildSettings.ReturnRolesOnRejoin,
|
|
|
|
|
GuildSettings.AutoStartEvents,
|
2023-07-24 19:51:05 +03:00
|
|
|
|
GuildSettings.RenameHoistedUsers,
|
2023-07-18 15:25:02 +03:00
|
|
|
|
GuildSettings.PublicFeedbackChannel,
|
|
|
|
|
GuildSettings.PrivateFeedbackChannel,
|
|
|
|
|
GuildSettings.EventNotificationChannel,
|
|
|
|
|
GuildSettings.DefaultRole,
|
|
|
|
|
GuildSettings.MuteRole,
|
|
|
|
|
GuildSettings.EventNotificationRole,
|
|
|
|
|
GuildSettings.EventEarlyNotificationOffset
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-09 16:32:14 +03:00
|
|
|
|
private readonly ICommandContext _context;
|
|
|
|
|
private readonly GuildDataService _dataService;
|
|
|
|
|
private readonly FeedbackService _feedbackService;
|
|
|
|
|
private readonly IDiscordRestUserAPI _userApi;
|
|
|
|
|
|
|
|
|
|
public SettingsCommandGroup(
|
|
|
|
|
ICommandContext context, GuildDataService dataService,
|
|
|
|
|
FeedbackService feedbackService, IDiscordRestUserAPI userApi) {
|
|
|
|
|
_context = context;
|
|
|
|
|
_dataService = dataService;
|
|
|
|
|
_feedbackService = feedbackService;
|
|
|
|
|
_userApi = userApi;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-07-18 15:25:02 +03:00
|
|
|
|
/// A slash command that lists current per-guild GuildSettings.
|
2023-07-09 16:32:14 +03:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// A feedback sending result which may or may not have succeeded.
|
|
|
|
|
/// </returns>
|
2023-07-09 18:15:39 +03:00
|
|
|
|
[Command("settingslist")]
|
2023-07-18 15:25:02 +03:00
|
|
|
|
[DiscordDefaultMemberPermissions(DiscordPermission.ManageGuild)]
|
|
|
|
|
[DiscordDefaultDMPermission(false)]
|
|
|
|
|
[RequireContext(ChannelContext.Guild)]
|
|
|
|
|
[RequireDiscordPermission(DiscordPermission.ManageGuild)]
|
2023-07-09 16:32:14 +03:00
|
|
|
|
[Description("Shows settings list for this server")]
|
2023-07-18 15:25:02 +03:00
|
|
|
|
[UsedImplicitly]
|
2023-07-25 17:53:11 +03:00
|
|
|
|
public async Task<Result> ExecuteSettingsListAsync(
|
|
|
|
|
[Description("Settings list page")] int page) {
|
2023-07-09 16:32:14 +03:00
|
|
|
|
if (!_context.TryGetContextIDs(out var guildId, out _, out _))
|
2023-07-28 19:58:55 +03:00
|
|
|
|
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
|
|
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
|
|
|
|
if (!currentUserResult.IsDefined(out var currentUser))
|
|
|
|
|
return Result.FromError(currentUserResult);
|
|
|
|
|
|
2023-07-24 14:57:41 +03:00
|
|
|
|
var cfg = await _dataService.GetSettings(guildId, CancellationToken);
|
2023-07-18 15:25:02 +03:00
|
|
|
|
Messages.Culture = GuildSettings.Language.Get(cfg);
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
2023-07-25 17:53:11 +03:00
|
|
|
|
return await SendSettingsListAsync(cfg, currentUser, page, CancellationToken);
|
2023-07-20 23:41:02 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-25 17:53:11 +03:00
|
|
|
|
private async Task<Result> SendSettingsListAsync(JsonNode cfg, IUser currentUser, int page, CancellationToken ct = default) {
|
2023-07-26 21:07:05 +03:00
|
|
|
|
var description = new StringBuilder();
|
2023-07-25 17:53:11 +03:00
|
|
|
|
var footer = new StringBuilder();
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
2023-07-26 21:07:05 +03:00
|
|
|
|
const int optionsPerPage = 10;
|
2023-07-25 17:53:11 +03:00
|
|
|
|
|
2023-07-26 21:07:05 +03:00
|
|
|
|
var totalPages = (AllOptions.Length + optionsPerPage - 1) / optionsPerPage;
|
|
|
|
|
var lastOptionOnPage = Math.Min(optionsPerPage * page, AllOptions.Length);
|
|
|
|
|
var firstOptionOnPage = optionsPerPage * page - optionsPerPage;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
2023-07-26 21:07:05 +03:00
|
|
|
|
if (firstOptionOnPage >= AllOptions.Length) {
|
|
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(Messages.PageNotFound, currentUser)
|
|
|
|
|
.WithDescription(string.Format(Messages.PagesAllowed, Markdown.Bold(totalPages.ToString())))
|
2023-07-25 17:53:11 +03:00
|
|
|
|
.WithColour(ColorsList.Red)
|
|
|
|
|
.Build();
|
|
|
|
|
|
2023-07-26 21:07:05 +03:00
|
|
|
|
return await _feedbackService.SendContextualEmbedResultAsync(embed, ct);
|
|
|
|
|
} else {
|
|
|
|
|
footer.Append($"{Messages.Page} {page}/{totalPages} ");
|
|
|
|
|
for (var i = 0; i < totalPages; i++) footer.Append(i + 1 == page ? "●" : "○");
|
|
|
|
|
|
|
|
|
|
for (var i = firstOptionOnPage; i < lastOptionOnPage; i++) {
|
|
|
|
|
var optionName = AllOptions[i].Name;
|
|
|
|
|
var optionValue = AllOptions[i].Display(cfg);
|
|
|
|
|
|
|
|
|
|
description.AppendLine($"- {$"Settings{optionName}".Localized()}")
|
|
|
|
|
.Append($" - {Markdown.InlineCode(optionName)}: ")
|
|
|
|
|
.AppendLine(optionValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(Messages.SettingsListTitle, currentUser)
|
|
|
|
|
.WithDescription(description.ToString())
|
|
|
|
|
.WithColour(ColorsList.Default)
|
|
|
|
|
.WithFooter(footer.ToString())
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
return await _feedbackService.SendContextualEmbedResultAsync(embed, ct);
|
|
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-07-18 15:25:02 +03:00
|
|
|
|
/// A slash command that modifies per-guild GuildSettings.
|
2023-07-09 16:32:14 +03:00
|
|
|
|
/// </summary>
|
2023-07-09 18:15:39 +03:00
|
|
|
|
/// <param name="setting">The setting to modify.</param>
|
|
|
|
|
/// <param name="value">The new value of the setting.</param>
|
|
|
|
|
/// <returns>A feedback sending result which may or may not have succeeded.</returns>
|
2023-07-09 16:32:14 +03:00
|
|
|
|
[Command("settings")]
|
2023-07-18 15:25:02 +03:00
|
|
|
|
[DiscordDefaultMemberPermissions(DiscordPermission.ManageGuild)]
|
|
|
|
|
[DiscordDefaultDMPermission(false)]
|
|
|
|
|
[RequireContext(ChannelContext.Guild)]
|
|
|
|
|
[RequireDiscordPermission(DiscordPermission.ManageGuild)]
|
2023-07-09 16:32:14 +03:00
|
|
|
|
[Description("Change settings for this server")]
|
2023-07-18 15:25:02 +03:00
|
|
|
|
[UsedImplicitly]
|
2023-07-20 23:41:02 +03:00
|
|
|
|
public async Task<Result> ExecuteSettingsAsync(
|
2023-07-09 18:15:39 +03:00
|
|
|
|
[Description("The setting whose value you want to change")]
|
|
|
|
|
string setting,
|
|
|
|
|
[Description("Setting value")] string value) {
|
2023-07-09 16:32:14 +03:00
|
|
|
|
if (!_context.TryGetContextIDs(out var guildId, out _, out _))
|
2023-07-28 19:58:55 +03:00
|
|
|
|
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
|
|
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
|
|
|
|
if (!currentUserResult.IsDefined(out var currentUser))
|
|
|
|
|
return Result.FromError(currentUserResult);
|
|
|
|
|
|
2023-07-24 14:57:41 +03:00
|
|
|
|
var data = await _dataService.GetData(guildId, CancellationToken);
|
2023-07-20 23:41:02 +03:00
|
|
|
|
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
|
|
|
|
|
|
|
|
|
return await EditSettingAsync(setting, value, data, currentUser, CancellationToken);
|
|
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
2023-07-20 23:41:02 +03:00
|
|
|
|
private async Task<Result> EditSettingAsync(
|
|
|
|
|
string setting, string value, GuildData data, IUser currentUser, CancellationToken ct = default) {
|
2023-07-18 15:25:02 +03:00
|
|
|
|
var option = AllOptions.Single(
|
|
|
|
|
o => string.Equals(setting, o.Name, StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
|
2023-07-20 23:41:02 +03:00
|
|
|
|
var setResult = option.Set(data.Settings, value);
|
2023-07-18 15:25:02 +03:00
|
|
|
|
if (!setResult.IsSuccess) {
|
2023-07-09 16:32:14 +03:00
|
|
|
|
var failedEmbed = new EmbedBuilder().WithSmallTitle(Messages.SettingNotChanged, currentUser)
|
2023-07-18 15:25:02 +03:00
|
|
|
|
.WithDescription(setResult.Error.Message)
|
2023-07-09 16:32:14 +03:00
|
|
|
|
.WithColour(ColorsList.Red)
|
|
|
|
|
.Build();
|
|
|
|
|
|
2023-07-20 23:41:02 +03:00
|
|
|
|
return await _feedbackService.SendContextualEmbedResultAsync(failedEmbed, ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var builder = new StringBuilder();
|
|
|
|
|
|
2023-07-18 15:25:02 +03:00
|
|
|
|
builder.Append(Markdown.InlineCode(option.Name))
|
2023-07-09 18:15:39 +03:00
|
|
|
|
.Append($" {Messages.SettingIsNow} ")
|
2023-07-20 23:41:02 +03:00
|
|
|
|
.Append(option.Display(data.Settings));
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
2023-07-09 18:15:39 +03:00
|
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(Messages.SettingSuccessfullyChanged, currentUser)
|
2023-07-09 16:32:14 +03:00
|
|
|
|
.WithDescription(builder.ToString())
|
|
|
|
|
.WithColour(ColorsList.Green)
|
|
|
|
|
.Build();
|
|
|
|
|
|
2023-07-20 23:41:02 +03:00
|
|
|
|
return await _feedbackService.SendContextualEmbedResultAsync(embed, ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
|
}
|
|
|
|
|
}
|