1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-05-06 14:06:29 +03:00

another two or three refactors

This commit is contained in:
l1ttleO 2022-05-14 18:12:24 +05:00
parent 868b6bcaa7
commit 790f77aa49
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
21 changed files with 1447 additions and 944 deletions

View file

@ -1,117 +1,122 @@
using Discord;
using Discord.Commands;
using Discord.Net;
// ReSharper disable UnusedType.Global
// ReSharper disable UnusedMember.Global
// ReSharper disable ClassNeverInstantiated.Global
using Discord.WebSocket;
namespace Boyfriend.Commands;
public class MuteCommand : Command {
public override async Task Run(SocketCommandContext context, string[] args) {
var author = context.Guild.GetUser(context.User.Id);
var config = Boyfriend.GetGuildConfig(context.Guild);
var reason = Utils.JoinString(args, 1);
var role = Utils.GetMuteRole(context.Guild);
var rolesRemoved = config.RolesRemovedOnMute!;
var toMute = await Utils.ParseMember(context.Guild, args[0]);
public override string[] Aliases { get; } = {"mute", "timeout", "заглушить", "мут"};
public override int ArgsLengthRequired => 2;
TimeSpan duration;
try {
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);
public override async Task Run(SocketCommandContext context, string[] args) {
var toMute = Utils.ParseMember(context.Guild, args[0]);
var reason = Utils.JoinString(ref args, 2);
var duration = Utils.GetTimeSpan(ref args[1]) ?? TimeSpan.FromMilliseconds(-1);
if (duration.TotalSeconds < 0) {
Warn(Messages.DurationParseFailed);
reason = Utils.JoinString(ref args, 1);
}
if (toMute == null)
throw new ApplicationException(Messages.UserNotInGuild);
if (toMute == null) {
Error(Messages.UserNotInGuild, false);
return;
}
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);
var guild = context.Guild;
var role = Utils.GetMuteRole(ref guild);
if (role != null) {
var hasMuteRole = false;
foreach (var x in toMute.Roles) {
if (x != role) continue;
hasMuteRole = true;
break;
}
if (hasMuteRole || (toMute.TimedOutUntil != null && toMute.TimedOutUntil.Value.ToUnixTimeMilliseconds() >
DateTimeOffset.Now.ToUnixTimeMilliseconds())) {
Error(Messages.MemberAlreadyMuted, false);
return;
}
}
var rolesRemoved = Boyfriend.GetRemovedRoles(context.Guild.Id);
if (rolesRemoved.ContainsKey(toMute.Id)) {
foreach (var roleId in rolesRemoved[toMute.Id]) await toMute.AddRoleAsync(roleId);
rolesRemoved.Remove(toMute.Id);
await config.Save();
throw new ApplicationException(Messages.RolesReturned);
CommandHandler.ConfigWriteScheduled = true;
Warn(Messages.RolesReturned);
}
await CommandHandler.CheckPermissions(author, GuildPermission.ModerateMembers, GuildPermission.ManageRoles);
await CommandHandler.CheckInteractions(author, toMute);
var author = (SocketGuildUser) context.User;
await MuteMember(context.Guild, context.Channel as ITextChannel, context.Guild.GetUser(context.User.Id), toMute,
duration, reason);
var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.ModerateMembers);
if (permissionCheckResponse != "") {
Error(permissionCheckResponse, true);
return;
}
var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref toMute);
if (interactionCheckResponse != "") {
Error(interactionCheckResponse, true);
return;
}
await MuteMember(guild, author, toMute, duration, reason);
Success(
string.Format(Messages.FeedbackMemberMuted, toMute.Mention, Utils.GetHumanizedTimeOffset(ref duration),
Utils.WrapInline(reason)), author.Mention, true);
}
private static async Task MuteMember(IGuild guild, ITextChannel? channel, IGuildUser author, IGuildUser toMute,
private static async Task MuteMember(SocketGuild guild, SocketUser author, SocketGuildUser toMute,
TimeSpan duration, string reason) {
await CommandHandler.CheckPermissions(author, GuildPermission.ManageMessages, GuildPermission.ManageRoles);
var authorMention = author.Mention;
var config = Boyfriend.GetGuildConfig(guild);
var requestOptions = Utils.GetRequestOptions($"({Utils.GetNameAndDiscrim(author)}) {reason}");
var role = Utils.GetMuteRole(guild);
var config = Boyfriend.GetGuildConfig(guild.Id);
var requestOptions = Utils.GetRequestOptions($"({author}) {reason}");
var role = Utils.GetMuteRole(ref guild);
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);
if (role != null) {
if (config.RemoveRolesOnMute.GetValueOrDefault(false)) {
if (config["RemoveRolesOnMute"] == "true") {
var rolesRemoved = new List<ulong>();
foreach (var roleId in toMute.RoleIds) {
foreach (var userRole in toMute.Roles)
try {
if (roleId == guild.Id) continue;
if (roleId == role.Id) continue;
await toMute.RemoveRoleAsync(roleId);
rolesRemoved.Add(roleId);
if (userRole == guild.EveryoneRole || userRole == role) continue;
await toMute.RemoveRoleAsync(role);
rolesRemoved.Add(userRole.Id);
} catch (HttpException e) {
await Warn(channel,
string.Format(Messages.RoleRemovalFailed, $"<@&{roleId}>", Utils.WrapInline(e.Reason)));
Warn(string.Format(Messages.RoleRemovalFailed, $"<@&{userRole}>", Utils.WrapInline(e.Reason)));
}
}
config.RolesRemovedOnMute!.Add(toMute.Id, rolesRemoved);
await config.Save();
Boyfriend.GetRemovedRoles(guild.Id).Add(toMute.Id, rolesRemoved.AsReadOnly());
CommandHandler.ConfigWriteScheduled = true;
if (hasDuration)
await Task.Run(async () => {
if (hasDuration) {
async void DelayUnmute() {
await Task.Delay(duration);
try {
await UnmuteCommand.UnmuteMember(guild, null, await guild.GetCurrentUserAsync(), toMute,
Messages.PunishmentExpired);
} catch (ApplicationException) {}
});
await UnmuteCommand.UnmuteMember(guild, guild.CurrentUser, toMute, Messages.PunishmentExpired);
}
var task = new Task(DelayUnmute);
task.Start();
}
}
await toMute.AddRoleAsync(role, requestOptions);
} else {
if (!hasDuration)
throw new ApplicationException(Messages.DurationRequiredForTimeOuts);
if (toMute.IsBot)
throw new ApplicationException(Messages.CannotTimeOutBot);
if (!hasDuration) {
Error(Messages.DurationRequiredForTimeOuts, false);
return;
}
if (toMute.IsBot) {
Error(Messages.CannotTimeOutBot, false);
return;
}
await toMute.SetTimeOutAsync(duration, requestOptions);
}
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);
}
public override List<string> GetAliases() {
return new List<string> {"mute", "мут", "мьют"};
}
public override int GetArgumentsAmountRequired() {
return 2;
}
public override string GetSummary() {
return "Глушит участника";
}
}