config.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package util
  2. import (
  3. "encoding/json"
  4. "os"
  5. "github.com/rivo/tview"
  6. )
  7. // Config consists of fields, such as theme, mouse, so on, that may be
  8. // customized by the user.
  9. type Config struct {
  10. Token string `json:"token"`
  11. Mouse bool `json:"mouse"`
  12. Notifications bool `json:"notifications"`
  13. UserAgent string `json:"userAgent"`
  14. GetMessagesLimit int `json:"getMessagesLimit"`
  15. Theme tview.Theme `json:"theme"`
  16. }
  17. // NewConfig reads the configuration file (if exists) and returns a new config.
  18. func NewConfig() *Config {
  19. c := Config{
  20. Token: "",
  21. Mouse: true,
  22. Notifications: true,
  23. UserAgent: "" +
  24. "Mozilla/5.0 (X11; Linux x86_64) " +
  25. "AppleWebKit/537.36 (KHTML, like Gecko) " +
  26. "Chrome/92.0.4515.131 Safari/537.36",
  27. GetMessagesLimit: 50,
  28. Theme: tview.Styles,
  29. }
  30. userHomeDir, err := os.UserHomeDir()
  31. if err != nil {
  32. return &c
  33. }
  34. configPath := userHomeDir + "/.config/discordo/config.json"
  35. if _, err := os.Stat(configPath); os.IsNotExist(err) {
  36. return &c
  37. }
  38. d, err := os.ReadFile(configPath)
  39. if err != nil {
  40. panic(err)
  41. }
  42. if err = json.Unmarshal(d, &c); err != nil {
  43. panic(err)
  44. }
  45. return &c
  46. }