fixed a few bugs in settings + code style consistency with string comparison

This commit is contained in:
l1ttleO 2022-07-15 21:23:45 +05:00
parent a06376443f
commit 2493e317d0
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
8 changed files with 72 additions and 51 deletions

View file

@ -20,7 +20,7 @@ public class BanCommand : Command {
var author = (SocketGuildUser)context.User; var author = (SocketGuildUser)context.User;
var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.BanMembers); var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.BanMembers);
if (permissionCheckResponse != "") { if (permissionCheckResponse is not "") {
Error(permissionCheckResponse, true); Error(permissionCheckResponse, true);
return; return;
} }
@ -30,7 +30,7 @@ public class BanCommand : Command {
if (memberToBan != null) { if (memberToBan != null) {
var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref memberToBan); var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref memberToBan);
if (interactionCheckResponse != "") { if (interactionCheckResponse is not "") {
Error(interactionCheckResponse, true); Error(interactionCheckResponse, true);
return; return;
} }
@ -41,7 +41,7 @@ public class BanCommand : Command {
Warn(Messages.DurationParseFailed); Warn(Messages.DurationParseFailed);
reason = Utils.JoinString(ref args, 1); reason = Utils.JoinString(ref args, 1);
if (reason == "") { if (reason is "") {
Error(Messages.ReasonRequired, false); Error(Messages.ReasonRequired, false);
return; return;
} }

View file

@ -5,16 +5,16 @@ using Discord.WebSocket;
namespace Boyfriend.Commands; namespace Boyfriend.Commands;
public class ClearCommand : Command { 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 int ArgsLengthRequired => 1;
public override async Task Run(SocketCommandContext context, string[] args) { 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(); if (context.Channel is not SocketTextChannel channel) throw new Exception();
var permissionCheckResponse = CommandHandler.HasPermission(ref user, GuildPermission.ManageMessages); var permissionCheckResponse = CommandHandler.HasPermission(ref user, GuildPermission.ManageMessages);
if (permissionCheckResponse != "") { if (permissionCheckResponse is not "") {
Error(permissionCheckResponse, true); Error(permissionCheckResponse, true);
return; return;
} }

View file

@ -12,7 +12,7 @@ public class KickCommand : Command {
var author = (SocketGuildUser)context.User; var author = (SocketGuildUser)context.User;
var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.KickMembers); var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.KickMembers);
if (permissionCheckResponse != "") { if (permissionCheckResponse is not "") {
Error(permissionCheckResponse, true); Error(permissionCheckResponse, true);
return; return;
} }
@ -25,7 +25,7 @@ public class KickCommand : Command {
} }
var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref toKick); var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref toKick);
if (interactionCheckResponse != "") { if (interactionCheckResponse is not "") {
Error(interactionCheckResponse, true); Error(interactionCheckResponse, true);
return; return;
} }

View file

