main.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "os"
  7. "runtime"
  8. "github.com/ayntgl/discordo/config"
  9. "github.com/ayntgl/discordo/ui"
  10. "github.com/diamondburned/arikawa/v3/api"
  11. "github.com/diamondburned/arikawa/v3/gateway"
  12. "github.com/rivo/tview"
  13. "github.com/zalando/go-keyring"
  14. )
  15. var (
  16. flagToken string
  17. flagConfig string
  18. flagLog string
  19. )
  20. func init() {
  21. tview.Borders.TopLeftFocus = tview.Borders.TopLeft
  22. tview.Borders.TopRightFocus = tview.Borders.TopRight
  23. tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft
  24. tview.Borders.BottomRightFocus = tview.Borders.BottomRight
  25. tview.Borders.HorizontalFocus = tview.Borders.Horizontal
  26. tview.Borders.VerticalFocus = tview.Borders.Vertical
  27. tview.Borders.TopLeft = 0
  28. tview.Borders.TopRight = 0
  29. tview.Borders.BottomLeft = 0
  30. tview.Borders.BottomRight = 0
  31. tview.Borders.Horizontal = 0
  32. tview.Borders.Vertical = 0
  33. api.UserAgent = fmt.Sprintf("%s/%s %s/%s", config.Name, "0.1", "arikawa", "v3")
  34. gateway.DefaultIdentity = gateway.IdentifyProperties{
  35. OS: runtime.GOOS,
  36. Browser: config.Name,
  37. Device: "",
  38. }
  39. flag.StringVar(&flagToken, "token", "", "The authentication token.")
  40. flag.StringVar(&flagConfig, "config", config.DefaultConfigPath(), "The path to the configuration file.")
  41. flag.StringVar(&flagLog, "log", config.DefaultLogPath(), "The path to the log file.")
  42. }
  43. func main() {
  44. flag.Parse()
  45. if flagLog != "" {
  46. // Set the standard logger output to the provided log file.
  47. f, err := os.OpenFile(flagLog, os.O_CREATE|os.O_WRONLY, 0666)
  48. if err != nil {
  49. log.Fatal(err)
  50. }
  51. log.SetOutput(f)
  52. log.SetFlags(log.LstdFlags | log.Lshortfile)
  53. }
  54. cfg := config.New()
  55. err := cfg.Load(flagConfig)
  56. if err != nil {
  57. log.Fatal(err)
  58. }
  59. var token string
  60. if flagToken != "" {
  61. token = flagToken
  62. go keyring.Set(config.Name, "token", token)
  63. } else {
  64. token, err = keyring.Get(config.Name, "token")
  65. if err != nil {
  66. log.Println(err)
  67. }
  68. }
  69. c := ui.NewCore(cfg)
  70. if token != "" {
  71. err = c.Run(token)
  72. if err != nil {
  73. log.Fatal(err)
  74. }
  75. c.Draw()
  76. } else {
  77. loginView := ui.NewLoginView(c)
  78. c.App.SetRoot(loginView, true)
  79. }
  80. err = c.App.Run()
  81. if err != nil {
  82. log.Fatal(err)
  83. }
  84. }