2022-01-18 20:38:15 +03:00
|
|
|
|
using Discord;
|
2022-02-02 16:14:26 +03:00
|
|
|
|
using Discord.Net;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
using Discord.WebSocket;
|
2022-01-18 20:38:15 +03:00
|
|
|
|
|
|
|
|
|
namespace Boyfriend.Commands;
|
|
|
|
|
|
|
|
|
|
public class MuteCommand : Command {
|
2022-06-06 18:39:47 +03:00
|
|
|
|
public override string[] Aliases { get; } = { "mute", "timeout", "заглушить", "мут" };
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
2022-08-30 18:15:01 +03:00
|
|
|
|
public override async Task Run(CommandProcessor cmd, string[] args) {
|
|
|
|
|
var toMute = cmd.GetMember(args, 0, "ToMute");
|
|
|
|
|
if (toMute == null) return;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
2022-08-30 18:15:01 +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) {
|
2022-06-06 18:39:47 +03:00
|
|
|
|
if (toMute.Roles.Contains(role) || (toMute.TimedOutUntil != null &&
|
|
|
|
|
toMute.TimedOutUntil.Value.ToUnixTimeMilliseconds() >
|
|
|
|
|
DateTimeOffset.Now.ToUnixTimeMilliseconds())) {
|
2022-08-30 18:15:01 +03:00
|
|
|
|
cmd.Reply(Messages.MemberAlreadyMuted, ":x: ");
|
2022-05-14 16:12:24 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-08-30 18:15:01 +03:00
|
|
|
|
var rolesRemoved = Boyfriend.GetRemovedRoles(cmd.Context.Guild.Id);
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
if (rolesRemoved.ContainsKey(toMute.Id)) {
|
|
|
|
|
foreach (var roleId in rolesRemoved[toMute.Id]) await toMute.AddRoleAsync(roleId);
|
|
|
|
|
rolesRemoved.Remove(toMute.Id);
|
2022-08-30 18:15:01 +03:00
|
|
|
|
cmd.ConfigWriteScheduled = true;
|
|
|
|
|
cmd.Reply(Messages.RolesReturned, ":warning: ");
|
2022-05-14 16:12:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 18:15:01 +03:00
|
|
|
|
if (!cmd.HasPermission(GuildPermission.ModerateMembers) || !cmd.CanInteractWith(toMute, "Mute")) return;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
2022-08-30 18:15:01 +03:00
|
|
|
|
await MuteMember(cmd, toMute, duration, reason);
|
2022-01-18 20:38:15 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 18:15:01 +03:00
|
|
|
|
private static async Task MuteMember(CommandProcessor cmd, SocketGuildUser toMute,
|
2022-01-18 20:38:15 +03:00
|
|
|
|
TimeSpan duration, string reason) {
|
2022-08-30 18:15:01 +03:00
|
|
|
|
var guild = cmd.Context.Guild;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
var config = Boyfriend.GetGuildConfig(guild.Id);
|
2022-08-30 18:15:01 +03:00
|
|
|
|
var requestOptions = Utils.GetRequestOptions($"({cmd.Context.User}) {reason}");
|
|
|
|
|
var role = Utils.GetMuteRole(guild);
|
2022-02-21 20:08:55 +03:00
|
|
|
|
var hasDuration = duration.TotalSeconds > 0;
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
|
|
|
|
if (role != null) {
|
2022-07-15 19:23:45 +03:00
|
|
|
|
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) {
|
2022-08-30 18:15:01 +03:00
|
|
|
|
cmd.Reply(string.Format(Messages.RoleRemovalFailed, $"<@&{userRole}>", Utils.Wrap(e.Reason)),
|
|
|
|
|
":warning: ");
|
2022-02-21 20:08:55 +03:00
|
|
|
|
}
|
2022-01-18 20:38:15 +03:00
|
|
|
|
|
2022-05-14 16:12:24 +03:00
|
|
|
|
Boyfriend.GetRemovedRoles(guild.Id).Add(toMute.Id, rolesRemoved.AsReadOnly());
|
2022-08-30 18:15:01 +03:00
|
|
|
|
cmd.ConfigWriteScheduled = true;
|
2022-02-02 16:14:26 +03:00
|
|
|
|
}
|
2022-01-18 20:38:15 +03:00
|
|
|
|
|
2022-02-02 16:14:26 +03:00
|
|
|
|
await toMute.AddRoleAsync(role, requestOptions);
|
2022-08-30 18:15:01 +03:00
|
|
|
|
|
|
|
|
|
if (hasDuration) {
|
|
|
|
|
var _ = async () => {
|
|
|
|
|
await Task.Delay(duration);
|
|
|
|
|
await UnmuteCommand.UnmuteMember(cmd, toMute, Messages.PunishmentExpired);
|
|
|
|
|
};
|
|
|
|
|
}
|
2022-08-22 17:48:51 +03:00
|
|
|
|
} else {
|
|
|
|
|
if (!hasDuration || duration.TotalDays > 28) {
|
2022-08-30 18:15:01 +03:00
|
|
|
|
cmd.Reply(Messages.DurationRequiredForTimeOuts, ":x: ");
|
2022-05-14 16:12:24 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-06-06 18:39:47 +03:00
|
|
|
|
|
2022-05-14 16:12:24 +03:00
|
|
|
|
if (toMute.IsBot) {
|
2022-08-30 18:15:01 +03:00
|
|
|
|
cmd.Reply(Messages.CannotTimeOutBot, ":x: ");
|
2022-05-14 16:12:24 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-02-21 20:08:55 +03:00
|
|
|
|
|
2022-02-02 16:14:26 +03:00
|
|
|
|
await toMute.SetTimeOutAsync(duration, requestOptions);
|
2022-02-21 20:08:55 +03:00
|
|
|
|
}
|
2022-08-22 17:48:51 +03:00
|
|
|
|
|
|
|
|
|
var feedback = string.Format(Messages.FeedbackMemberMuted, toMute.Mention,
|
2022-08-30 18:15:01 +03:00
|
|
|
|
Utils.GetHumanizedTimeOffset(duration),
|
2022-08-22 17:48:51 +03:00
|
|
|
|
Utils.Wrap(reason));
|
2022-08-30 18:15:01 +03:00
|
|
|
|
cmd.Reply(feedback, ":mute: ");
|
|
|
|
|
cmd.Audit(feedback);
|
2022-01-18 20:38:15 +03:00
|
|
|
|
}
|
2022-08-30 18:15:01 +03:00
|
|
|
|
}
|