|
| 1 | +using DSharpPlus; |
| 2 | +using DSharpPlus.Entities; |
| 3 | +using Humanizer; |
| 4 | +using SmartFormat; |
| 5 | +using SuggestionBot.Configuration; |
| 6 | +using SuggestionBot.Data; |
| 7 | +using SuggestionBot.Resources; |
| 8 | +using X10D.DSharpPlus; |
| 9 | + |
| 10 | +namespace SuggestionBot.Services; |
| 11 | + |
| 12 | +internal sealed class MailmanService |
| 13 | +{ |
| 14 | + private readonly DiscordClient _discordClient; |
| 15 | + private readonly ConfigurationService _configurationService; |
| 16 | + private readonly SuggestionService _suggestionService; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Initializes a new instance of the <see cref="MailmanService" /> class. |
| 20 | + /// </summary> |
| 21 | + /// <param name="discordClient">The <see cref="DiscordClient" />.</param> |
| 22 | + /// <param name="configurationService">The <see cref="ConfigurationService" />.</param> |
| 23 | + /// <param name="suggestionService">The <see cref="SuggestionService" />.</param> |
| 24 | + public MailmanService(DiscordClient discordClient, |
| 25 | + ConfigurationService configurationService, |
| 26 | + SuggestionService suggestionService) |
| 27 | + { |
| 28 | + _discordClient = discordClient; |
| 29 | + _configurationService = configurationService; |
| 30 | + _suggestionService = suggestionService; |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Sends a suggestion to the author via DM. |
| 35 | + /// </summary> |
| 36 | + /// <param name="suggestion">The <see cref="Suggestion" /> to send.</param> |
| 37 | + /// <exception cref="ArgumentNullException"><paramref name="suggestion" /> is <see langword="null" />.</exception> |
| 38 | + public async Task SendSuggestionAsync(Suggestion suggestion) |
| 39 | + { |
| 40 | + if (suggestion is null) |
| 41 | + { |
| 42 | + throw new ArgumentNullException(nameof(suggestion)); |
| 43 | + } |
| 44 | + |
| 45 | + if (!_discordClient.Guilds.TryGetValue(suggestion.GuildId, out DiscordGuild? guild)) |
| 46 | + { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + DiscordUser author = _suggestionService.GetAuthor(suggestion); |
| 51 | + DiscordMember? member = await author.GetAsMemberOfAsync(guild).ConfigureAwait(false); |
| 52 | + if (member is null) |
| 53 | + { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + Uri suggestionLink = _suggestionService.GetSuggestionLink(suggestion); |
| 58 | + string iconUrl = guild.GetIconUrl(ImageFormat.Png); |
| 59 | + |
| 60 | + var embed = new DiscordEmbedBuilder(); |
| 61 | + embed.WithThumbnail(iconUrl); |
| 62 | + embed.WithFooter(guild.Name, iconUrl); |
| 63 | + |
| 64 | + if (!TryBuildEmbed(suggestion, embed, author, guild)) |
| 65 | + { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + suggestion.Content = suggestion.Content.Truncate(250, "..."); |
| 70 | + embed.AddField("Your Suggestion", suggestion.Content); |
| 71 | + |
| 72 | + if (!string.IsNullOrWhiteSpace(suggestion.Remarks)) |
| 73 | + { |
| 74 | + embed.AddField("Staff Remarks", suggestion.Remarks); |
| 75 | + } |
| 76 | + |
| 77 | + await member.SendMessageAsync(embed).ConfigureAwait(false); |
| 78 | + } |
| 79 | + |
| 80 | + private bool TryBuildEmbed(Suggestion suggestion, DiscordEmbedBuilder embed, DiscordUser author, DiscordGuild guild) |
| 81 | + { |
| 82 | + if (!_configurationService.TryGetGuildConfiguration(guild, out GuildConfiguration? configuration)) |
| 83 | + { |
| 84 | + configuration = new GuildConfiguration(); |
| 85 | + } |
| 86 | + |
| 87 | + switch (suggestion.Status) |
| 88 | + { |
| 89 | + case SuggestionStatus.Rejected: |
| 90 | + embed.WithColor(configuration.RejectedColor); |
| 91 | + embed.WithTitle("Suggestion Rejected"); |
| 92 | + embed.WithDescription(PrivateMessages.RejectedDescription.FormatSmart(new { user = author, guild })); |
| 93 | + break; |
| 94 | + |
| 95 | + case SuggestionStatus.Accepted: |
| 96 | + embed.WithColor(configuration.AcceptedColor); |
| 97 | + embed.WithTitle("Suggestion Accepted"); |
| 98 | + embed.WithDescription(PrivateMessages.AcceptedDescription.FormatSmart(new { user = author, guild })); |
| 99 | + break; |
| 100 | + |
| 101 | + case SuggestionStatus.Implemented: |
| 102 | + embed.WithColor(configuration.ImplementedColor); |
| 103 | + embed.WithTitle("Suggestion Implemented"); |
| 104 | + embed.WithDescription(PrivateMessages.ImplementedDescription.FormatSmart(new { user = author, guild })); |
| 105 | + break; |
| 106 | + |
| 107 | + default: |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + return true; |
| 112 | + } |
| 113 | +} |
0 commit comments