config.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package config
  2. import (
  3. "log/slog"
  4. "os"
  5. "path/filepath"
  6. "time"
  7. "github.com/BurntSushi/toml"
  8. "github.com/ayn2op/discordo/internal/consts"
  9. "github.com/diamondburned/arikawa/v3/discord"
  10. )
  11. const fileName = "config.toml"
  12. type (
  13. Timestamps struct {
  14. Enabled bool `toml:"enabled"`
  15. Format string `toml:"format"`
  16. }
  17. Identify struct {
  18. Status discord.Status `toml:"status"`
  19. Browser string `toml:"browser"`
  20. BrowserVersion string `toml:"browser_version"`
  21. UserAgent string `toml:"user_agent"`
  22. }
  23. Notifications struct {
  24. Enabled bool `toml:"enabled"`
  25. Duration int `toml:"duration"`
  26. Sound Sound `toml:"sound"`
  27. }
  28. Sound struct {
  29. Enabled bool `toml:"enabled"`
  30. OnlyOnPing bool `toml:"only_on_ping"`
  31. }
  32. Config struct {
  33. Mouse bool `toml:"mouse"`
  34. Editor string `toml:"editor"`
  35. HideBlockedUsers bool `toml:"hide_blocked_users"`
  36. ShowAttachmentLinks bool `toml:"show_attachment_links"`
  37. MessagesLimit uint8 `toml:"messages_limit"`
  38. MarkdownEnabled bool `toml:"markdown_enabled"`
  39. Timestamps Timestamps `toml:"timestamps"`
  40. Identify Identify `toml:"identify"`
  41. Notifications Notifications `toml:"notifications"`
  42. Keys Keys `toml:"keys"`
  43. Theme Theme `toml:"theme"`
  44. }
  45. )
  46. func defaultConfig() *Config {
  47. return &Config{
  48. Mouse: true,
  49. Editor: "default",
  50. HideBlockedUsers: true,
  51. ShowAttachmentLinks: true,
  52. MessagesLimit: 50,
  53. MarkdownEnabled: true,
  54. Timestamps: Timestamps{
  55. Enabled: true,
  56. Format: time.Kitchen,
  57. },
  58. Identify: Identify{
  59. Status: discord.OnlineStatus,
  60. Browser: consts.Browser,
  61. BrowserVersion: consts.BrowserVersion,
  62. UserAgent: consts.UserAgent,
  63. },
  64. Notifications: Notifications{
  65. Enabled: true,
  66. Duration: 500,
  67. Sound: Sound{
  68. Enabled: true,
  69. OnlyOnPing: true,
  70. },
  71. },
  72. Keys: defaultKeys(),
  73. Theme: defaultTheme(),
  74. }
  75. }
  76. func DefaultPath() string {
  77. path, err := os.UserConfigDir()
  78. if err != nil {
  79. slog.Info("user configuration directory path cannot be determined; falling back to the current directory path")
  80. path = "."
  81. }
  82. return filepath.Join(path, consts.Name, fileName)
  83. }
  84. // Reads the configuration file and parses it.
  85. func Load(path string) (*Config, error) {
  86. f, err := os.Open(path)
  87. cfg := defaultConfig()
  88. if os.IsNotExist(err) {
  89. slog.Info("the configuration file does not exist, falling back to the default configuration", "path", path, "err", err)
  90. return cfg, nil
  91. }
  92. if err != nil {
  93. return nil, err
  94. }
  95. defer f.Close()
  96. if _, err := toml.NewDecoder(f).Decode(&cfg); err != nil {
  97. return nil, err
  98. }
  99. return cfg, nil
  100. }