app.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package ui
  2. import (
  3. "sort"
  4. "strings"
  5. "github.com/ayntgl/discordgo"
  6. "github.com/ayntgl/discordo/config"
  7. "github.com/gdamore/tcell/v2"
  8. "github.com/rivo/tview"
  9. )
  10. type App struct {
  11. *tview.Application
  12. MainFlex *tview.Flex
  13. GuildsList *GuildsList
  14. ChannelsTreeView *ChannelsTreeView
  15. MessagesTextView *MessagesTextView
  16. MessageInputField *MessageInputField
  17. Session *discordgo.Session
  18. SelectedChannel *discordgo.Channel
  19. Config *config.Config
  20. SelectedMessage int
  21. }
  22. func NewApp(c *config.Config) *App {
  23. app := &App{
  24. MainFlex: tview.NewFlex(),
  25. Config: c,
  26. SelectedMessage: -1,
  27. }
  28. app.GuildsList = NewGuildsList(app)
  29. app.ChannelsTreeView = NewChannelsTreeView(app)
  30. app.MessagesTextView = NewMessagesTextView(app)
  31. app.MessageInputField = NewMessageInputField(app)
  32. app.Session, _ = discordgo.New()
  33. app.Application = tview.NewApplication()
  34. app.EnableMouse(app.Config.General.Mouse)
  35. app.SetInputCapture(app.onInputCapture)
  36. return app
  37. }
  38. func (app *App) Connect(token string) error {
  39. // For user accounts, all of the guilds, the user is in, are dispatched in the READY gateway event.
  40. // Whereas, for bot accounts, the guilds are dispatched discretely in the GUILD_CREATE gateway events.
  41. if !strings.HasPrefix(token, "Bot") {
  42. app.Session.UserAgent = app.Config.General.UserAgent
  43. app.Session.Identify = discordgo.Identify{
  44. Compress: false,
  45. LargeThreshold: 0,
  46. Intents: 0,
  47. Properties: discordgo.IdentifyProperties{
  48. OS: app.Config.General.Identify.Os,
  49. Browser: app.Config.General.Identify.Browser,
  50. },
  51. }
  52. app.Session.AddHandlerOnce(app.onSessionReady)
  53. }
  54. app.Session.Token = token
  55. app.Session.Identify.Token = token
  56. app.Session.AddHandler(app.onSessionGuildCreate)
  57. app.Session.AddHandler(app.onSessionMessageCreate)
  58. return app.Session.Open()
  59. }
  60. func (app *App) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
  61. if app.MessageInputField.HasFocus() {
  62. return e
  63. }
  64. if app.MainFlex.GetItemCount() != 0 {
  65. switch e.Name() {
  66. case app.Config.Keybindings.ToggleGuildsList:
  67. app.SetFocus(app.GuildsList)
  68. return nil
  69. case app.Config.Keybindings.ToggleChannelsTreeView:
  70. app.SetFocus(app.ChannelsTreeView)
  71. return nil
  72. case app.Config.Keybindings.ToggleMessagesTextView:
  73. app.SetFocus(app.MessagesTextView)
  74. return nil
  75. case app.Config.Keybindings.ToggleMessageInputField:
  76. app.SetFocus(app.MessageInputField)
  77. return nil
  78. }
  79. }
  80. return e
  81. }
  82. func (app *App) DrawMainFlex() {
  83. leftFlex := tview.NewFlex().
  84. SetDirection(tview.FlexRow).
  85. AddItem(app.GuildsList, 10, 1, false).
  86. AddItem(app.ChannelsTreeView, 0, 1, false)
  87. rightFlex := tview.NewFlex().
  88. SetDirection(tview.FlexRow).
  89. AddItem(app.MessagesTextView, 0, 1, false).
  90. AddItem(app.MessageInputField, 3, 1, false)
  91. app.MainFlex.
  92. AddItem(leftFlex, 0, 1, false).
  93. AddItem(rightFlex, 0, 4, false)
  94. app.SetRoot(app.MainFlex, true)
  95. }
  96. func (app *App) onSessionReady(_ *discordgo.Session, r *discordgo.Ready) {
  97. sort.Slice(r.Guilds, func(a, b int) bool {
  98. found := false
  99. for _, guildID := range r.Settings.GuildPositions {
  100. if found && guildID == r.Guilds[b].ID {
  101. return true
  102. }
  103. if !found && guildID == r.Guilds[a].ID {
  104. found = true
  105. }
  106. }
  107. return false
  108. })
  109. for _, g := range r.Guilds {
  110. app.GuildsList.AddItem(g.Name, "", 0, nil)
  111. }
  112. app.GuildsList.AddItem("Direct Messages", "", 0, nil)
  113. }
  114. func (app *App) onSessionGuildCreate(_ *discordgo.Session, g *discordgo.GuildCreate) {
  115. app.GuildsList.AddItem(g.Name, "", 0, nil)
  116. }
  117. func (app *App) onSessionMessageCreate(_ *discordgo.Session, m *discordgo.MessageCreate) {
  118. if app.SelectedChannel != nil && app.SelectedChannel.ID == m.ChannelID {
  119. app.SelectedChannel.Messages = append(app.SelectedChannel.Messages, m.Message)
  120. _, err := app.MessagesTextView.Write(buildMessage(app, m.Message))
  121. if err != nil {
  122. return
  123. }
  124. if len(app.MessagesTextView.GetHighlights()) == 0 {
  125. app.MessagesTextView.ScrollToEnd()
  126. }
  127. }
  128. }