2022-12-06 18:33:46 +03:00
|
|
|
using Discord;
|
2022-01-18 20:38:15 +03:00
|
|
|
|
|
|
|
namespace Boyfriend.Commands;
|
|
|
|
|
2022-09-18 17:41:29 +03:00
|
|
|
public sealed class SettingsCommand : ICommand {
|
|
|
|
public string[] Aliases { get; } = { "settings", "config", "настройки", "конфиг" };
|
2022-05-14 16:12:24 +03:00
|
|
|
|
2022-09-18 17:41:29 +03:00
|
|
|
public Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
2022-08-30 18:15:01 +03:00
|
|
|
if (!cmd.HasPermission(GuildPermission.ManageGuild)) return Task.CompletedTask;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
2022-08-30 18:15:01 +03:00
|
|
|
var guild = cmd.Context.Guild;
|
2022-05-14 16:12:24 +03:00
|
|
|
var config = Boyfriend.GetGuildConfig(guild.Id);
|
2022-02-02 16:14:26 +03:00
|
|
|
|
2022-11-11 22:59:11 +03:00
|
|
|
if (args.Length is 0) {
|
2022-05-14 16:12:24 +03:00
|
|
|
var currentSettings = Boyfriend.StringBuilder.AppendLine(Messages.CurrentSettings);
|
|
|
|
|
|
|
|
foreach (var setting in Boyfriend.DefaultConfig) {
|
|
|
|
var format = "{0}";
|
2022-11-12 09:02:44 +03:00
|
|
|
var currentValue = config[setting.Key] is "default" ? Messages.DefaultWelcomeMessage : config[setting.Key];
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
|
|
if (setting.Key.EndsWith("Channel")) {
|
2022-11-11 22:59:11 +03:00
|
|
|
if (guild.GetTextChannel(ulong.Parse(currentValue)) is not null) format = "<#{0}>";
|
|
|
|
else currentValue = Messages.ChannelNotSpecified;
|
2022-08-05 19:01:06 +03:00
|
|
|
} else if (setting.Key.EndsWith("Role")) {
|
2022-11-11 22:59:11 +03:00
|
|
|
if (guild.GetRole(ulong.Parse(currentValue)) is not null) format = "<@&{0}>";
|
|
|
|
else currentValue = Messages.RoleNotSpecified;
|
2022-08-05 19:01:06 +03:00
|
|
|
} else {
|
2022-11-11 22:59:11 +03:00
|
|
|
if (!IsBool(currentValue)) format = Utils.Wrap("{0}")!;
|
|
|
|
else currentValue = YesOrNo(currentValue is "true");
|
2022-05-14 16:12:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
currentSettings.Append($"{Utils.GetMessage($"Settings{setting.Key}")} (`{setting.Key}`): ")
|
|
|
|
.AppendFormat(format, currentValue).AppendLine();
|
|
|
|
}
|
|
|
|
|
2022-12-06 18:33:46 +03:00
|
|
|
cmd.Reply(currentSettings.ToString(), ReplyEmojis.SettingsList);
|
2022-05-14 16:12:24 +03:00
|
|
|
currentSettings.Clear();
|
|
|
|
return Task.CompletedTask;
|
2022-01-18 20:38:15 +03:00
|
|
|
}
|
|
|
|
|
2022-05-14 16:12:24 +03:00
|
|
|
var selectedSetting = args[0].ToLower();
|
2022-02-02 16:14:26 +03:00
|
|
|
|
2022-05-14 16:12:24 +03:00
|
|
|
var exists = false;
|
2022-06-06 18:39:47 +03:00
|
|
|
// ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
|
2022-09-18 17:41:29 +03:00
|
|
|
// Too many allocations
|
2022-06-06 18:39:47 +03:00
|
|
|
foreach (var setting in Boyfriend.DefaultConfig.Keys) {
|
|
|
|
if (selectedSetting != setting.ToLower()) continue;
|
|
|
|
selectedSetting = setting;
|
2022-05-14 16:12:24 +03:00
|
|
|
exists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!exists) {
|
2022-12-06 18:33:46 +03:00
|
|
|
cmd.Reply(Messages.SettingDoesntExist, ReplyEmojis.Error);
|
2022-05-14 16:12:24 +03:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2022-08-30 18:15:01 +03:00
|
|
|
string? value;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
|
|
if (args.Length >= 2) {
|
2022-08-30 18:15:01 +03:00
|
|
|
value = cmd.GetRemaining(args, 1, "Setting");
|
2022-11-11 22:59:11 +03:00
|
|
|
if (value is null) return Task.CompletedTask;
|
2022-07-15 19:23:45 +03:00
|
|
|
if (selectedSetting is "EventStartedReceivers") {
|
2022-05-14 16:12:24 +03:00
|
|
|
value = value.Replace(" ", "").ToLower();
|
2022-11-11 22:59:11 +03:00
|
|
|
if (value.StartsWith(",") || value.Count(x => x is ',') > 1 ||
|
|
|
|
(!value.Contains("interested") && !value.Contains("users") && !value.Contains("role"))) {
|
2022-12-06 18:33:46 +03:00
|
|
|
cmd.Reply(Messages.InvalidSettingValue, ReplyEmojis.Error);
|
2022-07-15 19:23:45 +03:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
2022-02-21 20:08:55 +03:00
|
|
|
}
|
2022-08-05 19:01:06 +03:00
|
|
|
} else { value = "reset"; }
|
2022-02-21 20:08:55 +03:00
|
|
|
|
2022-05-14 16:12:24 +03:00
|
|
|
if (IsBool(Boyfriend.DefaultConfig[selectedSetting]) && !IsBool(value)) {
|
|
|
|
value = value switch {
|
2022-07-15 19:23:45 +03:00
|
|
|
"y" or "yes" or "д" or "да" => "true",
|
|
|
|
"n" or "no" or "н" or "нет" => "false",
|
2022-05-14 16:12:24 +03:00
|
|
|
_ => value
|
|
|
|
};
|
|
|
|
if (!IsBool(value)) {
|
2022-12-06 18:33:46 +03:00
|
|
|
cmd.Reply(Messages.InvalidSettingValue, ReplyEmojis.Error);
|
2022-05-14 16:12:24 +03:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var localizedSelectedSetting = Utils.GetMessage($"Settings{selectedSetting}");
|
|
|
|
|
|
|
|
var mention = Utils.ParseMention(value);
|
2022-11-11 22:59:11 +03:00
|
|
|
if (mention is not 0 && selectedSetting is not "WelcomeMessage") value = mention.ToString();
|
2022-05-14 16:12:24 +03:00
|
|
|
|
2022-06-06 18:39:47 +03:00
|
|
|
var formatting = Utils.Wrap("{0}")!;
|
2022-07-15 19:23:45 +03:00
|
|
|
if (selectedSetting is not "WelcomeMessage") {
|
2022-11-11 22:59:11 +03:00
|
|
|
if (selectedSetting.EndsWith("Channel")) formatting = "<#{0}>";
|
|
|
|
if (selectedSetting.EndsWith("Role")) formatting = "<@&{0}>";
|
2022-07-15 19:23:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var formattedValue = selectedSetting switch {
|
|
|
|
"WelcomeMessage" => Utils.Wrap(Messages.DefaultWelcomeMessage),
|
|
|
|
"EventStartedReceivers" => Utils.Wrap(Boyfriend.DefaultConfig[selectedSetting])!,
|
2022-08-05 19:01:06 +03:00
|
|
|
_ => value is "reset" or "default" ? Messages.SettingNotDefined
|
|
|
|
: IsBool(value) ? YesOrNo(value is "true")
|
|
|
|
: string.Format(formatting, value)
|
2022-07-15 19:23:45 +03:00
|
|
|
};
|
2022-02-21 20:08:55 +03:00
|
|
|
|
|
|
|
if (value is "reset" or "default") {
|
2022-11-11 22:59:11 +03:00
|
|
|
config[selectedSetting] = Boyfriend.DefaultConfig[selectedSetting];
|
2022-08-05 19:01:06 +03:00
|
|
|
} else {
|
2022-05-14 16:12:24 +03:00
|
|
|
if (value == config[selectedSetting]) {
|
2022-08-30 18:15:01 +03:00
|
|
|
cmd.Reply(string.Format(Messages.SettingsNothingChanged, localizedSelectedSetting, formattedValue),
|
2022-12-06 18:33:46 +03:00
|
|
|
ReplyEmojis.Error);
|
2022-05-14 16:12:24 +03:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2022-11-11 22:59:11 +03:00
|
|
|
if (selectedSetting is "Lang" && !Utils.CultureInfoCache.ContainsKey(value)) {
|
2022-12-06 18:33:46 +03:00
|
|
|
cmd.Reply(Messages.LanguageNotSupported, ReplyEmojis.Error);
|
2022-05-14 16:12:24 +03:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2022-11-11 22:59:11 +03:00
|
|
|
if (selectedSetting.EndsWith("Channel") && guild.GetTextChannel(mention) is null) {
|
2022-12-06 18:33:46 +03:00
|
|
|
cmd.Reply(Messages.InvalidChannel, ReplyEmojis.Error);
|
2022-05-14 16:12:24 +03:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2022-11-11 22:59:11 +03:00
|
|
|
if (selectedSetting.EndsWith("Role") && guild.GetRole(mention) is null) {
|
2022-12-06 18:33:46 +03:00
|
|
|
cmd.Reply(Messages.InvalidRole, ReplyEmojis.Error);
|
2022-05-14 16:12:24 +03:00
|
|
|
return Task.CompletedTask;
|
2022-02-21 20:08:55 +03:00
|
|
|
}
|
2022-05-14 16:12:24 +03:00
|
|
|
|
2022-07-15 19:23:45 +03:00
|
|
|
if (selectedSetting is "MuteRole") Utils.RemoveMuteRoleFromCache(ulong.Parse(config[selectedSetting]));
|
2022-06-06 18:39:47 +03:00
|
|
|
|
2022-05-14 16:12:24 +03:00
|
|
|
config[selectedSetting] = value;
|
2022-01-18 20:38:15 +03:00
|
|
|
}
|
|
|
|
|
2022-07-15 19:23:45 +03:00
|
|
|
if (selectedSetting is "Lang") {
|
2022-05-14 16:12:24 +03:00
|
|
|
Utils.SetCurrentLanguage(guild.Id);
|
|
|
|
localizedSelectedSetting = Utils.GetMessage($"Settings{selectedSetting}");
|
|
|
|
}
|
2022-01-18 20:38:15 +03:00
|
|
|
|
2022-08-30 18:15:01 +03:00
|
|
|
cmd.ConfigWriteScheduled = true;
|
2022-01-18 20:38:15 +03:00
|
|
|
|
2022-08-30 18:15:01 +03:00
|
|
|
var replyFormat = string.Format(Messages.FeedbackSettingsUpdated, localizedSelectedSetting, formattedValue);
|
2022-12-06 18:33:46 +03:00
|
|
|
cmd.Reply(replyFormat, ReplyEmojis.SettingsSet);
|
2022-08-30 18:15:01 +03:00
|
|
|
cmd.Audit(replyFormat, false);
|
2022-05-14 16:12:24 +03:00
|
|
|
return Task.CompletedTask;
|
2022-01-18 20:38:15 +03:00
|
|
|
}
|
|
|
|
|
2022-05-14 16:12:24 +03:00
|
|
|
private static string YesOrNo(bool isYes) {
|
|
|
|
return isYes ? Messages.Yes : Messages.No;
|
2022-01-18 20:38:15 +03:00
|
|
|
}
|
|
|
|
|
2022-05-14 16:12:24 +03:00
|
|
|
private static bool IsBool(string value) {
|
|
|
|
return value is "true" or "false";
|
2022-01-18 20:38:15 +03:00
|
|
|
}
|
2022-09-18 17:41:29 +03:00
|
|
|
}
|