main.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package main
  2. import (
  3. "os"
  4. "github.com/ayntgl/discordo/discord"
  5. "github.com/ayntgl/discordo/ui"
  6. "github.com/gdamore/tcell/v2"
  7. "github.com/rivo/tview"
  8. "github.com/zalando/go-keyring"
  9. )
  10. const keyringServiceName = "discordo"
  11. func main() {
  12. app := ui.NewApp()
  13. token := os.Getenv("DISCORDO_TOKEN")
  14. if token == "" {
  15. token, _ = keyring.Get(keyringServiceName, "token")
  16. }
  17. if token != "" {
  18. err := app.Connect(token)
  19. if err != nil {
  20. panic(err)
  21. }
  22. app.DrawMainFlex()
  23. app.SetFocus(app.GuildsList)
  24. } else {
  25. loginForm := ui.NewLoginForm(false)
  26. loginForm.AddButton("Login", func() {
  27. email := loginForm.GetFormItem(0).(*tview.InputField).GetText()
  28. password := loginForm.GetFormItem(1).(*tview.InputField).GetText()
  29. if email == "" || password == "" {
  30. return
  31. }
  32. // Login using the email and password
  33. lr, err := discord.Login(app.Session, email, password)
  34. if err != nil {
  35. panic(err)
  36. }
  37. if lr.Token != "" && !lr.MFA {
  38. err = app.Connect(lr.Token)
  39. if err != nil {
  40. panic(err)
  41. }
  42. app.DrawMainFlex()
  43. app.SetFocus(app.GuildsList)
  44. go keyring.Set(keyringServiceName, "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 = discord.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.DrawMainFlex()
  62. app.SetFocus(app.GuildsList)
  63. go keyring.Set(keyringServiceName, "token", lr.Token)
  64. })
  65. }
  66. })
  67. app.SetRoot(loginForm, true)
  68. }
  69. tview.Borders.TopLeftFocus = tview.Borders.TopLeft
  70. tview.Borders.TopRightFocus = tview.Borders.TopRight
  71. tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft
  72. tview.Borders.BottomRightFocus = tview.Borders.BottomRight
  73. tview.Borders.HorizontalFocus = tview.Borders.Horizontal
  74. tview.Borders.VerticalFocus = tview.Borders.Vertical
  75. tview.Borders.TopLeft = 0
  76. tview.Borders.TopRight = 0
  77. tview.Borders.BottomLeft = 0
  78. tview.Borders.BottomRight = 0
  79. tview.Borders.Horizontal = 0
  80. tview.Borders.Vertical = 0
  81. tview.Styles.PrimitiveBackgroundColor = tcell.GetColor(app.Config.Theme.Background)
  82. tview.Styles.BorderColor = tcell.GetColor(app.Config.Theme.Border)
  83. tview.Styles.TitleColor = tcell.GetColor(app.Config.Theme.Title)
  84. err := app.Run()
  85. if err != nil {
  86. panic(err)
  87. }
  88. }