notifications.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. isChannelDM := channel.Type == discord.DirectMessage || channel.Type == discord.GroupDM
  55. shouldChime := cfg.Notifications.Sound.Enabled && (!cfg.Notifications.Sound.OnlyOnPing || (isChannelDM || mentions.Has(ningen.MessageMentions|ningen.MessageNotifies)))
  56. if err := sendDesktopNotification(title, 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 := filepath.Join(consts.CacheDir(), "avatars")
  63. if err := os.MkdirAll(path, os.ModePerm); err != nil {
  64. return "", err
  65. }
  66. path = filepath.Join(path, avatarHash+".png")
  67. if _, err := os.Stat(path); err == nil {
  68. return path, nil
  69. }
  70. file, err := os.Create(path)
  71. if err != nil {
  72. return "", err
  73. }
  74. defer file.Close()
  75. resp, err := http.Get(url)
  76. if err != nil {
  77. return "", err
  78. }
  79. defer resp.Body.Close()
  80. if _, err := io.Copy(file, resp.Body); err != nil {
  81. return "", err
  82. }
  83. return path, nil
  84. }