notifications.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. channel, err := state.Cabinet.Channel(msg.ChannelID)
  20. if err != nil {
  21. return err
  22. }
  23. isChannelDM := channel.Type == discord.DirectMessage || channel.Type == discord.GroupDM
  24. guild := (*discord.Guild)(nil)
  25. if !isChannelDM {
  26. guild, err = state.Cabinet.Guild(channel.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, err := state.Cabinet.Member(channel.GuildID, msg.Author.ID)
  42. if err != nil {
  43. return err
  44. }
  45. if member.Nick != "" {
  46. notifTitle = member.Nick
  47. }
  48. notifTitle = notifTitle + " (#" + channel.Name + ", " + guild.Name + ")"
  49. }
  50. hash := msg.Author.Avatar
  51. if hash == "" {
  52. hash = "default"
  53. }
  54. imagePath, err := getCachedProfileImage(hash, msg.Author.AvatarURLWithType(discord.PNGImage))
  55. if err != nil {
  56. slog.Error("Failed to retrieve avatar image for notification", "err", err)
  57. }
  58. shouldChime := cfg.Notifications.Sound.Enabled && (!cfg.Notifications.Sound.OnlyOnPing || (isChannelDM || state.MessageMentions(&msg.Message) == 3))
  59. if err := sendDesktopNotification(notifTitle, content, imagePath, shouldChime, cfg.Notifications.Duration); err != nil {
  60. return err
  61. }
  62. return nil
  63. }
  64. func getCachedProfileImage(avatarHash discord.Hash, url string) (string, error) {
  65. path, err := os.UserCacheDir()
  66. if err != nil {
  67. return "", err
  68. }
  69. path = filepath.Join(path, consts.Name, "assets")
  70. if err := os.MkdirAll(path, os.ModePerm); err != nil {
  71. return "", err
  72. }
  73. path = filepath.Join(path, avatarHash+".png")
  74. if _, err := os.Stat(path); err == nil {
  75. return path, nil
  76. }
  77. image, err := os.Create(path)
  78. if err != nil {
  79. return "", err
  80. }
  81. defer image.Close()
  82. resp, err := http.Get(url)
  83. if err != nil {
  84. return "", err
  85. }
  86. defer resp.Body.Close()
  87. if _, err := io.Copy(image, resp.Body); err != nil {
  88. return "", err
  89. }
  90. return path, nil
  91. }