diff options
| author | Jake Mannens <jake@asger.xyz> | 2024-10-17 10:43:24 +1100 |
|---|---|---|
| committer | Jake Mannens <jake@asger.xyz> | 2024-10-29 13:06:09 +1100 |
| commit | eb295edf539042f37888fac4b08d02eba743d01d (patch) | |
| tree | 05f32f4667c6bc3ed8f0f307f8bb61da7ac04ad7 /Handlers/DiscordHandler.cs | |
| parent | b39dc97286456159a99b8afcdfdb6e0aae759495 (diff) | |
Initial commitDiscordMessageRecord
Diffstat (limited to 'Handlers/DiscordHandler.cs')
| -rw-r--r-- | Handlers/DiscordHandler.cs | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/Handlers/DiscordHandler.cs b/Handlers/DiscordHandler.cs index 461aac4..7701a3d 100644 --- a/Handlers/DiscordHandler.cs +++ b/Handlers/DiscordHandler.cs @@ -2,6 +2,7 @@ using System.ComponentModel.DataAnnotations.Schema; using System.Text.RegularExpressions; using Discord; +using Discord.Rest; using Discord.WebSocket; using Microsoft.EntityFrameworkCore; using PagerParser.Handlers; @@ -10,6 +11,7 @@ namespace PagerParser { public partial class PagerContext { public DbSet<DiscordAlertChannel> DiscordAlertChannels { get; set; } public DbSet<DiscordAlertSubscription> DiscordAlertSubscriptions { get; set; } + public DbSet<DiscordAlertMessage> DiscordAlertMessages { get; set; } } } @@ -243,14 +245,34 @@ namespace PagerParser.Handlers { $"\nPaged Services: {pagedServicesText}"; // Actually post the message + RestUserMessage messageResult; try { - await guild + messageResult = await guild .GetTextChannel(alertChannel.ChannelId) .SendMessageAsync(discordMessage); } catch(Exception e) { logger.LogError( e, "Error sending Discord message"); + return; + } + + // Create a record linking the pager message in the database to the Discord + // message we've just posted so that the Discord message may be amended later + // if so required. + var messageRecord = new DiscordAlertMessage() { + DiscordMessageId = messageResult.Id, + ParsedPagerMessage = pm + }; + + // Commit the record to the database + try { + await db.AddAsync(messageRecord); + await db.SaveChangesAsync(); + } catch (Exception e) { + logger.LogError( + e, + "Error committing Discord message result to the database"); } } } @@ -638,6 +660,12 @@ namespace PagerParser.Handlers { public DiscordPrincipalType PrincipalType { get; set; } } + public class DiscordAlertMessage { + [Key] + public ulong DiscordMessageId { get; set; } + public ParsedPagerMessage ParsedPagerMessage { get; set; } + } + public enum DiscordPrincipalType { User, Role |
