From 2493e317d02ec44dc1bcb56d17d6b93d7cfa5080 Mon Sep 17 00:00:00 2001 From: l1ttleO Date: Fri, 15 Jul 2022 21:23:45 +0500 Subject: [PATCH] fixed a few bugs in settings + code style consistency with string comparison --- Boyfriend/Commands/BanCommand.cs | 8 ++-- Boyfriend/Commands/ClearCommand.cs | 6 +-- Boyfriend/Commands/KickCommand.cs | 4 +- Boyfriend/Commands/MuteCommand.cs | 16 ++++--- Boyfriend/Commands/SettingsCommand.cs | 68 +++++++++++++++++---------- Boyfriend/Commands/UnbanCommand.cs | 2 +- Boyfriend/Commands/UnmuteCommand.cs | 9 ++-- Boyfriend/EventHandler.cs | 10 ++-- 8 files changed, 72 insertions(+), 51 deletions(-) diff --git a/Boyfriend/Commands/BanCommand.cs b/Boyfriend/Commands/BanCommand.cs index b2cad58..ce83d35 100644 --- a/Boyfriend/Commands/BanCommand.cs +++ b/Boyfriend/Commands/BanCommand.cs @@ -20,7 +20,7 @@ public class BanCommand : Command { var author = (SocketGuildUser)context.User; var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.BanMembers); - if (permissionCheckResponse != "") { + if (permissionCheckResponse is not "") { Error(permissionCheckResponse, true); return; } @@ -30,7 +30,7 @@ public class BanCommand : Command { if (memberToBan != null) { var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref memberToBan); - if (interactionCheckResponse != "") { + if (interactionCheckResponse is not "") { Error(interactionCheckResponse, true); return; } @@ -41,7 +41,7 @@ public class BanCommand : Command { Warn(Messages.DurationParseFailed); reason = Utils.JoinString(ref args, 1); - if (reason == "") { + if (reason is "") { Error(Messages.ReasonRequired, false); return; } @@ -73,4 +73,4 @@ public class BanCommand : Command { new Task(DelayUnban).Start(); } } -} +} \ No newline at end of file diff --git a/Boyfriend/Commands/ClearCommand.cs b/Boyfriend/Commands/ClearCommand.cs index 504c396..6b51075 100644 --- a/Boyfriend/Commands/ClearCommand.cs +++ b/Boyfriend/Commands/ClearCommand.cs @@ -5,16 +5,16 @@ using Discord.WebSocket; namespace Boyfriend.Commands; public class ClearCommand : Command { - public override string[] Aliases { get; } = {"clear", "purge", "очистить", "стереть"}; + public override string[] Aliases { get; } = { "clear", "purge", "очистить", "стереть" }; public override int ArgsLengthRequired => 1; public override async Task Run(SocketCommandContext context, string[] args) { - var user = (SocketGuildUser) context.User; + var user = (SocketGuildUser)context.User; if (context.Channel is not SocketTextChannel channel) throw new Exception(); var permissionCheckResponse = CommandHandler.HasPermission(ref user, GuildPermission.ManageMessages); - if (permissionCheckResponse != "") { + if (permissionCheckResponse is not "") { Error(permissionCheckResponse, true); return; } diff --git a/Boyfriend/Commands/KickCommand.cs b/Boyfriend/Commands/KickCommand.cs index 505178e..fa1ab32 100644 --- a/Boyfriend/Commands/KickCommand.cs +++ b/Boyfriend/Commands/KickCommand.cs @@ -12,7 +12,7 @@ public class KickCommand : Command { var author = (SocketGuildUser)context.User; var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.KickMembers); - if (permissionCheckResponse != "") { + if (permissionCheckResponse is not "") { Error(permissionCheckResponse, true); return; } @@ -25,7 +25,7 @@ public class KickCommand : Command { } var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref toKick); - if (interactionCheckResponse != "") { + if (interactionCheckResponse is not "") { Error(interactionCheckResponse, true); return; } diff --git a/Boyfriend/Commands/MuteCommand.cs b/Boyfriend/Commands/MuteCommand.cs index 47c3ded..7e9fb13 100644 --- a/Boyfriend/Commands/MuteCommand.cs +++ b/Boyfriend/Commands/MuteCommand.cs @@ -18,7 +18,7 @@ public class MuteCommand : Command { Warn(Messages.DurationParseFailed); reason = Utils.JoinString(ref args, 1); - if (reason == "") { + if (reason is "") { Error(Messages.ReasonRequired, false); return; } @@ -53,13 +53,13 @@ public class MuteCommand : Command { var author = (SocketGuildUser)context.User; var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.ModerateMembers); - if (permissionCheckResponse != "") { + if (permissionCheckResponse is not "") { Error(permissionCheckResponse, true); return; } var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref toMute); - if (interactionCheckResponse != "") { + if (interactionCheckResponse is not "") { Error(interactionCheckResponse, true); return; } @@ -79,14 +79,15 @@ public class MuteCommand : Command { var hasDuration = duration.TotalSeconds > 0; if (role != null) { - if (config["RemoveRolesOnMute"] == "true") { + if (config["RemoveRolesOnMute"] is "true") { var rolesRemoved = new List(); foreach (var userRole in toMute.Roles) try { if (userRole == guild.EveryoneRole || userRole == role) continue; await toMute.RemoveRoleAsync(role); rolesRemoved.Add(userRole.Id); - } catch (HttpException e) { + } + catch (HttpException e) { Warn(string.Format(Messages.RoleRemovalFailed, $"<@&{userRole}>", Utils.Wrap(e.Reason))); } @@ -104,7 +105,8 @@ public class MuteCommand : Command { } await toMute.AddRoleAsync(role, requestOptions); - } else { + } + else { if (!hasDuration) { Error(Messages.DurationRequiredForTimeOuts, false); return; @@ -118,4 +120,4 @@ public class MuteCommand : Command { await toMute.SetTimeOutAsync(duration, requestOptions); } } -} +} \ No newline at end of file diff --git a/Boyfriend/Commands/SettingsCommand.cs b/Boyfriend/Commands/SettingsCommand.cs index cfd1c23..42bfb1c 100644 --- a/Boyfriend/Commands/SettingsCommand.cs +++ b/Boyfriend/Commands/SettingsCommand.cs @@ -12,7 +12,7 @@ public class SettingsCommand : Command { var author = (SocketGuildUser)context.User; var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.ManageGuild); - if (permissionCheckResponse != "") { + if (permissionCheckResponse is not "") { Error(permissionCheckResponse, true); return Task.CompletedTask; } @@ -32,14 +32,16 @@ public class SettingsCommand : Command { format = "<#{0}>"; else currentValue = Messages.ChannelNotSpecified; - } else if (setting.Key.EndsWith("Role")) { + } + else if (setting.Key.EndsWith("Role")) { if (guild.GetRole(Convert.ToUInt64(currentValue)) != null) format = "<@&{0}>"; else currentValue = Messages.RoleNotSpecified; - } else { + } + else { if (IsBool(currentValue)) - currentValue = YesOrNo(currentValue == "true"); + currentValue = YesOrNo(currentValue is "true"); else format = Utils.Wrap("{0}")!; } @@ -74,18 +76,23 @@ public class SettingsCommand : Command { if (args.Length >= 2) { value = Utils.JoinString(ref args, 1); - if (selectedSetting != "WelcomeMessage") + if (selectedSetting is "EventStartedReceivers") { value = value.Replace(" ", "").ToLower(); - if (value.StartsWith(",") || value.Count(x => x == ',') > 1) { - Error(Messages.InvalidSettingValue, false); - return Task.CompletedTask; + if (value.StartsWith(",") || value.Count(x => x == ',') > 1 || + (!value.Contains("interested") && !value.Contains("role"))) { + Error(Messages.InvalidSettingValue, false); + return Task.CompletedTask; + } } - } else { value = "reset"; } + } + else { + value = "reset"; + } if (IsBool(Boyfriend.DefaultConfig[selectedSetting]) && !IsBool(value)) { value = value switch { - "y" or "yes" => "true", - "n" or "no" => "false", + "y" or "yes" or "д" or "да" => "true", + "n" or "no" or "н" or "нет" => "false", _ => value }; if (!IsBool(value)) { @@ -97,26 +104,37 @@ public class SettingsCommand : Command { var localizedSelectedSetting = Utils.GetMessage($"Settings{selectedSetting}"); var mention = Utils.ParseMention(value); - if (mention != 0) value = mention.ToString(); + if (mention != 0 && selectedSetting is not "WelcomeMessage") value = mention.ToString(); var formatting = Utils.Wrap("{0}")!; - if (selectedSetting.EndsWith("Channel")) - formatting = "<#{0}>"; - if (selectedSetting.EndsWith("Role")) - formatting = "<@&{0}>"; - if (value is "0" or "reset" or "default") - formatting = Messages.SettingNotDefined; - var formattedValue = IsBool(value) ? YesOrNo(value == "true") : string.Format(formatting, value); + 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" + ? IsBool(value) ? YesOrNo(value is "true") : string.Format(formatting, value) + : Messages.SettingNotDefined + }; if (value is "reset" or "default") { - config[selectedSetting] = Boyfriend.DefaultConfig[selectedSetting]; - } else { + if (selectedSetting is "WelcomeMessage") + config[selectedSetting] = Messages.DefaultWelcomeMessage; + else + config[selectedSetting] = Boyfriend.DefaultConfig[selectedSetting]; + } + else { if (value == config[selectedSetting]) { Error(string.Format(Messages.SettingsNothingChanged, localizedSelectedSetting, formattedValue), false); return Task.CompletedTask; } - if (selectedSetting == "Lang" && value is not "ru" and not "en") { + if (selectedSetting is "Lang" && value is not "ru" and not "en") { Error(Messages.LanguageNotSupported, false); return Task.CompletedTask; } @@ -131,12 +149,12 @@ public class SettingsCommand : Command { return Task.CompletedTask; } - if (selectedSetting == "MuteRole") Utils.RemoveMuteRoleFromCache(ulong.Parse(config[selectedSetting])); + if (selectedSetting is "MuteRole") Utils.RemoveMuteRoleFromCache(ulong.Parse(config[selectedSetting])); config[selectedSetting] = value; } - if (selectedSetting == "Lang") { + if (selectedSetting is "Lang") { Utils.SetCurrentLanguage(guild.Id); localizedSelectedSetting = Utils.GetMessage($"Settings{selectedSetting}"); } @@ -155,4 +173,4 @@ public class SettingsCommand : Command { private static bool IsBool(string value) { return value is "true" or "false"; } -} \ No newline at end of file +} diff --git a/Boyfriend/Commands/UnbanCommand.cs b/Boyfriend/Commands/UnbanCommand.cs index a6576af..ac2edef 100644 --- a/Boyfriend/Commands/UnbanCommand.cs +++ b/Boyfriend/Commands/UnbanCommand.cs @@ -12,7 +12,7 @@ public class UnbanCommand : Command { var author = (SocketGuildUser)context.User; var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.BanMembers); - if (permissionCheckResponse != "") { + if (permissionCheckResponse is not "") { Error(permissionCheckResponse, true); return; } diff --git a/Boyfriend/Commands/UnmuteCommand.cs b/Boyfriend/Commands/UnmuteCommand.cs index 5659e17..a5e8d76 100644 --- a/Boyfriend/Commands/UnmuteCommand.cs +++ b/Boyfriend/Commands/UnmuteCommand.cs @@ -12,7 +12,7 @@ public class UnmuteCommand : Command { var author = (SocketGuildUser)context.User; var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.ModerateMembers); - if (permissionCheckResponse != "") { + if (permissionCheckResponse is not "") { Error(permissionCheckResponse, true); return; } @@ -25,7 +25,7 @@ public class UnmuteCommand : Command { } var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref toUnmute); - if (interactionCheckResponse != "") { + if (interactionCheckResponse is not "") { Error(interactionCheckResponse, true); return; } @@ -49,7 +49,8 @@ public class UnmuteCommand : Command { } await toUnmute.RemoveRoleAsync(role, requestOptions); - } else { + } + else { if (toUnmute.TimedOutUntil == null || toUnmute.TimedOutUntil.Value.ToUnixTimeMilliseconds() < DateTimeOffset.Now.ToUnixTimeMilliseconds()) { Error(Messages.MemberNotMuted, false); @@ -63,4 +64,4 @@ public class UnmuteCommand : Command { Success(feedback, author.Mention, false, false); await Utils.SendFeedback(feedback, guild.Id, author.Mention, true); } -} +} \ No newline at end of file diff --git a/Boyfriend/EventHandler.cs b/Boyfriend/EventHandler.cs index f562d38..4b09fd7 100644 --- a/Boyfriend/EventHandler.cs +++ b/Boyfriend/EventHandler.cs @@ -29,7 +29,7 @@ public class EventHandler { var channel = guild.GetTextChannel(Convert.ToUInt64(config["BotLogChannel"])); Utils.SetCurrentLanguage(guild.Id); - if (config["ReceiveStartupMessages"] != "true" || channel == null) continue; + if (config["ReceiveStartupMessages"] is not "true" || channel == null) continue; await channel.SendMessageAsync(string.Format(Messages.Ready, Utils.GetBeep(i))); } } @@ -112,11 +112,11 @@ public class EventHandler { var guild = user.Guild; var config = Boyfriend.GetGuildConfig(guild.Id); - if (config["SendWelcomeMessages"] == "true") + if (config["SendWelcomeMessages"] is "true") await Utils.SilentSendAsync(guild.SystemChannel, string.Format(config["WelcomeMessage"], user.Mention, guild.Name)); - if (config["StarterRole"] != "0") + if (config["StarterRole"] is not "0") await user.AddRoleAsync(ulong.Parse(config["StarterRole"])); } @@ -147,7 +147,7 @@ public class EventHandler { var channel = guild.GetTextChannel(Convert.ToUInt64(eventConfig["EventCancelledChannel"])); if (channel != null) await channel.SendMessageAsync(string.Format(Messages.EventCancelled, Utils.Wrap(scheduledEvent.Name), - eventConfig["FrowningFace"] == "true" ? $" {Messages.SettingsFrowningFace}" : "")); + eventConfig["FrowningFace"] is "true" ? $" {Messages.SettingsFrowningFace}" : "")); } private static async Task ScheduledEventStartedEvent(SocketGuildEvent scheduledEvent) { @@ -180,4 +180,4 @@ public class EventHandler { await channel.SendMessageAsync(string.Format(Messages.EventCompleted, Utils.Wrap(scheduledEvent.Name), Utils.Wrap(scheduledEvent.StartTime.Subtract(DateTimeOffset.Now).Negate().ToString()))); } -} +} \ No newline at end of file