New: Customizable Discord Notifications

(cherry picked from commit 470c9101ae825f8ad492e02f62e70d51d0e5b11a)
pull/1923/head
Qstick 4 years ago
parent f8f857376a
commit 05323bd498

@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using FluentValidation.Results; using FluentValidation.Results;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.MediaCover;
using NzbDrone.Core.Music; using NzbDrone.Core.Music;
using NzbDrone.Core.Notifications.Discord.Payloads; using NzbDrone.Core.Notifications.Discord.Payloads;
using NzbDrone.Core.Validation; using NzbDrone.Core.Validation;
@ -22,34 +24,184 @@ namespace NzbDrone.Core.Notifications.Discord
public override void OnGrab(GrabMessage message) public override void OnGrab(GrabMessage message)
{ {
var embeds = new List<Embed> var artist = message.Artist;
var albums = message.Album.Albums;
var artistMetadata = message.Artist.Metadata.Value;
var embed = new Embed
{ {
new Embed Author = new DiscordAuthor
{ {
Description = message.Message, Name = Settings.Author.IsNullOrWhiteSpace() ? Environment.MachineName : Settings.Author,
Title = message.Artist.Name, IconUrl = "https://raw.githubusercontent.com/lidarr/Lidarr/develop/Logo/256.png"
Text = message.Message, },
Color = (int)DiscordColors.Warning Url = $"https://musicbrainz.org/artist/{artist.ForeignArtistId}",
Description = "Album Grabbed",
Title = GetTitle(artist, albums),
Color = (int)DiscordColors.Standard,
Fields = new List<DiscordField>(),
Timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
};
if (Settings.GrabFields.Contains((int)DiscordGrabFieldType.Poster))
{
embed.Thumbnail = new DiscordImage
{
Url = albums.First().Images.FirstOrDefault(x => x.CoverType == MediaCoverTypes.Cover)?.Url
};
} }
if (Settings.GrabFields.Contains((int)DiscordGrabFieldType.Fanart))
{
embed.Image = new DiscordImage
{
Url = artistMetadata.Images.FirstOrDefault(x => x.CoverType == MediaCoverTypes.Fanart)?.Url
}; };
var payload = CreatePayload($"Grabbed: {message.Message}", embeds); }
foreach (var field in Settings.GrabFields)
{
var discordField = new DiscordField();
switch ((DiscordGrabFieldType)field)
{
case DiscordGrabFieldType.Overview:
var overview = albums.First().Overview ?? "";
discordField.Name = "Overview";
discordField.Value = overview.Length <= 300 ? overview : overview.Substring(0, 300) + "...";
break;
case DiscordGrabFieldType.Rating:
discordField.Name = "Rating";
discordField.Value = albums.First().Ratings.Value.ToString();
break;
case DiscordGrabFieldType.Genres:
discordField.Name = "Genres";
discordField.Value = albums.First().Genres.Take(5).Join(", ");
break;
case DiscordGrabFieldType.Quality:
discordField.Name = "Quality";
discordField.Inline = true;
discordField.Value = message.Quality.Quality.Name;
break;
case DiscordGrabFieldType.Group:
discordField.Name = "Group";
discordField.Value = message.Album.ParsedAlbumInfo.ReleaseGroup;
break;
case DiscordGrabFieldType.Size:
discordField.Name = "Size";
discordField.Value = BytesToString(message.Album.Release.Size);
discordField.Inline = true;
break;
case DiscordGrabFieldType.Release:
discordField.Name = "Release";
discordField.Value = string.Format("```{0}```", message.Album.Release.Title);
break;
case DiscordGrabFieldType.Links:
discordField.Name = "Links";
discordField.Value = GetLinksString(artist);
break;
}
if (discordField.Name.IsNotNullOrWhiteSpace() && discordField.Value.IsNotNullOrWhiteSpace())
{
embed.Fields.Add(discordField);
}
}
var payload = CreatePayload(null, new List<Embed> { embed });
_proxy.SendPayload(payload, Settings); _proxy.SendPayload(payload, Settings);
} }
public override void OnReleaseImport(AlbumDownloadMessage message) public override void OnReleaseImport(AlbumDownloadMessage message)
{ {
var attachments = new List<Embed> var artist = message.Artist;
var artistMetadata = message.Artist.Metadata.Value;
var album = message.Album;
var episodes = message.TrackFiles;
var isUpgrade = message.OldFiles.Count > 0;
var embed = new Embed
{ {
new Embed Author = new DiscordAuthor
{ {
Description = message.Message, Name = Settings.Author.IsNullOrWhiteSpace() ? Environment.MachineName : Settings.Author,
Title = message.Artist.Name, IconUrl = "https://raw.githubusercontent.com/lidarr/Lidarr/develop/Logo/256.png"
Text = message.Message, },
Color = (int)DiscordColors.Success Url = $"https://musicbrainz.org/artist/{artist.ForeignArtistId}",
Description = isUpgrade ? "Album Upgraded" : "Album Imported",
Title = GetTitle(artist, new List<Album> { album }),
Color = isUpgrade ? (int)DiscordColors.Upgrade : (int)DiscordColors.Standard,
Fields = new List<DiscordField>(),
Timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
};
if (Settings.ImportFields.Contains((int)DiscordImportFieldType.Poster))
{
embed.Thumbnail = new DiscordImage
{
Url = album.Images.FirstOrDefault(x => x.CoverType == MediaCoverTypes.Cover)?.Url
};
} }
if (Settings.ImportFields.Contains((int)DiscordImportFieldType.Fanart))
{
embed.Image = new DiscordImage
{
Url = artistMetadata.Images.FirstOrDefault(x => x.CoverType == MediaCoverTypes.Fanart)?.Url
}; };
var payload = CreatePayload($"Imported: {message.Message}", attachments); }
foreach (var field in Settings.ImportFields)
{
var discordField = new DiscordField();
switch ((DiscordImportFieldType)field)
{
case DiscordImportFieldType.Overview:
var overview = album.Overview ?? "";
discordField.Name = "Overview";
discordField.Value = overview.Length <= 300 ? overview : overview.Substring(0, 300) + "...";
break;
case DiscordImportFieldType.Rating:
discordField.Name = "Rating";
discordField.Value = album.Ratings.Value.ToString();
break;
case DiscordImportFieldType.Genres:
discordField.Name = "Genres";
discordField.Value = album.Genres.Take(5).Join(", ");
break;
case DiscordImportFieldType.Quality:
discordField.Name = "Quality";
discordField.Inline = true;
discordField.Value = message.TrackFiles.First().Quality.Quality.Name;
break;
case DiscordImportFieldType.Group:
discordField.Name = "Group";
discordField.Value = message.TrackFiles.First().ReleaseGroup;
break;
case DiscordImportFieldType.Size:
discordField.Name = "Size";
discordField.Value = BytesToString(message.TrackFiles.Sum(x => x.Size));
discordField.Inline = true;
break;
case DiscordImportFieldType.Release:
discordField.Name = "Release";
discordField.Value = message.TrackFiles.First().SceneName;
break;
case DiscordImportFieldType.Links:
discordField.Name = "Links";
discordField.Value = GetLinksString(artist);
break;
}
if (discordField.Name.IsNotNullOrWhiteSpace() && discordField.Value.IsNotNullOrWhiteSpace())
{
embed.Fields.Add(discordField);
}
}
var payload = CreatePayload(null, new List<Embed> { embed });
_proxy.SendPayload(payload, Settings); _proxy.SendPayload(payload, Settings);
} }
@ -75,13 +227,19 @@ namespace NzbDrone.Core.Notifications.Discord
{ {
new Embed new Embed
{ {
Author = new DiscordAuthor
{
Name = Settings.Author.IsNullOrWhiteSpace() ? Environment.MachineName : Settings.Author,
IconUrl = "https://raw.githubusercontent.com/lidarr/Lidarr/develop/Logo/256.png"
},
Title = healthCheck.Source.Name, Title = healthCheck.Source.Name,
Text = healthCheck.Message, Description = healthCheck.Message,
Timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"),
Color = healthCheck.Type == HealthCheck.HealthCheckResult.Warning ? (int)DiscordColors.Warning : (int)DiscordColors.Danger Color = healthCheck.Type == HealthCheck.HealthCheckResult.Warning ? (int)DiscordColors.Warning : (int)DiscordColors.Danger
} }
}; };
var payload = CreatePayload("Health Issue", attachments); var payload = CreatePayload(null, attachments);
_proxy.SendPayload(payload, Settings); _proxy.SendPayload(payload, Settings);
} }
@ -92,6 +250,11 @@ namespace NzbDrone.Core.Notifications.Discord
{ {
new Embed new Embed
{ {
Author = new DiscordAuthor
{
Name = Settings.Author.IsNullOrWhiteSpace() ? Environment.MachineName : Settings.Author,
IconUrl = "https://raw.githubusercontent.com/lidarr/Lidarr/develop/Logo/256.png"
},
Title = TRACK_RETAGGED_TITLE, Title = TRACK_RETAGGED_TITLE,
Text = message.Message Text = message.Message
} }
@ -108,6 +271,11 @@ namespace NzbDrone.Core.Notifications.Discord
{ {
new Embed new Embed
{ {
Author = new DiscordAuthor
{
Name = Settings.Author.IsNullOrWhiteSpace() ? Environment.MachineName : Settings.Author,
IconUrl = "https://raw.githubusercontent.com/lidarr/Lidarr/develop/Logo/256.png"
},
Description = message.Message, Description = message.Message,
Title = message.SourceTitle, Title = message.SourceTitle,
Text = message.Message, Text = message.Message,
@ -125,6 +293,11 @@ namespace NzbDrone.Core.Notifications.Discord
{ {
new Embed new Embed
{ {
Author = new DiscordAuthor
{
Name = Settings.Author.IsNullOrWhiteSpace() ? Environment.MachineName : Settings.Author,
IconUrl = "https://raw.githubusercontent.com/lidarr/Lidarr/develop/Logo/256.png"
},
Description = message.Message, Description = message.Message,
Title = message.Album.Title, Title = message.Album.Title,
Text = message.Message, Text = message.Message,
@ -185,5 +358,35 @@ namespace NzbDrone.Core.Notifications.Discord
return payload; return payload;
} }
private string BytesToString(long byteCount)
{
string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB
if (byteCount == 0)
{
return "0 " + suf[0];
}
var bytes = Math.Abs(byteCount);
var place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
var num = Math.Round(bytes / Math.Pow(1024, place), 1);
return string.Format("{0} {1}", (Math.Sign(byteCount) * num).ToString(), suf[place]);
}
private string GetLinksString(Artist artist)
{
var links = new List<string>();
links.Add($"[MusicBrainz](https://musicbrainz.org/artist/{artist.ForeignArtistId})");
return string.Join(" / ", links);
}
private string GetTitle(Artist artist, List<Album> albums)
{
var albumTitles = string.Join(" + ", albums.Select(e => e.Title));
return $"{artist.Name} - {albumTitles}";
}
} }
} }

@ -1,9 +1,11 @@
namespace NzbDrone.Core.Notifications.Discord namespace NzbDrone.Core.Notifications.Discord
{ {
public enum DiscordColors public enum DiscordColors
{ {
Danger = 15749200, Danger = 15749200,
Success = 2605644, Success = 2605644,
Warning = 16753920 Warning = 16753920,
Standard = 16761392,
Upgrade = 7105644
} }
} }

@ -0,0 +1,30 @@
namespace NzbDrone.Core.Notifications.Discord
{
public enum DiscordGrabFieldType
{
Overview,
Rating,
Genres,
Quality,
Group,
Size,
Links,
Release,
Poster,
Fanart
}
public enum DiscordImportFieldType
{
Overview,
Rating,
Genres,
Quality,
Group,
Size,
Links,
Release,
Poster,
Fanart
}
}

@ -1,3 +1,4 @@
using System.Collections.Generic;
using FluentValidation; using FluentValidation;
using NzbDrone.Core.Annotations; using NzbDrone.Core.Annotations;
using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.ThingiProvider;
@ -15,6 +16,13 @@ namespace NzbDrone.Core.Notifications.Discord
public class DiscordSettings : IProviderConfig public class DiscordSettings : IProviderConfig
{ {
public DiscordSettings()
{
//Set Default Fields
GrabFields = new int[] { 0, 1, 2, 3, 5, 6, 7, 8, 9 };
ImportFields = new int[] { 0, 1, 2, 3, 5, 6, 7, 8, 9 };
}
private static readonly DiscordSettingsValidator Validator = new DiscordSettingsValidator(); private static readonly DiscordSettingsValidator Validator = new DiscordSettingsValidator();
[FieldDefinition(0, Label = "Webhook URL", HelpText = "Discord channel webhook url")] [FieldDefinition(0, Label = "Webhook URL", HelpText = "Discord channel webhook url")]
@ -26,6 +34,15 @@ namespace NzbDrone.Core.Notifications.Discord
[FieldDefinition(2, Label = "Avatar", HelpText = "Change the avatar that is used for messages from this integration", Type = FieldType.Textbox)] [FieldDefinition(2, Label = "Avatar", HelpText = "Change the avatar that is used for messages from this integration", Type = FieldType.Textbox)]
public string Avatar { get; set; } public string Avatar { get; set; }
[FieldDefinition(3, Label = "Host", Advanced = true, HelpText = "Override the Host that shows for this notification, Blank is machine name", Type = FieldType.Textbox)]
public string Author { get; set; }
[FieldDefinition(4, Label = "On Grab Fields", Advanced = true, SelectOptions = typeof(DiscordGrabFieldType), HelpText = "Change the fields that are passed in for this 'on grab' notification", Type = FieldType.TagSelect)]
public IEnumerable<int> GrabFields { get; set; }
[FieldDefinition(5, Label = "On Import Fields", Advanced = true, SelectOptions = typeof(DiscordImportFieldType), HelpText = "Change the fields that are passed for this 'on import' notification", Type = FieldType.TagSelect)]
public IEnumerable<int> ImportFields { get; set; }
public NzbDroneValidationResult Validate() public NzbDroneValidationResult Validate()
{ {
return new NzbDroneValidationResult(Validator.Validate(this)); return new NzbDroneValidationResult(Validator.Validate(this));

@ -0,0 +1,12 @@
using Newtonsoft.Json;
namespace NzbDrone.Core.Notifications.Discord.Payloads
{
public class DiscordAuthor
{
public string Name { get; set; }
[JsonProperty("icon_url")]
public string IconUrl { get; set; }
}
}

@ -0,0 +1,9 @@
namespace NzbDrone.Core.Notifications.Discord.Payloads
{
public class DiscordField
{
public string Name { get; set; }
public string Value { get; set; }
public bool Inline { get; set; }
}
}

@ -0,0 +1,7 @@
namespace NzbDrone.Core.Notifications.Discord.Payloads
{
public class DiscordImage
{
public string Url { get; set; }
}
}

@ -1,3 +1,5 @@
using System.Collections.Generic;
namespace NzbDrone.Core.Notifications.Discord.Payloads namespace NzbDrone.Core.Notifications.Discord.Payloads
{ {
public class Embed public class Embed
@ -6,5 +8,11 @@ namespace NzbDrone.Core.Notifications.Discord.Payloads
public string Title { get; set; } public string Title { get; set; }
public string Text { get; set; } public string Text { get; set; }
public int Color { get; set; } public int Color { get; set; }
public string Url { get; set; }
public DiscordAuthor Author { get; set; }
public DiscordImage Thumbnail { get; set; }
public DiscordImage Image { get; set; }
public string Timestamp { get; set; }
public List<DiscordField> Fields { get; set; }
} }
} }

Loading…
Cancel
Save