This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
OctobotStealth/Boyfriend/Commands/MuteCommand.cs

91 lines
3.6 KiB
C#
Raw Normal View History

using Discord;
2022-02-02 16:14:26 +03:00
using Discord.Net;
2022-05-14 16:12:24 +03:00
using Discord.WebSocket;
namespace Boyfriend.Commands;
public sealed class MuteCommand : ICommand {
public string[] Aliases { get; } = { "mute", "timeout", "заглушить", "мут" };
2022-05-14 16:12:24 +03:00
public async Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
var toMute = cmd.GetMember(args, cleanArgs, 0, "ToMute");
if (toMute == null) return;
2022-05-14 16:12:24 +03:00
var duration = CommandProcessor.GetTimeSpan(args, 1);
var reason = cmd.GetRemaining(args, duration.TotalSeconds < 1 ? 1 : 2, "MuteReason");
if (reason == null) return;
var role = Utils.GetMuteRole(cmd.Context.Guild);
2022-05-14 16:12:24 +03:00
if ((role != null && toMute.Roles.Contains(role))
|| (toMute.TimedOutUntil != null
&& toMute.TimedOutUntil.Value.ToUnixTimeSeconds()
> DateTimeOffset.Now.ToUnixTimeSeconds())) {
cmd.Reply(Messages.MemberAlreadyMuted, ":x: ");
return;
2022-05-14 16:12:24 +03:00
}
2022-02-02 16:14:26 +03:00
var rolesRemoved = Boyfriend.GetRemovedRoles(cmd.Context.Guild.Id);
2022-02-02 16:14:26 +03:00
if (rolesRemoved.TryGetValue(toMute.Id, out var mutedRemovedRoles)) {
foreach (var roleId in mutedRemovedRoles) await toMute.AddRoleAsync(roleId);
rolesRemoved.Remove(toMute.Id);
cmd.ConfigWriteScheduled = true;
cmd.Reply(Messages.RolesReturned, ":warning: ");
2022-05-14 16:12:24 +03:00
}
if (!cmd.HasPermission(GuildPermission.ModerateMembers) || !cmd.CanInteractWith(toMute, "Mute")) return;
2022-05-14 16:12:24 +03:00
await MuteMemberAsync(cmd, toMute, duration, reason);
}
private static async Task MuteMemberAsync(CommandProcessor cmd, SocketGuildUser toMute,
TimeSpan duration, string reason) {
var guild = cmd.Context.Guild;
2022-05-14 16:12:24 +03:00
var config = Boyfriend.GetGuildConfig(guild.Id);
var requestOptions = Utils.GetRequestOptions($"({cmd.Context.User}) {reason}");
var role = Utils.GetMuteRole(guild);
var hasDuration = duration.TotalSeconds > 0;
2022-02-02 16:14:26 +03:00
if (role != null) {
if (config["RemoveRolesOnMute"] is "true") {
2022-02-02 16:14:26 +03:00
var rolesRemoved = new List<ulong>();
2022-05-14 16:12:24 +03:00
foreach (var userRole in toMute.Roles)
2022-02-02 16:14:26 +03:00
try {
2022-05-14 16:12:24 +03:00
if (userRole == guild.EveryoneRole || userRole == role) continue;
await toMute.RemoveRoleAsync(role);
rolesRemoved.Add(userRole.Id);
2022-08-22 17:48:51 +03:00
} catch (HttpException e) {
cmd.Reply(string.Format(Messages.RoleRemovalFailed, $"<@&{userRole}>", Utils.Wrap(e.Reason)),
":warning: ");
}
2022-05-14 16:12:24 +03:00
Boyfriend.GetRemovedRoles(guild.Id).Add(toMute.Id, rolesRemoved.AsReadOnly());
cmd.ConfigWriteScheduled = true;
2022-02-02 16:14:26 +03:00
}
2022-02-02 16:14:26 +03:00
await toMute.AddRoleAsync(role, requestOptions);
if (hasDuration)
await Task.FromResult(Utils.DelayedUnmuteAsync(cmd, toMute, Messages.PunishmentExpired, duration));
2022-08-22 17:48:51 +03:00
} else {
if (!hasDuration || duration.TotalDays > 28) {
cmd.Reply(Messages.DurationRequiredForTimeOuts, ":x: ");
2022-05-14 16:12:24 +03:00
return;
}
2022-05-14 16:12:24 +03:00
if (toMute.IsBot) {
cmd.Reply(Messages.CannotTimeOutBot, ":x: ");
2022-05-14 16:12:24 +03:00
return;
}
2022-02-02 16:14:26 +03:00
await toMute.SetTimeOutAsync(duration, requestOptions);
}
2022-08-22 17:48:51 +03:00
var feedback = string.Format(Messages.FeedbackMemberMuted, toMute.Mention,
Utils.GetHumanizedTimeOffset(duration),
2022-08-22 17:48:51 +03:00
Utils.Wrap(reason));
cmd.Reply(feedback, ":mute: ");
cmd.Audit(feedback);
}
}