diff --git a/internal/consts/consts.go b/internal/consts/consts.go index 4b5c3a4..ca08788 100644 --- a/internal/consts/consts.go +++ b/internal/consts/consts.go @@ -2,7 +2,10 @@ package consts import ( "encoding/json" + "log/slog" "net/http" + "os" + "path/filepath" "github.com/diamondburned/arikawa/v3/discord" "github.com/diamondburned/arikawa/v3/gateway" @@ -75,3 +78,22 @@ func GetIdentifyProps() gateway.IdentifyProperties { HasClientMods: false, } } + +var cacheDir string + +func CacheDir() string { + return cacheDir +} + +func init() { + userCacheDir, err := os.UserCacheDir() + if err != nil { + userCacheDir = os.TempDir() + slog.Warn("failed to get user cache dir; falling back to temp dir", "err", err, "path", userCacheDir) + } + + cacheDir = filepath.Join(userCacheDir, Name) + if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil { + slog.Error("failed to create cache dir", "err", err, "path", cacheDir) + } +} diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 32df043..177a5d6 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -13,15 +13,7 @@ import ( const fileName = "logs.txt" func DefaultPath() string { - path, err := os.UserCacheDir() - if err != nil { - slog.Info( - "user cache directory path cannot be determined; falling back to the current directory path", - ) - path = "." - } - - return filepath.Join(path, consts.Name, fileName) + return filepath.Join(consts.CacheDir(), fileName) } // Load opens the log file and configures default logger. diff --git a/internal/notifications/notifications.go b/internal/notifications/notifications.go index b7d4516..200e755 100644 --- a/internal/notifications/notifications.go +++ b/internal/notifications/notifications.go @@ -75,12 +75,7 @@ func Notify(state *ningen.State, msg *gateway.MessageCreateEvent, cfg *config.Co } func getCachedProfileImage(avatarHash discord.Hash, url string) (string, error) { - path, err := os.UserCacheDir() - if err != nil { - return "", err - } - - path = filepath.Join(path, consts.Name, "assets") + path := filepath.Join(consts.CacheDir(), "avatars") if err := os.MkdirAll(path, os.ModePerm); err != nil { return "", err }