notifications.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, 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. if member := msg.Member; member != nil && member.Nick != "" {
  39. title = member.Nick
  40. }
  41. title += " (#" + channel.Name + ", " + guild.Name + ")"
  42. }
  43. hash := msg.Author.Avatar
  44. if hash == "" {
  45. hash = "default"
  46. }
  47. imagePath, err := getCachedProfileImage(hash, msg.Author.AvatarURLWithType(discord.PNGImage))
  48. if err != nil {
  49. slog.Info("failed to get profile image from cache for notification", "err", err, "hash", hash)
  50. }
  51. isChannelDM := channel.Type == discord.DirectMessage || channel.Type == discord.GroupDM
  52. shouldChime := cfg.Notifications.Sound.Enabled && (!cfg.Notifications.Sound.OnlyOnPing || (isChannelDM || state.MessageMentions(&msg.Message) == 3))
  53. if err := sendDesktopNotification(title, content, imagePath, shouldChime, cfg.Notifications.Duration); err != nil {
  54. return err
  55. }
  56. return nil
  57. }
  58. func getCachedProfileImage(avatarHash discord.Hash, url string) (string, error) {
  59. path, err := os.UserCacheDir()
  60. if err != nil {
  61. return "", err
  62. }
  63. path = filepath.Join(path, consts.Name, "assets")
  64. if err := os.MkdirAll(path, os.ModePerm); err != nil {
  65. return "", err
  66. }
  67. path = filepath.Join(path, avatarHash+".png")
  68. if _, err := os.Stat(path); err == nil {
  69. return path, nil
  70. }
  71. image, err := os.Create(path)
  72. if err != nil {
  73. return "", err
  74. }
  75. defer image.Close()
  76. resp, err := http.Get(url)
  77. if err != nil {
  78. return "", err
  79. }
  80. defer resp.Body.Close()
  81. if _, err := io.Copy(image, resp.Body); err != nil {
  82. return "", err
  83. }
  84. return path, nil
  85. }