1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 17:19:00 +03:00
Octobot/Commands/MuteCommand.cs

72 lines
2.7 KiB
C#
Raw Normal View History

using Boyfriend.Data;
using Discord;
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, 0);
if (toMute is 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 is null) return;
var guildData = GuildData.Get(cmd.Context.Guild);
var role = guildData.MuteRole;
2022-05-14 16:12:24 +03:00
if ((role is not null && toMute.Roles.Contains(role))
|| (toMute.TimedOutUntil is not null
&& toMute.TimedOutUntil.Value
> DateTimeOffset.Now)) {
cmd.Reply(Messages.MemberAlreadyMuted, ReplyEmojis.Error);
return;
2022-05-14 16:12:24 +03:00
}
2022-02-02 16:14:26 +03:00
if (cmd.HasPermission(GuildPermission.ModerateMembers) && cmd.CanInteractWith(toMute, "Mute"))
await MuteMemberAsync(cmd, toMute, duration, guildData, reason);
}
private static async Task MuteMemberAsync(
CommandProcessor cmd, IGuildUser toMute,
TimeSpan duration, GuildData data, string reason) {
var requestOptions = Utils.GetRequestOptions($"({cmd.Context.User}) {reason}");
var role = data.MuteRole;
var hasDuration = duration.TotalSeconds > 0;
var memberData = data.MemberData[toMute.Id];
2022-02-02 16:14:26 +03:00
if (role is not null) {
if (data.Preferences["RemoveRolesOnMute"] is "true") {
memberData.Roles = toMute.RoleIds.ToList();
memberData.Roles.Remove(cmd.Context.Guild.Id);
await toMute.RemoveRolesAsync(memberData.Roles, requestOptions);
}
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) {
cmd.Reply(Messages.DurationRequiredForTimeOuts, ReplyEmojis.Error);
2022-05-14 16:12:24 +03:00
return;
}
2022-05-14 16:12:24 +03:00
if (toMute.IsBot) {
cmd.Reply(Messages.CannotTimeOutBot, ReplyEmojis.Error);
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
memberData.MutedUntil = DateTimeOffset.Now.Add(duration);
cmd.ConfigWriteScheduled = true;
var feedback = string.Format(
Messages.FeedbackMemberMuted, toMute.Mention,
Utils.GetHumanizedTimeSpan(duration),
2022-08-22 17:48:51 +03:00
Utils.Wrap(reason));
cmd.Reply(feedback, ReplyEmojis.Muted);
cmd.Audit(feedback);
}
}