mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-01-31 00:19:00 +03:00
fixed a few bugs in settings + code style consistency with string comparison
This commit is contained in:
parent
a06376443f
commit
2493e317d0
8 changed files with 72 additions and 51 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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<ulong>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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())));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue