notifications.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package notifications
  2. import (
  3. "fmt"
  4. "io"
  5. "log/slog"
  6. "net/http"
  7. "os"
  8. "path/filepath"
  9. "github.com/ayn2op/discordo/internal/config"
  10. "github.com/ayn2op/discordo/internal/consts"
  11. "github.com/diamondburned/arikawa/v3/discord"
  12. "github.com/diamondburned/arikawa/v3/gateway"
  13. "github.com/diamondburned/ningen/v3"
  14. )
  15. func HandleIncomingMessage(state *ningen.State, msg *gateway.MessageCreateEvent, cfg *config.Config) error {
  16. // Only display notification if enabled and unmuted
  17. if !cfg.Notifications.Enabled || state.MessageMentions(&msg.Message) == 0 || cfg.Status == discord.DoNotDisturbStatus {
  18. return nil
  19. }
  20. // Handle sent files
  21. content := msg.Content
  22. if msg.Content == "" && len(msg.Attachments) > 0 {
  23. content = "Uploaded " + msg.Message.Attachments[0].Filename
  24. }
  25. if content == "" {
  26. return nil
  27. }
  28. title := msg.Author.DisplayOrUsername()
  29. channel, err := state.Cabinet.Channel(msg.ChannelID)
  30. if err != nil {
  31. return fmt.Errorf("failed to get channel from state: %w", err)
  32. }
  33. if channel.GuildID.IsValid() {
  34. guild, err := state.Cabinet.Guild(channel.GuildID)
  35. if err != nil {
  36. return fmt.Errorf("failed to get guild from state: %w", err)
  37. }
  38. member, err := state.Cabinet.Member(guild.ID, msg.Author.ID)
  39. if err != nil {
  40. slog.Info("failed to get member from state", "err", err, "guild_id", channel.GuildID, "user_id", msg.Author.ID)
  41. } else {
  42. if member.Nick != "" {
  43. title = member.Nick
  44. }
  45. }
  46. title += " (#" + channel.Name + ", " + guild.Name + ")"
  47. }
  48. hash := msg.Author.Avatar
  49. if hash == "" {
  50. hash = "default"
  51. }
  52. imagePath, err := getCachedProfileImage(hash, msg.Author.AvatarURLWithType(discord.PNGImage))
  53. if err != nil {
  54. slog.Info("failed to get profile image from cache for notification", "err", err, "hash", hash)
  55. }
  56. isChannelDM := channel.Type == discord.DirectMessage || channel.Type == discord.GroupDM
  57. shouldChime := cfg.Notifications.Sound.Enabled && (!cfg.Notifications.Sound.OnlyOnPing || (isChannelDM || state.MessageMentions(&msg.Message) == 3))
  58. if err := sendDesktopNotification(title, content, imagePath, shouldChime, cfg.Notifications.Duration); err != nil {
  59. return err
  60. }
  61. return nil
  62. }
  63. func getCachedProfileImage(avatarHash discord.Hash, url string) (string, error) {
  64. path, err := os.UserCacheDir()
  65. if err != nil {
  66. return "", err
  67. }
  68. path = filepath.Join(path, consts.Name, "assets")
  69. if err := os.MkdirAll(path, os.ModePerm); err != nil {
  70. return "", err
  71. }
  72. path = filepath.Join(path, avatarHash+".png")
  73. if _, err := os.Stat(path); err == nil {
  74. return path, nil
  75. }
  76. image, err := os.Create(path)
  77. if err != nil {
  78. return "", err
  79. }
  80. defer image.Close()
  81. resp, err := http.Get(url)
  82. if err != nil {
  83. return "", err
  84. }
  85. defer resp.Body.Close()
  86. if _, err := io.Copy(image, resp.Body); err != nil {
  87. return "", err
  88. }
  89. return path, nil
  90. }