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;
|
2024-05-16 18:34:26 +03:00
|
|
|
using Microsoft.Extensions.Logging;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
2024-05-16 18:34:26 +03:00
|
|
|
using Remora.Discord.API.Objects;
|
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;
|
2024-05-16 18:34:26 +03:00
|
|
|
using TeamOctolings.Octobot.Attributes;
|
|
|
|
using TeamOctolings.Octobot.Data;
|
|
|
|
using TeamOctolings.Octobot.Extensions;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2024-05-16 18:34:26 +03:00
|
|
|
namespace TeamOctolings.Octobot;
|
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-12-20 19:33:52 +03:00
|
|
|
public sealed class Utility
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2024-05-16 18:34:26 +03:00
|
|
|
public static readonly AllowedMentions NoMentions = new(
|
|
|
|
Array.Empty<MentionType>(), Array.Empty<Snowflake>(), Array.Empty<Snowflake>());
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
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;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-12-20 19:33:52 +03:00
|
|
|
public Utility(
|
2024-03-24 21:29:10 +03:00
|
|
|
IDiscordRestChannelAPI channelApi, IDiscordRestGuildScheduledEventAPI eventApi, IDiscordRestGuildAPI guildApi)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 00:08:44 +03:00
|
|
|
_channelApi = channelApi;
|
|
|
|
_eventApi = eventApi;
|
2023-07-09 16:32:14 +03:00
|
|
|
_guildApi = guildApi;
|
|
|
|
}
|
|
|
|
|
2024-05-16 18:34:26 +03:00
|
|
|
[StaticCallersOnly]
|
|
|
|
public static ILogger<Program>? StaticLogger { get; set; }
|
|
|
|
|
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-10-17 15:07:01 +03:00
|
|
|
/// <param name="data">The data 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-10-17 15:07:01 +03:00
|
|
|
IGuildScheduledEvent scheduledEvent, GuildData data, CancellationToken ct = default)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
var builder = new StringBuilder();
|
2023-10-17 15:07:01 +03:00
|
|
|
var role = GuildSettings.EventNotificationRole.Get(data.Settings);
|
2023-10-04 18:21:10 +03:00
|
|
|
var subscribersResult = await _eventApi.GetGuildScheduledEventUsersAsync(
|
2023-10-17 15:07:01 +03:00
|
|
|
scheduledEvent.GuildID, scheduledEvent.ID, ct: ct);
|
2023-10-04 18:21:10 +03:00
|
|
|
if (!subscribersResult.IsDefined(out var subscribers))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-10-04 18:21:10 +03:00
|
|
|
return Result<string>.FromError(subscribersResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-09-30 18:53:05 +03:00
|
|
|
if (!role.Empty())
|
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
|
|
|
|
2023-10-04 18:21:10 +03:00
|
|
|
builder = subscribers.Where(
|
2023-10-17 15:07:01 +03:00
|
|
|
subscriber => !data.GetOrCreateMemberData(subscriber.User.ID).Roles.Contains(role.Value))
|
2023-10-04 18:21:10 +03:00
|
|
|
.Aggregate(builder, (current, subscriber) => current.Append($"{Mention.User(subscriber.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>
|
2024-02-06 21:39:20 +03:00
|
|
|
public void LogAction(
|
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
|
|
|
{
|
2024-02-06 21:39:20 +03:00
|
|
|
return;
|
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();
|
|
|
|
|
|
|
|
// 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-12-17 19:47:52 +03:00
|
|
|
_ = _channelApi.CreateMessageWithEmbedResultAsync(
|
|
|
|
publicChannel, embedResult: logEmbed,
|
2023-07-20 00:08:44 +03:00
|
|
|
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-12-17 19:47:52 +03:00
|
|
|
_ = _channelApi.CreateMessageWithEmbedResultAsync(
|
|
|
|
privateChannel, embedResult: logEmbed,
|
2023-07-20 00:08:44 +03:00
|
|
|
ct: ct);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-20 00:08:44 +03:00
|
|
|
}
|
2023-11-04 21:33:37 +03:00
|
|
|
|
2024-06-25 13:09:45 +03:00
|
|
|
public async Task<Result<Snowflake>> GetEmergencyFeedbackChannel(IGuild guild, GuildData data, CancellationToken ct = default)
|
2023-11-04 21:33:37 +03:00
|
|
|
{
|
|
|
|
var privateFeedback = GuildSettings.PrivateFeedbackChannel.Get(data.Settings);
|
|
|
|
if (!privateFeedback.Empty())
|
|
|
|
{
|
|
|
|
return privateFeedback;
|
|
|
|
}
|
|
|
|
|
|
|
|
var publicFeedback = GuildSettings.PublicFeedbackChannel.Get(data.Settings);
|
|
|
|
if (!publicFeedback.Empty())
|
|
|
|
{
|
|
|
|
return publicFeedback;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (guild.SystemChannelID.AsOptional().IsDefined(out var systemChannel))
|
|
|
|
{
|
|
|
|
return systemChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
var channelsResult = await _guildApi.GetGuildChannelsAsync(guild.ID, ct);
|
|
|
|
|
|
|
|
return channelsResult.IsDefined(out var channels)
|
|
|
|
? channels[0].ID
|
|
|
|
: Result<Snowflake>.FromError(channelsResult);
|
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|