main.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package main
  2. import (
  3. "os"
  4. "github.com/ayntgl/discordo/discord"
  5. "github.com/ayntgl/discordo/ui"
  6. "github.com/rivo/tview"
  7. "github.com/zalando/go-keyring"
  8. )
  9. const keyringServiceName = "discordo"
  10. func main() {
  11. app := ui.NewApp()
  12. token := os.Getenv("DISCORDO_TOKEN")
  13. if token == "" {
  14. token, _ = keyring.Get(keyringServiceName, "token")
  15. }
  16. if token != "" {
  17. err := app.Connect(token)
  18. if err != nil {
  19. panic(err)
  20. }
  21. app.DrawMainFlex()
  22. app.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 := discord.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.DrawMainFlex()
  42. app.SetFocus(app.GuildsList)
  43. go keyring.Set(keyringServiceName, "token", lr.Token)
  44. } else {
  45. // The account has MFA enabled, reattempt login with MFA code and ticket.
  46. mfaLoginForm := ui.NewLoginForm(true)
  47. mfaLoginForm.AddButton("Login", func() {
  48. mfaCode := loginForm.GetFormItem(0).(*tview.InputField).GetText()
  49. if mfaCode == "" {
  50. return
  51. }
  52. lr, err = discord.TOTP(app.Session, mfaCode, lr.Ticket)
  53. if err != nil {
  54. panic(err)
  55. }
  56. err = app.Connect(lr.Token)
  57. if err != nil {
  58. panic(err)
  59. }
  60. app.DrawMainFlex()
  61. app.SetFocus(app.GuildsList)
  62. go keyring.Set(keyringServiceName, "token", lr.Token)
  63. })
  64. }
  65. })
  66. app.SetRoot(loginForm, true)
  67. }
  68. tview.Borders.TopLeftFocus = tview.Borders.TopLeft
  69. tview.Borders.TopRightFocus = tview.Borders.TopRight
  70. tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft
  71. tview.Borders.BottomRightFocus = tview.Borders.BottomRight
  72. tview.Borders.HorizontalFocus = tview.Borders.Horizontal
  73. tview.Borders.VerticalFocus = tview.Borders.Vertical
  74. tview.Borders.TopLeft = 0
  75. tview.Borders.TopRight = 0
  76. tview.Borders.BottomLeft = 0
  77. tview.Borders.BottomRight = 0
  78. tview.Borders.Horizontal = 0
  79. tview.Borders.Vertical = 0
  80. err := app.Run()
  81. if err != nil {
  82. panic(err)
  83. }
  84. }