2023-08-05 21:02:40 +03:00
|
|
|
using System.Text.Json.Nodes;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
|
|
|
using Remora.Discord.API.Objects;
|
|
|
|
using Remora.Discord.Extensions.Embeds;
|
|
|
|
using Remora.Discord.Extensions.Formatting;
|
|
|
|
using Remora.Rest.Core;
|
|
|
|
using Remora.Results;
|
2024-05-16 18:34:26 +03:00
|
|
|
using TeamOctolings.Octobot.Data;
|
|
|
|
using TeamOctolings.Octobot.Extensions;
|
2023-08-05 21:02:40 +03:00
|
|
|
|
2024-05-16 18:34:26 +03:00
|
|
|
namespace TeamOctolings.Octobot.Services.Update;
|
2023-08-05 21:02:40 +03:00
|
|
|
|
|
|
|
public sealed class ScheduledEventUpdateService : BackgroundService
|
|
|
|
{
|
|
|
|
private readonly IDiscordRestChannelAPI _channelApi;
|
|
|
|
private readonly IDiscordRestGuildScheduledEventAPI _eventApi;
|
|
|
|
private readonly GuildDataService _guildData;
|
|
|
|
private readonly ILogger<ScheduledEventUpdateService> _logger;
|
2023-12-20 19:33:52 +03:00
|
|
|
private readonly Utility _utility;
|
2023-08-05 21:02:40 +03:00
|
|
|
|
|
|
|
public ScheduledEventUpdateService(IDiscordRestChannelAPI channelApi, IDiscordRestGuildScheduledEventAPI eventApi,
|
2023-12-20 19:33:52 +03:00
|
|
|
GuildDataService guildData, ILogger<ScheduledEventUpdateService> logger, Utility utility)
|
2023-08-05 21:02:40 +03:00
|
|
|
{
|
|
|
|
_channelApi = channelApi;
|
|
|
|
_eventApi = eventApi;
|
|
|
|
_guildData = guildData;
|
|
|
|
_logger = logger;
|
|
|
|
_utility = utility;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken ct)
|
|
|
|
{
|
|
|
|
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(1));
|
|
|
|
|
|
|
|
while (await timer.WaitForNextTickAsync(ct))
|
|
|
|
{
|
|
|
|
var guildIds = _guildData.GetGuildIds();
|
2023-09-30 18:41:46 +03:00
|
|
|
foreach (var id in guildIds)
|
2023-08-05 21:02:40 +03:00
|
|
|
{
|
|
|
|
var tickResult = await TickScheduledEventsAsync(id, ct);
|
|
|
|
_logger.LogResult(tickResult, $"Error in scheduled events update for guild {id}.");
|
2023-09-30 18:41:46 +03:00
|
|
|
}
|
2023-08-05 21:02:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<Result> TickScheduledEventsAsync(Snowflake guildId, CancellationToken ct)
|
|
|
|
{
|
|
|
|
var failedResults = new List<Result>();
|
|
|
|
var data = await _guildData.GetData(guildId, ct);
|
|
|
|
var eventsResult = await _eventApi.ListScheduledEventsForGuildAsync(guildId, ct: ct);
|
|
|
|
if (!eventsResult.IsDefined(out var events))
|
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(eventsResult);
|
2023-08-05 21:02:40 +03:00
|
|
|
}
|
|
|
|
|
2023-09-30 18:38:52 +03:00
|
|
|
SyncScheduledEvents(data, events);
|
|
|
|
|
2023-08-14 16:24:22 +03:00
|
|
|
foreach (var storedEvent in data.ScheduledEvents.Values)
|
2023-08-05 21:02:40 +03:00
|
|
|
{
|
2023-08-14 16:24:22 +03:00
|
|
|
var scheduledEvent = TryGetScheduledEvent(events, storedEvent.Id);
|
|
|
|
if (!scheduledEvent.IsSuccess)
|
2023-08-05 21:02:40 +03:00
|
|
|
{
|
2023-08-14 16:24:22 +03:00
|
|
|
storedEvent.ScheduleOnStatusUpdated = true;
|
|
|
|
storedEvent.Status = storedEvent.ActualStartTime != null
|
|
|
|
? GuildScheduledEventStatus.Completed
|
|
|
|
: GuildScheduledEventStatus.Canceled;
|
2023-08-05 21:02:40 +03:00
|
|
|
}
|
|
|
|
|
2023-08-14 16:24:22 +03:00
|
|
|
if (!storedEvent.ScheduleOnStatusUpdated)
|
2023-08-05 21:02:40 +03:00
|
|
|
{
|
2023-09-30 18:25:22 +03:00
|
|
|
var tickResult =
|
|
|
|
await TickScheduledEventAsync(guildId, data, scheduledEvent.Entity, storedEvent, ct);
|
2023-08-05 21:02:40 +03:00
|
|
|
failedResults.AddIfFailed(tickResult);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-08-14 16:24:22 +03:00
|
|
|
var statusUpdatedResponseResult = storedEvent.Status switch
|
2023-08-05 21:02:40 +03:00
|
|
|
{
|
|
|
|
GuildScheduledEventStatus.Scheduled =>
|
2023-08-14 16:24:22 +03:00
|
|
|
await SendScheduledEventCreatedMessage(scheduledEvent.Entity, data.Settings, ct),
|
|
|
|
GuildScheduledEventStatus.Canceled =>
|
|
|
|
await SendScheduledEventCancelledMessage(storedEvent, data, ct),
|
|
|
|
GuildScheduledEventStatus.Active =>
|
|
|
|
await SendScheduledEventStartedMessage(scheduledEvent.Entity, data, ct),
|
|
|
|
GuildScheduledEventStatus.Completed =>
|
|
|
|
await SendScheduledEventCompletedMessage(storedEvent, data, ct),
|
|
|
|
_ => new ArgumentOutOfRangeError(nameof(storedEvent.Status))
|
2023-08-05 21:02:40 +03:00
|
|
|
};
|
2023-08-14 16:24:22 +03:00
|
|
|
if (statusUpdatedResponseResult.IsSuccess)
|
2023-08-12 16:47:14 +03:00
|
|
|
{
|
2023-08-14 16:24:22 +03:00
|
|
|
storedEvent.ScheduleOnStatusUpdated = false;
|
2023-08-12 16:47:14 +03:00
|
|
|
}
|
|
|
|
|
2023-08-14 16:24:22 +03:00
|
|
|
failedResults.AddIfFailed(statusUpdatedResponseResult);
|
2023-08-05 21:02:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return failedResults.AggregateErrors();
|
|
|
|
}
|
|
|
|
|
2023-09-30 18:38:52 +03:00
|
|
|
private static void SyncScheduledEvents(GuildData data, IEnumerable<IGuildScheduledEvent> events)
|
2023-09-30 18:25:22 +03:00
|
|
|
{
|
2023-09-30 18:38:52 +03:00
|
|
|
foreach (var @event in events)
|
2023-09-30 18:25:22 +03:00
|
|
|
{
|
2023-11-22 11:19:45 +03:00
|
|
|
if (!data.ScheduledEvents.TryGetValue(@event.ID.Value, out var eventData))
|
2023-09-30 18:25:22 +03:00
|
|
|
{
|
2023-09-30 18:38:52 +03:00
|
|
|
data.ScheduledEvents.Add(@event.ID.Value,
|
|
|
|
new ScheduledEventData(@event.ID.Value, @event.Name, @event.ScheduledStartTime, @event.Status));
|
|
|
|
continue;
|
2023-09-30 18:25:22 +03:00
|
|
|
}
|
2023-09-30 18:38:52 +03:00
|
|
|
|
|
|
|
eventData.Name = @event.Name;
|
|
|
|
eventData.ScheduledStartTime = @event.ScheduledStartTime;
|
2023-10-17 15:25:15 +03:00
|
|
|
if (!eventData.ScheduleOnStatusUpdated)
|
|
|
|
{
|
|
|
|
eventData.ScheduleOnStatusUpdated = eventData.Status != @event.Status;
|
|
|
|
}
|
|
|
|
|
2023-09-30 18:38:52 +03:00
|
|
|
eventData.Status = @event.Status;
|
2023-09-30 18:25:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-14 16:24:22 +03:00
|
|
|
private static Result<IGuildScheduledEvent> TryGetScheduledEvent(IEnumerable<IGuildScheduledEvent> from, ulong id)
|
|
|
|
{
|
|
|
|
var filtered = from.Where(schEvent => schEvent.ID == id);
|
|
|
|
var filteredArray = filtered.ToArray();
|
2023-12-04 18:18:56 +03:00
|
|
|
return filteredArray.Length > 0
|
2023-08-14 16:24:22 +03:00
|
|
|
? Result<IGuildScheduledEvent>.FromSuccess(filteredArray.Single())
|
|
|
|
: new NotFoundError();
|
|
|
|
}
|
|
|
|
|
2023-08-05 21:02:40 +03:00
|
|
|
private async Task<Result> TickScheduledEventAsync(
|
|
|
|
Snowflake guildId, GuildData data, IGuildScheduledEvent scheduledEvent, ScheduledEventData eventData,
|
|
|
|
CancellationToken ct)
|
|
|
|
{
|
|
|
|
if (GuildSettings.AutoStartEvents.Get(data.Settings)
|
|
|
|
&& DateTimeOffset.UtcNow >= scheduledEvent.ScheduledStartTime
|
|
|
|
&& scheduledEvent.Status is not GuildScheduledEventStatus.Active)
|
|
|
|
{
|
|
|
|
return await AutoStartEventAsync(guildId, scheduledEvent, ct);
|
|
|
|
}
|
|
|
|
|
|
|
|
var offset = GuildSettings.EventEarlyNotificationOffset.Get(data.Settings);
|
|
|
|
if (offset == TimeSpan.Zero
|
|
|
|
|| eventData.EarlyNotificationSent
|
|
|
|
|| DateTimeOffset.UtcNow < scheduledEvent.ScheduledStartTime - offset)
|
|
|
|
{
|
2024-03-21 18:55:34 +03:00
|
|
|
return Result.Success;
|
2023-08-05 21:02:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var sendResult = await SendEarlyEventNotificationAsync(scheduledEvent, data, ct);
|
|
|
|
if (sendResult.IsSuccess)
|
|
|
|
{
|
|
|
|
eventData.EarlyNotificationSent = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sendResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<Result> AutoStartEventAsync(
|
|
|
|
Snowflake guildId, IGuildScheduledEvent scheduledEvent, CancellationToken ct)
|
|
|
|
{
|
|
|
|
return (Result)await _eventApi.ModifyGuildScheduledEventAsync(
|
|
|
|
guildId, scheduledEvent.ID,
|
|
|
|
status: GuildScheduledEventStatus.Active, ct: ct);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles sending a notification, mentioning the <see cref="GuildSettings.EventNotificationRole" /> if one is
|
|
|
|
/// set,
|
|
|
|
/// when a scheduled event is created
|
|
|
|
/// in a guild's <see cref="GuildSettings.EventNotificationChannel" /> if one is set.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="scheduledEvent">The scheduled event that has just been created.</param>
|
|
|
|
/// <param name="settings">The settings of the guild containing the scheduled event.</param>
|
|
|
|
/// <param name="ct">The cancellation token for this operation.</param>
|
|
|
|
/// <returns>A notification sending result which may or may not have succeeded.</returns>
|
|
|
|
private async Task<Result> SendScheduledEventCreatedMessage(
|
|
|
|
IGuildScheduledEvent scheduledEvent, JsonNode settings, CancellationToken ct = default)
|
|
|
|
{
|
2023-10-17 15:23:14 +03:00
|
|
|
if (GuildSettings.EventNotificationChannel.Get(settings).Empty())
|
|
|
|
{
|
2024-03-21 18:55:34 +03:00
|
|
|
return Result.Success;
|
2023-10-17 15:23:14 +03:00
|
|
|
}
|
|
|
|
|
2023-08-05 21:02:40 +03:00
|
|
|
if (!scheduledEvent.Creator.IsDefined(out var creator))
|
|
|
|
{
|
|
|
|
return new ArgumentNullError(nameof(scheduledEvent.Creator));
|
|
|
|
}
|
|
|
|
|
|
|
|
var eventDescription = scheduledEvent.Description.IsDefined(out var description)
|
|
|
|
? description
|
|
|
|
: string.Empty;
|
|
|
|
var embedDescriptionResult = scheduledEvent.EntityType switch
|
|
|
|
{
|
|
|
|
GuildScheduledEventEntityType.StageInstance or GuildScheduledEventEntityType.Voice =>
|
|
|
|
GetLocalEventCreatedEmbedDescription(scheduledEvent, eventDescription),
|
|
|
|
GuildScheduledEventEntityType.External => GetExternalScheduledEventCreatedEmbedDescription(
|
|
|
|
scheduledEvent, eventDescription),
|
|
|
|
_ => new ArgumentOutOfRangeError(nameof(scheduledEvent.EntityType))
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!embedDescriptionResult.IsDefined(out var embedDescription))
|
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(embedDescriptionResult);
|
2023-08-05 21:02:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var embed = new EmbedBuilder()
|
|
|
|
.WithSmallTitle(string.Format(Messages.EventCreatedTitle, creator.GetTag()), creator)
|
2023-10-12 13:56:24 +03:00
|
|
|
.WithTitle(Markdown.Sanitize(scheduledEvent.Name))
|
2023-08-05 21:02:40 +03:00
|
|
|
.WithDescription(embedDescription)
|
|
|
|
.WithEventCover(scheduledEvent.ID, scheduledEvent.Image)
|
|
|
|
.WithCurrentTimestamp()
|
|
|
|
.WithColour(ColorsList.White)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
var roleMention = !GuildSettings.EventNotificationRole.Get(settings).Empty()
|
|
|
|
? Mention.Role(GuildSettings.EventNotificationRole.Get(settings))
|
|
|
|
: string.Empty;
|
|
|
|
|
|
|
|
var button = new ButtonComponent(
|
2023-10-11 22:16:52 +03:00
|
|
|
ButtonComponentStyle.Link,
|
2023-12-17 21:35:09 +03:00
|
|
|
Messages.ButtonOpenEventInfo,
|
2024-04-01 15:57:49 +03:00
|
|
|
new PartialEmoji(Name: "\ud83d\udccb"), // 'CLIPBOARD' (U+1F4CB)
|
2023-10-11 22:16:52 +03:00
|
|
|
URL: $"https://discord.com/events/{scheduledEvent.GuildID}/{scheduledEvent.ID}"
|
2023-08-05 21:02:40 +03:00
|
|
|
);
|
|
|
|
|
2023-12-17 19:47:52 +03:00
|
|
|
return await _channelApi.CreateMessageWithEmbedResultAsync(
|
|
|
|
GuildSettings.EventNotificationChannel.Get(settings), roleMention, embedResult: embed,
|
2023-08-05 21:02:40 +03:00
|
|
|
components: new[] { new ActionRowComponent(new[] { button }) }, ct: ct);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Result<string> GetExternalScheduledEventCreatedEmbedDescription(
|
|
|
|
IGuildScheduledEvent scheduledEvent, string eventDescription)
|
|
|
|
{
|
|
|
|
var dataResult = scheduledEvent.TryGetExternalEventData(out var endTime, out var location);
|
|
|
|
if (!dataResult.IsSuccess)
|
|
|
|
{
|
|
|
|
return Result<string>.FromError(dataResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $"{eventDescription}\n\n{Markdown.BlockQuote(
|
|
|
|
string.Format(
|
|
|
|
Messages.DescriptionExternalEventCreated,
|
|
|
|
Markdown.Timestamp(scheduledEvent.ScheduledStartTime),
|
|
|
|
Markdown.Timestamp(endTime),
|
|
|
|
Markdown.InlineCode(location ?? string.Empty)
|
|
|
|
))}";
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Result<string> GetLocalEventCreatedEmbedDescription(
|
|
|
|
IGuildScheduledEvent scheduledEvent, string eventDescription)
|
|
|
|
{
|
|
|
|
if (scheduledEvent.ChannelID is null)
|
|
|
|
{
|
|
|
|
return new ArgumentNullError(nameof(scheduledEvent.ChannelID));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $"{eventDescription}\n\n{Markdown.BlockQuote(
|
|
|
|
string.Format(
|
|
|
|
Messages.DescriptionLocalEventCreated,
|
|
|
|
Markdown.Timestamp(scheduledEvent.ScheduledStartTime),
|
|
|
|
Mention.Channel(scheduledEvent.ChannelID.Value)
|
|
|
|
))}";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles sending a notification, mentioning the <see cref="GuildSettings.EventNotificationRole" /> and event
|
|
|
|
/// subscribers,
|
|
|
|
/// when a scheduled event has started or completed
|
|
|
|
/// in a guild's <see cref="GuildSettings.EventNotificationChannel" /> if one is set.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="scheduledEvent">The scheduled event that is about to start, has started or completed.</param>
|
|
|
|
/// <param name="data">The data for the guild containing the scheduled event.</param>
|
|
|
|
/// <param name="ct">The cancellation token for this operation</param>
|
|
|
|
/// <returns>A reminder/notification sending result which may or may not have succeeded.</returns>
|
2023-08-14 16:24:22 +03:00
|
|
|
private async Task<Result> SendScheduledEventStartedMessage(
|
2023-08-05 21:02:40 +03:00
|
|
|
IGuildScheduledEvent scheduledEvent, GuildData data, CancellationToken ct = default)
|
|
|
|
{
|
2023-08-14 16:24:22 +03:00
|
|
|
data.ScheduledEvents[scheduledEvent.ID.Value].ActualStartTime = DateTimeOffset.UtcNow;
|
2023-08-05 21:02:40 +03:00
|
|
|
|
2023-10-17 15:23:14 +03:00
|
|
|
if (GuildSettings.EventNotificationChannel.Get(data.Settings).Empty())
|
|
|
|
{
|
2024-03-21 18:55:34 +03:00
|
|
|
return Result.Success;
|
2023-10-17 15:23:14 +03:00
|
|
|
}
|
|
|
|
|
2023-08-14 16:24:22 +03:00
|
|
|
var embedDescriptionResult = scheduledEvent.EntityType switch
|
|
|
|
{
|
|
|
|
GuildScheduledEventEntityType.StageInstance or GuildScheduledEventEntityType.Voice =>
|
|
|
|
GetLocalEventStartedEmbedDescription(scheduledEvent),
|
|
|
|
GuildScheduledEventEntityType.External => GetExternalEventStartedEmbedDescription(scheduledEvent),
|
|
|
|
_ => new ArgumentOutOfRangeError(nameof(scheduledEvent.EntityType))
|
|
|
|
};
|
2023-08-05 21:02:40 +03:00
|
|
|
|
2023-08-14 16:24:22 +03:00
|
|
|
var contentResult = await _utility.GetEventNotificationMentions(
|
2023-10-17 15:07:01 +03:00
|
|
|
scheduledEvent, data, ct);
|
2023-08-14 16:24:22 +03:00
|
|
|
if (!contentResult.IsDefined(out var content))
|
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(contentResult);
|
2023-08-14 16:24:22 +03:00
|
|
|
}
|
2023-08-05 21:02:40 +03:00
|
|
|
|
2023-08-14 16:24:22 +03:00
|
|
|
if (!embedDescriptionResult.IsDefined(out var embedDescription))
|
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(embedDescriptionResult);
|
2023-08-05 21:02:40 +03:00
|
|
|
}
|
|
|
|
|
2023-10-17 15:23:14 +03:00
|
|
|
var startedEmbed = new EmbedBuilder()
|
|
|
|
.WithTitle(string.Format(Messages.EventStarted, Markdown.Sanitize(scheduledEvent.Name)))
|
2023-08-14 16:24:22 +03:00
|
|
|
.WithDescription(embedDescription)
|
|
|
|
.WithColour(ColorsList.Green)
|
|
|
|
.WithCurrentTimestamp()
|
|
|
|
.Build();
|
|
|
|
|
2023-12-17 19:47:52 +03:00
|
|
|
return await _channelApi.CreateMessageWithEmbedResultAsync(
|
2023-08-14 16:24:22 +03:00
|
|
|
GuildSettings.EventNotificationChannel.Get(data.Settings),
|
2023-12-17 19:47:52 +03:00
|
|
|
content, embedResult: startedEmbed, ct: ct);
|
2023-08-14 16:24:22 +03:00
|
|
|
}
|
2023-08-05 21:02:40 +03:00
|
|
|
|
2023-08-14 16:24:22 +03:00
|
|
|
private async Task<Result> SendScheduledEventCompletedMessage(ScheduledEventData eventData, GuildData data,
|
|
|
|
CancellationToken ct)
|
|
|
|
{
|
2023-09-30 18:50:58 +03:00
|
|
|
if (GuildSettings.EventNotificationChannel.Get(data.Settings).Empty())
|
|
|
|
{
|
|
|
|
data.ScheduledEvents.Remove(eventData.Id);
|
2024-03-21 18:55:34 +03:00
|
|
|
return Result.Success;
|
2023-09-30 18:50:58 +03:00
|
|
|
}
|
|
|
|
|
2023-10-17 15:23:14 +03:00
|
|
|
var completedEmbed = new EmbedBuilder()
|
|
|
|
.WithTitle(string.Format(Messages.EventCompleted, Markdown.Sanitize(eventData.Name)))
|
2023-08-05 21:02:40 +03:00
|
|
|
.WithDescription(
|
|
|
|
string.Format(
|
|
|
|
Messages.EventDuration,
|
|
|
|
DateTimeOffset.UtcNow.Subtract(
|
2023-08-14 16:24:22 +03:00
|
|
|
eventData.ActualStartTime
|
|
|
|
?? eventData.ScheduledStartTime).ToString()))
|
2023-08-05 21:02:40 +03:00
|
|
|
.WithColour(ColorsList.Black)
|
|
|
|
.WithCurrentTimestamp()
|
|
|
|
.Build();
|
|
|
|
|
2023-12-17 19:47:52 +03:00
|
|
|
var createResult = await _channelApi.CreateMessageWithEmbedResultAsync(
|
2023-08-05 21:02:40 +03:00
|
|
|
GuildSettings.EventNotificationChannel.Get(data.Settings),
|
2023-12-17 19:47:52 +03:00
|
|
|
embedResult: completedEmbed, ct: ct);
|
2023-08-14 16:24:22 +03:00
|
|
|
if (createResult.IsSuccess)
|
|
|
|
{
|
|
|
|
data.ScheduledEvents.Remove(eventData.Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return createResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<Result> SendScheduledEventCancelledMessage(ScheduledEventData eventData, GuildData data,
|
|
|
|
CancellationToken ct)
|
|
|
|
{
|
|
|
|
if (GuildSettings.EventNotificationChannel.Get(data.Settings).Empty())
|
|
|
|
{
|
2023-09-30 18:50:58 +03:00
|
|
|
data.ScheduledEvents.Remove(eventData.Id);
|
2024-03-21 18:55:34 +03:00
|
|
|
return Result.Success;
|
2023-08-14 16:24:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var embed = new EmbedBuilder()
|
2023-10-12 13:56:24 +03:00
|
|
|
.WithSmallTitle(string.Format(Messages.EventCancelled, Markdown.Sanitize(eventData.Name)))
|
2023-08-14 16:24:22 +03:00
|
|
|
.WithDescription(":(")
|
|
|
|
.WithColour(ColorsList.Red)
|
|
|
|
.WithCurrentTimestamp()
|
|
|
|
.Build();
|
|
|
|
|
2023-12-17 19:47:52 +03:00
|
|
|
var createResult = await _channelApi.CreateMessageWithEmbedResultAsync(
|
|
|
|
GuildSettings.EventNotificationChannel.Get(data.Settings), embedResult: embed, ct: ct);
|
2023-08-14 16:24:22 +03:00
|
|
|
if (createResult.IsSuccess)
|
|
|
|
{
|
|
|
|
data.ScheduledEvents.Remove(eventData.Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return createResult;
|
2023-08-05 21:02:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private static Result<string> GetLocalEventStartedEmbedDescription(IGuildScheduledEvent scheduledEvent)
|
|
|
|
{
|
|
|
|
if (scheduledEvent.ChannelID is null)
|
|
|
|
{
|
|
|
|
return new ArgumentNullError(nameof(scheduledEvent.ChannelID));
|
|
|
|
}
|
|
|
|
|
|
|
|
return string.Format(
|
|
|
|
Messages.DescriptionLocalEventStarted,
|
|
|
|
Mention.Channel(scheduledEvent.ChannelID.Value)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Result<string> GetExternalEventStartedEmbedDescription(IGuildScheduledEvent scheduledEvent)
|
|
|
|
{
|
|
|
|
var dataResult = scheduledEvent.TryGetExternalEventData(out var endTime, out var location);
|
|
|
|
if (!dataResult.IsSuccess)
|
|
|
|
{
|
|
|
|
return Result<string>.FromError(dataResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
return string.Format(
|
|
|
|
Messages.DescriptionExternalEventStarted,
|
|
|
|
Markdown.InlineCode(location ?? string.Empty),
|
|
|
|
Markdown.Timestamp(endTime)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<Result> SendEarlyEventNotificationAsync(
|
|
|
|
IGuildScheduledEvent scheduledEvent, GuildData data, CancellationToken ct)
|
|
|
|
{
|
2023-10-17 15:23:14 +03:00
|
|
|
if (GuildSettings.EventNotificationChannel.Get(data.Settings).Empty())
|
|
|
|
{
|
2024-03-21 18:55:34 +03:00
|
|
|
return Result.Success;
|
2023-10-17 15:23:14 +03:00
|
|
|
}
|
|
|
|
|
2023-08-05 21:02:40 +03:00
|
|
|
var contentResult = await _utility.GetEventNotificationMentions(
|
2023-10-17 15:07:01 +03:00
|
|
|
scheduledEvent, data, ct);
|
2023-08-05 21:02:40 +03:00
|
|
|
if (!contentResult.IsDefined(out var content))
|
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(contentResult);
|
2023-08-05 21:02:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var earlyResult = new EmbedBuilder()
|
2023-08-12 23:19:07 +03:00
|
|
|
.WithDescription(
|
2023-10-12 13:56:24 +03:00
|
|
|
string.Format(Messages.EventEarlyNotification, Markdown.Sanitize(scheduledEvent.Name),
|
2023-08-12 23:19:07 +03:00
|
|
|
Markdown.Timestamp(scheduledEvent.ScheduledStartTime, TimestampStyle.RelativeTime)))
|
2023-08-05 21:02:40 +03:00
|
|
|
.WithColour(ColorsList.Default)
|
|
|
|
.Build();
|
|
|
|
|
2023-12-17 19:47:52 +03:00
|
|
|
return await _channelApi.CreateMessageWithEmbedResultAsync(
|
2023-08-05 21:02:40 +03:00
|
|
|
GuildSettings.EventNotificationChannel.Get(data.Settings),
|
|
|
|
content,
|
2023-12-17 19:47:52 +03:00
|
|
|
embedResult: earlyResult, ct: ct);
|
2023-08-05 21:02:40 +03:00
|
|
|
}
|
|
|
|
}
|