notifications.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 Notify(state *ningen.State, message *gateway.MessageCreateEvent, cfg *config.Config) error {
  16. if !cfg.Notifications.Enabled || cfg.Status == discord.DoNotDisturbStatus {
  17. return nil
  18. }
  19. mentions := state.MessageMentions(&message.Message)
  20. if mentions == 0 {
  21. return nil
  22. }
  23. // Handle sent files
  24. content := message.Content
  25. if message.Content == "" && len(message.Attachments) > 0 {
  26. content = "Uploaded " + message.Attachments[0].Filename
  27. }
  28. if content == "" {
  29. return nil
  30. }
  31. title := message.Author.DisplayOrUsername()
  32. channel, err := state.Cabinet.Channel(message.ChannelID)
  33. if err != nil {
  34. return fmt.Errorf("failed to get channel from state: %w", err)
  35. }
  36. if channel.GuildID.IsValid() {
  37. guild, err := state.Cabinet.Guild(channel.GuildID)
  38. if err != nil {
  39. return fmt.Errorf("failed to get guild from state: %w", err)
  40. }
  41. if member := message.Member; member != nil && member.Nick != "" {
  42. title = member.Nick
  43. }
  44. title += " (#" + channel.Name + ", " + guild.Name + ")"
  45. }
  46. hash := message.Author.Avatar
  47. if hash == "" {
  48. hash = "default"
  49. }
  50. imagePath, err := getCachedProfileImage(hash, message.Author.AvatarURLWithType(discord.PNGImage))
  51. if err != nil {
  52. slog.Info("failed to get profile image from cache for notification", "err", err, "hash", hash)
  53. }
  54. shouldChime := cfg.Notifications.Sound.Enabled && (!cfg.Notifications.Sound.OnlyOnPing || mentions.Has(ningen.MessageMentions|ningen.MessageNotifies))
  55. if err := sendDesktopNotification(title, content, imagePath, shouldChime, cfg.Notifications.Duration); err != nil {
  56. return err
  57. }
  58. return nil
  59. }
  60. func getCachedProfileImage(avatarHash discord.Hash, url string) (string, error) {
  61. path := filepath.Join(consts.CacheDir(), "avatars")
  62. if err := os.MkdirAll(path, os.ModePerm); err != nil {
  63. return "", err
  64. }
  65. path = filepath.Join(path, avatarHash+".png")
  66. if _, err := os.Stat(path); err == nil {
  67. return path, nil
  68. }
  69. file, err := os.Create(path)
  70. if err != nil {
  71. return "", err
  72. }
  73. defer file.Close()
  74. resp, err := http.Get(url)
  75. if err != nil {
  76. return "", err
  77. }
  78. defer resp.Body.Close()
  79. if _, err := io.Copy(file, resp.Body); err != nil {
  80. return "", err
  81. }
  82. return path, nil
  83. }