From 580cd24810c1a96230ed1ad1e2c01ac6e026fb66 Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Tue, 17 Oct 2023 17:23:14 +0500 Subject: [PATCH] Do not try to send messages in empty EventNotificationChannels (#167) This PR fixes an error that would occur if an event was created, was about to start or started and the EventNotificationChannel was empty. --- .../Update/ScheduledEventUpdateService.cs | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Services/Update/ScheduledEventUpdateService.cs b/src/Services/Update/ScheduledEventUpdateService.cs index 792eef9..1672e00 100644 --- a/src/Services/Update/ScheduledEventUpdateService.cs +++ b/src/Services/Update/ScheduledEventUpdateService.cs @@ -177,6 +177,11 @@ public sealed class ScheduledEventUpdateService : BackgroundService private async Task SendScheduledEventCreatedMessage( IGuildScheduledEvent scheduledEvent, JsonNode settings, CancellationToken ct = default) { + if (GuildSettings.EventNotificationChannel.Get(settings).Empty()) + { + return Result.FromSuccess(); + } + if (!scheduledEvent.Creator.IsDefined(out var creator)) { return new ArgumentNullError(nameof(scheduledEvent.Creator)); @@ -277,6 +282,11 @@ public sealed class ScheduledEventUpdateService : BackgroundService { data.ScheduledEvents[scheduledEvent.ID.Value].ActualStartTime = DateTimeOffset.UtcNow; + if (GuildSettings.EventNotificationChannel.Get(data.Settings).Empty()) + { + return Result.FromSuccess(); + } + var embedDescriptionResult = scheduledEvent.EntityType switch { GuildScheduledEventEntityType.StageInstance or GuildScheduledEventEntityType.Voice => @@ -297,7 +307,8 @@ public sealed class ScheduledEventUpdateService : BackgroundService return Result.FromError(embedDescriptionResult); } - var startedEmbed = new EmbedBuilder().WithTitle(string.Format(Messages.EventStarted, Markdown.Sanitize(scheduledEvent.Name))) + var startedEmbed = new EmbedBuilder() + .WithTitle(string.Format(Messages.EventStarted, Markdown.Sanitize(scheduledEvent.Name))) .WithDescription(embedDescription) .WithColour(ColorsList.Green) .WithCurrentTimestamp() @@ -322,7 +333,8 @@ public sealed class ScheduledEventUpdateService : BackgroundService return Result.FromSuccess(); } - var completedEmbed = new EmbedBuilder().WithTitle(string.Format(Messages.EventCompleted, Markdown.Sanitize(eventData.Name))) + var completedEmbed = new EmbedBuilder() + .WithTitle(string.Format(Messages.EventCompleted, Markdown.Sanitize(eventData.Name))) .WithDescription( string.Format( Messages.EventDuration, @@ -411,6 +423,11 @@ public sealed class ScheduledEventUpdateService : BackgroundService private async Task SendEarlyEventNotificationAsync( IGuildScheduledEvent scheduledEvent, GuildData data, CancellationToken ct) { + if (GuildSettings.EventNotificationChannel.Get(data.Settings).Empty()) + { + return Result.FromSuccess(); + } + var contentResult = await _utility.GetEventNotificationMentions( scheduledEvent, data, ct); if (!contentResult.IsDefined(out var content))