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

125 lines
4.6 KiB
C#
Raw Normal View History

using Discord;
using Discord.Commands;
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 class MuteCommand : Command {
public override string[] Aliases { get; } = { "mute", "timeout", "заглушить", "мут" };
2022-05-14 16:12:24 +03:00
public override int ArgsLengthRequired => 2;
public override async Task Run(SocketCommandContext context, string[] args) {
2022-05-14 16:12:24 +03:00
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);
2022-08-22 17:48:51 +03:00
}
2022-08-22 17:48:51 +03:00
if (reason is "") {
Error(Messages.ReasonRequired, false);
return;
}
2022-05-14 16:12:24 +03:00
if (toMute == null) {
Error(Messages.UserNotInGuild, false);
return;
}
var guild = context.Guild;
var role = Utils.GetMuteRole(ref guild);
if (role != null) {
if (toMute.Roles.Contains(role) || (toMute.TimedOutUntil != null &&
toMute.TimedOutUntil.Value.ToUnixTimeMilliseconds() >
DateTimeOffset.Now.ToUnixTimeMilliseconds())) {
2022-05-14 16:12:24 +03:00
Error(Messages.MemberAlreadyMuted, false);
return;
}
}
2022-02-02 16:14:26 +03:00
2022-05-14 16:12:24 +03:00
var rolesRemoved = Boyfriend.GetRemovedRoles(context.Guild.Id);
2022-02-02 16:14:26 +03:00
if (rolesRemoved.ContainsKey(toMute.Id)) {
foreach (var roleId in rolesRemoved[toMute.Id]) await toMute.AddRoleAsync(roleId);
rolesRemoved.Remove(toMute.Id);
2022-05-14 16:12:24 +03:00
CommandHandler.ConfigWriteScheduled = true;
Warn(Messages.RolesReturned);
}
var author = (SocketGuildUser)context.User;
2022-05-14 16:12:24 +03:00
var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.ModerateMembers);
if (permissionCheckResponse is not "") {
2022-05-14 16:12:24 +03:00
Error(permissionCheckResponse, true);
return;
}
2022-05-14 16:12:24 +03:00
var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref toMute);
if (interactionCheckResponse is not "") {
2022-05-14 16:12:24 +03:00
Error(interactionCheckResponse, true);
return;
}
await MuteMember(guild, author, toMute, duration, reason);
}
2022-05-14 16:12:24 +03:00
private static async Task MuteMember(SocketGuild guild, SocketUser author, SocketGuildUser toMute,
TimeSpan duration, string reason) {
2022-05-14 16:12:24 +03:00
var config = Boyfriend.GetGuildConfig(guild.Id);
var requestOptions = Utils.GetRequestOptions($"({author}) {reason}");
var role = Utils.GetMuteRole(ref 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) {
Warn(string.Format(Messages.RoleRemovalFailed, $"<@&{userRole}>", Utils.Wrap(e.Reason)));
}
2022-05-14 16:12:24 +03:00
Boyfriend.GetRemovedRoles(guild.Id).Add(toMute.Id, rolesRemoved.AsReadOnly());
CommandHandler.ConfigWriteScheduled = true;
2022-05-14 16:12:24 +03:00
if (hasDuration) {
async void DelayUnmute() {
await Task.Delay(duration);
2022-05-14 16:12:24 +03:00
await UnmuteCommand.UnmuteMember(guild, guild.CurrentUser, toMute, Messages.PunishmentExpired);
}
new Task(DelayUnmute).Start();
2022-05-14 16:12:24 +03:00
}
2022-02-02 16:14:26 +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-05-14 16:12:24 +03:00
Error(Messages.DurationRequiredForTimeOuts, false);
return;
}
2022-05-14 16:12:24 +03:00
if (toMute.IsBot) {
Error(Messages.CannotTimeOutBot, false);
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(ref duration),
Utils.Wrap(reason));
Success(feedback, author.Mention, false, false);
await Utils.SendFeedback(feedback, guild.Id, author.Mention, true);
}
2022-08-22 17:48:51 +03:00
}