2023-07-09 16:32:14 +03:00
|
|
|
using System.ComponentModel;
|
|
|
|
using System.Text;
|
2023-07-18 15:25:02 +03:00
|
|
|
using Boyfriend.Data;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Boyfriend.Services;
|
2023-07-18 15:25:02 +03:00
|
|
|
using JetBrains.Annotations;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Commands.Attributes;
|
|
|
|
using Remora.Commands.Groups;
|
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
2023-07-18 15:25:02 +03:00
|
|
|
using Remora.Discord.Commands.Attributes;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Discord.Commands.Conditions;
|
|
|
|
using Remora.Discord.Commands.Contexts;
|
|
|
|
using Remora.Discord.Commands.Feedback.Services;
|
|
|
|
using Remora.Discord.Extensions.Embeds;
|
|
|
|
using Remora.Discord.Extensions.Formatting;
|
2023-07-20 23:41:02 +03:00
|
|
|
using Remora.Rest.Core;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Results;
|
|
|
|
|
|
|
|
namespace Boyfriend.Commands;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles commands related to mute management: /mute and /unmute.
|
|
|
|
/// </summary>
|
2023-07-18 15:25:02 +03:00
|
|
|
[UsedImplicitly]
|
2023-08-02 23:51:16 +03:00
|
|
|
public class MuteCommandGroup : CommandGroup
|
|
|
|
{
|
|
|
|
private readonly ICommandContext _context;
|
|
|
|
private readonly FeedbackService _feedback;
|
2023-07-20 23:41:02 +03:00
|
|
|
private readonly IDiscordRestGuildAPI _guildApi;
|
2023-08-02 23:51:16 +03:00
|
|
|
private readonly GuildDataService _guildData;
|
|
|
|
private readonly IDiscordRestUserAPI _userApi;
|
|
|
|
private readonly UtilityService _utility;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
public MuteCommandGroup(
|
2023-08-02 23:51:16 +03:00
|
|
|
ICommandContext context, GuildDataService guildData, FeedbackService feedback,
|
|
|
|
IDiscordRestGuildAPI guildApi, IDiscordRestUserAPI userApi, UtilityService utility)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
_context = context;
|
2023-08-02 23:51:16 +03:00
|
|
|
_guildData = guildData;
|
|
|
|
_feedback = feedback;
|
2023-07-09 16:32:14 +03:00
|
|
|
_guildApi = guildApi;
|
|
|
|
_userApi = userApi;
|
|
|
|
_utility = utility;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2023-07-09 18:15:39 +03:00
|
|
|
/// A slash command that mutes a Discord member with the specified reason.
|
2023-07-09 16:32:14 +03:00
|
|
|
/// </summary>
|
2023-07-09 18:15:39 +03:00
|
|
|
/// <param name="target">The member to mute.</param>
|
|
|
|
/// <param name="duration">The duration for this mute. The member will be automatically unmuted after this duration.</param>
|
2023-07-09 16:32:14 +03:00
|
|
|
/// <param name="reason">
|
|
|
|
/// The reason for this mute. Must be encoded with <see cref="Extensions.EncodeHeader" /> when passed to
|
|
|
|
/// <see cref="IDiscordRestGuildAPI.ModifyGuildMemberAsync" />.
|
|
|
|
/// </param>
|
|
|
|
/// <returns>
|
2023-07-09 18:15:39 +03:00
|
|
|
/// A feedback sending result which may or may not have succeeded. A successful result does not mean that the member
|
2023-07-09 16:32:14 +03:00
|
|
|
/// was muted and vice-versa.
|
|
|
|
/// </returns>
|
2023-07-20 23:41:02 +03:00
|
|
|
/// <seealso cref="ExecuteUnmute" />
|
2023-07-09 16:32:14 +03:00
|
|
|
[Command("mute", "мут")]
|
2023-07-18 15:25:02 +03:00
|
|
|
[DiscordDefaultMemberPermissions(DiscordPermission.ModerateMembers)]
|
|
|
|
[DiscordDefaultDMPermission(false)]
|
2023-07-09 16:32:14 +03:00
|
|
|
[RequireContext(ChannelContext.Guild)]
|
|
|
|
[RequireDiscordPermission(DiscordPermission.ModerateMembers)]
|
|
|
|
[RequireBotDiscordPermissions(DiscordPermission.ModerateMembers)]
|
|
|
|
[Description("Mute member")]
|
2023-07-18 15:25:02 +03:00
|
|
|
[UsedImplicitly]
|
2023-07-20 23:41:02 +03:00
|
|
|
public async Task<Result> ExecuteMute(
|
2023-08-02 23:51:16 +03:00
|
|
|
[Description("Member to mute")] IUser target,
|
|
|
|
[Description("Mute reason")] string reason,
|
|
|
|
[Description("Mute duration")] TimeSpan duration)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-28 19:58:55 +03:00
|
|
|
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
// The current user's avatar is used when sending error messages
|
|
|
|
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
|
|
|
if (!currentUserResult.IsDefined(out var currentUser))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result.FromError(currentUserResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-07-24 14:57:41 +03:00
|
|
|
var userResult = await _userApi.GetUserAsync(userId, CancellationToken);
|
2023-07-20 23:41:02 +03:00
|
|
|
if (!userResult.IsDefined(out var user))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 23:41:02 +03:00
|
|
|
return Result.FromError(userResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-20 23:41:02 +03:00
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
var data = await _guildData.GetData(guildId, CancellationToken);
|
2023-07-20 23:41:02 +03:00
|
|
|
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
|
|
|
|
2023-07-24 14:57:41 +03:00
|
|
|
var memberResult = await _guildApi.GetGuildMemberAsync(guildId, target.ID, CancellationToken);
|
2023-08-02 23:51:16 +03:00
|
|
|
if (!memberResult.IsSuccess)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(Messages.UserNotFoundShort, currentUser)
|
|
|
|
.WithColour(ColorsList.Red).Build();
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
return await _feedback.SendContextualEmbedResultAsync(embed, CancellationToken);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
|
2023-07-20 23:41:02 +03:00
|
|
|
return await MuteUserAsync(
|
2023-07-24 14:57:41 +03:00
|
|
|
target, reason, duration, guildId, data, channelId, user, currentUser, CancellationToken);
|
2023-07-20 23:41:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<Result> MuteUserAsync(
|
2023-08-02 23:51:16 +03:00
|
|
|
IUser target, string reason, TimeSpan duration, Snowflake guildId, GuildData data, Snowflake channelId,
|
|
|
|
IUser user, IUser currentUser, CancellationToken ct = default)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
var interactionResult
|
|
|
|
= await _utility.CheckInteractionsAsync(
|
2023-07-20 23:41:02 +03:00
|
|
|
guildId, user.ID, target.ID, "Mute", ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
if (!interactionResult.IsSuccess)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result.FromError(interactionResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
if (interactionResult.Entity is not null)
|
|
|
|
{
|
2023-07-20 23:41:02 +03:00
|
|
|
var failedEmbed = new EmbedBuilder().WithSmallTitle(interactionResult.Entity, currentUser)
|
2023-07-09 16:32:14 +03:00
|
|
|
.WithColour(ColorsList.Red).Build();
|
2023-07-20 23:41:02 +03:00
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
|
2023-07-20 23:41:02 +03:00
|
|
|
var until = DateTimeOffset.UtcNow.Add(duration); // >:)
|
|
|
|
var muteResult = await _guildApi.ModifyGuildMemberAsync(
|
|
|
|
guildId, target.ID, reason: $"({user.GetTag()}) {reason}".EncodeHeader(),
|
|
|
|
communicationDisabledUntil: until, ct: ct);
|
|
|
|
if (!muteResult.IsSuccess)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 23:41:02 +03:00
|
|
|
return Result.FromError(muteResult.Error);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-20 23:41:02 +03:00
|
|
|
|
|
|
|
var title = string.Format(Messages.UserMuted, target.GetTag());
|
|
|
|
var description = new StringBuilder().AppendLine(string.Format(Messages.DescriptionActionReason, reason))
|
|
|
|
.Append(
|
|
|
|
string.Format(
|
|
|
|
Messages.DescriptionActionExpiresAt, Markdown.Timestamp(until))).ToString();
|
|
|
|
|
|
|
|
var logResult = _utility.LogActionAsync(
|
2023-07-23 23:07:36 +03:00
|
|
|
data.Settings, channelId, user, title, description, target, ColorsList.Red, ct: ct);
|
2023-07-20 23:41:02 +03:00
|
|
|
if (!logResult.IsSuccess)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 23:41:02 +03:00
|
|
|
return Result.FromError(logResult.Error);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-20 23:41:02 +03:00
|
|
|
|
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(
|
|
|
|
string.Format(Messages.UserMuted, target.GetTag()), target)
|
|
|
|
.WithColour(ColorsList.Green).Build();
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2023-07-09 18:15:39 +03:00
|
|
|
/// A slash command that unmutes a Discord member with the specified reason.
|
2023-07-09 16:32:14 +03:00
|
|
|
/// </summary>
|
2023-07-09 18:15:39 +03:00
|
|
|
/// <param name="target">The member to unmute.</param>
|
2023-07-09 16:32:14 +03:00
|
|
|
/// <param name="reason">
|
|
|
|
/// The reason for this unmute. Must be encoded with <see cref="Extensions.EncodeHeader" /> when passed to
|
|
|
|
/// <see cref="IDiscordRestGuildAPI.ModifyGuildMemberAsync" />.
|
|
|
|
/// </param>
|
|
|
|
/// <returns>
|
2023-07-09 18:15:39 +03:00
|
|
|
/// A feedback sending result which may or may not have succeeded. A successful result does not mean that the member
|
2023-07-09 16:32:14 +03:00
|
|
|
/// was unmuted and vice-versa.
|
|
|
|
/// </returns>
|
2023-07-20 23:41:02 +03:00
|
|
|
/// <seealso cref="ExecuteMute" />
|
2023-08-02 23:51:16 +03:00
|
|
|
/// <seealso cref="GuildUpdateService.TickGuildAsync" />
|
2023-07-09 16:32:14 +03:00
|
|
|
[Command("unmute", "размут")]
|
2023-07-18 15:25:02 +03:00
|
|
|
[DiscordDefaultMemberPermissions(DiscordPermission.ModerateMembers)]
|
|
|
|
[DiscordDefaultDMPermission(false)]
|
2023-07-09 16:32:14 +03:00
|
|
|
[RequireContext(ChannelContext.Guild)]
|
|
|
|
[RequireDiscordPermission(DiscordPermission.ModerateMembers)]
|
|
|
|
[RequireBotDiscordPermissions(DiscordPermission.ModerateMembers)]
|
|
|
|
[Description("Unmute member")]
|
2023-07-18 15:25:02 +03:00
|
|
|
[UsedImplicitly]
|
2023-07-20 23:41:02 +03:00
|
|
|
public async Task<Result> ExecuteUnmute(
|
2023-08-02 23:51:16 +03:00
|
|
|
[Description("Member to unmute")] IUser target,
|
|
|
|
[Description("Unmute reason")] string reason)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-28 19:58:55 +03:00
|
|
|
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
// The current user's avatar is used when sending error messages
|
|
|
|
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
|
|
|
if (!currentUserResult.IsDefined(out var currentUser))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result.FromError(currentUserResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-07-20 23:41:02 +03:00
|
|
|
// Needed to get the tag and avatar
|
2023-07-24 14:57:41 +03:00
|
|
|
var userResult = await _userApi.GetUserAsync(userId, CancellationToken);
|
2023-07-20 23:41:02 +03:00
|
|
|
if (!userResult.IsDefined(out var user))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 23:41:02 +03:00
|
|
|
return Result.FromError(userResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-20 23:41:02 +03:00
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
var data = await _guildData.GetData(guildId, CancellationToken);
|
2023-07-20 23:41:02 +03:00
|
|
|
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-07-24 14:57:41 +03:00
|
|
|
var memberResult = await _guildApi.GetGuildMemberAsync(guildId, target.ID, CancellationToken);
|
2023-08-02 23:51:16 +03:00
|
|
|
if (!memberResult.IsSuccess)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(Messages.UserNotFoundShort, currentUser)
|
|
|
|
.WithColour(ColorsList.Red).Build();
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
return await _feedback.SendContextualEmbedResultAsync(embed, CancellationToken);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
|
2023-07-20 23:41:02 +03:00
|
|
|
return await UnmuteUserAsync(
|
2023-07-24 14:57:41 +03:00
|
|
|
target, reason, guildId, data, channelId, user, currentUser, CancellationToken);
|
2023-07-20 23:41:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<Result> UnmuteUserAsync(
|
2023-08-02 23:51:16 +03:00
|
|
|
IUser target, string reason, Snowflake guildId, GuildData data, Snowflake channelId, IUser user,
|
|
|
|
IUser currentUser, CancellationToken ct = default)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
var interactionResult
|
|
|
|
= await _utility.CheckInteractionsAsync(
|
2023-07-20 23:41:02 +03:00
|
|
|
guildId, user.ID, target.ID, "Unmute", ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
if (!interactionResult.IsSuccess)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result.FromError(interactionResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
if (interactionResult.Entity is not null)
|
|
|
|
{
|
2023-07-20 23:41:02 +03:00
|
|
|
var failedEmbed = new EmbedBuilder().WithSmallTitle(interactionResult.Entity, currentUser)
|
|
|
|
.WithColour(ColorsList.Red).Build();
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct);
|
2023-07-20 23:41:02 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
var unmuteResult = await _guildApi.ModifyGuildMemberAsync(
|
2023-07-20 23:41:02 +03:00
|
|
|
guildId, target.ID, $"({user.GetTag()}) {reason}".EncodeHeader(),
|
|
|
|
communicationDisabledUntil: null, ct: ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
if (!unmuteResult.IsSuccess)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result.FromError(unmuteResult.Error);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-07-20 23:41:02 +03:00
|
|
|
var title = string.Format(Messages.UserUnmuted, target.GetTag());
|
|
|
|
var description = string.Format(Messages.DescriptionActionReason, reason);
|
|
|
|
var logResult = _utility.LogActionAsync(
|
2023-07-23 23:07:36 +03:00
|
|
|
data.Settings, channelId, user, title, description, target, ColorsList.Green, ct: ct);
|
2023-07-20 23:41:02 +03:00
|
|
|
if (!logResult.IsSuccess)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 23:41:02 +03:00
|
|
|
return Result.FromError(logResult.Error);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-20 23:41:02 +03:00
|
|
|
|
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(
|
2023-07-09 16:32:14 +03:00
|
|
|
string.Format(Messages.UserUnmuted, target.GetTag()), target)
|
|
|
|
.WithColour(ColorsList.Green).Build();
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
}
|