2023-01-18 17:39:24 +03:00
|
|
|
using Boyfriend.Data;
|
2022-12-06 18:33:46 +03:00
|
|
|
using Discord;
|
2022-01-18 20:38:15 +03:00
|
|
|
|
|
|
|
namespace Boyfriend.Commands;
|
|
|
|
|
2022-09-18 17:41:29 +03:00
|
|
|
public sealed class MuteCommand : ICommand {
|
|
|
|
public string[] Aliases { get; } = { "mute", "timeout", "заглушить", "мут" };
|
2022-05-14 16:12:24 +03:00
|
|
|
|
2022-09-18 17:41:29 +03:00
|
|
|
public async Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
2023-01-18 17:39:24 +03:00
|
|
|
var toMute = cmd.GetMember(args, 0);
|
2022-11-11 22:59:11 +03:00
|
|
|
if (toMute is 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");
|
2022-11-11 22:59:11 +03:00
|
|
|
if (reason is null) return;
|
2023-01-18 17:39:24 +03:00
|
|
|
var guildData = GuildData.Get(cmd.Context.Guild);
|
|
|
|
var role = guildData.MuteRole;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
2022-11-11 22:59:11 +03:00
|
|
|
if ((role is not null && toMute.Roles.Contains(role))
|
|
|
|
|| (toMute.TimedOutUntil is not null
|
2023-01-18 17:39:24 +03:00
|
|
|
&& toMute.TimedOutUntil.Value
|
|
|
|
> DateTimeOffset.Now)) {
|
2022-12-06 18:33:46 +03:00
|
|
|
cmd.Reply(Messages.MemberAlreadyMuted, ReplyEmojis.Error);
|
2022-09-18 17:41:29 +03:00
|
|
|
return;
|
2022-05-14 16:12:24 +03:00
|
|
|
}
|
2022-02-02 16:14:26 +03:00
|
|
|
|
2022-11-11 22:59:11 +03:00
|
|
|
if (cmd.HasPermission(GuildPermission.ModerateMembers) && cmd.CanInteractWith(toMute, "Mute"))
|
2023-01-18 17:39:24 +03:00
|
|
|
await MuteMemberAsync(cmd, toMute, duration, guildData, reason);
|
2022-01-18 20:38:15 +03:00
|
|
|
}
|
|
|
|
|
2023-01-27 07:53:04 +03:00
|
|
|
private static async Task MuteMemberAsync(
|
|
|
|
CommandProcessor cmd, IGuildUser toMute,
|
|
|
|
TimeSpan duration, GuildData data, string reason) {
|
2022-08-30 18:15:01 +03:00
|
|
|
var requestOptions = Utils.GetRequestOptions($"({cmd.Context.User}) {reason}");
|
2023-01-18 17:39:24 +03:00
|
|
|
var role = data.MuteRole;
|
2022-02-21 20:08:55 +03:00
|
|
|
var hasDuration = duration.TotalSeconds > 0;
|
2023-01-27 07:46:03 +03:00
|
|
|
var memberData = data.MemberData[toMute.Id];
|
2022-02-02 16:14:26 +03:00
|
|
|
|
2022-11-11 22:59:11 +03:00
|
|
|
if (role is not null) {
|
2023-02-02 21:04:29 +03:00
|
|
|
memberData.MutedUntil = DateTimeOffset.Now.Add(duration);
|
2023-01-27 07:53:04 +03:00
|
|
|
if (data.Preferences["RemoveRolesOnMute"] is "true") {
|
|
|
|
memberData.Roles = toMute.RoleIds.ToList();
|
|
|
|
memberData.Roles.Remove(cmd.Context.Guild.Id);
|
2023-01-27 07:46:03 +03:00
|
|
|
await toMute.RemoveRolesAsync(memberData.Roles, requestOptions);
|
2023-01-27 07:53:04 +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-22 17:48:51 +03:00
|
|
|
} else {
|
|
|
|
if (!hasDuration || duration.TotalDays > 28) {
|
2022-12-06 18:33:46 +03:00
|
|
|
cmd.Reply(Messages.DurationRequiredForTimeOuts, ReplyEmojis.Error);
|
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-12-06 18:33:46 +03:00
|
|
|
cmd.Reply(Messages.CannotTimeOutBot, ReplyEmojis.Error);
|
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
|
|
|
|
2023-01-18 17:39:24 +03:00
|
|
|
cmd.ConfigWriteScheduled = true;
|
|
|
|
|
2023-01-27 07:53:04 +03:00
|
|
|
var feedback = string.Format(
|
|
|
|
Messages.FeedbackMemberMuted, toMute.Mention,
|
2023-01-18 17:39:24 +03:00
|
|
|
Utils.GetHumanizedTimeSpan(duration),
|
2022-08-22 17:48:51 +03:00
|
|
|
Utils.Wrap(reason));
|
2022-12-06 18:33:46 +03:00
|
|
|
cmd.Reply(feedback, ReplyEmojis.Muted);
|
2022-08-30 18:15:01 +03:00
|
|
|
cmd.Audit(feedback);
|
2022-01-18 20:38:15 +03:00
|
|
|
}
|
2022-09-18 17:41:29 +03:00
|
|
|
}
|