1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 17:19:00 +03:00
Octobot/Boyfriend/Commands/SettingsCommand.cs

154 lines
6.1 KiB
C#
Raw Normal View History

2022-05-14 16:12:24 +03:00
using Discord;
namespace Boyfriend.Commands;
public sealed class SettingsCommand : ICommand {
public string[] Aliases { get; } = { "settings", "config", "настройки", "конфиг" };
2022-05-14 16:12:24 +03:00
public Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
if (!cmd.HasPermission(GuildPermission.ManageGuild)) return Task.CompletedTask;
2022-05-14 16:12:24 +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
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")) {
if (guild.GetTextChannel(ulong.Parse(currentValue)) is not null) format = "<#{0}>";
else currentValue = Messages.ChannelNotSpecified;
} else if (setting.Key.EndsWith("Role")) {
if (guild.GetRole(ulong.Parse(currentValue)) is not null) format = "<@&{0}>";
else currentValue = Messages.RoleNotSpecified;
} else {
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();
}
cmd.Reply(currentSettings.ToString(), ":gear: ");
2022-05-14 16:12:24 +03:00
currentSettings.Clear();
return Task.CompletedTask;
}
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;
// ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
// Too many allocations
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) {
cmd.Reply(Messages.SettingDoesntExist, ":x: ");
2022-05-14 16:12:24 +03:00
return Task.CompletedTask;
}
string? value;
2022-05-14 16:12:24 +03:00
if (args.Length >= 2) {
value = cmd.GetRemaining(args, 1, "Setting");
if (value is null) return Task.CompletedTask;
if (selectedSetting is "EventStartedReceivers") {
2022-05-14 16:12:24 +03:00
value = value.Replace(" ", "").ToLower();
if (value.StartsWith(",") || value.Count(x => x is ',') > 1 ||
(!value.Contains("interested") && !value.Contains("users") && !value.Contains("role"))) {
cmd.Reply(Messages.InvalidSettingValue, ":x: ");
return Task.CompletedTask;
}
}
} else { value = "reset"; }
2022-05-14 16:12:24 +03:00
if (IsBool(Boyfriend.DefaultConfig[selectedSetting]) && !IsBool(value)) {
value = value switch {
"y" or "yes" or "д" or "да" => "true",
"n" or "no" or "н" or "нет" => "false",
2022-05-14 16:12:24 +03:00
_ => value
};
if (!IsBool(value)) {
cmd.Reply(Messages.InvalidSettingValue, ":x: ");
2022-05-14 16:12:24 +03:00
return Task.CompletedTask;
}
}
var localizedSelectedSetting = Utils.GetMessage($"Settings{selectedSetting}");
var mention = Utils.ParseMention(value);
if (mention is not 0 && selectedSetting is not "WelcomeMessage") value = mention.ToString();
2022-05-14 16:12:24 +03:00
var formatting = Utils.Wrap("{0}")!;
if (selectedSetting is not "WelcomeMessage") {
if (selectedSetting.EndsWith("Channel")) formatting = "<#{0}>";
if (selectedSetting.EndsWith("Role")) formatting = "<@&{0}>";
}
var formattedValue = selectedSetting switch {
"WelcomeMessage" => Utils.Wrap(Messages.DefaultWelcomeMessage),
"EventStartedReceivers" => Utils.Wrap(Boyfriend.DefaultConfig[selectedSetting])!,
_ => value is "reset" or "default" ? Messages.SettingNotDefined
: IsBool(value) ? YesOrNo(value is "true")
: string.Format(formatting, value)
};
if (value is "reset" or "default") {
config[selectedSetting] = Boyfriend.DefaultConfig[selectedSetting];
} else {
2022-05-14 16:12:24 +03:00
if (value == config[selectedSetting]) {
cmd.Reply(string.Format(Messages.SettingsNothingChanged, localizedSelectedSetting, formattedValue),
":x: ");
2022-05-14 16:12:24 +03:00
return Task.CompletedTask;
}
if (selectedSetting is "Lang" && !Utils.CultureInfoCache.ContainsKey(value)) {
cmd.Reply(Messages.LanguageNotSupported, ":x: ");
2022-05-14 16:12:24 +03:00
return Task.CompletedTask;
}
if (selectedSetting.EndsWith("Channel") && guild.GetTextChannel(mention) is null) {
cmd.Reply(Messages.InvalidChannel, ":x: ");
2022-05-14 16:12:24 +03:00
return Task.CompletedTask;
}
if (selectedSetting.EndsWith("Role") && guild.GetRole(mention) is null) {
cmd.Reply(Messages.InvalidRole, ":x: ");
2022-05-14 16:12:24 +03:00
return Task.CompletedTask;
}
2022-05-14 16:12:24 +03:00
if (selectedSetting is "MuteRole") Utils.RemoveMuteRoleFromCache(ulong.Parse(config[selectedSetting]));
2022-05-14 16:12:24 +03:00
config[selectedSetting] = value;
}
if (selectedSetting is "Lang") {
2022-05-14 16:12:24 +03:00
Utils.SetCurrentLanguage(guild.Id);
localizedSelectedSetting = Utils.GetMessage($"Settings{selectedSetting}");
}
cmd.ConfigWriteScheduled = true;
var replyFormat = string.Format(Messages.FeedbackSettingsUpdated, localizedSelectedSetting, formattedValue);
cmd.Reply(replyFormat, ":control_knobs: ");
cmd.Audit(replyFormat, false);
2022-05-14 16:12:24 +03:00
return Task.CompletedTask;
}
2022-05-14 16:12:24 +03:00
private static string YesOrNo(bool isYes) {
return isYes ? Messages.Yes : Messages.No;
}
2022-05-14 16:12:24 +03:00
private static bool IsBool(string value) {
return value is "true" or "false";
}
}