mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-05-04 21:16:29 +03:00
time-out failsafes and new warnings
rewrote setting values in SettingsCommand.cs fixed a bug with message edited notification on mobile fixed an exploit with WrapInline where you could escape the code block by simply using ` moved a few things in MuteCommand.cs cleaned up code updated library to 3.3.2
This commit is contained in:
parent
e41a459f6f
commit
868b6bcaa7
12 changed files with 220 additions and 345 deletions
|
@ -16,6 +16,7 @@ public class BanCommand : Command {
|
|||
duration = Utils.GetTimeSpan(args[1]);
|
||||
reason = Utils.JoinString(args, 2);
|
||||
} catch (Exception e) when (e is ArgumentNullException or FormatException or OverflowException) {
|
||||
await Warn(context.Channel as ITextChannel, Messages.DurationParseFailed);
|
||||
duration = TimeSpan.FromMilliseconds(-1);
|
||||
}
|
||||
|
||||
|
@ -28,10 +29,8 @@ public class BanCommand : Command {
|
|||
var authorMention = author.Mention;
|
||||
var guildBanMessage = $"({Utils.GetNameAndDiscrim(author)}) {reason}";
|
||||
var memberToBan = await guild.GetUserAsync(toBan.Id);
|
||||
var expiresIn = duration.TotalSeconds > 0
|
||||
? string.Format(Messages.PunishmentExpiresIn, Environment.NewLine,
|
||||
DateTimeOffset.Now.ToUnixTimeSeconds() + duration.TotalSeconds)
|
||||
: "";
|
||||
var expiresIn = duration.TotalSeconds > 0 ? string.Format(Messages.PunishmentExpiresIn, Environment.NewLine,
|
||||
DateTimeOffset.Now.ToUnixTimeSeconds() + duration.TotalSeconds) : "";
|
||||
var notification = string.Format(Messages.UserBanned, authorMention, toBan.Mention, Utils.WrapInline(reason),
|
||||
expiresIn);
|
||||
|
||||
|
@ -39,25 +38,24 @@ public class BanCommand : Command {
|
|||
if (memberToBan != null)
|
||||
await CommandHandler.CheckInteractions(author, memberToBan);
|
||||
|
||||
await Utils.SendDirectMessage(toBan, string.Format(Messages.YouWereBanned, author.Mention, guild.Name,
|
||||
Utils.WrapInline(reason)));
|
||||
await Utils.SendDirectMessage(toBan,
|
||||
string.Format(Messages.YouWereBanned, author.Mention, guild.Name, Utils.WrapInline(reason)));
|
||||
|
||||
await guild.AddBanAsync(toBan, 0, guildBanMessage);
|
||||
|
||||
await Utils.SilentSendAsync(channel, string.Format(Messages.BanResponse, toBan.Mention,
|
||||
Utils.WrapInline(reason)));
|
||||
await Utils.SilentSendAsync(channel,
|
||||
string.Format(Messages.BanResponse, toBan.Mention, Utils.WrapInline(reason)));
|
||||
await Utils.SilentSendAsync(await guild.GetSystemChannelAsync(), notification);
|
||||
await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(guild), notification);
|
||||
|
||||
if (duration.TotalSeconds > 0) {
|
||||
var task = new Task(async () => {
|
||||
await Task.Run(async () => {
|
||||
await Task.Delay(duration);
|
||||
try {
|
||||
await UnbanCommand.UnbanUser(guild, null, await guild.GetCurrentUserAsync(), toBan,
|
||||
Messages.PunishmentExpired);
|
||||
} catch (ApplicationException) {}
|
||||
});
|
||||
task.Start();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -72,4 +70,4 @@ public class BanCommand : Command {
|
|||
public override string GetSummary() {
|
||||
return "Банит пользователя";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
|
@ -13,7 +12,7 @@ public abstract class Command {
|
|||
|
||||
public abstract string GetSummary();
|
||||
|
||||
protected static async Task Warn(ISocketMessageChannel channel, string warning) {
|
||||
await Utils.SilentSendAsync(channel as ITextChannel, ":warning: " + warning);
|
||||
protected static async Task Warn(ITextChannel? channel, string warning) {
|
||||
await Utils.SilentSendAsync(channel, ":warning: " + warning);
|
||||
}
|
||||
}
|
|
@ -22,23 +22,22 @@ public class MuteCommand : Command {
|
|||
duration = Utils.GetTimeSpan(args[1]);
|
||||
reason = Utils.JoinString(args, 2);
|
||||
} catch (Exception e) when (e is ArgumentNullException or FormatException or OverflowException) {
|
||||
await Warn(context.Channel as ITextChannel, Messages.DurationParseFailed);
|
||||
duration = TimeSpan.FromMilliseconds(-1);
|
||||
}
|
||||
|
||||
if (toMute == null)
|
||||
throw new ApplicationException(Messages.UserNotInGuild);
|
||||
|
||||
if (role != null && toMute.RoleIds.Any(x => x == role.Id) ||
|
||||
toMute.TimedOutUntil != null && toMute.TimedOutUntil.Value.ToUnixTimeMilliseconds()
|
||||
> DateTimeOffset.Now.ToUnixTimeMilliseconds())
|
||||
if (role != null && toMute.RoleIds.Any(x => x == role.Id) || toMute.TimedOutUntil != null &&
|
||||
toMute.TimedOutUntil.Value.ToUnixTimeMilliseconds() > DateTimeOffset.Now.ToUnixTimeMilliseconds())
|
||||
throw new ApplicationException(Messages.MemberAlreadyMuted);
|
||||
|
||||
if (rolesRemoved.ContainsKey(toMute.Id)) {
|
||||
foreach (var roleId in rolesRemoved[toMute.Id]) await toMute.AddRoleAsync(roleId);
|
||||
rolesRemoved.Remove(toMute.Id);
|
||||
await config.Save();
|
||||
await Warn(context.Channel, Messages.RolesReturned);
|
||||
return;
|
||||
throw new ApplicationException(Messages.RolesReturned);
|
||||
}
|
||||
|
||||
await CommandHandler.CheckPermissions(author, GuildPermission.ModerateMembers, GuildPermission.ManageRoles);
|
||||
|
@ -55,10 +54,9 @@ public class MuteCommand : Command {
|
|||
var config = Boyfriend.GetGuildConfig(guild);
|
||||
var requestOptions = Utils.GetRequestOptions($"({Utils.GetNameAndDiscrim(author)}) {reason}");
|
||||
var role = Utils.GetMuteRole(guild);
|
||||
var expiresIn = duration.TotalSeconds > 0
|
||||
? string.Format(Messages.PunishmentExpiresIn, Environment.NewLine,
|
||||
DateTimeOffset.Now.ToUnixTimeSeconds() + duration.TotalSeconds)
|
||||
: "";
|
||||
var hasDuration = duration.TotalSeconds > 0;
|
||||
var expiresIn = hasDuration ? string.Format(Messages.PunishmentExpiresIn, Environment.NewLine,
|
||||
DateTimeOffset.Now.ToUnixTimeSeconds() + duration.TotalSeconds) : "";
|
||||
var notification = string.Format(Messages.MemberMuted, authorMention, toMute.Mention, Utils.WrapInline(reason),
|
||||
expiresIn);
|
||||
|
||||
|
@ -71,31 +69,38 @@ public class MuteCommand : Command {
|
|||
if (roleId == role.Id) continue;
|
||||
await toMute.RemoveRoleAsync(roleId);
|
||||
rolesRemoved.Add(roleId);
|
||||
} catch (HttpException) {}
|
||||
} catch (HttpException e) {
|
||||
await Warn(channel,
|
||||
string.Format(Messages.RoleRemovalFailed, $"<@&{roleId}>", Utils.WrapInline(e.Reason)));
|
||||
}
|
||||
}
|
||||
|
||||
config.RolesRemovedOnMute!.Add(toMute.Id, rolesRemoved);
|
||||
await config.Save();
|
||||
|
||||
if (hasDuration)
|
||||
await Task.Run(async () => {
|
||||
await Task.Delay(duration);
|
||||
try {
|
||||
await UnmuteCommand.UnmuteMember(guild, null, await guild.GetCurrentUserAsync(), toMute,
|
||||
Messages.PunishmentExpired);
|
||||
} catch (ApplicationException) {}
|
||||
});
|
||||
}
|
||||
|
||||
await toMute.AddRoleAsync(role, requestOptions);
|
||||
} else
|
||||
} else {
|
||||
if (!hasDuration)
|
||||
throw new ApplicationException(Messages.DurationRequiredForTimeOuts);
|
||||
if (toMute.IsBot)
|
||||
throw new ApplicationException(Messages.CannotTimeOutBot);
|
||||
|
||||
await toMute.SetTimeOutAsync(duration, requestOptions);
|
||||
await Utils.SilentSendAsync(channel, string.Format(Messages.MuteResponse, toMute.Mention,
|
||||
Utils.WrapInline(reason)));
|
||||
}
|
||||
await Utils.SilentSendAsync(channel,
|
||||
string.Format(Messages.MuteResponse, toMute.Mention, Utils.WrapInline(reason)));
|
||||
await Utils.SilentSendAsync(await guild.GetSystemChannelAsync(), notification);
|
||||
await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(guild), notification);
|
||||
|
||||
if (role != null && duration.TotalSeconds > 0) {
|
||||
var task = new Task(async () => {
|
||||
await Task.Delay(duration);
|
||||
try {
|
||||
await UnmuteCommand.UnmuteMember(guild, null, await guild.GetCurrentUserAsync(), toMute,
|
||||
Messages.PunishmentExpired);
|
||||
} catch (ApplicationException) {}
|
||||
});
|
||||
task.Start();
|
||||
}
|
||||
}
|
||||
|
||||
public override List<string> GetAliases() {
|
||||
|
@ -109,4 +114,4 @@ public class MuteCommand : Command {
|
|||
public override string GetSummary() {
|
||||
return "Глушит участника";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
|
||||
|
@ -16,117 +16,83 @@ public class SettingsCommand : Command {
|
|||
|
||||
if (args.Length == 0) {
|
||||
var nl = Environment.NewLine;
|
||||
var adminLogChannel = guild.GetTextChannel(config.AdminLogChannel.GetValueOrDefault(0));
|
||||
var admin = adminLogChannel == null ? Messages.ChannelNotSpecified : adminLogChannel.Mention;
|
||||
var botLogChannel = guild.GetTextChannel(config.BotLogChannel.GetValueOrDefault(0));
|
||||
var bot = botLogChannel == null ? Messages.ChannelNotSpecified : botLogChannel.Mention;
|
||||
var muteRole = guild.GetRole(config.MuteRole.GetValueOrDefault(0));
|
||||
var mute = muteRole == null ? Messages.RoleNotSpecified : muteRole.Mention;
|
||||
var defaultRole = guild.GetRole(config.DefaultRole.GetValueOrDefault(0));
|
||||
var defaultr = defaultRole == null ? Messages.RoleNotSpecified : defaultRole.Mention;
|
||||
dynamic forCheck;
|
||||
var adminLogChannel = (forCheck = guild.GetTextChannel(config.AdminLogChannel.GetValueOrDefault(0))) == null
|
||||
? Messages.ChannelNotSpecified : forCheck.Mention;
|
||||
var botLogChannel = (forCheck = guild.GetTextChannel(config.BotLogChannel.GetValueOrDefault(0))) == null
|
||||
? Messages.ChannelNotSpecified : forCheck.Mention;
|
||||
var muteRole = (forCheck = guild.GetRole(config.MuteRole.GetValueOrDefault(0))) == null
|
||||
? Messages.RoleNotSpecified : forCheck.Mention;
|
||||
var defaultRole = (forCheck = guild.GetRole(config.DefaultRole.GetValueOrDefault(0))) == null
|
||||
? Messages.RoleNotSpecified : forCheck.Mention;
|
||||
var toSend = string.Format(Messages.CurrentSettings, nl) +
|
||||
string.Format(Messages.CurrentSettingsLang, config.Lang, nl) +
|
||||
string.Format(Messages.CurrentSettingsPrefix, config.Prefix, nl) +
|
||||
string.Format(Messages.CurrentSettingsRemoveRoles, YesOrNo(
|
||||
config.RemoveRolesOnMute.GetValueOrDefault(false)), nl) +
|
||||
string.Format(Messages.CurrentSettingsUseSystemChannel, YesOrNo(
|
||||
config.UseSystemChannel.GetValueOrDefault(true)), nl) +
|
||||
string.Format(Messages.CurrentSettingsSendWelcomeMessages, YesOrNo(
|
||||
config.SendWelcomeMessages.GetValueOrDefault(true)), nl) +
|
||||
string.Format(Messages.CurrentSettingsReceiveStartupMessages, YesOrNo(
|
||||
config.ReceiveStartupMessages.GetValueOrDefault(true)), nl) +
|
||||
string.Format(Messages.CurrentSettingsRemoveRoles,
|
||||
YesOrNo(config.RemoveRolesOnMute.GetValueOrDefault(false)), nl) +
|
||||
string.Format(Messages.CurrentSettingsUseSystemChannel,
|
||||
YesOrNo(config.UseSystemChannel.GetValueOrDefault(true)), nl) +
|
||||
string.Format(Messages.CurrentSettingsSendWelcomeMessages,
|
||||
YesOrNo(config.SendWelcomeMessages.GetValueOrDefault(true)), nl) +
|
||||
string.Format(Messages.CurrentSettingsReceiveStartupMessages,
|
||||
YesOrNo(config.ReceiveStartupMessages.GetValueOrDefault(true)), nl) +
|
||||
string.Format(Messages.CurrentSettingsWelcomeMessage, config.WelcomeMessage, nl) +
|
||||
string.Format(Messages.CurrentSettingsDefaultRole, defaultr, nl) +
|
||||
string.Format(Messages.CurrentSettingsMuteRole, mute, nl) +
|
||||
string.Format(Messages.CurrentSettingsAdminLogChannel, admin, nl) +
|
||||
string.Format(Messages.CurrentSettingsBotLogChannel, bot);
|
||||
string.Format(Messages.CurrentSettingsDefaultRole, defaultRole, nl) +
|
||||
string.Format(Messages.CurrentSettingsMuteRole, muteRole, nl) +
|
||||
string.Format(Messages.CurrentSettingsAdminLogChannel, adminLogChannel, nl) +
|
||||
string.Format(Messages.CurrentSettingsBotLogChannel, botLogChannel);
|
||||
await Utils.SilentSendAsync(context.Channel as ITextChannel ?? throw new ApplicationException(), toSend);
|
||||
return;
|
||||
}
|
||||
|
||||
var setting = args[0].ToLower();
|
||||
var shouldDefault = false;
|
||||
var value = "";
|
||||
|
||||
if (args.Length >= 2)
|
||||
value = args[1].ToLower();
|
||||
else
|
||||
shouldDefault = true;
|
||||
try {
|
||||
value = args[1].ToLower();
|
||||
} catch (IndexOutOfRangeException) {
|
||||
throw new ApplicationException(Messages.InvalidSettingValue);
|
||||
}
|
||||
|
||||
var boolValue = ParseBool(value);
|
||||
var channel = await Utils.ParseChannelNullable(value) as IGuildChannel;
|
||||
var role = Utils.ParseRoleNullable(guild, value);
|
||||
PropertyInfo? property = null;
|
||||
foreach (var prop in typeof(GuildConfig).GetProperties())
|
||||
if (setting == prop.Name.ToLower())
|
||||
property = prop;
|
||||
if (property == null || !property.CanWrite)
|
||||
throw new ApplicationException(Messages.SettingDoesntExist);
|
||||
var type = property.PropertyType;
|
||||
|
||||
switch (setting) {
|
||||
case "reset":
|
||||
Boyfriend.ResetGuildConfig(guild);
|
||||
break;
|
||||
case "lang" when value is not ("ru" or "en"):
|
||||
if (value is "reset" or "default") {
|
||||
property.SetValue(config, null);
|
||||
} else if (type == typeof(string)) {
|
||||
if (setting == "lang" && value is not ("ru" or "en"))
|
||||
throw new ApplicationException(Messages.LanguageNotSupported);
|
||||
case "lang":
|
||||
config.Lang = shouldDefault ? "ru" : value;
|
||||
Messages.Culture = new CultureInfo(shouldDefault ? "ru" : value);
|
||||
break;
|
||||
case "prefix":
|
||||
config.Prefix = shouldDefault ? "!" : value;
|
||||
break;
|
||||
case "removerolesonmute":
|
||||
config.RemoveRolesOnMute = !shouldDefault && GetBoolValue(boolValue);
|
||||
break;
|
||||
case "usesystemchannel":
|
||||
config.UseSystemChannel = shouldDefault || GetBoolValue(boolValue);
|
||||
break;
|
||||
case "sendwelcomemessages":
|
||||
config.SendWelcomeMessages = shouldDefault || GetBoolValue(boolValue);
|
||||
break;
|
||||
case "receivestartupmessages":
|
||||
config.ReceiveStartupMessages = shouldDefault || GetBoolValue(boolValue);
|
||||
break;
|
||||
case "welcomemessage":
|
||||
config.WelcomeMessage = shouldDefault ? Messages.DefaultWelcomeMessage : value;
|
||||
break;
|
||||
case "defaultrole":
|
||||
config.DefaultRole = shouldDefault ? 0 : GetRoleId(role);
|
||||
break;
|
||||
case "muterole":
|
||||
config.MuteRole = shouldDefault ? 0 : GetRoleId(role);
|
||||
break;
|
||||
case "adminlogchannel":
|
||||
config.AdminLogChannel = shouldDefault ? 0 : GetChannelId(channel);
|
||||
break;
|
||||
case "botlogchannel":
|
||||
config.BotLogChannel = shouldDefault ? 0 : GetChannelId(channel);
|
||||
break;
|
||||
default:
|
||||
await context.Channel.SendMessageAsync(Messages.SettingDoesntExist);
|
||||
return;
|
||||
property.SetValue(config, value);
|
||||
} else {
|
||||
try {
|
||||
if (type == typeof(bool?))
|
||||
property.SetValue(config, Convert.ToBoolean(value));
|
||||
|
||||
if (type == typeof(ulong?)) {
|
||||
var id = Convert.ToUInt64(value);
|
||||
if (property.Name.EndsWith("Channel") && guild.GetTextChannel(id) == null)
|
||||
throw new ApplicationException(Messages.InvalidChannel);
|
||||
if (property.Name.EndsWith("Role") && guild.GetRole(id) == null)
|
||||
throw new ApplicationException(Messages.InvalidRole);
|
||||
|
||||
property.SetValue(config, id);
|
||||
}
|
||||
} catch (Exception e) when (e is FormatException or OverflowException) {
|
||||
throw new ApplicationException(Messages.InvalidSettingValue);
|
||||
}
|
||||
}
|
||||
config.Validate();
|
||||
|
||||
await config.Save();
|
||||
|
||||
await context.Channel.SendMessageAsync(Messages.SettingsUpdated);
|
||||
}
|
||||
|
||||
private static bool? ParseBool(string toParse) {
|
||||
try {
|
||||
return bool.Parse(toParse.ToLower());
|
||||
} catch (FormatException) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool GetBoolValue(bool? from) {
|
||||
return from ?? throw new ApplicationException(Messages.InvalidBoolean);
|
||||
}
|
||||
|
||||
private static ulong GetRoleId(IRole? role) {
|
||||
return (role ?? throw new ApplicationException(Messages.InvalidRoleSpecified)).Id;
|
||||
}
|
||||
|
||||
private static ulong GetChannelId(IGuildChannel? channel) {
|
||||
return (channel ?? throw new ApplicationException(Messages.InvalidChannelSpecified)).Id;
|
||||
}
|
||||
|
||||
private static string YesOrNo(bool isYes) {
|
||||
return isYes ? Messages.Yes : Messages.No;
|
||||
}
|
||||
|
@ -142,4 +108,4 @@ public class SettingsCommand : Command {
|
|||
public override string GetSummary() {
|
||||
return "Настраивает бота отдельно для этого сервера";
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue