From c01abbf3b524e5befbb065cc074d602ea8a5a86c Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 14 Jul 2024 21:57:40 -0700 Subject: [PATCH] Add 'On Import Complete' for Discord notifications --- .../Notifications/Discord/Discord.cs | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/src/NzbDrone.Core/Notifications/Discord/Discord.cs b/src/NzbDrone.Core/Notifications/Discord/Discord.cs index 0f32c1f86..8ef8dbf62 100644 --- a/src/NzbDrone.Core/Notifications/Discord/Discord.cs +++ b/src/NzbDrone.Core/Notifications/Discord/Discord.cs @@ -236,6 +236,96 @@ namespace NzbDrone.Core.Notifications.Discord _proxy.SendPayload(payload, Settings); } + public override void OnImportComplete(ImportCompleteMessage message) + { + var series = message.Series; + var episodes = message.Episodes; + + var embed = new Embed + { + Author = new DiscordAuthor + { + Name = Settings.Author.IsNullOrWhiteSpace() ? Environment.MachineName : Settings.Author, + IconUrl = "https://raw.githubusercontent.com/Sonarr/Sonarr/develop/Logo/256.png" + }, + Url = $"http://thetvdb.com/?tab=series&id={series.TvdbId}", + Description = "Import Complete", + Title = GetTitle(series, episodes), + Color = (int)DiscordColors.Success, + Fields = new List(), + Timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ") + }; + + if (Settings.ImportFields.Contains((int)DiscordImportFieldType.Poster)) + { + embed.Thumbnail = new DiscordImage + { + Url = series.Images.FirstOrDefault(x => x.CoverType == MediaCoverTypes.Poster)?.RemoteUrl + }; + } + + if (Settings.ImportFields.Contains((int)DiscordImportFieldType.Fanart)) + { + embed.Image = new DiscordImage + { + Url = series.Images.FirstOrDefault(x => x.CoverType == MediaCoverTypes.Fanart)?.RemoteUrl + }; + } + + foreach (var field in Settings.ImportFields) + { + var discordField = new DiscordField(); + + switch ((DiscordImportFieldType)field) + { + case DiscordImportFieldType.Overview: + var overview = episodes.First().Overview ?? ""; + discordField.Name = "Overview"; + discordField.Value = overview.Length <= 300 ? overview : $"{overview.AsSpan(0, 300)}..."; + break; + case DiscordImportFieldType.Rating: + discordField.Name = "Rating"; + discordField.Value = episodes.First().Ratings.Value.ToString(); + break; + case DiscordImportFieldType.Genres: + discordField.Name = "Genres"; + discordField.Value = series.Genres.Take(5).Join(", "); + break; + case DiscordImportFieldType.Quality: + discordField.Name = "Quality"; + discordField.Inline = true; + discordField.Value = message.ReleaseQuality.Quality.Name; + break; + case DiscordImportFieldType.Group: + discordField.Name = "Group"; + discordField.Value = message.ReleaseGroup; + break; + case DiscordImportFieldType.Size: + discordField.Name = "Size"; + discordField.Value = BytesToString(message.Release?.Size ?? message.EpisodeFiles.Sum(f => f.Size)); + discordField.Inline = true; + break; + case DiscordImportFieldType.Release: + discordField.Name = "Release"; + discordField.Value = $"```{message.Release?.Title ?? message.SourceTitle}```"; + break; + case DiscordImportFieldType.Links: + discordField.Name = "Links"; + discordField.Value = GetLinksString(series); + break; + } + + if (discordField.Name.IsNotNullOrWhiteSpace() && discordField.Value.IsNotNullOrWhiteSpace()) + { + embed.Fields.Add(discordField); + } + } + + var payload = CreatePayload(null, new List { embed }); + + _proxy.SendPayload(payload, Settings); + } + public override void OnRename(Series series, List renamedFiles) { var attachments = new List