@ -18,7 +18,7 @@ public class MuteCommand : Command {
Warn(Messages.DurationParseFailed); Warn(Messages.DurationParseFailed);
reason = Utils.JoinString(ref args, 1); reason = Utils.JoinString(ref args, 1);
if (reason == "") { if (reason is "") {
Error(Messages.ReasonRequired, false); Error(Messages.ReasonRequired, false);
return; return;
} }
@ -53,13 +53,13 @@ public class MuteCommand : Command {
var author = (SocketGuildUser)context.User; var author = (SocketGuildUser)context.User;
var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.ModerateMembers); var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.ModerateMembers);
if (permissionCheckResponse != "") { if (permissionCheckResponse is not "") {
Error(permissionCheckResponse, true); Error(permissionCheckResponse, true);
return; return;
} }
var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref toMute); var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref toMute);
if (interactionCheckResponse != "") { if (interactionCheckResponse is not "") {
Error(interactionCheckResponse, true); Error(interactionCheckResponse, true);
return; return;
} }
@ -79,14 +79,15 @@ public class MuteCommand : Command {
var hasDuration = duration.TotalSeconds > 0; var hasDuration = duration.TotalSeconds > 0;
if (role != null) { if (role != null) {
if (config["RemoveRolesOnMute"] == "true") { if (config["RemoveRolesOnMute"] is "true") {
var rolesRemoved = new List<ulong>(); var rolesRemoved = new List<ulong>();
foreach (var userRole in toMute.Roles) foreach (var userRole in toMute.Roles)
try { try {
if (userRole == guild.EveryoneRole || userRole == role) continue; if (userRole == guild.EveryoneRole || userRole == role) continue;
await toMute.RemoveRoleAsync(role); await toMute.RemoveRoleAsync(role);
rolesRemoved.Add(userRole.Id); rolesRemoved.Add(userRole.Id);
} catch (HttpException e) { }
catch (HttpException e) {
Warn(string.Format(Messages.RoleRemovalFailed, $"<@&{userRole}>", Utils.Wrap(e.Reason))); Warn(string.Format(Messages.RoleRemovalFailed, $"<@&{userRole}>", Utils.Wrap(e.Reason)));
} }
@ -104,7 +105,8 @@ public class MuteCommand : Command {
} }
await toMute.AddRoleAsync(role, requestOptions); await toMute.AddRoleAsync(role, requestOptions);
} else { }
else {
if (!hasDuration) { if (!hasDuration) {
Error(Messages.DurationRequiredForTimeOuts, false); Error(Messages.DurationRequiredForTimeOuts, false);
return; return;

View file

@ -12,7 +12,7 @@ public class SettingsCommand : Command {
var author = (SocketGuildUser)context.User; var author = (SocketGuildUser)context.User;
var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.ManageGuild); var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.ManageGuild);
if (permissionCheckResponse != "") { if (permissionCheckResponse is not "") {
Error(permissionCheckResponse, true); Error(permissionCheckResponse, true);
return Task.CompletedTask; return Task.CompletedTask;
} }
@ -32,14 +32,16 @@ public class SettingsCommand : Command {
format = "<#{0}>"; format = "<#{0}>";
else else
currentValue = Messages.ChannelNotSpecified; currentValue = Messages.ChannelNotSpecified;
} else if (setting.Key.EndsWith("Role")) { }
else if (setting.Key.EndsWith("Role")) {
if (guild.GetRole(Convert.ToUInt64(currentValue)) != null) if (guild.GetRole(Convert.ToUInt64(currentValue)) != null)
format = "<@&{0}>"; format = "<@&{0}>";
else else
currentValue = Messages.RoleNotSpecified; currentValue = Messages.RoleNotSpecified;
} else { }
else {
if (IsBool(currentValue)) if (IsBool(currentValue))
currentValue = YesOrNo(currentValue == "true"); currentValue = YesOrNo(currentValue is "true");
else else
format = Utils.Wrap("{0}")!; format = Utils.Wrap("{0}")!;
} }
@ -74,18 +76,23 @@ public class SettingsCommand : Command {
if (args.Length >= 2) { if (args.Length >= 2) {
value = Utils.JoinString(ref args, 1); value = Utils.JoinString(ref args, 1);
if (selectedSetting != "WelcomeMessage") if (selectedSetting is "EventStartedReceivers") {
value = value.Replace(" ", "").ToLower(); value = value.Replace(" ", "").ToLower();
if (value.StartsWith(",") || value.Count(x => x == ',') > 1) { if (value.StartsWith(",") || value.Count(x => x == ',') > 1 ||
(!value.Contains("interested") && !value.Contains("role"))) {
Error(Messages.InvalidSettingValue, false); Error(Messages.InvalidSettingValue, false);
return Task.CompletedTask; return Task.CompletedTask;
} }
} else { value = "reset"; } }
}
else {
value = "reset";
}
if (IsBool(Boyfriend.DefaultConfig[selectedSetting]) && !IsBool(value)) { if (IsBool(Boyfriend.DefaultConfig[selectedSetting]) && !IsBool(value)) {
value = value switch { value = value switch {
"y" or "yes" => "true", "y" or "yes" or "д" or "да" => "true",
"n" or "no" => "false", "n" or "no" or "н" or "нет" => "false",
_ => value _ => value
}; };
if (!IsBool(value)) { if (!IsBool(value)) {
@ -97,26 +104,37 @@ public class SettingsCommand : Command {
var localizedSelectedSetting = Utils.GetMessage($"Settings{selectedSetting}"); var localizedSelectedSetting = Utils.GetMessage($"Settings{selectedSetting}");
var mention = Utils.ParseMention(value); 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}")!; var formatting = Utils.Wrap("{0}")!;
if (selectedSetting is not "WelcomeMessage") {
if (selectedSetting.EndsWith("Channel")) if (selectedSetting.EndsWith("Channel"))
formatting = "<#{0}>"; formatting = "<#{0}>";
if (selectedSetting.EndsWith("Role")) if (selectedSetting.EndsWith("Role"))
formatting = "<@&{0}>"; formatting = "<@&{0}>";
if (value is "0" or "reset" or "default") }
formatting = Messages.SettingNotDefined;
var formattedValue = IsBool(value) ? YesOrNo(value == "true") : string.Format(formatting, value); 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") { if (value is "reset" or "default") {
if (selectedSetting is "WelcomeMessage")
config[selectedSetting] = Messages.DefaultWelcomeMessage;
else
config[selectedSetting] = Boyfriend.DefaultConfig[selectedSetting]; config[selectedSetting] = Boyfriend.DefaultConfig[selectedSetting];
} else { }
else {
if (value == config[selectedSetting]) { if (value == config[selectedSetting]) {
Error(string.Format(Messages.SettingsNothingChanged, localizedSelectedSetting, formattedValue), false); Error(string.Format(Messages.SettingsNothingChanged, localizedSelectedSetting, formattedValue), false);
return Task.CompletedTask; 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); Error(Messages.LanguageNotSupported, false);
return Task.CompletedTask; return Task.CompletedTask;
} }
@ -131,12 +149,12 @@ public class SettingsCommand : Command {
return Task.CompletedTask; 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; config[selectedSetting] = value;
} }
if (selectedSetting == "Lang") { if (selectedSetting is "Lang") {
Utils.SetCurrentLanguage(guild.Id); Utils.SetCurrentLanguage(guild.Id);
localizedSelectedSetting = Utils.GetMessage($"Settings{selectedSetting}"); localizedSelectedSetting = Utils.GetMessage($"Settings{selectedSetting}");
} }

View file

@ -12,7 +12,7 @@ public class UnbanCommand : Command {
var author = (SocketGuildUser)context.User; var author = (SocketGuildUser)context.User;
var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.BanMembers); var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.BanMembers);
if (permissionCheckResponse != "") { if (permissionCheckResponse is not "") {
Error(permissionCheckResponse, true); Error(permissionCheckResponse, true);
return; return;
} }

View file

@ -12,7 +12,7 @@ public class UnmuteCommand : Command {
var author = (SocketGuildUser)context.User; var author = (SocketGuildUser)context.User;
var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.ModerateMembers); var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.ModerateMembers);
if (permissionCheckResponse != "") { if (permissionCheckResponse is not "") {
Error(permissionCheckResponse, true); Error(permissionCheckResponse, true);
return; return;
} }
@ -25,7 +25,7 @@ public class UnmuteCommand : Command {
} }
var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref toUnmute); var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref toUnmute);
if (interactionCheckResponse != "") { if (interactionCheckResponse is not "") {
Error(interactionCheckResponse, true); Error(interactionCheckResponse, true);
return; return;
} }
@ -49,7 +49,8 @@ public class UnmuteCommand : Command {
} }
await toUnmute.RemoveRoleAsync(role, requestOptions); await toUnmute.RemoveRoleAsync(role, requestOptions);
} else { }
else {
if (toUnmute.TimedOutUntil == null || toUnmute.TimedOutUntil.Value.ToUnixTimeMilliseconds() < if (toUnmute.TimedOutUntil == null || toUnmute.TimedOutUntil.Value.ToUnixTimeMilliseconds() <
DateTimeOffset.Now.ToUnixTimeMilliseconds()) { DateTimeOffset.Now.ToUnixTimeMilliseconds()) {
Error(Messages.MemberNotMuted, false); Error(Messages.MemberNotMuted, false);

View file

@ -29,7 +29,7 @@ public class EventHandler {
var channel = guild.GetTextChannel(Convert.ToUInt64(config["BotLogChannel"])); var channel = guild.GetTextChannel(Convert.ToUInt64(config["BotLogChannel"]));
Utils.SetCurrentLanguage(guild.Id); 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))); await channel.SendMessageAsync(string.Format(Messages.Ready, Utils.GetBeep(i)));
} }
} }
@ -112,11 +112,11 @@ public class EventHandler {
var guild = user.Guild; var guild = user.Guild;
var config = Boyfriend.GetGuildConfig(guild.Id); var config = Boyfriend.GetGuildConfig(guild.Id);
if (config["SendWelcomeMessages"] == "true") if (config["SendWelcomeMessages"] is "true")
await Utils.SilentSendAsync(guild.SystemChannel, await Utils.SilentSendAsync(guild.SystemChannel,
string.Format(config["WelcomeMessage"], user.Mention, guild.Name)); 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"])); await user.AddRoleAsync(ulong.Parse(config["StarterRole"]));
} }
@ -147,7 +147,7 @@ public class EventHandler {
var channel = guild.GetTextChannel(Convert.ToUInt64(eventConfig["EventCancelledChannel"])); var channel = guild.GetTextChannel(Convert.ToUInt64(eventConfig["EventCancelledChannel"]));
if (channel != null) if (channel != null)
await channel.SendMessageAsync(string.Format(Messages.EventCancelled, Utils.Wrap(scheduledEvent.Name), 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) { private static async Task ScheduledEventStartedEvent(SocketGuildEvent scheduledEvent) {