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/MuteModule.cs

50 lines
2.3 KiB
C#
Raw Normal View History

2021-12-10 14:25:29 +03:00
using Discord;
using Discord.Commands;
// ReSharper disable UnusedType.Global
// ReSharper disable UnusedMember.Global
// ReSharper disable ClassNeverInstantiated.Global
namespace Boyfriend.Commands;
public class MuteModule : ModuleBase<SocketCommandContext> {
[Command("mute")]
[Summary("Глушит пользователя")]
[Alias("мут")]
2021-12-15 09:19:14 +03:00
public async Task Run(string user, string durationString, [Remainder]string reason) {
TimeSpan duration;
try {
duration = TimeSpan.Parse(durationString);
} catch (Exception e) when (e is ArgumentNullException or FormatException or OverflowException) {
duration = TimeSpan.FromMilliseconds(-1);
reason = durationString + reason;
}
var author = Context.Guild.GetUser(Context.User.Id);
var toMute = await Utils.ParseMember(Context.Guild, user);
await CommandHandler.CheckPermissions(author, GuildPermission.ManageMessages, GuildPermission.ManageRoles);
await CommandHandler.CheckInteractions(author, toMute);
MuteMember(Context.Guild, Context.Guild.GetUser(Context.User.Id), toMute, duration, reason);
2021-12-10 14:25:29 +03:00
}
2021-12-15 09:19:14 +03:00
private static async void MuteMember(IGuild guild, IGuildUser author, IGuildUser toMute, TimeSpan duration,
2021-12-10 14:25:29 +03:00
string reason) {
2021-12-15 09:19:14 +03:00
await CommandHandler.CheckPermissions(author, GuildPermission.ManageMessages, GuildPermission.ManageRoles);
2021-12-10 14:25:29 +03:00
var authorMention = author.Mention;
var role = Utils.GetMuteRole(guild);
2021-12-15 09:19:14 +03:00
if (Boyfriend.GetGuildConfig(guild).RemoveRolesOnMute) {
foreach (var roleId in toMute.RoleIds) {
await toMute.RemoveRoleAsync(roleId);
}
}
2021-12-10 14:25:29 +03:00
await toMute.AddRoleAsync(role);
var notification = $"{authorMention} глушит {toMute.Mention} за {Utils.WrapInline(reason)}";
await Utils.SilentSendAsync(guild.GetSystemChannelAsync().Result, notification);
await Utils.SilentSendAsync(Utils.GetAdminLogChannel(), notification);
var task = new Task(() => UnmuteModule.UnmuteMember(guild, guild.GetCurrentUserAsync().Result, toMute,
"Время наказания истекло"));
await Utils.StartDelayed(task, duration, () => toMute.RoleIds.Any(x => x == role.Id));
}
}