notifications.go 2.6 KB

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