main.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package main
  2. import (
  3. "os"
  4. "github.com/ayntgl/discordo/ui"
  5. "github.com/ayntgl/discordo/util"
  6. "github.com/rivo/tview"
  7. "github.com/zalando/go-keyring"
  8. )
  9. func main() {
  10. app := ui.NewApp()
  11. token := os.Getenv("DISCORDO_TOKEN")
  12. if token == "" {
  13. token, _ = keyring.Get("discordo", "token")
  14. }
  15. if token != "" {
  16. err := app.Connect(token)
  17. if err != nil {
  18. panic(err)
  19. }
  20. app.
  21. SetRoot(ui.NewMainFlex(app), true).
  22. SetFocus(app.GuildsList)
  23. } else {
  24. loginForm := ui.NewLoginForm(false)
  25. loginForm.AddButton("Login", func() {
  26. email := loginForm.GetFormItem(0).(*tview.InputField).GetText()
  27. password := loginForm.GetFormItem(1).(*tview.InputField).GetText()
  28. if email == "" || password == "" {
  29. return
  30. }
  31. // Login using the email and password
  32. lr, err := util.Login(app.Session, email, password)
  33. if err != nil {
  34. panic(err)
  35. }
  36. if lr.Token != "" && !lr.MFA {
  37. err = app.Connect(lr.Token)
  38. if err != nil {
  39. panic(err)
  40. }
  41. app.
  42. SetRoot(ui.NewMainFlex(app), true).
  43. SetFocus(app.GuildsList)
  44. go keyring.Set("discordo", "token", lr.Token)
  45. } else {
  46. // The account has MFA enabled, reattempt login with MFA code and ticket.
  47. mfaLoginForm := ui.NewLoginForm(true)
  48. mfaLoginForm.AddButton("Login", func() {
  49. mfaCode := loginForm.GetFormItem(0).(*tview.InputField).GetText()
  50. if mfaCode == "" {
  51. return
  52. }
  53. lr, err = util.TOTP(app.Session, mfaCode, lr.Ticket)
  54. if err != nil {
  55. panic(err)
  56. }
  57. err = app.Connect(lr.Token)
  58. if err != nil {
  59. panic(err)
  60. }
  61. app.
  62. SetRoot(ui.NewMainFlex(app), true).
  63. SetFocus(app.GuildsList)
  64. go keyring.Set("discordo", "token", lr.Token)
  65. })
  66. }
  67. })
  68. app.SetRoot(loginForm, true)
  69. }
  70. if err := app.EnableMouse(app.Config.General.Mouse).Run(); err != nil {
  71. panic(err)
  72. }
  73. }