2023-07-23 23:07:36 +03:00
|
|
|
using System.Drawing;
|
2023-07-09 16:32:14 +03:00
|
|
|
using System.Text;
|
2023-07-18 15:25:02 +03:00
|
|
|
using System.Text.Json.Nodes;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Microsoft.Extensions.Hosting;
|
2023-09-30 16:58:32 +03:00
|
|
|
using Octobot.Data;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
2023-07-20 00:08:44 +03:00
|
|
|
using Remora.Discord.Extensions.Embeds;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Discord.Extensions.Formatting;
|
|
|
|
using Remora.Rest.Core;
|
|
|
|
using Remora.Results;
|
|
|
|
|
2023-09-30 16:58:32 +03:00
|
|
|
namespace Octobot.Services;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Provides utility methods that cannot be transformed to extension methods because they require usage
|
|
|
|
/// of some Discord APIs.
|
|
|
|
/// </summary>
|
2023-08-02 23:51:16 +03:00
|
|
|
public sealed class UtilityService : IHostedService
|
|
|
|
{
|
|
|
|
private readonly IDiscordRestChannelAPI _channelApi;
|
2023-07-09 16:32:14 +03:00
|
|
|
private readonly IDiscordRestGuildScheduledEventAPI _eventApi;
|
2023-08-02 23:51:16 +03:00
|
|
|
private readonly IDiscordRestGuildAPI _guildApi;
|
|
|
|
private readonly IDiscordRestUserAPI _userApi;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
public UtilityService(
|
2023-07-20 00:08:44 +03:00
|
|
|
IDiscordRestChannelAPI channelApi, IDiscordRestGuildScheduledEventAPI eventApi, IDiscordRestGuildAPI guildApi,
|
2023-08-02 23:51:16 +03:00
|
|
|
IDiscordRestUserAPI userApi)
|
|
|
|
{
|
2023-07-20 00:08:44 +03:00
|
|
|
_channelApi = channelApi;
|
|
|
|
_eventApi = eventApi;
|
2023-07-09 16:32:14 +03:00
|
|
|
_guildApi = guildApi;
|
|
|
|
_userApi = userApi;
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public Task StartAsync(CancellationToken ct)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public Task StopAsync(CancellationToken ct)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks whether or not a member can interact with another member
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="guildId">The ID of the guild in which an operation is being performed.</param>
|
|
|
|
/// <param name="interacterId">The executor of the operation.</param>
|
|
|
|
/// <param name="targetId">The target of the operation.</param>
|
|
|
|
/// <param name="action">The operation.</param>
|
|
|
|
/// <param name="ct">The cancellation token for this operation.</param>
|
|
|
|
/// <returns>
|
|
|
|
/// <list type="bullet">
|
|
|
|
/// <item>A result which has succeeded with a null string if the member can interact with the target.</item>
|
|
|
|
/// <item>
|
|
|
|
/// A result which has succeeded with a non-null string containing the error message if the member cannot
|
|
|
|
/// interact with the target.
|
|
|
|
/// </item>
|
|
|
|
/// <item>A result which has failed if an error occurred during the execution of this method.</item>
|
|
|
|
/// </list>
|
|
|
|
/// </returns>
|
|
|
|
public async Task<Result<string?>> CheckInteractionsAsync(
|
2023-08-02 23:51:16 +03:00
|
|
|
Snowflake guildId, Snowflake interacterId, Snowflake targetId, string action, CancellationToken ct = default)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
if (interacterId == targetId)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result<string?>.FromSuccess($"UserCannot{action}Themselves".Localized());
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
var currentUserResult = await _userApi.GetCurrentUserAsync(ct);
|
|
|
|
if (!currentUserResult.IsDefined(out var currentUser))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result<string?>.FromError(currentUserResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
var guildResult = await _guildApi.GetGuildAsync(guildId, ct: ct);
|
|
|
|
if (!guildResult.IsDefined(out var guild))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result<string?>.FromError(guildResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
var targetMemberResult = await _guildApi.GetGuildMemberAsync(guildId, targetId, ct);
|
|
|
|
if (!targetMemberResult.IsDefined(out var targetMember))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result<string?>.FromSuccess(null);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
var currentMemberResult = await _guildApi.GetGuildMemberAsync(guildId, currentUser.ID, ct);
|
|
|
|
if (!currentMemberResult.IsDefined(out var currentMember))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result<string?>.FromError(currentMemberResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
var rolesResult = await _guildApi.GetGuildRolesAsync(guildId, ct);
|
|
|
|
if (!rolesResult.IsDefined(out var roles))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result<string?>.FromError(rolesResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-07-20 10:25:03 +03:00
|
|
|
var interacterResult = await _guildApi.GetGuildMemberAsync(guildId, interacterId, ct);
|
|
|
|
return interacterResult.IsDefined(out var interacter)
|
|
|
|
? CheckInteractions(action, guild, roles, targetMember, currentMember, interacter)
|
|
|
|
: Result<string?>.FromError(interacterResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Result<string?> CheckInteractions(
|
|
|
|
string action, IGuild guild, IReadOnlyList<IRole> roles, IGuildMember targetMember, IGuildMember currentMember,
|
2023-08-02 23:51:16 +03:00
|
|
|
IGuildMember interacter)
|
|
|
|
{
|
2023-07-20 10:25:03 +03:00
|
|
|
if (!targetMember.User.IsDefined(out var targetUser))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-28 19:58:55 +03:00
|
|
|
return new ArgumentNullError(nameof(targetMember.User));
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-20 10:25:03 +03:00
|
|
|
if (!interacter.User.IsDefined(out var interacterUser))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-28 19:58:55 +03:00
|
|
|
return new ArgumentNullError(nameof(interacter.User));
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-20 10:25:03 +03:00
|
|
|
|
|
|
|
if (currentMember.User == targetMember.User)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 10:25:03 +03:00
|
|
|
return Result<string?>.FromSuccess($"UserCannot{action}Bot".Localized());
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-20 10:25:03 +03:00
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
if (targetUser.ID == guild.OwnerID)
|
|
|
|
{
|
|
|
|
return Result<string?>.FromSuccess($"UserCannot{action}Owner".Localized());
|
|
|
|
}
|
2023-07-20 10:25:03 +03:00
|
|
|
|
2023-07-09 16:32:14 +03:00
|
|
|
var targetRoles = roles.Where(r => targetMember.Roles.Contains(r.ID)).ToList();
|
|
|
|
var botRoles = roles.Where(r => currentMember.Roles.Contains(r.ID));
|
|
|
|
|
|
|
|
var targetBotRoleDiff = targetRoles.MaxOrDefault(r => r.Position) - botRoles.MaxOrDefault(r => r.Position);
|
|
|
|
if (targetBotRoleDiff >= 0)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result<string?>.FromSuccess($"BotCannot{action}Target".Localized());
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-07-20 10:25:03 +03:00
|
|
|
if (interacterUser.ID == guild.OwnerID)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result<string?>.FromSuccess(null);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
var interacterRoles = roles.Where(r => interacter.Roles.Contains(r.ID));
|
|
|
|
var targetInteracterRoleDiff
|
|
|
|
= targetRoles.MaxOrDefault(r => r.Position) - interacterRoles.MaxOrDefault(r => r.Position);
|
2023-07-20 10:25:03 +03:00
|
|
|
return targetInteracterRoleDiff < 0
|
|
|
|
? Result<string?>.FromSuccess(null)
|
|
|
|
: Result<string?>.FromSuccess($"UserCannot{action}Target".Localized());
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2023-08-02 23:51:16 +03:00
|
|
|
/// Gets the string mentioning the <see cref="GuildSettings.EventNotificationRole" /> and event subscribers related to
|
|
|
|
/// a scheduled
|
2023-07-09 16:32:14 +03:00
|
|
|
/// event.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="scheduledEvent">
|
2023-07-18 15:25:02 +03:00
|
|
|
/// The scheduled event whose subscribers will be mentioned.
|
2023-07-09 16:32:14 +03:00
|
|
|
/// </param>
|
2023-07-18 15:25:02 +03:00
|
|
|
/// <param name="settings">The settings of the guild containing the scheduled event</param>
|
2023-07-09 16:32:14 +03:00
|
|
|
/// <param name="ct">The cancellation token for this operation.</param>
|
|
|
|
/// <returns>A result containing the string which may or may not have succeeded.</returns>
|
|
|
|
public async Task<Result<string>> GetEventNotificationMentions(
|
2023-08-02 23:51:16 +03:00
|
|
|
IGuildScheduledEvent scheduledEvent, JsonNode settings, CancellationToken ct = default)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
var builder = new StringBuilder();
|
2023-07-18 15:25:02 +03:00
|
|
|
var role = GuildSettings.EventNotificationRole.Get(settings);
|
2023-07-09 16:32:14 +03:00
|
|
|
var usersResult = await _eventApi.GetGuildScheduledEventUsersAsync(
|
|
|
|
scheduledEvent.GuildID, scheduledEvent.ID, withMember: true, ct: ct);
|
2023-08-02 23:51:16 +03:00
|
|
|
if (!usersResult.IsDefined(out var users))
|
|
|
|
{
|
|
|
|
return Result<string>.FromError(usersResult);
|
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-07-18 15:25:02 +03:00
|
|
|
if (role.Value is not 0)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
builder.Append($"{Mention.Role(role)} ");
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
builder = users.Where(
|
2023-08-02 23:51:16 +03:00
|
|
|
user => user.GuildMember.IsDefined(out var member) && !member.Roles.Contains(role))
|
2023-07-18 15:25:02 +03:00
|
|
|
.Aggregate(builder, (current, user) => current.Append($"{Mention.User(user.User)} "));
|
2023-07-09 16:32:14 +03:00
|
|
|
return builder.ToString();
|
|
|
|
}
|
2023-07-20 00:08:44 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs an action in the <see cref="GuildSettings.PublicFeedbackChannel" /> and
|
|
|
|
/// <see cref="GuildSettings.PrivateFeedbackChannel" />.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="cfg">The guild configuration.</param>
|
|
|
|
/// <param name="channelId">The ID of the channel where the action was executed.</param>
|
2023-07-20 10:36:21 +03:00
|
|
|
/// <param name="user">The user who performed the action.</param>
|
2023-07-20 00:08:44 +03:00
|
|
|
/// <param name="title">The title for the embed.</param>
|
|
|
|
/// <param name="description">The description of the embed.</param>
|
2023-07-20 10:36:21 +03:00
|
|
|
/// <param name="avatar">The user whose avatar will be displayed next to the <paramref name="title" /> of the embed.</param>
|
2023-07-23 23:07:36 +03:00
|
|
|
/// <param name="color">The color of the embed.</param>
|
2023-08-02 23:51:16 +03:00
|
|
|
/// <param name="isPublic">
|
|
|
|
/// Whether or not the embed should be sent in <see cref="GuildSettings.PublicFeedbackChannel" />
|
|
|
|
/// </param>
|
2023-07-20 00:08:44 +03:00
|
|
|
/// <param name="ct">The cancellation token for this operation.</param>
|
2023-07-23 23:07:36 +03:00
|
|
|
/// <returns>A result which has succeeded.</returns>
|
2023-07-20 00:08:44 +03:00
|
|
|
public Result LogActionAsync(
|
2023-08-02 23:51:16 +03:00
|
|
|
JsonNode cfg, Snowflake channelId, IUser user, string title, string description, IUser avatar,
|
|
|
|
Color color, bool isPublic = true, CancellationToken ct = default)
|
|
|
|
{
|
2023-07-20 00:08:44 +03:00
|
|
|
var publicChannel = GuildSettings.PublicFeedbackChannel.Get(cfg);
|
|
|
|
var privateChannel = GuildSettings.PrivateFeedbackChannel.Get(cfg);
|
|
|
|
if (GuildSettings.PublicFeedbackChannel.Get(cfg).EmptyOrEqualTo(channelId)
|
|
|
|
&& GuildSettings.PrivateFeedbackChannel.Get(cfg).EmptyOrEqualTo(channelId))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 00:08:44 +03:00
|
|
|
return Result.FromSuccess();
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-20 00:08:44 +03:00
|
|
|
|
|
|
|
var logEmbed = new EmbedBuilder().WithSmallTitle(title, avatar)
|
|
|
|
.WithDescription(description)
|
|
|
|
.WithActionFooter(user)
|
|
|
|
.WithCurrentTimestamp()
|
2023-07-23 23:07:36 +03:00
|
|
|
.WithColour(color)
|
2023-07-20 00:08:44 +03:00
|
|
|
.Build();
|
|
|
|
|
|
|
|
if (!logEmbed.IsDefined(out var logBuilt))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 00:08:44 +03:00
|
|
|
return Result.FromError(logEmbed);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-20 00:08:44 +03:00
|
|
|
|
|
|
|
var builtArray = new[] { logBuilt };
|
|
|
|
|
|
|
|
// Not awaiting to reduce response time
|
2023-07-24 14:57:41 +03:00
|
|
|
if (isPublic && publicChannel != channelId)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 00:08:44 +03:00
|
|
|
_ = _channelApi.CreateMessageAsync(
|
|
|
|
publicChannel, embeds: builtArray,
|
|
|
|
ct: ct);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-20 00:08:44 +03:00
|
|
|
if (privateChannel != publicChannel
|
2023-07-24 14:57:41 +03:00
|
|
|
&& privateChannel != channelId)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 00:08:44 +03:00
|
|
|
_ = _channelApi.CreateMessageAsync(
|
|
|
|
privateChannel, embeds: builtArray,
|
|
|
|
ct: ct);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-20 00:08:44 +03:00
|
|
|
|
|
|
|
return Result.FromSuccess();
|
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|