2021-08-26 03:18:45 +00:00
|
|
|
|
using Discord.Net;
|
2022-07-21 01:55:08 +00:00
|
|
|
|
using RegexBot.Common;
|
2021-08-26 03:18:45 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
namespace RegexBot;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Contains information on various success/failure outcomes for a ban or kick operation.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class BanKickResult {
|
|
|
|
|
private readonly bool _userNotFound; // possible to receive this error by means other than exception
|
|
|
|
|
private readonly ulong _rptTargetId;
|
2021-08-26 03:18:45 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// Gets a value indicating whether the kick or ban succeeded.
|
2021-08-26 03:18:45 +00:00
|
|
|
|
/// </summary>
|
2022-03-29 05:03:01 +00:00
|
|
|
|
public bool OperationSuccess {
|
|
|
|
|
get {
|
|
|
|
|
if (ErrorNotFound) return false;
|
|
|
|
|
if (OperationError != null) return false;
|
|
|
|
|
return true;
|
2021-08-26 03:18:45 +00:00
|
|
|
|
}
|
2022-03-29 05:03:01 +00:00
|
|
|
|
}
|
2021-08-26 03:18:45 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The exception thrown, if any, when attempting to kick or ban the target.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public HttpException? OperationError { get; }
|
2021-08-26 03:18:45 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Indicates if the operation failed due to being unable to find the user.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This may return true even if <see cref="OperationError"/> returns null.
|
|
|
|
|
/// This type of error may appear in cases that do not involve an exception being thrown.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public bool ErrorNotFound {
|
|
|
|
|
get {
|
|
|
|
|
if (_userNotFound) return true;
|
|
|
|
|
return OperationError?.HttpCode == System.Net.HttpStatusCode.NotFound;
|
2021-08-26 03:18:45 +00:00
|
|
|
|
}
|
2022-03-29 05:03:01 +00:00
|
|
|
|
}
|
2021-08-26 03:18:45 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Indicates if the operation failed due to a permissions issue.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool ErrorForbidden {
|
|
|
|
|
get {
|
|
|
|
|
if (OperationSuccess) return false;
|
|
|
|
|
return OperationError?.HttpCode == System.Net.HttpStatusCode.Forbidden;
|
2021-08-26 03:18:45 +00:00
|
|
|
|
}
|
2022-03-29 05:03:01 +00:00
|
|
|
|
}
|
2021-08-26 03:18:45 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating whether the user was able to receive the ban or kick message.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>
|
|
|
|
|
/// <see langword="false"/> if an error was encountered when attempting to send the target a DM. Will always
|
|
|
|
|
/// return <see langword="true"/> otherwise, including cases in which no message was sent.
|
|
|
|
|
/// </value>
|
|
|
|
|
public bool MessageSendSuccess { get; }
|
2021-08-26 03:18:45 +00:00
|
|
|
|
|
2022-12-03 04:28:23 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating if this result represents a ban.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsBan { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating if this result represents a kick.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsKick { get => !IsBan; }
|
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
internal BanKickResult(HttpException? error, bool notificationSuccess, bool errorNotFound,
|
2022-12-03 04:28:23 +00:00
|
|
|
|
bool isBan, ulong rtarget) {
|
2022-03-29 05:03:01 +00:00
|
|
|
|
OperationError = error;
|
|
|
|
|
MessageSendSuccess = notificationSuccess;
|
|
|
|
|
_userNotFound = errorNotFound;
|
2022-12-03 04:28:23 +00:00
|
|
|
|
IsBan = isBan;
|
2022-03-29 05:03:01 +00:00
|
|
|
|
_rptTargetId = rtarget;
|
|
|
|
|
}
|
2021-08-26 03:18:45 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a message representative of the ban/kick result that may be posted as-is
|
|
|
|
|
/// within the a Discord channel.
|
|
|
|
|
/// </summary>
|
2022-05-12 03:26:28 +00:00
|
|
|
|
public string GetResultString(RegexbotClient bot) {
|
2022-03-29 05:03:01 +00:00
|
|
|
|
string msg;
|
2021-08-26 03:18:45 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
if (OperationSuccess) msg = ":white_check_mark: ";
|
|
|
|
|
else msg = ":x: Failed to ";
|
2021-08-26 03:18:45 +00:00
|
|
|
|
|
2022-12-03 04:28:23 +00:00
|
|
|
|
if (IsBan) {
|
2022-03-29 05:03:01 +00:00
|
|
|
|
if (OperationSuccess) msg += "Banned";
|
|
|
|
|
else msg += "ban";
|
2022-12-03 04:28:23 +00:00
|
|
|
|
} else {
|
2022-03-29 05:03:01 +00:00
|
|
|
|
if (OperationSuccess) msg += "Kicked";
|
|
|
|
|
else msg += "kick";
|
|
|
|
|
}
|
2021-08-26 03:18:45 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
if (_rptTargetId != 0) {
|
2022-05-12 03:26:28 +00:00
|
|
|
|
var user = bot.EcQueryUser(_rptTargetId.ToString());
|
2022-03-29 05:03:01 +00:00
|
|
|
|
if (user != null) {
|
2023-11-15 05:09:21 +00:00
|
|
|
|
msg += $" user **{user.GetDisplayableUsername()}**";
|
2022-05-12 03:26:28 +00:00
|
|
|
|
} else {
|
|
|
|
|
msg += $" user with ID **{_rptTargetId}**";
|
2021-08-26 03:18:45 +00:00
|
|
|
|
}
|
2022-03-29 05:03:01 +00:00
|
|
|
|
}
|
2021-08-26 03:18:45 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
if (OperationSuccess) {
|
|
|
|
|
msg += ".";
|
|
|
|
|
if (!MessageSendSuccess) msg += "\n(User was unable to receive notification message.)";
|
|
|
|
|
} else {
|
|
|
|
|
if (ErrorNotFound) msg += ": The specified user could not be found.";
|
2022-07-21 01:55:08 +00:00
|
|
|
|
else if (ErrorForbidden) msg += ": " + Messages.ForbiddenGenericError;
|
2021-08-26 03:18:45 +00:00
|
|
|
|
}
|
2022-03-29 05:03:01 +00:00
|
|
|
|
|
|
|
|
|
return msg;
|
2021-08-26 03:18:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|